Skip to content

Commit

Permalink
fix: http sniffer skip ip
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyxim committed Aug 17, 2022
1 parent 4611fbf commit 835cab5
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion component/sniffer/http_sniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sniffer
import (
"bytes"
"errors"
"fmt"
C "github.com/Dreamacro/clash/constant"
"net"
"strings"
Expand Down Expand Up @@ -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
}

0 comments on commit 835cab5

Please sign in to comment.