generated from bsv-blockchain/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhelpers.go
More file actions
39 lines (34 loc) · 821 Bytes
/
helpers.go
File metadata and controls
39 lines (34 loc) · 821 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package p2p
import (
"context"
"io"
"net"
"net/http"
"time"
)
// GetPublicIP fetches the public IP address from ifconfig.me
func GetPublicIP(ctx context.Context) (string, error) {
transport := &http.Transport{
DialContext: func(ctx context.Context, _, addr string) (net.Conn, error) {
// Force the use of IPv4 by specifying 'tcp4' as the network
return (&net.Dialer{}).DialContext(ctx, "tcp4", addr)
},
TLSHandshakeTimeout: 10 * time.Second,
}
client := &http.Client{
Transport: transport,
}
req, err := http.NewRequestWithContext(ctx, "GET", "https://ifconfig.me/ip", nil)
if err != nil {
return "", err
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), resp.Body.Close()
}