优化智能DNS相关代码

This commit is contained in:
GoEdgeLab
2022-09-24 14:07:19 +08:00
parent e6e73991cf
commit a0191fc546
6 changed files with 106 additions and 0 deletions

View File

@@ -17,9 +17,12 @@ type NSCluster struct {
Udp dbs.JSON `field:"udp"` // UDP设置
DdosProtection dbs.JSON `field:"ddosProtection"` // DDoS防护设置
Hosts dbs.JSON `field:"hosts"` // DNS主机地址
Soa dbs.JSON `field:"soa"` // SOA配置
AutoRemoteStart bool `field:"autoRemoteStart"` // 自动远程启动
TimeZone string `field:"timeZone"` // 时区
Answer dbs.JSON `field:"answer"` // 应答设置
SoaSerial uint64 `field:"soaSerial"` // SOA序列号
Email string `field:"email"` // 管理员邮箱
}
type NSClusterOperator struct {
@@ -36,9 +39,12 @@ type NSClusterOperator struct {
Udp any // UDP设置
DdosProtection any // DDoS防护设置
Hosts any // DNS主机地址
Soa any // SOA配置
AutoRemoteStart any // 自动远程启动
TimeZone any // 时区
Answer any // 应答设置
SoaSerial any // SOA序列号
Email any // 管理员邮箱
}
func NewNSClusterOperator() *NSClusterOperator {

View File

@@ -45,6 +45,19 @@ func (this *NSCluster) DecodeHosts() []string {
return hosts
}
// DecodeSOAConfig 解析SOA设置
func (this *NSCluster) DecodeSOAConfig() *dnsconfigs.NSSOAConfig {
var config = dnsconfigs.DefaultNSSOAConfig()
if IsNull(this.Soa) {
return config
}
err := json.Unmarshal(this.Soa, config)
if err != nil {
remotelogs.Error("NSCluster.DecodeSOAConfig", "decode failed: "+err.Error())
}
return config
}
// DecodeAnswerConfig 解析应答设置
func (this *NSCluster) DecodeAnswerConfig() *dnsconfigs.NSAnswerConfig {
var config = dnsconfigs.DefaultNSAnswerConfig()

15
internal/utils/compare.go Normal file
View File

@@ -0,0 +1,15 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package utils
import (
"bytes"
"encoding/json"
)
// EqualConfig 使用JSON对比配置
func EqualConfig(config1 any, config2 any) bool {
config1JSON, _ := json.Marshal(config1)
config2JSON, _ := json.Marshal(config2)
return bytes.Equal(config1JSON, config2JSON)
}

View File

@@ -0,0 +1,38 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package utils_test
import (
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"testing"
)
func TestEqualConfig(t *testing.T) {
type testType struct {
Name string `json:"name"`
Age int `json:"age"`
}
{
var c1 = &testType{
Name: "Lily",
Age: 12,
}
var c2 = &testType{
Name: "Lucy",
Age: 12,
}
t.Log(utils.EqualConfig(c1, c2))
}
{
var c1 = &testType{
Name: "Lily",
Age: 12,
}
var c2 = &testType{
Age: 12,
Name: "Lily",
}
t.Log(utils.EqualConfig(c1, c2))
}
}

12
internal/utils/email.go Normal file
View File

@@ -0,0 +1,12 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package utils
import "regexp"
var emailReg = regexp.MustCompile(`(?i)^[a-z\d]+([._+-]*[a-z\d]+)*@([a-z\d]+[a-z\d-]*[a-z\d]+\.)+[a-z\d]+$`)
// ValidateEmail 校验电子邮箱格式
func ValidateEmail(email string) bool {
return emailReg.MatchString(email)
}

View File

@@ -0,0 +1,22 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package utils_test
import (
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/iwind/TeaGo/assert"
"testing"
)
func TestValidateEmail(t *testing.T) {
var a = assert.NewAssertion(t)
a.IsTrue(utils.ValidateEmail("aaaa@gmail.com"))
a.IsTrue(utils.ValidateEmail("a.b@gmail.com"))
a.IsTrue(utils.ValidateEmail("a.b.c.d@gmail.com"))
a.IsTrue(utils.ValidateEmail("aaaa@gmail.com.cn"))
a.IsTrue(utils.ValidateEmail("hello.world.123@gmail.123.com"))
a.IsTrue(utils.ValidateEmail("10000@qq.com"))
a.IsFalse(utils.ValidateEmail("aaaa.@gmail.com"))
a.IsFalse(utils.ValidateEmail("aaaa@gmail"))
a.IsFalse(utils.ValidateEmail("aaaa@123"))
}