mirror of
https://github.com/router-for-me/CLIProxyAPIPlus.git
synced 2026-03-27 14:17:37 +00:00
feat(proxy): centralize proxy handling with `proxyutil` package and enhance test coverage - Added `proxyutil` package to simplify proxy handling across the codebase. - Refactored various components (`executor`, `cliproxy`, `auth`, etc.) to use `proxyutil` for consistent and reusable proxy logic. - Introduced support for "direct" proxy mode to explicitly bypass all proxies. - Updated tests to validate proxy behavior (e.g., `direct`, HTTP/HTTPS, and SOCKS5). - Enhanced YAML configuration documentation for proxy options.
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package cliproxy
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
|
|
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
|
|
"github.com/router-for-me/CLIProxyAPI/v6/sdk/proxyutil"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// defaultRoundTripperProvider returns a per-auth HTTP RoundTripper based on
|
|
// the Auth.ProxyURL value. It caches transports per proxy URL string.
|
|
type defaultRoundTripperProvider struct {
|
|
mu sync.RWMutex
|
|
cache map[string]http.RoundTripper
|
|
}
|
|
|
|
func newDefaultRoundTripperProvider() *defaultRoundTripperProvider {
|
|
return &defaultRoundTripperProvider{cache: make(map[string]http.RoundTripper)}
|
|
}
|
|
|
|
// RoundTripperFor implements coreauth.RoundTripperProvider.
|
|
func (p *defaultRoundTripperProvider) RoundTripperFor(auth *coreauth.Auth) http.RoundTripper {
|
|
if auth == nil {
|
|
return nil
|
|
}
|
|
proxyStr := strings.TrimSpace(auth.ProxyURL)
|
|
if proxyStr == "" {
|
|
return nil
|
|
}
|
|
p.mu.RLock()
|
|
rt := p.cache[proxyStr]
|
|
p.mu.RUnlock()
|
|
if rt != nil {
|
|
return rt
|
|
}
|
|
transport, _, errBuild := proxyutil.BuildHTTPTransport(proxyStr)
|
|
if errBuild != nil {
|
|
log.Errorf("%v", errBuild)
|
|
return nil
|
|
}
|
|
if transport == nil {
|
|
return nil
|
|
}
|
|
p.mu.Lock()
|
|
p.cache[proxyStr] = transport
|
|
p.mu.Unlock()
|
|
return transport
|
|
}
|