mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 04:30:05 +00:00
Fix Coroutine Leaks and Proxy Handling in http.Client (#304)
* fix: builtin variable conflict * feat: add pkg/utils/http HttpClientSkipTlsVerify HttpClient * fix: realtime create http Transport * fix: http keepalive connection may not reusable * feat: allow http request use proxy from environment
This commit is contained in:
47
pkg/utils/http.go
Normal file
47
pkg/utils/http.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
HttpClientSkipTlsVerify *http.Client
|
||||
HttpClient *http.Client
|
||||
)
|
||||
|
||||
func init() {
|
||||
HttpClientSkipTlsVerify = httpClient(_httpClient{
|
||||
Transport: httpTransport(_httpTransport{
|
||||
VerifySSL: true,
|
||||
}),
|
||||
})
|
||||
HttpClient = httpClient(_httpClient{
|
||||
Transport: httpTransport(_httpTransport{
|
||||
VerifySSL: false,
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
type _httpTransport struct {
|
||||
VerifySSL bool
|
||||
}
|
||||
|
||||
func httpTransport(conf _httpTransport) *http.Transport {
|
||||
return &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: conf.VerifySSL},
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
}
|
||||
}
|
||||
|
||||
type _httpClient struct {
|
||||
Transport *http.Transport
|
||||
}
|
||||
|
||||
func httpClient(conf _httpClient) *http.Client {
|
||||
return &http.Client{
|
||||
Transport: conf.Transport,
|
||||
Timeout: time.Minute * 10,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user