Skip to content

Commit ed93932

Browse files
committed
add the ability to control socks5 resolution
not all servers support remote resolution, and require one to send only ip addresses this add a new SOCKS5 dialer creation function that lets one pass in a custom *net.Resolver. if a resolver is set on the dialer, it will use it it convert non ip addresses into ip addresses. For instance, can simply be used with net.DefaultResolver to do local resolution
1 parent 296f09a commit ed93932

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

internal/socks/client.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ func (d *Dialer) connect(ctx context.Context, c net.Conn, address string) (_ net
2323
if err != nil {
2424
return nil, err
2525
}
26+
27+
if d.Resolver != nil {
28+
if ip := net.ParseIP(host); ip == nil {
29+
addresses, err := d.Resolver.LookupHost(ctx, host)
30+
if err != nil {
31+
return nil, err
32+
}
33+
host = addresses[0]
34+
}
35+
}
2636
if deadline, ok := ctx.Deadline(); ok && !deadline.IsZero() {
2737
c.SetDeadline(deadline)
2838
defer c.SetDeadline(noDeadline)

internal/socks/socks.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ type Dialer struct {
136136
// function. It must be non-nil when AuthMethods is not empty.
137137
// It must return an error when the authentication is failed.
138138
Authenticate func(context.Context, io.ReadWriter, AuthMethod) error
139+
140+
Resolver *net.Resolver
139141
}
140142

141143
// DialContext connects to the provided address on the provided
@@ -266,8 +268,8 @@ func (d *Dialer) pathAddrs(address string) (proxy, dst net.Addr, err error) {
266268

267269
// NewDialer returns a new Dialer that dials through the provided
268270
// proxy server's network and address.
269-
func NewDialer(network, address string) *Dialer {
270-
return &Dialer{proxyNetwork: network, proxyAddress: address, cmd: CmdConnect}
271+
func NewDialer(network, address string, resolver *net.Resolver) *Dialer {
272+
return &Dialer{proxyNetwork: network, proxyAddress: address, cmd: CmdConnect, Resolver: resolver}
271273
}
272274

273275
const (

proxy/socks5.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ import (
1414
// SOCKS5 returns a Dialer that makes SOCKSv5 connections to the given
1515
// address with an optional username and password.
1616
// See RFC 1928 and RFC 1929.
17-
func SOCKS5(network, address string, auth *Auth, forward Dialer) (Dialer, error) {
18-
d := socks.NewDialer(network, address)
17+
18+
func socks5Internal(network, address string, auth *Auth, forward Dialer, resolver *net.Resolver) (Dialer, error) {
19+
d := socks.NewDialer(network, address, resolver)
1920
if forward != nil {
2021
if f, ok := forward.(ContextDialer); ok {
2122
d.ProxyDial = func(ctx context.Context, network string, address string) (net.Conn, error) {
@@ -40,3 +41,10 @@ func SOCKS5(network, address string, auth *Auth, forward Dialer) (Dialer, error)
4041
}
4142
return d, nil
4243
}
44+
func SOCKS5(network, address string, auth *Auth, forward Dialer) (Dialer, error) {
45+
return socks5Internal(network, address, auth, forward, nil)
46+
}
47+
48+
func SOCKS5WithResolver(network, address string, auth *Auth, forward Dialer, resolver *net.Resolver) (Dialer, error) {
49+
return socks5Internal(network, address, auth, forward, resolver)
50+
}

0 commit comments

Comments
 (0)