2020-11-12 14:41:34 +08:00
|
|
|
package domainutils
|
|
|
|
|
|
|
|
|
|
import (
|
2021-06-01 16:42:25 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
2020-11-16 13:03:45 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
|
|
|
"github.com/iwind/TeaGo/lists"
|
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
2021-06-01 16:42:25 +08:00
|
|
|
"net"
|
2020-11-12 14:41:34 +08:00
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2021-06-01 16:42:25 +08:00
|
|
|
// ValidateDomainFormat 校验域名格式
|
2020-11-12 14:41:34 +08:00
|
|
|
func ValidateDomainFormat(domain string) bool {
|
|
|
|
|
pieces := strings.Split(domain, ".")
|
|
|
|
|
for _, piece := range pieces {
|
2021-06-01 16:42:25 +08:00
|
|
|
if piece == "-" ||
|
|
|
|
|
strings.HasPrefix(piece, "-") ||
|
|
|
|
|
strings.HasSuffix(piece, "-") ||
|
|
|
|
|
strings.Contains(piece, "--") ||
|
|
|
|
|
len(piece) > 63 ||
|
|
|
|
|
!regexp.MustCompile(`^[a-z0-9-]+$`).MatchString(piece) {
|
2020-11-12 14:41:34 +08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 16:42:25 +08:00
|
|
|
// 最后一段不能是全数字
|
|
|
|
|
if regexp.MustCompile(`^(\d+)$`).MatchString(pieces[len(pieces)-1]) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 14:41:34 +08:00
|
|
|
return true
|
|
|
|
|
}
|
2020-11-16 13:03:45 +08:00
|
|
|
|
2021-06-01 16:42:25 +08:00
|
|
|
// ConvertRoutesToMaps 转换线路列表
|
2020-11-16 13:03:45 +08:00
|
|
|
func ConvertRoutesToMaps(routes []*pb.DNSRoute) []maps.Map {
|
|
|
|
|
result := []maps.Map{}
|
|
|
|
|
for _, route := range routes {
|
|
|
|
|
result = append(result, maps.Map{
|
|
|
|
|
"name": route.Name,
|
|
|
|
|
"code": route.Code,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 16:42:25 +08:00
|
|
|
// FilterRoutes 筛选线路
|
2020-11-16 13:03:45 +08:00
|
|
|
func FilterRoutes(routes []*pb.DNSRoute, allRoutes []*pb.DNSRoute) []*pb.DNSRoute {
|
|
|
|
|
routeCodes := []string{}
|
|
|
|
|
for _, route := range allRoutes {
|
|
|
|
|
routeCodes = append(routeCodes, route.Code)
|
|
|
|
|
}
|
|
|
|
|
result := []*pb.DNSRoute{}
|
|
|
|
|
for _, route := range routes {
|
|
|
|
|
if lists.ContainsString(routeCodes, route.Code) {
|
|
|
|
|
result = append(result, route)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
2021-06-01 16:42:25 +08:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|