换成单集群模式

This commit is contained in:
robin
2026-03-02 20:07:53 +08:00
parent 5d0b7c7e91
commit 2a76d1773d
432 changed files with 5681 additions and 5095 deletions

View File

@@ -2,11 +2,14 @@ package node
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
)
type InstallAction struct {
@@ -55,7 +58,22 @@ func (this *InstallAction) RunGet(params struct {
apiEndpoints = []string{"http://127.0.0.1:7788"}
}
this.Data["apiEndpoints"] = "\"" + strings.Join(apiEndpoints, "\", \"") + "\""
// 从 NodeLogin 中提取 SSH 地址
this.Data["sshAddr"] = ""
loginMap, _ := node.Get("login").(maps.Map)
if loginMap != nil {
paramsMap, _ := loginMap.Get("params").(maps.Map)
if paramsMap != nil {
host := paramsMap.GetString("host")
if len(host) > 0 {
port := paramsMap.GetInt("port")
if port <= 0 {
port = 22
}
this.Data["sshAddr"] = fmt.Sprintf("%s:%d", configutils.QuoteIP(host), port)
}
}
}
this.Show()
}

View File

@@ -57,7 +57,21 @@ func findHTTPDNSNodeMap(parent *actionutils.ParentAction, nodeID int64) (maps.Ma
if installStatusMap.Has("ipAddresses") {
for _, addr := range installStatusMap.GetSlice("ipAddresses") {
if addrMap, ok := addr.(map[string]interface{}); ok {
ipAddresses = append(ipAddresses, maps.Map(addrMap))
m := maps.Map(addrMap)
// 确保必要字段存在,防止前端组件报错
if !m.Has("name") {
m["name"] = ""
}
if !m.Has("canAccess") {
m["canAccess"] = true
}
if !m.Has("isOn") {
m["isOn"] = true
}
if !m.Has("isUp") {
m["isUp"] = true
}
ipAddresses = append(ipAddresses, m)
}
}
} else {
@@ -87,11 +101,16 @@ func findHTTPDNSNodeMap(parent *actionutils.ParentAction, nodeID int64) (maps.Ma
return nil, err
}
// 构造 node.login 用于 index.html 展示 SSH 信息
// 构造 node.login 用于 index.html 展示 SSH 信息(从 NodeLogin 读取)
var loginMap maps.Map = nil
sshInfo := installStatusMap.GetMap("ssh")
if sshInfo != nil {
grantId := sshInfo.GetInt64("grantId")
if node.GetNodeLogin() != nil {
nodeLogin := node.GetNodeLogin()
sshLoginParams := maps.Map{}
if len(nodeLogin.Params) > 0 {
_ = json.Unmarshal(nodeLogin.Params, &sshLoginParams)
}
grantId := sshLoginParams.GetInt64("grantId")
var grantMap maps.Map = nil
if grantId > 0 {
grantResp, grantErr := parent.RPC().NodeGrantRPC().FindEnabledNodeGrant(parent.AdminContext(), &pb.FindEnabledNodeGrantRequest{
@@ -110,8 +129,8 @@ func findHTTPDNSNodeMap(parent *actionutils.ParentAction, nodeID int64) (maps.Ma
loginMap = maps.Map{
"params": maps.Map{
"host": sshInfo.GetString("host"),
"port": sshInfo.GetInt("port"),
"host": sshLoginParams.GetString("host"),
"port": sshLoginParams.GetInt("port"),
},
"grant": grantMap,
}
@@ -169,8 +188,8 @@ func decodeNodeStatus(raw []byte) maps.Map {
func decodeInstallStatus(raw []byte) maps.Map {
result := maps.Map{
"isRunning": false,
"isFinished": true,
"isOk": true,
"isFinished": false,
"isOk": false,
"error": "",
"errorCode": "",
}

View File

@@ -47,39 +47,62 @@ func (this *UpdateAction) RunGet(params struct {
sshPort := 22
ipAddresses := []maps.Map{}
var grantId int64
var loginId int64
this.Data["grant"] = nil
resp, err := this.RPC().HTTPDNSNodeRPC().FindHTTPDNSNode(this.AdminContext(), &pb.FindHTTPDNSNodeRequest{
NodeId: params.NodeId,
})
if err == nil && resp.GetNode() != nil {
// 从 NodeLogin 读取 SSH 信息
if resp.GetNode().GetNodeLogin() != nil {
nodeLogin := resp.GetNode().GetNodeLogin()
loginId = nodeLogin.Id
if len(nodeLogin.Params) > 0 {
sshLoginParams := maps.Map{}
_ = json.Unmarshal(nodeLogin.Params, &sshLoginParams)
if h := strings.TrimSpace(sshLoginParams.GetString("host")); len(h) > 0 {
sshHost = h
}
if p := sshLoginParams.GetInt("port"); p > 0 {
sshPort = p
}
grantId = sshLoginParams.GetInt64("grantId")
}
}
// IP 地址仍从 installStatus 读取
if len(resp.GetNode().GetInstallStatusJSON()) > 0 {
installStatus := maps.Map{}
_ = json.Unmarshal(resp.GetNode().GetInstallStatusJSON(), &installStatus)
sshInfo := installStatus.GetMap("ssh")
if sshInfo != nil {
if h := strings.TrimSpace(sshInfo.GetString("host")); len(h) > 0 {
sshHost = h
}
if p := sshInfo.GetInt("port"); p > 0 {
sshPort = p
}
grantId = sshInfo.GetInt64("grantId")
}
if installStatus.Has("ipAddresses") {
for _, addr := range installStatus.GetSlice("ipAddresses") {
if addrMap, ok := addr.(map[string]interface{}); ok {
ipAddresses = append(ipAddresses, maps.Map(addrMap))
m := maps.Map(addrMap)
// 确保必要字段存在,防止前端组件报错
if !m.Has("name") {
m["name"] = ""
}
if !m.Has("canAccess") {
m["canAccess"] = true
}
if !m.Has("isOn") {
m["isOn"] = true
}
if !m.Has("isUp") {
m["isUp"] = true
}
ipAddresses = append(ipAddresses, m)
}
}
} else if ip := strings.TrimSpace(installStatus.GetString("ipAddr")); len(ip) > 0 {
ipAddresses = append(ipAddresses, maps.Map{
"ip": ip,
"name": "",
"ip": ip,
"name": "",
"canAccess": true,
"isOn": true,
"isUp": true,
"isOn": true,
"isUp": true,
})
}
}
@@ -87,6 +110,7 @@ func (this *UpdateAction) RunGet(params struct {
this.Data["sshHost"] = sshHost
this.Data["sshPort"] = sshPort
this.Data["loginId"] = loginId
this.Data["ipAddresses"] = ipAddresses
if grantId > 0 {
@@ -107,11 +131,12 @@ func (this *UpdateAction) RunGet(params struct {
}
func (this *UpdateAction) RunPost(params struct {
NodeId int64
Name string
ClusterId int64
IsOn bool
SshHost string
NodeId int64
Name string
ClusterId int64
IsOn bool
LoginId int64
SshHost string
SshPort int
GrantId int64
IpAddressesJSON []byte
@@ -155,8 +180,6 @@ func (this *UpdateAction) RunPost(params struct {
}
}
needUpdateInstallStatus := hasSSHUpdate || len(ipAddresses) > 0
resp, err := this.RPC().HTTPDNSNodeRPC().FindHTTPDNSNode(this.AdminContext(), &pb.FindHTTPDNSNodeRequest{
NodeId: params.NodeId,
})
@@ -186,7 +209,31 @@ func (this *UpdateAction) RunPost(params struct {
return
}
if needUpdateInstallStatus {
// SSH 保存到 NodeLogin
if hasSSHUpdate {
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.ErrorPage(err)
return
}
}
// IP 地址仍保存在 installStatus 中
if len(ipAddresses) > 0 {
installStatus := maps.Map{
"isRunning": false,
"isFinished": true,
@@ -197,19 +244,10 @@ func (this *UpdateAction) RunPost(params struct {
if len(node.GetInstallStatusJSON()) > 0 {
_ = json.Unmarshal(node.GetInstallStatusJSON(), &installStatus)
}
if hasSSHUpdate {
installStatus["ssh"] = maps.Map{
"host": params.SshHost,
"port": params.SshPort,
"grantId": params.GrantId,
}
}
if len(ipAddresses) > 0 {
installStatus["ipAddresses"] = ipAddresses
} else {
delete(installStatus, "ipAddresses")
delete(installStatus, "ipAddr") // Cleanup legacy
}
installStatus["ipAddresses"] = ipAddresses
// 清理旧的 ssh 字段
delete(installStatus, "ssh")
delete(installStatus, "ipAddr")
installStatusJSON, _ := json.Marshal(installStatus)
_, err = this.RPC().HTTPDNSNodeRPC().UpdateHTTPDNSNodeStatus(this.AdminContext(), &pb.UpdateHTTPDNSNodeStatusRequest{

View File

@@ -34,7 +34,7 @@ func (this *UpdateInstallStatusAction) RunPost(params struct {
_ = json.Unmarshal(node.GetInstallStatusJSON(), &installStatus)
}
installStatus["isRunning"] = false
installStatus["isFinished"] = true
installStatus["isFinished"] = params.IsInstalled // 标记为未安装时重置为"未开始"状态
installStatus["isOk"] = params.IsInstalled
installStatus["error"] = ""
installStatus["errorCode"] = ""

View File

@@ -10,10 +10,8 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
)
type ClusterSettingsAction struct {
@@ -49,11 +47,9 @@ func (this *ClusterSettingsAction) RunGet(params struct {
"fallbackTimeout": cluster.GetInt("fallbackTimeout"),
"installDir": cluster.GetString("installDir"),
"isOn": cluster.GetBool("isOn"),
"isDefaultCluster": cluster.GetBool("isDefault"),
"isDefaultBackupCluster": false,
}
if settings.GetInt("cacheTtl") <= 0 {
settings["cacheTtl"] = 30
settings["cacheTtl"] = 60
}
if settings.GetInt("fallbackTimeout") <= 0 {
settings["fallbackTimeout"] = 300
@@ -62,22 +58,9 @@ func (this *ClusterSettingsAction) RunGet(params struct {
settings["installDir"] = "/opt/edge-httpdns"
}
defaultBackupResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{
Code: string(systemconfigs.SettingCodeHTTPDNSDefaultBackupClusterId),
})
if err != nil {
this.ErrorPage(err)
return
}
defaultBackupClusterId := int64(0)
if defaultBackupResp != nil && len(defaultBackupResp.GetValueJSON()) > 0 {
defaultBackupClusterId = types.Int64(string(defaultBackupResp.GetValueJSON()))
}
settings["isDefaultBackupCluster"] = defaultBackupClusterId == params.ClusterId
listenAddresses := []*serverconfigs.NetworkAddressConfig{
{
Protocol: serverconfigs.ProtocolHTTPS,
Protocol: serverconfigs.ProtocolTLS,
Host: "",
PortRange: "443",
},
@@ -126,9 +109,7 @@ func (this *ClusterSettingsAction) RunPost(params struct {
CacheTtl int32
FallbackTimeout int32
InstallDir string
IsOn bool
IsDefaultCluster bool
IsDefaultBackupCluster bool
IsOn bool
Addresses []byte
SslPolicyJSON []byte
@@ -145,7 +126,7 @@ func (this *ClusterSettingsAction) RunPost(params struct {
params.Must.Field("gatewayDomain", params.GatewayDomain).Require("请输入服务域名")
if params.CacheTtl <= 0 {
params.CacheTtl = 30
params.CacheTtl = 60
}
if params.FallbackTimeout <= 0 {
params.FallbackTimeout = 300
@@ -153,18 +134,6 @@ func (this *ClusterSettingsAction) RunPost(params struct {
if len(params.InstallDir) == 0 {
params.InstallDir = "/opt/edge-httpdns"
}
if params.IsDefaultCluster && !params.IsOn {
this.Fail("默认主集群必须保持启用状态")
return
}
if params.IsDefaultBackupCluster && !params.IsOn {
this.Fail("默认备用集群必须保持启用状态")
return
}
if params.IsDefaultCluster && params.IsDefaultBackupCluster {
this.Fail("默认主集群和默认备用集群不能是同一个集群")
return
}
cluster, err := findClusterMap(this.Parent(), params.ClusterId)
if err != nil {
@@ -213,35 +182,7 @@ func (this *ClusterSettingsAction) RunPost(params struct {
InstallDir: params.InstallDir,
TlsPolicyJSON: tlsPolicyJSON,
IsOn: params.IsOn,
IsDefault: params.IsDefaultCluster,
})
if err != nil {
this.ErrorPage(err)
return
}
backupClusterValue := int64(0)
if params.IsDefaultBackupCluster {
backupClusterValue = params.ClusterId
} else {
readResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{
Code: string(systemconfigs.SettingCodeHTTPDNSDefaultBackupClusterId),
})
if err != nil {
this.ErrorPage(err)
return
}
if readResp != nil && len(readResp.GetValueJSON()) > 0 {
oldBackupClusterId := types.Int64(string(readResp.GetValueJSON()))
if oldBackupClusterId != params.ClusterId {
backupClusterValue = oldBackupClusterId
}
}
}
_, err = this.RPC().SysSettingRPC().UpdateSysSetting(this.AdminContext(), &pb.UpdateSysSettingRequest{
Code: string(systemconfigs.SettingCodeHTTPDNSDefaultBackupClusterId),
ValueJSON: []byte(strconv.FormatInt(backupClusterValue, 10)),
IsDefault: false,
})
if err != nil {
this.ErrorPage(err)

View File

@@ -1,13 +1,11 @@
package clusters
import (
"strconv"
"strings"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/httpdns/httpdnsutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
"github.com/iwind/TeaGo/actions"
)
@@ -25,14 +23,12 @@ func (this *CreateAction) RunGet(params struct{}) {
}
func (this *CreateAction) RunPost(params struct {
Name string
GatewayDomain string
CacheTtl int32
FallbackTimeout int32
InstallDir string
IsOn bool
IsDefaultPrimary bool
IsDefaultBackup bool
Name string
GatewayDomain string
CacheTtl int32
FallbackTimeout int32
InstallDir string
IsOn bool
Must *actions.Must
}) {
@@ -43,7 +39,7 @@ func (this *CreateAction) RunPost(params struct {
params.InstallDir = "/opt/edge-httpdns"
}
if params.CacheTtl <= 0 {
params.CacheTtl = 30
params.CacheTtl = 60
}
if params.FallbackTimeout <= 0 {
params.FallbackTimeout = 300
@@ -52,19 +48,6 @@ func (this *CreateAction) RunPost(params struct {
params.Must.Field("name", params.Name).Require("请输入集群名称")
params.Must.Field("gatewayDomain", params.GatewayDomain).Require("请输入服务域名")
if params.IsDefaultPrimary && !params.IsOn {
this.Fail("默认主集群必须保持启用状态")
return
}
if params.IsDefaultBackup && !params.IsOn {
this.Fail("默认备用集群必须保持启用状态")
return
}
if params.IsDefaultPrimary && params.IsDefaultBackup {
this.Fail("默认主集群和默认备用集群不能是同一个集群")
return
}
resp, err := this.RPC().HTTPDNSClusterRPC().CreateHTTPDNSCluster(this.AdminContext(), &pb.CreateHTTPDNSClusterRequest{
Name: params.Name,
ServiceDomain: params.GatewayDomain,
@@ -72,24 +55,13 @@ func (this *CreateAction) RunPost(params struct {
FallbackTimeoutMs: params.FallbackTimeout,
InstallDir: params.InstallDir,
IsOn: params.IsOn,
IsDefault: params.IsDefaultPrimary,
IsDefault: false,
})
if err != nil {
this.ErrorPage(err)
return
}
if params.IsDefaultBackup {
_, err = this.RPC().SysSettingRPC().UpdateSysSetting(this.AdminContext(), &pb.UpdateSysSettingRequest{
Code: string(systemconfigs.SettingCodeHTTPDNSDefaultBackupClusterId),
ValueJSON: []byte(strconv.FormatInt(resp.GetClusterId(), 10)),
})
if err != nil {
this.ErrorPage(err)
return
}
}
this.Data["clusterId"] = resp.GetClusterId()
this.Success()
}

View File

@@ -37,10 +37,29 @@ func listClusterMaps(parent *actionutils.ParentAction, keyword string) ([]maps.M
}
}
port := "443"
if rawTLS := cluster.GetTlsPolicyJSON(); len(rawTLS) > 0 {
tlsConfig := maps.Map{}
if err := json.Unmarshal(rawTLS, &tlsConfig); err == nil {
if listenRaw := tlsConfig.Get("listen"); listenRaw != nil {
var listenAddresses []maps.Map
if data, err := json.Marshal(listenRaw); err == nil {
if err := json.Unmarshal(data, &listenAddresses); err == nil {
if len(listenAddresses) > 0 && len(listenAddresses[0].GetString("portRange")) > 0 {
port = listenAddresses[0].GetString("portRange")
}
}
}
}
}
}
apiAddress := "https://" + cluster.GetServiceDomain() + ":" + port
result = append(result, maps.Map{
"id": cluster.GetId(),
"name": cluster.GetName(),
"gatewayDomain": cluster.GetServiceDomain(),
"apiAddress": apiAddress,
"defaultTTL": cluster.GetDefaultTTL(),
"fallbackTimeout": cluster.GetFallbackTimeoutMs(),
"installDir": cluster.GetInstallDir(),
@@ -86,7 +105,7 @@ func findClusterMap(parent *actionutils.ParentAction, clusterID int64) (maps.Map
"id": int64(0),
"name": "",
"gatewayDomain": "",
"defaultTTL": 30,
"defaultTTL": 60,
"fallbackTimeout": 300,
"installDir": "/opt/edge-httpdns",
}, nil

View File

@@ -7,8 +7,8 @@ import (
"strings"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
@@ -47,25 +47,36 @@ func (this *UpdateNodeSSHAction) RunGet(params struct {
}
this.Data["loginId"] = 0
if resp.GetNode() != nil && len(resp.GetNode().GetInstallStatusJSON()) > 0 {
installStatus := maps.Map{}
_ = json.Unmarshal(resp.GetNode().GetInstallStatusJSON(), &installStatus)
sshInfo := installStatus.GetMap("ssh")
if sshInfo != nil {
if host := strings.TrimSpace(sshInfo.GetString("host")); len(host) > 0 {
loginParams["host"] = host
}
if port := sshInfo.GetInt("port"); port > 0 {
loginParams["port"] = port
}
if grantID := sshInfo.GetInt64("grantId"); grantID > 0 {
loginParams["grantId"] = grantID
}
// 从 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
this.Data["grant"] = nil
// 认证信息
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()
}
@@ -93,46 +104,23 @@ func (this *UpdateNodeSSHAction) RunPost(params struct {
this.Fail("SSH 主机地址 IP 格式错误")
}
resp, err := this.RPC().HTTPDNSNodeRPC().FindHTTPDNSNode(this.AdminContext(), &pb.FindHTTPDNSNodeRequest{
NodeId: params.NodeId,
})
if err != nil {
this.ErrorPage(err)
return
}
node := resp.GetNode()
if node == nil {
this.Fail("节点不存在")
return
login := &pb.NodeLogin{
Id: params.LoginId,
Name: "SSH",
Type: "ssh",
Params: maps.Map{
"grantId": params.GrantId,
"host": params.SshHost,
"port": params.SshPort,
}.AsJSON(),
}
installStatus := maps.Map{
"isRunning": false,
"isFinished": true,
"isOk": node.GetIsInstalled(),
"error": "",
"errorCode": "",
}
if len(node.GetInstallStatusJSON()) > 0 {
_ = json.Unmarshal(node.GetInstallStatusJSON(), &installStatus)
}
installStatus["ssh"] = maps.Map{
"host": params.SshHost,
"port": params.SshPort,
"grantId": params.GrantId,
}
installStatusJSON, _ := json.Marshal(installStatus)
_, err = this.RPC().HTTPDNSNodeRPC().UpdateHTTPDNSNodeStatus(this.AdminContext(), &pb.UpdateHTTPDNSNodeStatusRequest{
NodeId: params.NodeId,
IsUp: node.GetIsUp(),
IsInstalled: node.GetIsInstalled(),
IsActive: node.GetIsActive(),
StatusJSON: node.GetStatusJSON(),
InstallStatusJSON: installStatusJSON,
_, err := this.RPC().HTTPDNSNodeRPC().UpdateHTTPDNSNodeLogin(this.AdminContext(), &pb.UpdateHTTPDNSNodeLoginRequest{
NodeId: params.NodeId,
NodeLogin: login,
})
if err != nil {
this.ErrorPage(err)
this.Fail("保存SSH登录信息失败: " + err.Error())
return
}