mirror of
https://github.com/jeessy2/ddns-go.git
synced 2025-10-21 07:53:45 +08:00
92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
package util
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"net"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
var dialer = &net.Dialer{
|
|
Timeout: 30 * time.Second,
|
|
KeepAlive: 30 * time.Second,
|
|
}
|
|
|
|
var defaultTransport = &http.Transport{
|
|
// from http.DefaultTransport
|
|
Proxy: http.ProxyFromEnvironment,
|
|
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
return dialer.DialContext(ctx, network, address)
|
|
},
|
|
ForceAttemptHTTP2: true,
|
|
MaxIdleConns: 100,
|
|
IdleConnTimeout: 90 * time.Second,
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
}
|
|
|
|
// CreateHTTPClient Create Default HTTP Client
|
|
func CreateHTTPClient() *http.Client {
|
|
return &http.Client{
|
|
Timeout: 30 * time.Second,
|
|
Transport: defaultTransport,
|
|
}
|
|
}
|
|
|
|
var noProxyTcp4Transport = &http.Transport{
|
|
// no proxy
|
|
// DisableKeepAlives
|
|
DisableKeepAlives: true,
|
|
// tcp4
|
|
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
return dialer.DialContext(ctx, "tcp4", address)
|
|
},
|
|
// from http.DefaultTransport
|
|
ForceAttemptHTTP2: true,
|
|
MaxIdleConns: 100,
|
|
IdleConnTimeout: 90 * time.Second,
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
}
|
|
|
|
var noProxyTcp6Transport = &http.Transport{
|
|
// no proxy
|
|
// DisableKeepAlives
|
|
DisableKeepAlives: true,
|
|
// tcp6
|
|
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
return dialer.DialContext(ctx, "tcp6", address)
|
|
},
|
|
// from http.DefaultTransport
|
|
ForceAttemptHTTP2: true,
|
|
MaxIdleConns: 100,
|
|
IdleConnTimeout: 90 * time.Second,
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
}
|
|
|
|
// CreateNoProxyHTTPClient Create NoProxy HTTP Client
|
|
func CreateNoProxyHTTPClient(network string) *http.Client {
|
|
if network == "tcp6" {
|
|
return &http.Client{
|
|
Timeout: 30 * time.Second,
|
|
Transport: noProxyTcp6Transport,
|
|
}
|
|
}
|
|
|
|
return &http.Client{
|
|
Timeout: 30 * time.Second,
|
|
Transport: noProxyTcp4Transport,
|
|
}
|
|
}
|
|
|
|
// SetInsecureSkipVerify 将所有 http.Transport 的 InsecureSkipVerify 设置为 true
|
|
func SetInsecureSkipVerify() {
|
|
transports := []*http.Transport{defaultTransport, noProxyTcp4Transport, noProxyTcp6Transport}
|
|
|
|
for _, transport := range transports {
|
|
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
|
}
|
|
}
|