Add auth support for unix socks5 proxy

This commit is contained in:
Balakrishnan Balasubramanian 2022-05-23 12:45:25 -04:00
parent 472b306dbf
commit 67e479c865

View File

@ -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 {