blob: c91651f96db9264945ded8692569dc021dac08cf [file] [log] [blame]
Scott Baker8461e152019-10-01 14:44:30 -07001// 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
5package proxy
6
7import (
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.
17func SOCKS5(network, address string, auth *Auth, forward Dialer) (Dialer, error) {
18 d := socks.NewDialer(network, address)
19 if forward != nil {
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 }
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}