From 708c0de5476e15b4dd2802d131e56fd4ae567a4e Mon Sep 17 00:00:00 2001 From: Timothy Date: Wed, 28 Feb 2024 23:28:54 +0800 Subject: [PATCH] set the network based on the current ip mode --- pkg/lib/ip_helper.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/lib/ip_helper.go b/pkg/lib/ip_helper.go index b4798ca0..711a1edb 100644 --- a/pkg/lib/ip_helper.go +++ b/pkg/lib/ip_helper.go @@ -1,6 +1,7 @@ package lib import ( + "context" "errors" "fmt" "io" @@ -195,9 +196,27 @@ func (helper *IPHelper) getCurrentIP() { // getIPOnline gets public IP from internet. func (helper *IPHelper) getIPOnline() string { + transport := &http.Transport{ + DialContext: func(ctx context.Context, _, addr string) (net.Conn, error) { + proto := "tcp" + + if strings.ToUpper(helper.configuration.IPType) == utils.IPV4 { + // Force the network to "tcp4" to use only IPv4 + proto = "tcp4" + } + + return (&net.Dialer{ + Timeout: time.Second * utils.DefaultTimeout, + KeepAlive: 30 * time.Second, + }).DialContext(ctx, proto, addr) + }, + } + client := &http.Client{ - Timeout: time.Second * utils.DefaultTimeout, + Timeout: time.Second * utils.DefaultTimeout, + Transport: transport, } + var onlineIP string for {