// Use golang.org/x/net/proxy package to make a socks5 client. // For more information, see DefaultTransport in net.http package. func Socks5Client(conf ProxyConfig) (client *http.Client, err error) { var proxyAuth *proxy.Auth if conf.Username != "" { proxyAuth = new(proxy.Auth) proxyAuth.User = conf.Username proxyAuth.Password = conf.Password } dialer, err := proxy.SOCKS5( "tcp", conf.Addr, proxyAuth, &net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, }) if err != nil { return } var transport http.RoundTripper = &http.Transport{ Proxy: nil, Dial: dialer.Dial, TLSHandshakeTimeout: 10 * time.Second, } client = &http.Client{Transport: transport} return }
func socks5UnixProxy(u *url.URL, d proxy.Dialer) (proxy.Dialer, error) { var auth *proxy.Auth if u.User != nil { auth = &proxy.Auth{ User: u.User.Username(), } if p, ok := u.User.Password(); ok { auth.Password = p } } return proxy.SOCKS5("unix", u.Path, auth, d) }
func init() { proxy.RegisterDialerType("socks5+unix", func(u *url.URL, d proxy.Dialer) (proxy.Dialer, error) { var auth *proxy.Auth if u.User != nil { auth = &proxy.Auth{ User: u.User.Username(), } if p, ok := u.User.Password(); ok { auth.Password = p } } return proxy.SOCKS5("unix", u.Path, auth, d) }) }
func parseProxy(addr string) (*http.Client, error) { u, err := url.Parse(addr) if err != nil { return nil, err } switch u.Scheme { case "socks5": forward := &net.Dialer{ Timeout: 5 * time.Second, KeepAlive: 30 * time.Second, } var auth *proxy.Auth if u.User != nil { auth = &proxy.Auth{} auth.User = u.User.Username() auth.Password, _ = u.User.Password() } dialer, err := proxy.SOCKS5("tcp", u.Host, auth, forward) if err != nil { return nil, err } transport := &http.Transport{ Dial: dialer.Dial, TLSHandshakeTimeout: 10 * time.Second, } return &http.Client{ Transport: transport, }, nil case "http", "https": transport := &http.Transport{ Proxy: http.ProxyURL(u), Dial: (&net.Dialer{ Timeout: 5 * time.Second, KeepAlive: 30 * time.Second, }).Dial, TLSHandshakeTimeout: 10 * time.Second, } return &http.Client{ Transport: transport, }, nil default: return nil, errors.New("proxy: unsupported proxy type") } }