From a8dd2e33cfedf62c0d63519a022a433d5a8ed60e Mon Sep 17 00:00:00 2001 From: WaterLemons2k <62788816+WaterLemons2k@users.noreply.github.com> Date: Thu, 17 Aug 2023 16:13:23 +0800 Subject: [PATCH] refactor: set `InsecureSkipVerify` only once --- main.go | 2 +- util/http_client_util.go | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 8079a0291..279c5d606 100644 --- a/main.go +++ b/main.go @@ -80,7 +80,7 @@ func main() { os.Setenv(util.ConfigFilePathENV, absPath) } if *skipVerify { - os.Setenv(util.SkipVerifyENV, "true") + util.SetInsecureSkipVerify() } if *customDNSServer != "" { util.NewDialerResolver(*customDNSServer + ":53") diff --git a/util/http_client_util.go b/util/http_client_util.go index 1425229e0..ebdf983d3 100644 --- a/util/http_client_util.go +++ b/util/http_client_util.go @@ -5,12 +5,9 @@ import ( "crypto/tls" "net" "net/http" - "os" "time" ) -const SkipVerifyENV = "DDNS_SKIP_VERIFY" - var dialer = &net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, @@ -31,8 +28,6 @@ var defaultTransport = &http.Transport{ // CreateHTTPClient Create Default HTTP Client func CreateHTTPClient() *http.Client { - // SkipVerfiry - defaultTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: os.Getenv(SkipVerifyENV) == "true"} return &http.Client{ Timeout: 30 * time.Second, Transport: defaultTransport, @@ -74,18 +69,23 @@ var noProxyTcp6Transport = &http.Transport{ // CreateNoProxyHTTPClient Create NoProxy HTTP Client func CreateNoProxyHTTPClient(network string) *http.Client { if network == "tcp6" { - // SkipVerfiry - noProxyTcp6Transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: os.Getenv(SkipVerifyENV) == "true"} return &http.Client{ Timeout: 30 * time.Second, Transport: noProxyTcp6Transport, } } - // SkipVerfiry - noProxyTcp4Transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: os.Getenv(SkipVerifyENV) == "true"} 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} + } +}