refactor(handler/http): drop govalidator dep, inline DNS name check

Replace the sole govalidator.IsDNSName call in normalizeRequest with a local
isDNSName helper that mirrors the library's exact behavior (empty/size guard,
net.ParseIP exclusion, and the same label regex), removing the
asaskevich/govalidator dependency from x.
This commit is contained in:
ginuerzh
2026-09-02 17:01:58 +08:00
parent e119a973f2
commit de301e8cfb
3 changed files with 15 additions and 5 deletions
-1
View File
@@ -5,7 +5,6 @@ go 1.26.3
require (
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137
github.com/andybalholm/brotli v1.0.6
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/gin-contrib/cors v1.7.2
github.com/gin-gonic/gin v1.12.0
github.com/go-gost/core v0.6.1
-2
View File
@@ -6,8 +6,6 @@ github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVK
github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI=
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ=
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
+15 -2
View File
@@ -7,12 +7,25 @@ import (
"hash/crc32"
"net"
"net/http"
"regexp"
"strings"
"github.com/asaskevich/govalidator"
"golang.org/x/net/http/httpguts"
)
// isDNSName reports whether host is a valid DNS name. It mirrors
// govalidator.IsDNSName (which was removed as a dependency): IP literals and
// empty hosts are not DNS names, the dot-stripped length is capped at 255,
// and each dot-separated label follows RFC-ish [A-Za-z0-9_-] rules.
func isDNSName(host string) bool {
if host == "" || len(strings.ReplaceAll(host, ".", "")) > 255 {
return false
}
return net.ParseIP(host) == nil && dnsNameRE.MatchString(host)
}
var dnsNameRE = regexp.MustCompile(`^([a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*[\._]?$`)
// decodeServerName decodes a GOST v2 Gost-Target / X-Gost-Target header
// value. The encoding is: base64(CRC32(hostname) + base64(hostname)).
// It verifies the CRC32 checksum before returning the decoded hostname.
@@ -122,7 +135,7 @@ func normalizeRequest(req *http.Request) *NormalizedRequest {
if h, _, err := net.SplitHostPort(host); err == nil {
host = h
}
if govalidator.IsDNSName(host) || net.ParseIP(host) != nil {
if isDNSName(host) || net.ParseIP(host) != nil {
req.URL.Scheme = "http"
}
}