优化域名服务相关功能

This commit is contained in:
刘祥超
2021-06-01 16:42:25 +08:00
parent d834c2517a
commit 201c3e2f97
15 changed files with 204 additions and 41 deletions

View File

@@ -1,26 +1,38 @@
package domainutils
import (
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
"net"
"regexp"
"strings"
)
// 校验域名格式
// ValidateDomainFormat 校验域名格式
func ValidateDomainFormat(domain string) bool {
pieces := strings.Split(domain, ".")
for _, piece := range pieces {
if !regexp.MustCompile(`^[a-z0-9-]+$`).MatchString(piece) {
if piece == "-" ||
strings.HasPrefix(piece, "-") ||
strings.HasSuffix(piece, "-") ||
strings.Contains(piece, "--") ||
len(piece) > 63 ||
!regexp.MustCompile(`^[a-z0-9-]+$`).MatchString(piece) {
return false
}
}
// 最后一段不能是全数字
if regexp.MustCompile(`^(\d+)$`).MatchString(pieces[len(pieces)-1]) {
return false
}
return true
}
// 转换线路列表
// ConvertRoutesToMaps 转换线路列表
func ConvertRoutesToMaps(routes []*pb.DNSRoute) []maps.Map {
result := []maps.Map{}
for _, route := range routes {
@@ -32,7 +44,7 @@ func ConvertRoutesToMaps(routes []*pb.DNSRoute) []maps.Map {
return result
}
// 筛选线路
// FilterRoutes 筛选线路
func FilterRoutes(routes []*pb.DNSRoute, allRoutes []*pb.DNSRoute) []*pb.DNSRoute {
routeCodes := []string{}
for _, route := range allRoutes {
@@ -46,3 +58,84 @@ func FilterRoutes(routes []*pb.DNSRoute, allRoutes []*pb.DNSRoute) []*pb.DNSRout
}
return result
}
// ValidateRecordName 校验记录名
func ValidateRecordName(name string) bool {
if name == "*" || name == "@" || len(name) == 0 {
return true
}
pieces := strings.Split(name, ".")
for _, piece := range pieces {
if piece == "-" ||
strings.HasPrefix(piece, "-") ||
strings.HasSuffix(piece, "-") ||
strings.Contains(piece, "--") ||
len(piece) > 63 ||
!regexp.MustCompile(`^[a-z0-9-]+$`).MatchString(piece) {
return false
}
}
return true
}
// ValidateRecordValue 校验记录值
func ValidateRecordValue(recordType dnsconfigs.RecordType, value string) (message string, ok bool) {
switch recordType {
case dnsconfigs.RecordTypeA:
if !regexp.MustCompile(`^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$`).MatchString(value) {
message = "请输入正确格式的IP"
return
}
if net.ParseIP(value) == nil {
message = "请输入正确格式的IP"
return
}
case dnsconfigs.RecordTypeCNAME:
if strings.HasSuffix(value, ".") {
value = value[:len(value)-1]
}
if !strings.Contains(value, ".") || !ValidateDomainFormat(value) {
message = "请输入正确的域名"
return
}
case dnsconfigs.RecordTypeAAAA:
if !strings.Contains(value, ":") {
message = "请输入正确格式的IPv6地址"
return
}
if net.ParseIP(value) == nil {
message = "请输入正确格式的IPv6地址"
return
}
case dnsconfigs.RecordTypeNS:
if strings.HasSuffix(value, ".") {
value = value[:len(value)-1]
}
if !strings.Contains(value, ".") || !ValidateDomainFormat(value) {
message = "请输入正确的DNS服务器域名"
return
}
case dnsconfigs.RecordTypeMX:
if strings.HasSuffix(value, ".") {
value = value[:len(value)-1]
}
if !strings.Contains(value, ".") || !ValidateDomainFormat(value) {
message = "请输入正确的邮件服务器域名"
return
}
case dnsconfigs.RecordTypeTXT:
if len(value) > 512 {
message = "文本长度不能超出512字节"
return
}
}
if len(value) > 512 {
message = "记录值长度不能超出512字节"
return
}
ok = true
return
}

View File

@@ -0,0 +1,61 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package domainutils
import (
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
"github.com/iwind/TeaGo/assert"
"testing"
)
func TestValidateRecordValue(t *testing.T) {
a := assert.NewAssertion(t)
// A
{
_, ok := ValidateRecordValue(dnsconfigs.RecordTypeA, "1.2")
a.IsFalse(ok)
}
{
_, ok := ValidateRecordValue(dnsconfigs.RecordTypeA, "1.2.3.400")
a.IsFalse(ok)
}
{
_, ok := ValidateRecordValue(dnsconfigs.RecordTypeA, "1.2.3.4")
a.IsTrue(ok)
}
// CNAME
{
_, ok := ValidateRecordValue(dnsconfigs.RecordTypeCNAME, "example.com")
a.IsTrue(ok)
}
{
_, ok := ValidateRecordValue(dnsconfigs.RecordTypeCNAME, "example.com.")
a.IsTrue(ok)
}
{
_, ok := ValidateRecordValue(dnsconfigs.RecordTypeCNAME, "hello, world")
a.IsFalse(ok)
}
// AAAA
{
_, ok := ValidateRecordValue(dnsconfigs.RecordTypeAAAA, "1.2.3.4")
a.IsFalse(ok)
}
{
_, ok := ValidateRecordValue(dnsconfigs.RecordTypeAAAA, "2001:0db8:85a3:0000:0000:8a2e:0370:7334")
a.IsTrue(ok)
}
// NS
{
_, ok := ValidateRecordValue(dnsconfigs.RecordTypeNS, "1.2.3.4")
a.IsFalse(ok)
}
{
_, ok := ValidateRecordValue(dnsconfigs.RecordTypeNS, "example.com")
a.IsTrue(ok)
}
}

View File

@@ -7,6 +7,7 @@ import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/dns/domains/domainutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"strings"
)
type CreateAction struct {
@@ -34,6 +35,8 @@ func (this *CreateAction) RunPost(params struct {
this.CreateLogInfo("创建域名 %d", domainId)
}()
params.Name = strings.ToLower(params.Name)
params.Must.
Field("name", params.Name).
Require("请输入域名").

View File

@@ -4,6 +4,7 @@ package records
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/dns/domains/domainutils"
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
@@ -63,6 +64,17 @@ func (this *CreatePopupAction) RunPost(params struct {
this.CreateLogInfo("创建域名记录 %d", recordId)
}()
// 校验记录名
if !domainutils.ValidateRecordName(params.Name) {
this.FailField("name", "请输入正确的记录名")
}
// 校验记录值
message, ok := domainutils.ValidateRecordValue(params.Type, params.Value)
if !ok {
this.FailField("value", "记录值错误:"+message)
}
createResp, err := this.RPC().NSRecordRPC().CreateNSRecord(this.AdminContext(), &pb.CreateNSRecordRequest{
NsDomainId: params.DomainId,
Description: params.Description,

View File

@@ -4,6 +4,7 @@ package records
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/dns/domains/domainutils"
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
@@ -87,6 +88,17 @@ func (this *UpdatePopupAction) RunPost(params struct {
}) {
this.CreateLogInfo("修改域名记录 %d", params.RecordId)
// 校验记录名
if !domainutils.ValidateRecordName(params.Name) {
this.FailField("name", "请输入正确的记录名")
}
// 校验记录值
message, ok := domainutils.ValidateRecordValue(params.Type, params.Value)
if !ok {
this.FailField("value", "记录值错误:"+message)
}
_, err := this.RPC().NSRecordRPC().UpdateNSRecord(this.AdminContext(), &pb.UpdateNSRecordRequest{
NsRecordId: params.RecordId,
Description: params.Description,

View File

@@ -4,7 +4,6 @@ package domains
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/dns/domains/domainutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
@@ -57,7 +56,6 @@ func (this *UpdateAction) RunGet(params struct {
func (this *UpdateAction) RunPost(params struct {
DomainId int64
Name string
ClusterId int64
UserId int64
IsOn bool
@@ -68,15 +66,6 @@ func (this *UpdateAction) RunPost(params struct {
this.CreateLogInfo("修改域名 %d", params.DomainId)
params.Must.
Field("name", params.Name).
Require("请输入域名").
Expect(func() (message string, success bool) {
success = domainutils.ValidateDomainFormat(params.Name)
if !success {
message = "请输入正确的域名"
}
return
}).
Field("clusterId", params.ClusterId).
Gt(0, "请选择所属集群")
@@ -84,7 +73,6 @@ func (this *UpdateAction) RunPost(params struct {
NsDomainId: params.DomainId,
NsClusterId: params.ClusterId,
UserId: params.UserId,
Name: params.Name,
IsOn: params.IsOn,
})
if err != nil {