feat: Socks5 proxy supports auth (#5687)

This commit is contained in:
gladmo
2026-08-01 18:41:38 +08:00
committed by GitHub
parent 2ba1cbb627
commit 23139136fd
+22 -2
View File
@@ -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)
}