Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/socks: add the ability to control socks5 resolution #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}