diff --git a/internal/db/models/ns_cluster_model.go b/internal/db/models/ns_cluster_model.go index dadc6fc8..38a6e11b 100644 --- a/internal/db/models/ns_cluster_model.go +++ b/internal/db/models/ns_cluster_model.go @@ -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 { diff --git a/internal/db/models/ns_cluster_model_ext.go b/internal/db/models/ns_cluster_model_ext.go index 2cdcbd22..c487d72a 100644 --- a/internal/db/models/ns_cluster_model_ext.go +++ b/internal/db/models/ns_cluster_model_ext.go @@ -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() diff --git a/internal/utils/compare.go b/internal/utils/compare.go new file mode 100644 index 00000000..f4194b2f --- /dev/null +++ b/internal/utils/compare.go @@ -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) +} diff --git a/internal/utils/compare_test.go b/internal/utils/compare_test.go new file mode 100644 index 00000000..baa9ac9e --- /dev/null +++ b/internal/utils/compare_test.go @@ -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)) + } +} diff --git a/internal/utils/email.go b/internal/utils/email.go new file mode 100644 index 00000000..a729e6e7 --- /dev/null +++ b/internal/utils/email.go @@ -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) +} diff --git a/internal/utils/email_test.go b/internal/utils/email_test.go new file mode 100644 index 00000000..713acf1a --- /dev/null +++ b/internal/utils/email_test.go @@ -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")) +}