diff --git a/adapters/direct.go b/adapters/direct.go index 6c127037..8185f8f1 100644 --- a/adapters/direct.go +++ b/adapters/direct.go @@ -31,7 +31,7 @@ type Direct struct { } func (d *Direct) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) { - c, err := net.Dial("tcp", net.JoinHostPort(addr.Host, addr.Port)) + c, err := net.Dial("tcp", net.JoinHostPort(addr.String(), addr.Port)) if err != nil { return } diff --git a/adapters/shadowsocks.go b/adapters/shadowsocks.go index bc27d919..4159d7a2 100644 --- a/adapters/shadowsocks.go +++ b/adapters/shadowsocks.go @@ -87,10 +87,10 @@ func serializesSocksAddr(addr *C.Addr) []byte { host := []byte(addr.Host) buf = [][]byte{[]byte{aType, len}, host, port} case socks.AtypIPv4: - host := net.ParseIP(addr.Host).To4() + host := addr.IP.To4() buf = [][]byte{[]byte{aType}, host, port} case socks.AtypIPv6: - host := net.ParseIP(addr.Host).To16() + host := addr.IP.To16() buf = [][]byte{[]byte{aType}, host, port} } return bytes.Join(buf, []byte("")) diff --git a/constant/addr.go b/constant/addr.go index 6dcd9ad4..e25fa0ac 100644 --- a/constant/addr.go +++ b/constant/addr.go @@ -1,5 +1,9 @@ package constant +import ( + "net" +) + // Socks addr type const ( AtypIPv4 = 1 @@ -11,5 +15,13 @@ const ( type Addr struct { AddrType int Host string + IP *net.IP Port string } + +func (addr *Addr) String() string { + if addr.Host == "" { + return addr.IP.String() + } + return addr.Host +} diff --git a/proxy/http.go b/proxy/http.go index 232e378c..617430b9 100644 --- a/proxy/http.go +++ b/proxy/http.go @@ -90,7 +90,7 @@ func (h *HttpAdapter) Addr() *constant.Addr { func parseHttpAddr(target string) *constant.Addr { host, port, _ := net.SplitHostPort(target) - + ipAddr, _ := net.ResolveIPAddr("ip", host) var addType int ip := net.ParseIP(host) switch { @@ -105,6 +105,7 @@ func parseHttpAddr(target string) *constant.Addr { return &constant.Addr{ AddrType: addType, Host: host, + IP: &ipAddr.IP, Port: port, } } diff --git a/proxy/socks.go b/proxy/socks.go index 106d4826..c27f7526 100644 --- a/proxy/socks.go +++ b/proxy/socks.go @@ -65,22 +65,28 @@ func (s *SocksAdapter) Addr() *constant.Addr { func parseSocksAddr(target socks.Addr) *constant.Addr { var host, port string + var ip net.IP switch target[0] { case socks.AtypDomainName: host = string(target[2 : 2+target[1]]) port = strconv.Itoa((int(target[2+target[1]]) << 8) | int(target[2+target[1]+1])) + ipAddr, err := net.ResolveIPAddr("ip", host) + if err == nil { + ip = ipAddr.IP + } case socks.AtypIPv4: - host = net.IP(target[1 : 1+net.IPv4len]).String() + ip = net.IP(target[1 : 1+net.IPv4len]) port = strconv.Itoa((int(target[1+net.IPv4len]) << 8) | int(target[1+net.IPv4len+1])) case socks.AtypIPv6: - host = net.IP(target[1 : 1+net.IPv6len]).String() + ip = net.IP(target[1 : 1+net.IPv6len]) port = strconv.Itoa((int(target[1+net.IPv6len]) << 8) | int(target[1+net.IPv6len+1])) } return &constant.Addr{ AddrType: int(target[0]), Host: host, + IP: &ip, Port: port, } } diff --git a/rules/geoip.go b/rules/geoip.go index 6975eefb..9d981b9f 100644 --- a/rules/geoip.go +++ b/rules/geoip.go @@ -1,8 +1,6 @@ package rules import ( - "net" - C "github.com/Dreamacro/clash/constant" "github.com/oschwald/geoip2-golang" @@ -29,14 +27,10 @@ func (g *GEOIP) RuleType() C.RuleType { } func (g *GEOIP) IsMatch(addr *C.Addr) bool { - if addr.AddrType == C.AtypDomainName { + if addr.IP == nil { return false } - dstIP := net.ParseIP(addr.Host) - if dstIP == nil { - return false - } - record, _ := mmdb.Country(dstIP) + record, _ := mmdb.Country(*addr.IP) return record.Country.IsoCode == g.country } diff --git a/rules/ipcidr.go b/rules/ipcidr.go index 16eb08ba..57d08f50 100644 --- a/rules/ipcidr.go +++ b/rules/ipcidr.go @@ -16,15 +16,11 @@ func (i *IPCIDR) RuleType() C.RuleType { } func (i *IPCIDR) IsMatch(addr *C.Addr) bool { - if addr.AddrType == C.AtypDomainName { - return false - } - ip := net.ParseIP(addr.Host) - if ip == nil { + if addr.IP == nil { return false } - return i.ipnet.Contains(ip) + return i.ipnet.Contains(*addr.IP) } func (g *IPCIDR) Adapter() string { diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index 53cd4de9..c97a0f7b 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -117,7 +117,7 @@ func (t *Tunnel) match(addr *C.Addr) C.Proxy { if !ok { continue } - t.logCh <- newLog(INFO, "%v match %d using %s", addr.Host, rule.RuleType(), rule.Adapter()) + t.logCh <- newLog(INFO, "%v match %d using %s", addr.String(), rule.RuleType(), rule.Adapter()) return a } }