-
Notifications
You must be signed in to change notification settings - Fork 0
/
myip.go
111 lines (88 loc) · 2.59 KB
/
myip.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package myip
import (
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/netip"
"net/url"
"time"
"strings"
)
var DEFAULT_PROVIDERS = []string{
"https://checkip.amazonaws.com",
"https://ipconfig.io",
"https://icanhazip.com",
"https://ifconfig.me",
}
var (
ErrEmptyProviders = errors.New("provider list is empty")
ErrInvalidURL = errors.New("invalid URL")
ErrNoResponse = errors.New("failed to get response from provider")
ErrStatusCode = errors.New("provider returned a non OK status code")
ErrInvalidResponse = errors.New("failed to read IP from provider")
ErrParseResponse = errors.New("failed to parse IP from provider")
)
type IPDiscover struct {
providers []string
index int
client *http.Client
}
func NewIPDiscover() (*IPDiscover, error) {
return NewIPDiscoverWithProviders(DEFAULT_PROVIDERS)
}
func NewIPDiscoverWithProviders(providers []string) (*IPDiscover, error) {
if len(providers) == 0 {
return nil, fmt.Errorf("%w", ErrEmptyProviders)
}
for _, provider := range providers {
if _, err := url.ParseRequestURI(provider); err != nil {
return nil, fmt.Errorf("%w: %s", ErrInvalidURL, err)
}
}
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
var dailer net.Dialer
return dailer.DialContext(ctx, "tcp4", addr)
}
client := &http.Client{
Timeout: 10 * time.Second,
Transport: transport,
}
return &IPDiscover{providers: providers, index: 0, client: client}, nil
}
func (ipd *IPDiscover) Providers() []string {
providers := []string{}
for _, provider := range ipd.providers {
providers = append(providers, strings.Clone(provider))
}
return providers
}
func (ipd *IPDiscover) nextProvider() string {
provider := ipd.providers[ipd.index]
ipd.index = (ipd.index + 1) % len(ipd.providers)
return provider
}
func (ipd *IPDiscover) Discover() (*netip.Addr, error) {
provider := ipd.nextProvider()
response, err := ipd.client.Get(provider)
if err != nil {
return nil, fmt.Errorf("%w (%s): %s", ErrNoResponse, provider, err)
}
defer response.Body.Close()
statusOK := response.StatusCode >= 200 && response.StatusCode < 300
if !statusOK {
return nil, fmt.Errorf("%w (%s): %d", ErrStatusCode, provider, response.StatusCode)
}
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("%w (%s): %s", ErrInvalidResponse, provider, err)
}
ip, err := netip.ParseAddr(strings.TrimSpace(string(body)))
if err != nil {
return nil, fmt.Errorf("%w (%s): %s", ErrParseResponse, provider, err)
}
return &ip, nil
}