feat(domain): 支持通配符域名及关联功能适配 (#63)

* feat(domain): 支持通配符域名及关联功能适配

* fix(domain): preserve legacy Unicode imports and address review findings

* fix(domain): canonicalize legacy IDNs and exclude empty host labels

* fix(domain): normalize equivalent Unicode IDN spellings

---------

Co-authored-by: Ryan <63696351+Rain-kl@users.noreply.github.com>
Co-authored-by: ryan <ryan@arctel.net>
This commit is contained in:
Deqiying
2026-11-09 10:31:03 +08:00
committed by GitHub
parent 8d8b3e1ea8
commit 9a51a68206
37 changed files with 1614 additions and 113 deletions
+19 -7
View File
@@ -1,7 +1,7 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
// Package zone manages registered roots and their explicit hostnames.
// Package zone manages registered roots and their exact or wildcard hostnames.
package zone
import (
@@ -18,6 +18,8 @@ import (
"gorm.io/gorm"
)
const maxDomainLength = 253
// Input is the mutable Zone payload.
type Input struct {
Domain string `json:"domain"`
@@ -45,7 +47,8 @@ type ListItem struct {
}
func zoneRoot(domain string) (string, error) {
return publicsuffix.EffectiveTLDPlusOne(strings.ToLower(strings.TrimSpace(domain)))
domain = strings.ToLower(strings.TrimSpace(domain))
return publicsuffix.EffectiveTLDPlusOne(strings.TrimPrefix(domain, "*."))
}
func normalizeDomain(raw string) (string, error) {
@@ -53,12 +56,21 @@ func normalizeDomain(raw string) (string, error) {
if domain == "" {
return "", errors.New(errZoneDomainRequired)
}
if strings.Contains(domain, "*") {
return "", errors.New(errDomainWildcardUnsupported)
}
if strings.Contains(domain, "://") || strings.Contains(domain, "/") || strings.Contains(domain, "?") || strings.Contains(domain, "#") || strings.Contains(domain, "@") {
// 只允许最左侧完整标签为通配符;其余字符必须是 DNS 标签,不能进入配置语法。
host := strings.TrimPrefix(domain, "*.")
if len(domain) > maxDomainLength {
return "", errors.New(errDomainInvalid)
}
for _, label := range strings.Split(host, ".") {
if len(label) == 0 || len(label) > 63 || label[0] == '-' || label[len(label)-1] == '-' {
return "", errors.New(errDomainInvalid)
}
for _, char := range label {
if (char < 'a' || char > 'z') && (char < '0' || char > '9') && char != '-' {
return "", errors.New(errDomainInvalid)
}
}
}
if _, err := zoneRoot(domain); err != nil {
return "", errors.New(errDomainInvalid)
}
@@ -151,7 +163,7 @@ func GetOverview(ctx context.Context, id uint) (*Overview, error) {
return &Overview{Zone: *zone, Domains: domains}, nil
}
// CreateDomain adds a validated exact hostname to a Zone.
// CreateDomain adds a validated exact or wildcard hostname to a Zone.
func CreateDomain(ctx context.Context, zoneID uint, input DomainInput) (*model.ZoneDomain, error) {
zone, err := repository.GetZoneByID(ctx, zoneID)
if err != nil {