forked from folbricht/routedns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socks5.go
91 lines (79 loc) · 2.25 KB
/
socks5.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package rdns
import (
"context"
"net"
"sync"
"time"
"github.com/txthinking/socks5"
)
type Socks5Dialer struct {
*socks5.Client
opt Socks5DialerOptions
once sync.Once
addr string
}
type Socks5DialerOptions struct {
Username string
Password string
UDPTimeout time.Duration
TCPTimeout time.Duration
LocalAddr net.IP
// When the resolver is configured with a name, not an IP, e.g. one.one.one.one:53
// this setting will resolve that name locally rather than on the SOCKS proxy. The
// name will be resolved either on the local system, or via the bootstrap-resolver
// if one is setup.
ResolveLocal bool
}
var _ Dialer = (*Socks5Dialer)(nil)
func NewSocks5Dialer(addr string, opt Socks5DialerOptions) *Socks5Dialer {
client, _ := socks5.NewClient(
addr,
opt.Username,
opt.Password,
int(opt.TCPTimeout.Seconds()),
int(opt.UDPTimeout.Seconds()),
)
return &Socks5Dialer{Client: client, opt: opt}
}
func (d *Socks5Dialer) Dial(network string, address string) (net.Conn, error) {
d.once.Do(func() {
d.addr = address
// If the address uses a hostname and ResolveLocal is enabled, lookup
// the IP for it locally and use that when talking to the proxy going
// forward. This avoids the DNS server's address leaking out from the
// proxy.
if d.opt.ResolveLocal {
host, port, err := net.SplitHostPort(address)
if err != nil {
Log.WithError(err).Error("failed to parse socks5 address")
return
}
Log.WithField("addr", host).Debug("resolving dns server locally")
ip := net.ParseIP(host)
if ip != nil {
// Already an IP
return
}
timeout := d.opt.UDPTimeout
if timeout == 0 {
timeout = 5 * time.Second
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
ips, err := net.DefaultResolver.LookupIP(ctx, "ip4", host)
if err != nil {
Log.WithError(err).Errorf("failed to lookup %q locally", host)
return
}
if len(ips) == 0 {
Log.WithError(err).Error("failed to resolve dns server locally, forwarding to socks5 proxy")
return
}
d.addr = net.JoinHostPort(ips[0].String(), port)
}
})
if d.opt.LocalAddr != nil {
return d.Client.DialWithLocalAddr(network, d.opt.LocalAddr.String(), d.addr, nil)
}
return d.Client.Dial(network, d.addr)
}