feat(proxy): add SOCKS5 support and improve proxy handling

- Added SOCKS5 proxy support, including authentication.
- Improved handling of proxy schemes and associated error logging.
- Enhanced transport creation for HTTP, HTTPS, and SOCKS5 proxies with better configuration management.
This commit is contained in:
Luis Pater
2025-09-30 08:56:30 +08:00
parent 5010b09329
commit 6b5aefc27a

View File

@@ -1,12 +1,16 @@
package cliproxy package cliproxy
import ( import (
"context"
"net"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"sync" "sync"
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
log "github.com/sirupsen/logrus"
"golang.org/x/net/proxy"
) )
// defaultRoundTripperProvider returns a per-auth HTTP RoundTripper based on // defaultRoundTripperProvider returns a per-auth HTTP RoundTripper based on
@@ -25,27 +29,49 @@ func (p *defaultRoundTripperProvider) RoundTripperFor(auth *coreauth.Auth) http.
if auth == nil { if auth == nil {
return nil return nil
} }
proxy := strings.TrimSpace(auth.ProxyURL) proxyStr := strings.TrimSpace(auth.ProxyURL)
if proxy == "" { if proxyStr == "" {
return nil return nil
} }
p.mu.RLock() p.mu.RLock()
rt := p.cache[proxy] rt := p.cache[proxyStr]
p.mu.RUnlock() p.mu.RUnlock()
if rt != nil { if rt != nil {
return rt return rt
} }
// Build HTTP/HTTPS proxy transport; ignore SOCKS for simplicity here. // Parse the proxy URL to determine the scheme.
u, err := url.Parse(proxy) proxyURL, errParse := url.Parse(proxyStr)
if err != nil { if errParse != nil {
log.Errorf("parse proxy URL failed: %v", errParse)
return nil return nil
} }
if u.Scheme != "http" && u.Scheme != "https" { var transport *http.Transport
// Handle different proxy schemes.
if proxyURL.Scheme == "socks5" {
// Configure SOCKS5 proxy with optional authentication.
username := proxyURL.User.Username()
password, _ := proxyURL.User.Password()
proxyAuth := &proxy.Auth{User: username, Password: password}
dialer, errSOCKS5 := proxy.SOCKS5("tcp", proxyURL.Host, proxyAuth, proxy.Direct)
if errSOCKS5 != nil {
log.Errorf("create SOCKS5 dialer failed: %v", errSOCKS5)
return nil
}
// Set up a custom transport using the SOCKS5 dialer.
transport = &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.Dial(network, addr)
},
}
} else if proxyURL.Scheme == "http" || proxyURL.Scheme == "https" {
// Configure HTTP or HTTPS proxy.
transport = &http.Transport{Proxy: http.ProxyURL(proxyURL)}
} else {
log.Errorf("unsupported proxy scheme: %s", proxyURL.Scheme)
return nil return nil
} }
transport := &http.Transport{Proxy: http.ProxyURL(u)}
p.mu.Lock() p.mu.Lock()
p.cache[proxy] = transport p.cache[proxyStr] = transport
p.mu.Unlock() p.mu.Unlock()
return transport return transport
} }