mirror of
https://github.com/tobychui/zoraxy.git
synced 2025-10-18 08:39:40 +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"
|
"github.com/go-acme/lego/v4/registration"
|
||||||
"imuslab.com/zoraxy/mod/database"
|
"imuslab.com/zoraxy/mod/database"
|
||||||
"imuslab.com/zoraxy/mod/info/logger"
|
"imuslab.com/zoraxy/mod/info/logger"
|
||||||
|
"imuslab.com/zoraxy/mod/netutils"
|
||||||
"imuslab.com/zoraxy/mod/utils"
|
"imuslab.com/zoraxy/mod/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -438,7 +439,7 @@ func (a *ACMEHandler) HandleRenewCertificate(w http.ResponseWriter, r *http.Requ
|
|||||||
if domainPara != "" {
|
if domainPara != "" {
|
||||||
for _, d := range strings.Split(domainPara, ",") {
|
for _, d := range strings.Split(domainPara, ",") {
|
||||||
// Apply normalization on each domain
|
// Apply normalization on each domain
|
||||||
nd, err := utils.NormalizeDomain(d)
|
nd, err := netutils.NormalizeDomain(d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.SendErrorResponse(w, jsonEscape(err.Error()))
|
utils.SendErrorResponse(w, jsonEscape(err.Error()))
|
||||||
return
|
return
|
||||||
|
@@ -2,10 +2,13 @@ package netutils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"github.com/likexian/whois"
|
"github.com/likexian/whois"
|
||||||
"imuslab.com/zoraxy/mod/utils"
|
"imuslab.com/zoraxy/mod/utils"
|
||||||
@@ -167,3 +170,53 @@ func CheckIfPortOccupied(portNumber int) bool {
|
|||||||
listener.Close()
|
listener.Close()
|
||||||
return false
|
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"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -201,53 +200,3 @@ func ValidateListeningAddress(address string) bool {
|
|||||||
|
|
||||||
return true
|
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