|
|
@@ -35,9 +35,9 @@ func checkRedirect(req *http.Request, via []*http.Request) error {
|
|
|
|
|
|
func InitHttpClient() {
|
|
|
transport := &http.Transport{
|
|
|
- MaxIdleConns: common.RelayMaxIdleConns,
|
|
|
- MaxIdleConnsPerHost: common.RelayMaxIdleConnsPerHost,
|
|
|
- ForceAttemptHTTP2: true,
|
|
|
+ MaxIdleConns: common.RelayMaxIdleConns,
|
|
|
+ MaxIdleConnsPerHost: common.RelayMaxIdleConnsPerHost,
|
|
|
+ ForceAttemptHTTP2: true,
|
|
|
}
|
|
|
|
|
|
if common.RelayTimeout == 0 {
|
|
|
@@ -58,6 +58,14 @@ func GetHttpClient() *http.Client {
|
|
|
return httpClient
|
|
|
}
|
|
|
|
|
|
+// GetHttpClientWithProxy returns the default client or a proxy-enabled one when proxyURL is provided.
|
|
|
+func GetHttpClientWithProxy(proxyURL string) (*http.Client, error) {
|
|
|
+ if proxyURL == "" {
|
|
|
+ return GetHttpClient(), nil
|
|
|
+ }
|
|
|
+ return NewProxyHttpClient(proxyURL)
|
|
|
+}
|
|
|
+
|
|
|
// ResetProxyClientCache 清空代理客户端缓存,确保下次使用时重新初始化
|
|
|
func ResetProxyClientCache() {
|
|
|
proxyClientLock.Lock()
|
|
|
@@ -92,10 +100,10 @@ func NewProxyHttpClient(proxyURL string) (*http.Client, error) {
|
|
|
case "http", "https":
|
|
|
client := &http.Client{
|
|
|
Transport: &http.Transport{
|
|
|
- MaxIdleConns: common.RelayMaxIdleConns,
|
|
|
- MaxIdleConnsPerHost: common.RelayMaxIdleConnsPerHost,
|
|
|
- ForceAttemptHTTP2: true,
|
|
|
- Proxy: http.ProxyURL(parsedURL),
|
|
|
+ MaxIdleConns: common.RelayMaxIdleConns,
|
|
|
+ MaxIdleConnsPerHost: common.RelayMaxIdleConnsPerHost,
|
|
|
+ ForceAttemptHTTP2: true,
|
|
|
+ Proxy: http.ProxyURL(parsedURL),
|
|
|
},
|
|
|
CheckRedirect: checkRedirect,
|
|
|
}
|
|
|
@@ -127,9 +135,9 @@ func NewProxyHttpClient(proxyURL string) (*http.Client, error) {
|
|
|
|
|
|
client := &http.Client{
|
|
|
Transport: &http.Transport{
|
|
|
- MaxIdleConns: common.RelayMaxIdleConns,
|
|
|
- MaxIdleConnsPerHost: common.RelayMaxIdleConnsPerHost,
|
|
|
- ForceAttemptHTTP2: true,
|
|
|
+ MaxIdleConns: common.RelayMaxIdleConns,
|
|
|
+ MaxIdleConnsPerHost: common.RelayMaxIdleConnsPerHost,
|
|
|
+ ForceAttemptHTTP2: true,
|
|
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
return dialer.Dial(network, addr)
|
|
|
},
|