Skip to content

Commit

Permalink
Improve: simple dns prefetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamacro committed Jun 11, 2018
1 parent 4f192ef commit 4f769de
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion adapters/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions adapters/shadowsocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(""))
Expand Down
12 changes: 12 additions & 0 deletions constant/addr.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package constant

import (
"net"
)

// Socks addr type
const (
AtypIPv4 = 1
Expand All @@ -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
}
3 changes: 2 additions & 1 deletion proxy/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -105,6 +105,7 @@ func parseHttpAddr(target string) *constant.Addr {
return &constant.Addr{
AddrType: addType,
Host: host,
IP: &ipAddr.IP,
Port: port,
}
}
Expand Down
10 changes: 8 additions & 2 deletions proxy/socks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
10 changes: 2 additions & 8 deletions rules/geoip.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package rules

import (
"net"

C "github.com/Dreamacro/clash/constant"

"github.com/oschwald/geoip2-golang"
Expand All @@ -29,14 +27,10 @@ func (g *GEOIP) RuleType() C.RuleType {
}

func (g *GEOIP) IsMatch(addr *C.Addr) bool {
if addr.AddrType == C.AtypDomainName {
return false
}
dstIP := net.ParseIP(addr.Host)
if dstIP == nil {
if addr.IP == nil {
return false
}
record, _ := mmdb.Country(dstIP)
record, _ := mmdb.Country(*addr.IP)
return record.Country.IsoCode == g.country
}

Expand Down
8 changes: 2 additions & 6 deletions rules/ipcidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit 4f769de

Please sign in to comment.