修改SSH时自动填入SSH主机地址/节点设置--SSH设置增加连接测试

This commit is contained in:
刘祥超
2022-04-09 21:44:34 +08:00
parent 2d8224fd12
commit 75b2b2bdb2
6 changed files with 114 additions and 7 deletions

View File

@@ -54,6 +54,7 @@ func init() {
GetPost("/settings/dns", new(dns.IndexAction)). GetPost("/settings/dns", new(dns.IndexAction)).
GetPost("/settings/system", new(system.IndexAction)). GetPost("/settings/system", new(system.IndexAction)).
GetPost("/settings/ssh", new(ssh.IndexAction)). GetPost("/settings/ssh", new(ssh.IndexAction)).
GetPost("/settings/ssh/test", new(ssh.TestAction)).
GetPost("/settings/thresholds", new(thresholds.IndexAction)). GetPost("/settings/thresholds", new(thresholds.IndexAction)).
// 分组相关 // 分组相关

View File

@@ -10,6 +10,8 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions" "github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
"net"
"regexp"
) )
type IndexAction struct { type IndexAction struct {
@@ -33,7 +35,7 @@ func (this *IndexAction) RunGet(params struct {
// 登录信息 // 登录信息
var loginMap maps.Map = nil var loginMap maps.Map = nil
if node.NodeLogin != nil { if node.NodeLogin != nil {
loginParams := maps.Map{} var loginParams = maps.Map{}
if len(node.NodeLogin.Params) > 0 { if len(node.NodeLogin.Params) > 0 {
err = json.Unmarshal(node.NodeLogin.Params, &loginParams) err = json.Unmarshal(node.NodeLogin.Params, &loginParams)
if err != nil { if err != nil {
@@ -42,7 +44,7 @@ func (this *IndexAction) RunGet(params struct {
} }
} }
grantMap := maps.Map{} var grantMap = maps.Map{}
grantId := loginParams.GetInt64("grantId") grantId := loginParams.GetInt64("grantId")
if grantId > 0 { if grantId > 0 {
grantResp, err := this.RPC().NodeGrantRPC().FindEnabledNodeGrant(this.AdminContext(), &pb.FindEnabledNodeGrantRequest{NodeGrantId: grantId}) grantResp, err := this.RPC().NodeGrantRPC().FindEnabledNodeGrant(this.AdminContext(), &pb.FindEnabledNodeGrantRequest{NodeGrantId: grantId})
@@ -70,6 +72,27 @@ func (this *IndexAction) RunGet(params struct {
} }
} }
if loginMap == nil {
addressesResp, err := this.RPC().NodeIPAddressRPC().FindAllEnabledNodeIPAddressesWithNodeId(this.AdminContext(), &pb.FindAllEnabledNodeIPAddressesWithNodeIdRequest{NodeId: node.Id})
if err != nil {
this.ErrorPage(err)
return
}
if len(addressesResp.NodeIPAddresses) > 0 {
loginMap = maps.Map{
"id": 0,
"name": "",
"type": "ssh",
"params": maps.Map{
"host": addressesResp.NodeIPAddresses[0].Ip,
"port": 22,
"grantId": 0,
},
"grant": nil,
}
}
}
var nodeMap = this.Data["node"].(maps.Map) var nodeMap = this.Data["node"].(maps.Map)
nodeMap["login"] = loginMap nodeMap["login"] = loginMap
@@ -89,8 +112,13 @@ func (this *IndexAction) RunPost(params struct {
}) { }) {
defer this.CreateLogInfo("修改节点 %d SSH登录信息", params.NodeId) defer this.CreateLogInfo("修改节点 %d SSH登录信息", params.NodeId)
// 检查IP地址
if regexp.MustCompile(`^\d+\.\d+\.\d+\.\d+$`).MatchString(params.SshHost) && net.ParseIP(params.SshHost) == nil {
this.Fail("SSH主机地址 '" + params.SshHost + "' IP格式错误")
}
// TODO 检查登录授权 // TODO 检查登录授权
loginInfo := &pb.NodeLogin{ var loginInfo = &pb.NodeLogin{
Id: params.LoginId, Id: params.LoginId,
Name: "SSH", Name: "SSH",
Type: "ssh", Type: "ssh",

View File

@@ -0,0 +1,31 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package ssh
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
type TestAction struct {
actionutils.ParentAction
}
func (this *TestAction) RunPost(params struct {
GrantId int64
Host string
Port int32
}) {
resp, err := this.RPC().NodeGrantRPC().TestNodeGrant(this.AdminContext(), &pb.TestNodeGrantRequest{
NodeGrantId: params.GrantId,
Host: params.Host,
Port: params.Port,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["isOk"] = resp.IsOk
this.Data["error"] = resp.Error
this.Success()
}

View File

@@ -8,6 +8,8 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions" "github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
"net"
"regexp"
) )
type UpdateNodeSSHAction struct { type UpdateNodeSSHAction struct {
@@ -31,7 +33,7 @@ func (this *UpdateNodeSSHAction) RunGet(params struct {
return return
} }
node := nodeResp.Node var node = nodeResp.Node
this.Data["node"] = maps.Map{ this.Data["node"] = maps.Map{
"id": node.Id, "id": node.Id,
"name": node.Name, "name": node.Name,
@@ -43,7 +45,7 @@ func (this *UpdateNodeSSHAction) RunGet(params struct {
} }
// SSH // SSH
loginParams := maps.Map{ var loginParams = maps.Map{
"host": "", "host": "",
"port": "", "port": "",
"grantId": 0, "grantId": 0,
@@ -59,10 +61,22 @@ func (this *UpdateNodeSSHAction) RunGet(params struct {
} }
} }
} }
if len(loginParams.GetString("host")) == 0 {
addressesResp, err := this.RPC().NodeIPAddressRPC().FindAllEnabledNodeIPAddressesWithNodeId(this.AdminContext(), &pb.FindAllEnabledNodeIPAddressesWithNodeIdRequest{NodeId: node.Id})
if err != nil {
this.ErrorPage(err)
return
}
if len(addressesResp.NodeIPAddresses) > 0 {
loginParams["host"] = addressesResp.NodeIPAddresses[0].Ip
}
}
this.Data["params"] = loginParams this.Data["params"] = loginParams
// 认证信息 // 认证信息
grantId := loginParams.GetInt64("grantId") var grantId = loginParams.GetInt64("grantId")
grantResp, err := this.RPC().NodeGrantRPC().FindEnabledNodeGrant(this.AdminContext(), &pb.FindEnabledNodeGrantRequest{NodeGrantId: grantId}) grantResp, err := this.RPC().NodeGrantRPC().FindEnabledNodeGrant(this.AdminContext(), &pb.FindEnabledNodeGrantRequest{NodeGrantId: grantId})
if err != nil { if err != nil {
this.ErrorPage(err) this.ErrorPage(err)
@@ -101,7 +115,12 @@ func (this *UpdateNodeSSHAction) RunPost(params struct {
this.Fail("需要选择或填写至少一个认证信息") this.Fail("需要选择或填写至少一个认证信息")
} }
login := &pb.NodeLogin{ // 检查IP地址
if regexp.MustCompile(`^\d+\.\d+\.\d+\.\d+$`).MatchString(params.SshHost) && net.ParseIP(params.SshHost) == nil {
this.Fail("SSH主机地址 '" + params.SshHost + "' IP格式错误")
}
var login = &pb.NodeLogin{
Id: params.LoginId, Id: params.LoginId,
Name: "SSH", Name: "SSH",
Type: "ssh", Type: "ssh",

View File

@@ -30,6 +30,11 @@
</td> </td>
</tr> </tr>
</table> </table>
<div class="ui message" v-if="isTesting">正在测试是否连接 ...</div>
<div class="ui message green" v-if="resp != null && resp.isOk">连接成功!</div>
<div class="ui message red" v-if="resp != null && !resp.isOk">连接失败:{{resp.error}}</div>
<submit-btn></submit-btn> <submit-btn></submit-btn>
</form> </form>
</div> </div>

View File

@@ -27,4 +27,27 @@ Tea.context(function () {
} }
} }
} }
// 测试相关
this.resp = null
this.isTesting = false
if (this.grant != null && this.grant.id > 0 && this.sshHost.length > 0 && this.sshPort.toString().length > 0) {
this.isTesting = true
this.$delay(function () {
this.$post(".test")
.params({
grantId: this.grant.id,
host: this.sshHost,
port: this.sshPort
})
.success(function (resp) {
this.resp = resp.data
})
.done(function () {
this.isTesting = false
})
}, 1000)
}
}) })