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 // IPResult is the ip check response
24 type IPResult struct {
29 var timeout = time.Duration(5)
31 // GetIP return the ip of the current host
32 func GetIP() *IPResult {
33 resultCh := make(chan *IPResult, 1)
34 for _, s := range ipCheckServices {
35 go ipAddress(s, resultCh)
40 case result := <-resultCh:
42 case <-time.After(time.Second * timeout):
43 return &IPResult{false, ""}
48 func ipAddress(service string, done chan<- *IPResult) {
49 client := http.Client{Timeout: time.Duration(timeout * time.Second)}
50 resp, err := client.Get(service)
55 defer resp.Body.Close()
56 data, err := ioutil.ReadAll(resp.Body)
61 address := strings.TrimSpace(string(data))
62 if ip := net.ParseIP(address); ip != nil && ip.To4() != nil {
64 case done <- &IPResult{true, address}: