Files
yunzerwebsiteallinone/go/services/domain_verify.go
T
2026-11-16 17:22:50 +08:00

138 lines
3.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package services
import (
"fmt"
"net"
"regexp"
"strings"
beego "github.com/beego/beego/v2/server/web"
)
var hostnameRe = regexp.MustCompile(`^([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,}$`)
func NormalizeHost(raw string) string {
h := strings.TrimSpace(strings.ToLower(raw))
if h == "" {
return ""
}
if idx := strings.Index(h, "://"); idx >= 0 {
h = h[idx+3:]
}
if idx := strings.IndexAny(h, "/?#"); idx >= 0 {
h = h[:idx]
}
if idx := strings.LastIndex(h, "@"); idx >= 0 {
h = h[idx+1:]
}
if idx := strings.Index(h, ":"); idx >= 0 {
h = h[:idx]
}
h = strings.TrimSuffix(h, ".")
h = strings.TrimPrefix(h, "*.")
if len(h) > 253 || !hostnameRe.MatchString(h) {
return ""
}
return h
}
type CustomDomainGuide struct {
CnameTarget string `json:"cname_target"`
AIPs []string `json:"a_ips"`
}
func GetCustomDomainGuide() CustomDomainGuide {
target, _ := beego.AppConfig.String("customdomain_cname_target")
target = NormalizeHost(target)
ipsRaw, _ := beego.AppConfig.String("customdomain_a_ips")
ips := make([]string, 0, 2)
for _, item := range strings.Split(ipsRaw, ",") {
item = strings.TrimSpace(item)
if item == "" {
continue
}
if net.ParseIP(item) == nil {
continue
}
ips = append(ips, item)
}
return CustomDomainGuide{CnameTarget: target, AIPs: ips}
}
func VerifyCustomDomainDNS(host string) (bool, string) {
host = NormalizeHost(host)
if host == "" {
return false, "域名格式不正确"
}
guide := GetCustomDomainGuide()
if guide.CnameTarget == "" && len(guide.AIPs) == 0 {
return false, "平台尚未配置解析目标(customdomain_cname_target / customdomain_a_ips),请联系平台管理员"
}
if guide.CnameTarget != "" {
if cname, err := net.LookupCNAME(host); err == nil {
actual := NormalizeHost(cname)
if actual != "" && actual != host && actual == guide.CnameTarget {
return true, "CNAME 已指向 " + guide.CnameTarget
}
}
}
addrs, err := net.LookupHost(host)
if err != nil {
return false, "域名无法解析,请确认解析记录已添加并等待生效:" + trimDNSError(err)
}
if len(addrs) == 0 {
return false, "域名未解析到任何 IP,请确认解析记录已添加并等待生效"
}
got := make(map[string]bool, len(addrs))
for _, a := range addrs {
got[a] = true
}
for _, want := range guide.AIPs {
if got[want] {
return true, "解析已指向 " + want
}
}
if guide.CnameTarget != "" {
if targetAddrs, err := net.LookupHost(guide.CnameTarget); err == nil {
for _, want := range targetAddrs {
if got[want] {
return true, "解析已指向平台服务器(" + want + ")"
}
}
}
}
expect := describeExpectedTarget(guide)
return false, fmt.Sprintf("当前解析到 %s,与平台地址不一致。%s", strings.Join(addrs, ", "), expect)
}
func describeExpectedTarget(guide CustomDomainGuide) string {
parts := make([]string, 0, 2)
if guide.CnameTarget != "" {
parts = append(parts, "子域名请添加 CNAME 记录指向 "+guide.CnameTarget)
}
if len(guide.AIPs) > 0 {
parts = append(parts, "根域名请添加 A 记录指向 "+strings.Join(guide.AIPs, " 或 "))
}
if len(parts) == 0 {
return ""
}
return strings.Join(parts, ";") + "。"
}
func trimDNSError(err error) string {
msg := err.Error()
if idx := strings.LastIndex(msg, ": "); idx >= 0 && idx+2 < len(msg) {
msg = msg[idx+2:]
}
return msg
}