-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrom_service.go
128 lines (120 loc) · 2.61 KB
/
from_service.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
package pubip
import (
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"strings"
"time"
)
var services = []service{
{v6: "http://ident.me"},
{v4: "http://ipecho.net/plain"},
{
v6: "https://v6.ifconfig.co",
v4: "https://v4.ifconfig.co",
},
{v4: "https://ipinfo.io/ip"},
{
v6: "https://ipv6.icanhazip.com",
v4: "https://ipv4.icanhazip.com",
},
{v6: "http://bot.whatismyipaddress.com"},
{
v6: "https://myexternalip.com/raw",
v4: "https://ipv4.myexternalip.com/raw",
},
{v4: "http://checkip.amazonaws.com"},
{
v6: "https://6.ifcfg.me",
v4: "https://4.ifcfg.me",
},
{v6: "https://ip.tyk.nu"},
{v6: "https://tnx.nl/ip"},
{
v6: "https://l2.io/ip",
v4: "https://www.l2.io/ip",
},
{v6: "https://ip.appspot.com"},
{v4: "https://ipof.in/txt"},
{v6: "https://wgetip.com"},
{v4: "http://eth0.me"},
{v6: "https://tnx.nl/ip"},
}
const (
contentType = "Content-Type"
typeTextPlain = "text/plain"
errWrongStatus = "status code %d from %q"
)
type service struct {
v6 url
v4 url
}
func (ser service) ipv6func() IPFn {
return func() (string, error) {
ip, err := get(ser.v6)
if err == nil && !IsIPv6(ip) {
return "", errNotV6Address
}
return ip, err
}
}
func (ser service) ipv4func() IPFn {
return func() (string, error) {
ip, err := get(ser.v4)
if err == nil && !IsIPv4(ip) {
return "", errNotV4Address
}
return ip, err
}
}
// AllFuncs of IPFn with IPType t in random order.
// Don't forget to call before using this function once rand.Seed().
func AllFuncs(t IPType) IPFuncs {
var a IPFuncs
for _, ser := range services {
switch {
case (t == IPv6orIPv4 || t == IPv4) && len(ser.v4) > 0:
a = append(a, ser.ipv4func())
continue
case (t == IPv6orIPv4 || t == IPv6) && len(ser.v6) > 0:
a = append(a, ser.ipv6func())
continue
}
}
r := make(IPFuncs, len(a))
for k, v := range rand.Perm(len(a)) {
r[k] = a[v]
}
return r
}
// get returns the body of the response with a request timeout of 1 second.
func get(u url) (string, error) {
// setup request params
req, err := http.NewRequest("GET", string(u), nil)
if err != nil {
return "", err
}
req.Header.Set(contentType, typeTextPlain)
client := http.Client{
Timeout: time.Duration(2 * time.Second),
}
// query server
r, err := client.Do(req)
if err != nil {
return "", err
}
defer r.Body.Close()
// check status-code
if r.StatusCode != http.StatusOK {
return "", fmt.Errorf(errWrongStatus, r.StatusCode, u)
}
// read result
lr := io.LimitReader(r.Body, 64) // read max 64 bytes from server
body, err := ioutil.ReadAll(lr)
if err != nil {
return "", err
}
return strings.TrimSpace(string(body)), nil
}