11 var ipCheckServices = []string{
12 "http://members.3322.org/dyndns/getip",
13 "http://ifconfig.me/",
14 "http://icanhazip.com/",
15 "http://ifconfig.io/ip",
17 "http://whatismyip.akamai.com/",
18 "http://myip.dnsomatic.com/",
19 "http://diagnostic.opendns.com/myip",
20 "http://myexternalip.com/raw",
23 type IpResult struct {
28 var timeout = time.Duration(5)
30 func GetIP() *IpResult {
31 resultCh := make(chan *IpResult, 1)
32 for _, s := range ipCheckServices {
33 go ipAddress(s, resultCh)
38 case result := <-resultCh:
40 case <-time.After(time.Second * timeout):
41 return &IpResult{false, ""}
46 func ipAddress(service string, done chan<- *IpResult) {
47 client := http.Client{Timeout: time.Duration(timeout * time.Second)}
48 resp, err := client.Get(service)
53 defer resp.Body.Close()
54 data, err := ioutil.ReadAll(resp.Body)
59 address := strings.TrimSpace(string(data))
60 if net.ParseIP(address) != nil {
62 case done <- &IpResult{true, address}: