package clusters import ( "encoding/json" "net" "regexp" "strings" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/iwind/TeaGo/actions" "github.com/iwind/TeaGo/maps" ) type UpdateNodeSSHAction struct { actionutils.ParentAction } func (this *UpdateNodeSSHAction) RunGet(params struct { NodeId int64 }) { resp, err := this.RPC().HTTPDNSNodeRPC().FindHTTPDNSNode(this.AdminContext(), &pb.FindHTTPDNSNodeRequest{ NodeId: params.NodeId, }) if err != nil { this.ErrorPage(err) return } clusterId := int64(0) nodeName := "" if resp.GetNode() != nil { clusterId = resp.GetNode().GetClusterId() nodeName = resp.GetNode().GetName() } this.Data["nodeId"] = params.NodeId this.Data["clusterId"] = clusterId this.Data["node"] = maps.Map{ "id": params.NodeId, "name": nodeName, } loginParams := maps.Map{ "host": "", "port": 22, "grantId": 0, } this.Data["loginId"] = 0 // 从 NodeLogin 读取 SSH 信息 if resp.GetNode() != nil && resp.GetNode().GetNodeLogin() != nil { nodeLogin := resp.GetNode().GetNodeLogin() this.Data["loginId"] = nodeLogin.Id if len(nodeLogin.Params) > 0 { _ = json.Unmarshal(nodeLogin.Params, &loginParams) } } this.Data["params"] = loginParams // 认证信息 grantId := loginParams.GetInt64("grantId") var grantMap maps.Map = nil if grantId > 0 { grantResp, grantErr := this.RPC().NodeGrantRPC().FindEnabledNodeGrant(this.AdminContext(), &pb.FindEnabledNodeGrantRequest{ NodeGrantId: grantId, }) if grantErr == nil && grantResp.GetNodeGrant() != nil { g := grantResp.GetNodeGrant() grantMap = maps.Map{ "id": g.Id, "name": g.Name, "method": g.Method, "methodName": g.Method, } } } this.Data["grant"] = grantMap this.Show() } func (this *UpdateNodeSSHAction) RunPost(params struct { NodeId int64 LoginId int64 SshHost string SshPort int GrantId int64 Must *actions.Must }) { params.SshHost = strings.TrimSpace(params.SshHost) params.Must. Field("sshHost", params.SshHost). Require("请输入 SSH 主机地址"). Field("sshPort", params.SshPort). Gt(0, "SSH 端口必须大于 0"). Lt(65535, "SSH 端口必须小于 65535") if params.GrantId <= 0 { this.Fail("请选择节点登录认证信息") } if regexp.MustCompile(`^\d+\.\d+\.\d+\.\d+$`).MatchString(params.SshHost) && net.ParseIP(params.SshHost) == nil { this.Fail("SSH 主机地址 IP 格式错误") } login := &pb.NodeLogin{ Id: params.LoginId, Name: "SSH", Type: "ssh", Params: maps.Map{ "grantId": params.GrantId, "host": params.SshHost, "port": params.SshPort, }.AsJSON(), } _, err := this.RPC().HTTPDNSNodeRPC().UpdateHTTPDNSNodeLogin(this.AdminContext(), &pb.UpdateHTTPDNSNodeLoginRequest{ NodeId: params.NodeId, NodeLogin: login, }) if err != nil { this.Fail("保存SSH登录信息失败: " + err.Error()) return } this.Success() }