feat: Socks5 proxy supports auth (#5687)
This commit is contained in:
+22
-2
@@ -19,6 +19,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -62,12 +63,31 @@ func getProxyHttpClient() *http.Client {
|
||||
return &http.Client{}
|
||||
}
|
||||
|
||||
if !isAddressOpen(socks5Proxy) {
|
||||
if !strings.HasPrefix(socks5Proxy, "socks5://") {
|
||||
socks5Proxy = "socks5://" + socks5Proxy
|
||||
}
|
||||
|
||||
URL, err := url.Parse(socks5Proxy)
|
||||
if err != nil {
|
||||
fmt.Printf("Invalid socks5 proxy URL: %s, error: %v\n", socks5Proxy, err)
|
||||
return &http.Client{}
|
||||
}
|
||||
|
||||
if !isAddressOpen(URL.Host) {
|
||||
return &http.Client{}
|
||||
}
|
||||
|
||||
var auth *proxy.Auth
|
||||
if URL.User != nil {
|
||||
password, _ := URL.User.Password()
|
||||
auth = &proxy.Auth{
|
||||
User: URL.User.Username(),
|
||||
Password: password,
|
||||
}
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/questions/33585587/creating-a-go-socks5-client
|
||||
dialer, err := proxy.SOCKS5("tcp", socks5Proxy, nil, proxy.Direct)
|
||||
dialer, err := proxy.SOCKS5("tcp", URL.Host, auth, proxy.Direct)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user