From ed1b0ec673a322104ba934566fa7cef5b30cc8a5 Mon Sep 17 00:00:00 2001 From: Zen Wen Date: Mon, 13 Oct 2025 13:16:20 +0800 Subject: [PATCH] Move function:NormalizeDomain to netutils module --- src/mod/acme/acme.go | 3 +- src/mod/netutils/netutils.go | 53 ++++++++++++++++++++++++++++++++++++ src/mod/utils/utils.go | 51 ---------------------------------- 3 files changed, 55 insertions(+), 52 deletions(-) diff --git a/src/mod/acme/acme.go b/src/mod/acme/acme.go index 334e9c0..54baacb 100644 --- a/src/mod/acme/acme.go +++ b/src/mod/acme/acme.go @@ -27,6 +27,7 @@ import ( "github.com/go-acme/lego/v4/registration" "imuslab.com/zoraxy/mod/database" "imuslab.com/zoraxy/mod/info/logger" + "imuslab.com/zoraxy/mod/netutils" "imuslab.com/zoraxy/mod/utils" ) @@ -438,7 +439,7 @@ func (a *ACMEHandler) HandleRenewCertificate(w http.ResponseWriter, r *http.Requ if domainPara != "" { for _, d := range strings.Split(domainPara, ",") { // Apply normalization on each domain - nd, err := utils.NormalizeDomain(d) + nd, err := netutils.NormalizeDomain(d) if err != nil { utils.SendErrorResponse(w, jsonEscape(err.Error())) return diff --git a/src/mod/netutils/netutils.go b/src/mod/netutils/netutils.go index 46ebcd4..6890b99 100644 --- a/src/mod/netutils/netutils.go +++ b/src/mod/netutils/netutils.go @@ -2,10 +2,13 @@ package netutils import ( "encoding/json" + "errors" "fmt" "net" "net/http" "strconv" + "strings" + "unicode" "github.com/likexian/whois" "imuslab.com/zoraxy/mod/utils" @@ -167,3 +170,53 @@ func CheckIfPortOccupied(portNumber int) bool { listener.Close() return false } + +// NormalizeDomain cleans and validates a domain string. +// - Trims spaces around the domain +// - Converts to lowercase +// - Removes trailing dot (FQDN canonicalization) +// - Checks that the domain conforms to standard rules: +// - Each label ≤ 63 characters +// - Only letters, digits, and hyphens +// - Labels do not start or end with a hyphen +// - Full domain ≤ 253 characters +// +// Returns an empty string if the domain is invalid. +func NormalizeDomain(d string) (string, error) { + d = strings.TrimSpace(d) + d = strings.ToLower(d) + d = strings.TrimSuffix(d, ".") + + if len(d) == 0 { + return "", errors.New("domain is empty") + } + if len(d) > 253 { + return "", errors.New("domain exceeds 253 characters") + } + + labels := strings.Split(d, ".") + for index, label := range labels { + if index == 0 { + if len(label) == 1 && label == "*" { + continue + } + } + if len(label) == 0 { + return "", errors.New("Domain '" + d + "' not valid: Empty label") + } + if len(label) > 63 { + return "", errors.New("Domain not valid: label exceeds 63 characters") + } + + for i, r := range label { + if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-' { + return "", errors.New("Domain '" + d + "' not valid: Invalid character '" + string(r) + "' in label") + } + if (i == 0 || i == len(label)-1) && r == '-' { + return "", errors.New("Domain '" + d + "' not valid: label '" + label + "' starts or ends with hyphen") + } + } + } + + return d, nil +} diff --git a/src/mod/utils/utils.go b/src/mod/utils/utils.go index c99de72..21d2e40 100644 --- a/src/mod/utils/utils.go +++ b/src/mod/utils/utils.go @@ -9,7 +9,6 @@ import ( "strconv" "strings" "time" - "unicode" ) /* @@ -201,53 +200,3 @@ func ValidateListeningAddress(address string) bool { return true } - -// NormalizeDomain cleans and validates a domain string. -// - Trims spaces around the domain -// - Converts to lowercase -// - Removes trailing dot (FQDN canonicalization) -// - Checks that the domain conforms to standard rules: -// - Each label ≤ 63 characters -// - Only letters, digits, and hyphens -// - Labels do not start or end with a hyphen -// - Full domain ≤ 253 characters -// -// Returns an empty string if the domain is invalid. -func NormalizeDomain(d string) (string, error) { - d = strings.TrimSpace(d) - d = strings.ToLower(d) - d = strings.TrimSuffix(d, ".") - - if len(d) == 0 { - return "", errors.New("domain is empty") - } - if len(d) > 253 { - return "", errors.New("domain exceeds 253 characters") - } - - labels := strings.Split(d, ".") - for index, label := range labels { - if index == 0 { - if len(label) == 1 && label == "*" { - continue - } - } - if len(label) == 0 { - return "", errors.New("Domain '" + d + "' not valid: Empty label") - } - if len(label) > 63 { - return "", errors.New("Domain not valid: label exceeds 63 characters") - } - - for i, r := range label { - if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-' { - return "", errors.New("Domain '" + d + "' not valid: Invalid character '" + string(r) + "' in label") - } - if (i == 0 || i == len(label)-1) && r == '-' { - return "", errors.New("Domain '" + d + "' not valid: label '" + label + "' starts or ends with hyphen") - } - } - } - - return d, nil -}