-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wtfismyip.go
73 lines (64 loc) · 1.61 KB
/
wtfismyip.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
package main
import (
"encoding/json"
log "github.com/sirupsen/logrus"
"io"
"net/http"
)
// WTFIsMyIPData is a data representation with the same structure returned by https://wtfismyip.com/json
type WTFIsMyIPData struct {
YourFuckingIPAddress string `json:"YourFuckingIPAddress"`
}
type WTFIsMyIPClient interface {
getIpAddress(ipv6 bool) (string, error)
}
type wtfismyipClient struct {
}
func (wtfismyip wtfismyipClient) getIpAddress(ipv6 bool) (string, error) {
wtfismyipURL := "https://ipv4.wtfismyip.com/json"
if ipv6 {
wtfismyipURL = "https://ipv6.wtfismyip.com/json"
}
response, err := http.Get(wtfismyipURL)
if err != nil {
return "", err
}
b, err := io.ReadAll(response.Body)
if err != nil {
return "", err
}
data := WTFIsMyIPData{}
err = json.Unmarshal(b, &data)
if err != nil {
return "", err
}
return data.YourFuckingIPAddress, nil
}
func getIpAddresses(c configuration, wtfismyip WTFIsMyIPClient) (string, string) {
ipv4needed, ipv6needed := ipNeeded(c)
ipv4address := ""
if ipv4needed {
wtfIPv4address, err := wtfismyip.getIpAddress(false)
if err != nil {
resolveErrorsTotal.WithLabelValues("A").Inc()
log.Error(err)
}
ipv4address = wtfIPv4address
log.Debugf("Current host ipv4: %s", ipv4address)
} else {
log.Debug("No ipv4 needed")
}
ipv6address := ""
if ipv6needed {
wtfIPv6address, err := wtfismyip.getIpAddress(true)
if err != nil {
resolveErrorsTotal.WithLabelValues("AAAA").Inc()
log.Error(err)
}
ipv6address = wtfIPv6address
log.Debugf("Current host ipv6: %s", ipv6address)
} else {
log.Debug("No ipv6 needed")
}
return ipv4address, ipv6address
}