-
Notifications
You must be signed in to change notification settings - Fork 0
/
external-ip.go
151 lines (140 loc) · 3.21 KB
/
external-ip.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package externalip
import (
"io/ioutil"
"net"
"net/http"
"strings"
"github.com/miekg/dns"
)
var queriers = []func() string{
GoogleDNS,
OpenDNS,
AkamaiDNS,
}
// DNS queries all DNS based external IP resolvers in parallel and
// produces a result if there is a quorum - at least half of the results
// are the same.
func DNS() string {
// Run the DNS queries in a goroutine, channeling the results into ch
ch := make(chan string)
for _, f := range queriers {
go func(f func() string) {
ch <- f()
}(f)
}
// Receive the results from the queries from ch and produce a result
// if more than half of them concur.
result := make(chan string)
go func() {
ips := make(map[string]int)
done := false
for range queriers {
ip := <-ch
ips[ip]++
if !done && ips[ip] > len(queriers)/2 {
result <- ip
done = true
}
}
close(result)
}()
return <-result
}
// GoogleDNS queries Google Public DNS for the external IP address
func GoogleDNS() (ip string) {
msg := new(dns.Msg)
msg.SetQuestion("o-o.myaddr.l.google.com.", dns.TypeTXT)
in, err := dns.Exchange(msg, "ns1.google.com:53")
if err != nil {
return
}
if t, ok := in.Answer[0].(*dns.TXT); ok {
ip = net.ParseIP(t.Txt[0]).To4().String()
}
return
}
// OpenDNS queries Open DNS for the external IP address
func OpenDNS() (ip string) {
msg := new(dns.Msg)
msg.SetQuestion("myip.opendns.com.", dns.TypeA)
in, err := dns.Exchange(msg, "resolver1.opendns.com:53")
if err != nil {
return
}
if a, ok := in.Answer[0].(*dns.A); ok {
ip = a.A.To4().String()
}
return
}
// AkamaiDNS queries Akamai DNS for the external IP address
func AkamaiDNS() (ip string) {
msg := new(dns.Msg)
msg.SetQuestion("whoami.akamai.net.", dns.TypeA)
in, err := dns.Exchange(msg, "ns1-1.akamaitech.net:53")
if err != nil {
return
}
if a, ok := in.Answer[0].(*dns.A); ok {
ip = a.A.To4().String()
}
return
}
var urls = []string{
"http://v4.ident.me/",
"http://whatismyip.akamai.com/",
"http://checkip.amazonaws.com/",
"http://ipecho.net/plain",
"http://inet-ip.info/ip",
"http://eth0.me/",
"http://wgetip.com/",
"http://bot.whatismyipaddress.com/",
"http://ipof.in/txt",
"http://smart-ip.net/myip",
"https://ip.tyk.nu/",
"https://tnx.nl/ip",
"https://l2.io/ip",
"https://api.ipify.org/",
"https://myexternalip.com/raw",
"https://icanhazip.com",
"https://ifconfig.io/ip",
"https://wtfismyip.com/text",
}
// HTTP queries all HTTP based external IP resolvers in parallel and
// produces a result if there is a quorum - at least half of the results
// are the same.
func HTTP() string {
// Run the web queries in a goroutine, channeling the result into ch
ch := make(chan string)
for _, url := range urls {
go func(url string) {
ch <- urlGetReadAll(url)
}(url)
}
result := make(chan string)
go func() {
ips := make(map[string]int)
done := false
for range urls {
ip := <-ch
ips[ip]++
if !done && ips[ip] > len(urls)/2 {
result <- ip
done = true
}
}
close(result)
}()
return <-result
}
func urlGetReadAll(url string) (ip string) {
resp, err := http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
return strings.TrimSpace(string(bytes))
}