Skip to content

Commit

Permalink
set the network based on the current ip mode
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyYe committed Feb 28, 2024
1 parent db05821 commit 708c0de
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion pkg/lib/ip_helper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lib

import (
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 708c0de

Please sign in to comment.