Skip to content

Commit

Permalink
add the ability to control socks5 resolution
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sjpotter committed Feb 5, 2023
1 parent 296f09a commit ed93932
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
10 changes: 10 additions & 0 deletions internal/socks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ func (d *Dialer) connect(ctx context.Context, c net.Conn, address string) (_ net
if err != nil {
return nil, err
}

if d.Resolver != nil {
if ip := net.ParseIP(host); ip == nil {
addresses, err := d.Resolver.LookupHost(ctx, host)
if err != nil {
return nil, err
}
host = addresses[0]
}
}
if deadline, ok := ctx.Deadline(); ok && !deadline.IsZero() {
c.SetDeadline(deadline)
defer c.SetDeadline(noDeadline)
Expand Down
6 changes: 4 additions & 2 deletions internal/socks/socks.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ type Dialer struct {
// function. It must be non-nil when AuthMethods is not empty.
// It must return an error when the authentication is failed.
Authenticate func(context.Context, io.ReadWriter, AuthMethod) error

Resolver *net.Resolver
}

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

// NewDialer returns a new Dialer that dials through the provided
// proxy server's network and address.
func NewDialer(network, address string) *Dialer {
return &Dialer{proxyNetwork: network, proxyAddress: address, cmd: CmdConnect}
func NewDialer(network, address string, resolver *net.Resolver) *Dialer {
return &Dialer{proxyNetwork: network, proxyAddress: address, cmd: CmdConnect, Resolver: resolver}
}

const (
Expand Down
12 changes: 10 additions & 2 deletions proxy/socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
// SOCKS5 returns a Dialer that makes SOCKSv5 connections to the given
// address with an optional username and password.
// See RFC 1928 and RFC 1929.
func SOCKS5(network, address string, auth *Auth, forward Dialer) (Dialer, error) {
d := socks.NewDialer(network, address)

func socks5Internal(network, address string, auth *Auth, forward Dialer, resolver *net.Resolver) (Dialer, error) {
d := socks.NewDialer(network, address, resolver)
if forward != nil {
if f, ok := forward.(ContextDialer); ok {
d.ProxyDial = func(ctx context.Context, network string, address string) (net.Conn, error) {
Expand All @@ -40,3 +41,10 @@ func SOCKS5(network, address string, auth *Auth, forward Dialer) (Dialer, error)
}
return d, nil
}
func SOCKS5(network, address string, auth *Auth, forward Dialer) (Dialer, error) {
return socks5Internal(network, address, auth, forward, nil)
}

func SOCKS5WithResolver(network, address string, auth *Auth, forward Dialer, resolver *net.Resolver) (Dialer, error) {
return socks5Internal(network, address, auth, forward, resolver)
}

0 comments on commit ed93932

Please sign in to comment.