253 lines
7.4 KiB
Go
253 lines
7.4 KiB
Go
package clusters
|
|
|
|
import (
|
|
"encoding/json"
|
|
"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/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 {
|
|
actionutils.ParentAction
|
|
}
|
|
|
|
func (this *ClusterSettingsAction) Init() {
|
|
this.Nav("httpdns", "cluster", "settings")
|
|
}
|
|
|
|
func (this *ClusterSettingsAction) RunGet(params struct {
|
|
ClusterId int64
|
|
Section string
|
|
}) {
|
|
httpdnsutils.AddLeftMenu(this.Parent())
|
|
|
|
cluster, err := findClusterMap(this.Parent(), params.ClusterId)
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
httpdnsutils.AddClusterTabbar(this.Parent(), cluster.GetString("name"), params.ClusterId, "setting")
|
|
|
|
section := strings.TrimSpace(params.Section)
|
|
if len(section) == 0 {
|
|
section = "basic"
|
|
}
|
|
|
|
settings := maps.Map{
|
|
"name": cluster.GetString("name"),
|
|
"gatewayDomain": cluster.GetString("gatewayDomain"),
|
|
"cacheTtl": cluster.GetInt("defaultTTL"),
|
|
"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
|
|
}
|
|
if settings.GetInt("fallbackTimeout") <= 0 {
|
|
settings["fallbackTimeout"] = 300
|
|
}
|
|
if len(settings.GetString("installDir")) == 0 {
|
|
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,
|
|
Host: "",
|
|
PortRange: "443",
|
|
},
|
|
}
|
|
sslPolicy := &sslconfigs.SSLPolicy{
|
|
IsOn: true,
|
|
MinVersion: "TLS 1.1",
|
|
}
|
|
|
|
if rawTLS := strings.TrimSpace(cluster.GetString("tlsPolicyJSON")); len(rawTLS) > 0 {
|
|
tlsConfig := maps.Map{}
|
|
if err := json.Unmarshal([]byte(rawTLS), &tlsConfig); err == nil {
|
|
if listenRaw := tlsConfig.Get("listen"); listenRaw != nil {
|
|
if data, err := json.Marshal(listenRaw); err == nil {
|
|
_ = json.Unmarshal(data, &listenAddresses)
|
|
}
|
|
}
|
|
if sslRaw := tlsConfig.Get("sslPolicy"); sslRaw != nil {
|
|
if data, err := json.Marshal(sslRaw); err == nil {
|
|
_ = json.Unmarshal(data, sslPolicy)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
this.Data["activeSection"] = section
|
|
cid := strconv.FormatInt(params.ClusterId, 10)
|
|
this.Data["leftMenuItems"] = []map[string]interface{}{
|
|
{"name": "基础设置", "url": "/httpdns/clusters/cluster/settings?clusterId=" + cid + "§ion=basic", "isActive": section == "basic"},
|
|
{"name": "TLS", "url": "/httpdns/clusters/cluster/settings?clusterId=" + cid + "§ion=tls", "isActive": section == "tls"},
|
|
}
|
|
this.Data["cluster"] = cluster
|
|
this.Data["settings"] = settings
|
|
this.Data["tlsConfig"] = maps.Map{
|
|
"isOn": true,
|
|
"listen": listenAddresses,
|
|
"sslPolicy": sslPolicy,
|
|
}
|
|
this.Show()
|
|
}
|
|
|
|
func (this *ClusterSettingsAction) RunPost(params struct {
|
|
ClusterId int64
|
|
Name string
|
|
GatewayDomain string
|
|
CacheTtl int32
|
|
FallbackTimeout int32
|
|
InstallDir string
|
|
IsOn bool
|
|
IsDefaultCluster bool
|
|
IsDefaultBackupCluster bool
|
|
|
|
Addresses []byte
|
|
SslPolicyJSON []byte
|
|
|
|
Must *actions.Must
|
|
CSRF *actionutils.CSRF
|
|
}) {
|
|
params.Name = strings.TrimSpace(params.Name)
|
|
params.GatewayDomain = strings.TrimSpace(params.GatewayDomain)
|
|
params.InstallDir = strings.TrimSpace(params.InstallDir)
|
|
|
|
params.Must.Field("clusterId", params.ClusterId).Gt(0, "请选择集群")
|
|
params.Must.Field("name", params.Name).Require("请输入集群名称")
|
|
params.Must.Field("gatewayDomain", params.GatewayDomain).Require("请输入服务域名")
|
|
|
|
if params.CacheTtl <= 0 {
|
|
params.CacheTtl = 30
|
|
}
|
|
if params.FallbackTimeout <= 0 {
|
|
params.FallbackTimeout = 300
|
|
}
|
|
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 {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
tlsConfig := maps.Map{}
|
|
if rawTLS := strings.TrimSpace(cluster.GetString("tlsPolicyJSON")); len(rawTLS) > 0 {
|
|
_ = json.Unmarshal([]byte(rawTLS), &tlsConfig)
|
|
}
|
|
|
|
if len(params.Addresses) > 0 {
|
|
var addresses []*serverconfigs.NetworkAddressConfig
|
|
if err := json.Unmarshal(params.Addresses, &addresses); err != nil {
|
|
this.Fail("监听端口配置格式不正确")
|
|
return
|
|
}
|
|
tlsConfig["listen"] = addresses
|
|
}
|
|
|
|
if len(params.SslPolicyJSON) > 0 {
|
|
sslPolicy := &sslconfigs.SSLPolicy{}
|
|
if err := json.Unmarshal(params.SslPolicyJSON, sslPolicy); err != nil {
|
|
this.Fail("TLS 配置格式不正确")
|
|
return
|
|
}
|
|
tlsConfig["sslPolicy"] = sslPolicy
|
|
}
|
|
|
|
var tlsPolicyJSON []byte
|
|
if len(tlsConfig) > 0 {
|
|
tlsPolicyJSON, err = json.Marshal(tlsConfig)
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
}
|
|
|
|
_, err = this.RPC().HTTPDNSClusterRPC().UpdateHTTPDNSCluster(this.AdminContext(), &pb.UpdateHTTPDNSClusterRequest{
|
|
ClusterId: params.ClusterId,
|
|
Name: params.Name,
|
|
ServiceDomain: params.GatewayDomain,
|
|
DefaultTTL: params.CacheTtl,
|
|
FallbackTimeoutMs: params.FallbackTimeout,
|
|
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)),
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
this.Success()
|
|
}
|