Skip to content

Commit

Permalink
refactor: set InsecureSkipVerify only once (#822)
Browse files Browse the repository at this point in the history
  • Loading branch information
WaterLemons2k authored Aug 17, 2023
1 parent 98756f2 commit 5b77583
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
18 changes: 9 additions & 9 deletions util/http_client_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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}
}
}

0 comments on commit 5b77583

Please sign in to comment.