104 lines
2.7 KiB
Go
104 lines
2.7 KiB
Go
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
|
|
package domains
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
|
|
"github.com/TeaOSLab/EdgeUser/internal/web/actions/default/ns/domains/domainutils"
|
|
"github.com/iwind/TeaGo/actions"
|
|
)
|
|
|
|
type HealthCheckAction struct {
|
|
actionutils.ParentAction
|
|
}
|
|
|
|
func (this *HealthCheckAction) Init() {
|
|
this.Nav("", "", "healthCheck")
|
|
}
|
|
|
|
func (this *HealthCheckAction) RunGet(params struct {
|
|
DomainId int64
|
|
}) {
|
|
err := domainutils.InitDomain(this.Parent(), params.DomainId)
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
this.Data["domainId"] = params.DomainId
|
|
|
|
// 检查当前用户权限
|
|
canUpdate, err := domainutils.CheckHealthCheckPermission(this.RPC(), this.UserContext(), params.DomainId)
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
this.Data["canUpdate"] = canUpdate
|
|
|
|
// 健康检查配置
|
|
healthCheckResp, err := this.RPC().NSDomainRPC().FindNSDomainRecordsHealthCheck(this.UserContext(), &pb.FindNSDomainRecordsHealthCheckRequest{NsDomainId: params.DomainId})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
var healthCheckConfig = dnsconfigs.NewNSRecordsHealthCheckConfig()
|
|
if len(healthCheckResp.NsDomainRecordsHealthCheckJSON) > 0 {
|
|
err = json.Unmarshal(healthCheckResp.NsDomainRecordsHealthCheckJSON, healthCheckConfig)
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
}
|
|
this.Data["config"] = healthCheckConfig
|
|
|
|
this.Show()
|
|
}
|
|
|
|
func (this *HealthCheckAction) RunPost(params struct {
|
|
DomainId int64
|
|
RecordsHealthCheckJSON []byte
|
|
|
|
Must *actions.Must
|
|
CSRF *actionutils.CSRF
|
|
}) {
|
|
defer this.CreateLogInfo(codes.NSDomain_LogUpdateNSDomainHealthCheck, params.DomainId)
|
|
|
|
canUpdate, err := domainutils.CheckHealthCheckPermission(this.RPC(), this.UserContext(), params.DomainId)
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
if !canUpdate {
|
|
this.Fail("当前用户没有权限修改健康检查设置")
|
|
return
|
|
}
|
|
|
|
var config = dnsconfigs.NewNSRecordsHealthCheckConfig()
|
|
err = json.Unmarshal(params.RecordsHealthCheckJSON, config)
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
err = config.Init()
|
|
if err != nil {
|
|
this.Fail("校验配置失败:" + err.Error())
|
|
return
|
|
}
|
|
|
|
_, err = this.RPC().NSDomainRPC().UpdateNSDomainRecordsHealthCheck(this.UserContext(), &pb.UpdateNSDomainRecordsHealthCheckRequest{
|
|
NsDomainId: params.DomainId,
|
|
NsDomainRecordsHealthCheckJSON: params.RecordsHealthCheckJSON,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
this.Success()
|
|
}
|