From 35eaa7eb068cd5df3b738887a84d8f00771ae088 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Wed, 17 Aug 2022 12:41:36 +0800 Subject: [PATCH] fix: http sniffer skip ip --- component/sniffer/http_sniffer.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/component/sniffer/http_sniffer.go b/component/sniffer/http_sniffer.go index 97ae607b..f75285ed 100644 --- a/component/sniffer/http_sniffer.go +++ b/component/sniffer/http_sniffer.go @@ -3,6 +3,7 @@ package sniffer import ( "bytes" "errors" + "fmt" C "github.com/Dreamacro/clash/constant" "net" "strings" @@ -88,13 +89,32 @@ func SniffHTTP(b []byte) (*string, error) { host, _, err := net.SplitHostPort(rawHost) if err != nil { if addrError, ok := err.(*net.AddrError); ok && strings.Contains(addrError.Err, "missing port") { - host = rawHost + return parseHost(rawHost) } else { return nil, err } } + + if net.ParseIP(host) != nil { + return nil, fmt.Errorf("host is ip") + } + return &host, nil } } return nil, ErrNoClue } + +func parseHost(host string) (*string, error) { + if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") { + if net.ParseIP(host[1:len(host)-1]) != nil { + return nil, fmt.Errorf("host is ip") + } + } + + if net.ParseIP(host) != nil { + return nil, fmt.Errorf("host is ip") + } + + return &host, nil +}