mirror of
https://github.com/tobychui/zoraxy.git
synced 2025-10-28 20:44:05 +01:00
Move function:NormalizeDomain to utils module
This commit is contained in:
@@ -438,7 +438,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 := NormalizeDomain(d)
|
nd, err := utils.NormalizeDomain(d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.SendErrorResponse(w, jsonEscape(err.Error()))
|
utils.SendErrorResponse(w, jsonEscape(err.Error()))
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -7,8 +7,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
"strings"
|
|
||||||
"unicode"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Get the issuer name from pem file
|
// Get the issuer name from pem file
|
||||||
@@ -116,48 +114,3 @@ func CertExpireSoon(certBytes []byte, numberOfDays int) bool {
|
|||||||
}
|
}
|
||||||
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 _, label := range labels {
|
|
||||||
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,6 +9,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -200,3 +201,47 @@ 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 _, label := range labels {
|
||||||
|
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