mirror of
https://github.com/tobychui/zoraxy.git
synced 2025-10-16 15:49:34 +02:00
Move function:NormalizeDomain to netutils module
This commit is contained in:
@@ -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
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user