blob: 56345ec8b63943694108d4871b074608437045e8 [file] [log] [blame]
Scott Bakered4efab2020-01-13 19:12:25 -08001// 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 d.ProxyDial = func(_ context.Context, network string, address string) (net.Conn, error) {
21 return forward.Dial(network, address)
22 }
23 }
24 if auth != nil {
25 up := socks.UsernamePassword{
26 Username: auth.User,
27 Password: auth.Password,
28 }
29 d.AuthMethods = []socks.AuthMethod{
30 socks.AuthMethodNotRequired,
31 socks.AuthMethodUsernamePassword,
32 }
33 d.Authenticate = up.Authenticate
34 }
35 return d, nil
36}