1.4.5.2
This commit is contained in:
@@ -0,0 +1,253 @@
|
||||
//go:build plus
|
||||
|
||||
package clusterutils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// ClusterHelper 单个集群的帮助
|
||||
type ClusterHelper struct {
|
||||
helpers.LangHelper
|
||||
}
|
||||
|
||||
func NewClusterHelper() *ClusterHelper {
|
||||
return &ClusterHelper{}
|
||||
}
|
||||
|
||||
func (this *ClusterHelper) BeforeAction(actionPtr actions.ActionWrapper) (goNext bool) {
|
||||
var action = actionPtr.Object()
|
||||
if action.Request.Method != http.MethodGet {
|
||||
return true
|
||||
}
|
||||
|
||||
action.Data["teaMenu"] = "ns"
|
||||
|
||||
selectedTabbar := action.Data.GetString("mainTab")
|
||||
clusterId := action.ParamInt64("clusterId")
|
||||
clusterIdString := strconv.FormatInt(clusterId, 10)
|
||||
action.Data["clusterId"] = clusterId
|
||||
|
||||
if clusterId > 0 {
|
||||
rpcClient, err := rpc.SharedRPC()
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
clusterResp, err := rpcClient.NSClusterRPC().FindNSCluster(actionPtr.(actionutils.ActionInterface).AdminContext(), &pb.FindNSClusterRequest{
|
||||
NsClusterId: clusterId,
|
||||
})
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
var cluster = clusterResp.NsCluster
|
||||
if cluster == nil {
|
||||
action.WriteString("can not find ns cluster")
|
||||
return
|
||||
}
|
||||
action.Data["currentCluster"] = maps.Map{
|
||||
"id": cluster.Id,
|
||||
"name": cluster.Name,
|
||||
}
|
||||
|
||||
var nodeId = action.ParamInt64("nodeId")
|
||||
var isInCluster = nodeId <= 0
|
||||
|
||||
var tabbar = actionutils.NewTabbar()
|
||||
{
|
||||
var url = "/ns/clusters"
|
||||
if !isInCluster {
|
||||
url = "/ns/clusters/cluster?clusterId=" + clusterIdString
|
||||
}
|
||||
tabbar.Add("", "", url, "arrow left", false)
|
||||
}
|
||||
{
|
||||
var url = "/ns/clusters/cluster?clusterId=" + clusterIdString
|
||||
var item = tabbar.Add(cluster.Name, "", url, "angle right", true)
|
||||
item.IsTitle = true
|
||||
}
|
||||
{
|
||||
var item = tabbar.Add(this.Lang(action, codes.NSCluster_TabNodes), "", "/ns/clusters/cluster?clusterId="+clusterIdString, "server", selectedTabbar == "node")
|
||||
item.IsDisabled = !isInCluster
|
||||
}
|
||||
{
|
||||
var item = tabbar.Add(this.Lang(action, codes.NSCluster_TabSetting), "", "/ns/clusters/cluster/settings?clusterId="+clusterIdString, "setting", selectedTabbar == "setting")
|
||||
item.IsDisabled = !isInCluster
|
||||
}
|
||||
{
|
||||
var item = tabbar.Add(this.Lang(action, codes.NSCluster_TabDelete), "", "/ns/clusters/cluster/delete?clusterId="+clusterIdString, "trash", selectedTabbar == "delete")
|
||||
item.IsDisabled = !isInCluster
|
||||
}
|
||||
actionutils.SetTabbar(action, tabbar)
|
||||
|
||||
// 左侧菜单
|
||||
secondMenuItem := action.Data.GetString("secondMenuItem")
|
||||
switch selectedTabbar {
|
||||
case "setting":
|
||||
var menuItems = this.createSettingMenu(cluster, secondMenuItem, actionPtr)
|
||||
action.Data["leftMenuItems"] = menuItems
|
||||
|
||||
// 当前菜单
|
||||
action.Data["leftMenuActiveItem"] = nil
|
||||
for _, item := range menuItems {
|
||||
if item.GetBool("isActive") {
|
||||
action.Data["leftMenuActiveItem"] = item
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// 设置菜单
|
||||
func (this *ClusterHelper) createSettingMenu(cluster *pb.NSCluster, selectedItem string, actionPtr actions.ActionWrapper) (items []maps.Map) {
|
||||
var clusterId = types.String(cluster.Id)
|
||||
|
||||
// TCP
|
||||
var tcpConfig = &serverconfigs.TCPProtocolConfig{}
|
||||
if len(cluster.TcpJSON) > 0 {
|
||||
err := json.Unmarshal(cluster.TcpJSON, tcpConfig)
|
||||
if err != nil {
|
||||
// ignore error
|
||||
logs.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// TLS
|
||||
var tlsConfig = &serverconfigs.TLSProtocolConfig{}
|
||||
if len(cluster.TlsJSON) > 0 {
|
||||
err := json.Unmarshal(cluster.TlsJSON, tlsConfig)
|
||||
if err != nil {
|
||||
// ignore error
|
||||
logs.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// UDP
|
||||
var udpConfig = &serverconfigs.UDPProtocolConfig{}
|
||||
if len(cluster.UdpJSON) > 0 {
|
||||
err := json.Unmarshal(cluster.UdpJSON, udpConfig)
|
||||
if err != nil {
|
||||
// ignore error
|
||||
logs.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// DoH
|
||||
var dohConfig = dnsconfigs.NewNSDoHConfig()
|
||||
if len(cluster.DohJSON) > 0 {
|
||||
err := json.Unmarshal(cluster.DohJSON, dohConfig)
|
||||
if err != nil {
|
||||
// ignore error
|
||||
logs.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// 应答
|
||||
var answerConfig = dnsconfigs.DefaultNSAnswerConfig()
|
||||
var answerConfigIsChanged = false
|
||||
if len(cluster.AnswerJSON) > 0 {
|
||||
err := json.Unmarshal(cluster.AnswerJSON, answerConfig)
|
||||
if err != nil {
|
||||
// ignore error
|
||||
} else {
|
||||
answerConfigIsChanged = !answerConfig.IsSame(dnsconfigs.DefaultNSAnswerConfig())
|
||||
}
|
||||
}
|
||||
|
||||
// SOA
|
||||
var soaConfig = dnsconfigs.DefaultNSSOAConfig()
|
||||
var soaConfigIsChanged = false
|
||||
if len(cluster.SoaJSON) > 0 {
|
||||
err := json.Unmarshal(cluster.SoaJSON, soaConfig)
|
||||
if err != nil {
|
||||
// ignore error
|
||||
} else {
|
||||
soaConfigIsChanged = !soaConfig.IsSame(dnsconfigs.DefaultNSSOAConfig())
|
||||
}
|
||||
}
|
||||
|
||||
return []maps.Map{
|
||||
{
|
||||
"name": this.Lang(actionPtr, codes.NSCluster_MenuBasic),
|
||||
"url": "/ns/clusters/cluster/settings?clusterId=" + clusterId,
|
||||
"isActive": selectedItem == "basic",
|
||||
},
|
||||
{
|
||||
"name": "-",
|
||||
},
|
||||
{
|
||||
"name": this.Lang(actionPtr, codes.NSCluster_MenuUDP),
|
||||
"url": "/ns/clusters/cluster/settings/udp?clusterId=" + clusterId,
|
||||
"isActive": selectedItem == "udp",
|
||||
"isOn": udpConfig != nil && udpConfig.IsOn,
|
||||
},
|
||||
{
|
||||
"name": this.Lang(actionPtr, codes.NSCluster_MenuTCP),
|
||||
"url": "/ns/clusters/cluster/settings/tcp?clusterId=" + clusterId,
|
||||
"isActive": selectedItem == "tcp",
|
||||
"isOn": tcpConfig != nil && tcpConfig.IsOn,
|
||||
},
|
||||
{
|
||||
"name": this.Lang(actionPtr, codes.NSCluster_MenuTLS),
|
||||
"url": "/ns/clusters/cluster/settings/tls?clusterId=" + clusterId,
|
||||
"isActive": selectedItem == "tls",
|
||||
"isOn": tlsConfig != nil && tlsConfig.IsOn,
|
||||
},
|
||||
{
|
||||
"name": this.Lang(actionPtr, codes.NSCluster_MenuDoH),
|
||||
"url": "/ns/clusters/cluster/settings/doh?clusterId=" + clusterId,
|
||||
"isActive": selectedItem == "doh",
|
||||
"isOn": dohConfig != nil && dohConfig.IsOn,
|
||||
},
|
||||
{
|
||||
"name": "-",
|
||||
},
|
||||
{
|
||||
"name": this.Lang(actionPtr, codes.NSCluster_MenuAccessLogs),
|
||||
"url": "/ns/clusters/cluster/settings/accessLog?clusterId=" + clusterId,
|
||||
"isActive": selectedItem == "accessLog",
|
||||
},
|
||||
{
|
||||
"name": this.Lang(actionPtr, codes.NSCluster_MenuAnswerSetting),
|
||||
"url": "/ns/clusters/cluster/settings/answer?clusterId=" + clusterId,
|
||||
"isActive": selectedItem == "answer",
|
||||
"isOn": answerConfigIsChanged,
|
||||
},
|
||||
{
|
||||
"name": this.Lang(actionPtr, codes.NSCluster_MenuSOA),
|
||||
"url": "/ns/clusters/cluster/settings/soa?clusterId=" + clusterId,
|
||||
"isActive": selectedItem == "soa",
|
||||
"isOn": soaConfigIsChanged,
|
||||
},
|
||||
{
|
||||
"name": this.Lang(actionPtr, codes.NSCluster_MenuDNSRecursion),
|
||||
"url": "/ns/clusters/cluster/settings/recursion?clusterId=" + clusterId,
|
||||
"isActive": selectedItem == "recursion",
|
||||
},
|
||||
{
|
||||
"name": "-",
|
||||
},
|
||||
{
|
||||
"name": this.Lang(actionPtr, codes.NSCluster_MenuDDoSProtection),
|
||||
"url": "/ns/clusters/cluster/settings/ddos-protection?clusterId=" + clusterId,
|
||||
"isActive": selectedItem == "ddosProtection",
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user