Initial commit (code only without large binaries)
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package domainutils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/rpc"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
)
|
||||
|
||||
// InitDomain 初始化域名信息
|
||||
func InitDomain(parent *actionutils.ParentAction, domainId int64) error {
|
||||
rpcClient, err := rpc.SharedRPC()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
domainResp, err := rpcClient.NSDomainRPC().FindNSDomain(parent.UserContext(), &pb.FindNSDomainRequest{NsDomainId: domainId})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var domain = domainResp.NsDomain
|
||||
if domain == nil {
|
||||
return errors.New("InitDomain: can not find domain with id '" + types.String(domainId) + "'")
|
||||
}
|
||||
|
||||
// 记录数量
|
||||
countRecordsResp, err := rpcClient.NSRecordRPC().CountAllNSRecords(parent.UserContext(), &pb.CountAllNSRecordsRequest{
|
||||
NsDomainId: domainId,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var countRecords = countRecordsResp.Count
|
||||
|
||||
// Key数量
|
||||
countKeysResp, err := rpcClient.NSKeyRPC().CountAllNSKeys(parent.UserContext(), &pb.CountAllNSKeysRequest{
|
||||
NsDomainId: domainId,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var countKeys = countKeysResp.Count
|
||||
|
||||
// 健康检查
|
||||
var healthCheckConfig = dnsconfigs.NewNSRecordsHealthCheckConfig()
|
||||
if len(domain.RecordsHealthCheckJSON) > 0 {
|
||||
err = json.Unmarshal(domain.RecordsHealthCheckJSON, healthCheckConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
parent.Data["domain"] = maps.Map{
|
||||
"id": domain.Id,
|
||||
"name": domain.Name,
|
||||
"countRecords": countRecords,
|
||||
"countKeys": countKeys,
|
||||
"enableHealthCheck": healthCheckConfig.IsOn,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// VerifyMessageWithCode 通过代号获取验证消息
|
||||
func VerifyMessageWithCode(code string) string {
|
||||
var message = ""
|
||||
switch code {
|
||||
case "DomainNotFound":
|
||||
message = "无法找到域名"
|
||||
case "InvalidStatus":
|
||||
message = "在当前域名状态下无法提交验证"
|
||||
case "InvalidDNSHosts":
|
||||
message = "没有设置正确的DNS主机地址或者NS记录尚未生效"
|
||||
case "TXTNotFound":
|
||||
message = "没有找到TXT记录或者TXT记录尚未生效"
|
||||
case "InvalidTXT":
|
||||
message = "没有设置正确的TXT记录或者TXT记录尚未生效"
|
||||
case "TXTExpired":
|
||||
message = "验证信息已失效,请刷新后重试"
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
||||
// CheckHealthCheckPermission 检查是否能够修改健康检查
|
||||
func CheckHealthCheckPermission(rpcClient *rpc.RPCClient, ctx context.Context, domainId int64) (canUpdate bool, err error) {
|
||||
if domainId <= 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
domainResp, err := rpcClient.NSDomainRPC().FindNSDomain(ctx, &pb.FindNSDomainRequest{NsDomainId: domainId})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
var domain = domainResp.NsDomain
|
||||
if domain == nil {
|
||||
return false, errors.New("the domain is not found")
|
||||
}
|
||||
if domain.UserId > 0 {
|
||||
// 检查套餐
|
||||
userPlanResp, err := rpcClient.NSUserPlanRPC().FindNSUserPlan(ctx, &pb.FindNSUserPlanRequest{
|
||||
UserId: domain.UserId,
|
||||
NsUserPlanId: 0,
|
||||
})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
var userPlan = userPlanResp.NsUserPlan
|
||||
if userPlan == nil || userPlan.NsPlanId == 0 || userPlan.DayTo < timeutil.Format("Ymd") {
|
||||
return checkHealthCheckInUserSetting(rpcClient, ctx)
|
||||
} else {
|
||||
planResp, err := rpcClient.NSPlanRPC().FindNSPlan(ctx, &pb.FindNSPlanRequest{NsPlanId: userPlan.NsPlanId})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if planResp.NsPlan == nil || len(planResp.NsPlan.ConfigJSON) == 0 {
|
||||
return checkHealthCheckInUserSetting(rpcClient, ctx)
|
||||
}
|
||||
var planConfig = dnsconfigs.DefaultNSPlanConfig()
|
||||
err = json.Unmarshal(planResp.NsPlan.ConfigJSON, planConfig)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return planConfig.SupportHealthCheck, nil
|
||||
}
|
||||
} else {
|
||||
return checkHealthCheckInUserSetting(rpcClient, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// 检查用户基础设置
|
||||
func checkHealthCheckInUserSetting(rpcClient *rpc.RPCClient, ctx context.Context) (bool, error) {
|
||||
resp, err := rpcClient.SysSettingRPC().ReadSysSetting(ctx, &pb.ReadSysSettingRequest{
|
||||
Code: systemconfigs.SettingCodeNSUserConfig,
|
||||
})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
var config = dnsconfigs.NewNSUserConfig()
|
||||
if len(resp.ValueJSON) > 0 {
|
||||
err = json.Unmarshal(resp.ValueJSON, config)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if config.DefaultPlanConfig == nil {
|
||||
config.DefaultPlanConfig = dnsconfigs.DefaultNSUserPlanConfig()
|
||||
}
|
||||
return config.DefaultPlanConfig.SupportHealthCheck, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
Reference in New Issue
Block a user