Skip to content

Commit 708c0de

Browse files
committed
set the network based on the current ip mode
1 parent db05821 commit 708c0de

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

pkg/lib/ip_helper.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package lib
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"io"
@@ -195,9 +196,27 @@ func (helper *IPHelper) getCurrentIP() {
195196

196197
// getIPOnline gets public IP from internet.
197198
func (helper *IPHelper) getIPOnline() string {
199+
transport := &http.Transport{
200+
DialContext: func(ctx context.Context, _, addr string) (net.Conn, error) {
201+
proto := "tcp"
202+
203+
if strings.ToUpper(helper.configuration.IPType) == utils.IPV4 {
204+
// Force the network to "tcp4" to use only IPv4
205+
proto = "tcp4"
206+
}
207+
208+
return (&net.Dialer{
209+
Timeout: time.Second * utils.DefaultTimeout,
210+
KeepAlive: 30 * time.Second,
211+
}).DialContext(ctx, proto, addr)
212+
},
213+
}
214+
198215
client := &http.Client{
199-
Timeout: time.Second * utils.DefaultTimeout,
216+
Timeout: time.Second * utils.DefaultTimeout,
217+
Transport: transport,
200218
}
219+
201220
var onlineIP string
202221

203222
for {

0 commit comments

Comments
 (0)