Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 1 | // Copyright 2011 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package proxy |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | "net" |
| 10 | |
| 11 | "golang.org/x/net/internal/socks" |
| 12 | ) |
| 13 | |
| 14 | // SOCKS5 returns a Dialer that makes SOCKSv5 connections to the given |
| 15 | // address with an optional username and password. |
| 16 | // See RFC 1928 and RFC 1929. |
| 17 | func SOCKS5(network, address string, auth *Auth, forward Dialer) (Dialer, error) { |
| 18 | d := socks.NewDialer(network, address) |
| 19 | if forward != nil { |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 20 | if f, ok := forward.(ContextDialer); ok { |
| 21 | d.ProxyDial = func(ctx context.Context, network string, address string) (net.Conn, error) { |
| 22 | return f.DialContext(ctx, network, address) |
| 23 | } |
| 24 | } else { |
| 25 | d.ProxyDial = func(ctx context.Context, network string, address string) (net.Conn, error) { |
| 26 | return dialContext(ctx, forward, network, address) |
| 27 | } |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | if auth != nil { |
| 31 | up := socks.UsernamePassword{ |
| 32 | Username: auth.User, |
| 33 | Password: auth.Password, |
| 34 | } |
| 35 | d.AuthMethods = []socks.AuthMethod{ |
| 36 | socks.AuthMethodNotRequired, |
| 37 | socks.AuthMethodUsernamePassword, |
| 38 | } |
| 39 | d.Authenticate = up.Authenticate |
| 40 | } |
| 41 | return d, nil |
| 42 | } |