fix: http sniffer return host that was handled correctly

This commit is contained in:
MetaCubeX 2022-05-02 09:51:26 +08:00
parent 5fee0b5bf1
commit 4b04faa88b

View file

@ -5,7 +5,6 @@ import (
"errors" "errors"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"net" "net"
"strconv"
"strings" "strings"
) )
@ -85,23 +84,15 @@ func SniffHTTP(b []byte) (*string, error) {
} }
key := strings.ToLower(string(parts[0])) key := strings.ToLower(string(parts[0]))
if key == "host" { if key == "host" {
port := 80
rawHost := strings.ToLower(string(bytes.TrimSpace(parts[1]))) rawHost := strings.ToLower(string(bytes.TrimSpace(parts[1])))
host, rawPort, err := net.SplitHostPort(rawHost) host, _, err := net.SplitHostPort(rawHost)
if err != nil { if err != nil {
if addrError, ok := err.(*net.AddrError); ok && strings.Contains(addrError.Err, "missing port") { if addrError, ok := err.(*net.AddrError); ok && strings.Contains(addrError.Err, "missing port") {
host = rawHost host = rawHost
} else { } else {
return nil, err return nil, err
} }
} else if len(rawPort) > 0 {
intPort, err := strconv.ParseUint(rawPort, 0, 16)
if err != nil {
return nil, err
} }
port = int(intPort)
}
host = net.JoinHostPort(host, strconv.Itoa(port))
return &host, nil return &host, nil
} }
} }