From 67e479c8654cd2a84621bf7b98349173a60955e8 Mon Sep 17 00:00:00 2001 From: balki <3070606-balki@users.noreply.gitlab.com> Date: Mon, 23 May 2022 12:45:25 -0400 Subject: [PATCH] Add auth support for unix socks5 proxy --- proxy/proxy.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index 77f0ade..eef7a7a 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -20,7 +20,7 @@ func GetTransport(proxy string) (http.RoundTripper, error) { } if proxyUrl.Host == "unix" && proxyUrl.Scheme == "socks5" && len(proxyUrl.Path) > 1 /* Path cannot be empty or just / */ { - return unixSocks5Proxy(proxyUrl.Path) + return unixSocks5Proxy(proxyUrl) } return proxyTcp(proxyUrl) } @@ -38,14 +38,24 @@ func (d *forwardDialer) Dial(network, address string) (net.Conn, error) { //return defaultTransport().Dial(network, address) } -func unixSocks5Proxy(path string) (http.RoundTripper, error) { +func unixSocks5Proxy(proxyUrl *url.URL) (http.RoundTripper, error) { trans := defaultTransport().Clone() if trans.DialContext == nil { panic("DefaultTransport has nil DialContext") } - dialer, err := proxy.SOCKS5("unix", path, nil /*auth*/, &forwardDialer{trans.DialContext}) + var auth *proxy.Auth + username := proxyUrl.User.Username() + password, _ := proxyUrl.User.Password() + // Both username and password should have atleast one char + if username != "" && password != "" { + auth = &proxy.Auth{ + User: username, + Password: password, + } + } + dialer, err := proxy.SOCKS5("unix", proxyUrl.Path, auth, &forwardDialer{trans.DialContext}) if err != nil { - return nil, fmt.Errorf("failed to make socks proxy, path: %s, err: %w", path, err) + return nil, fmt.Errorf("failed to make socks proxy, url: %s, err: %w", proxyUrl, err) } ctxDialer, ok := dialer.(proxy.ContextDialer) if !ok {