github.com/Andyfoo/golang/x/net@v0.0.0-20190901054642-57c1bf301704/proxy/socks5.go (about)

     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  	"github.com/Andyfoo/golang/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 {
    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  }