Get your public Ip by looking it up on public ip services.
This package calls several public ip lookup services and when it matches ip from at least two sources it returns it, otherwise it returns an error.
$ go get -u github.com/hamochi/whatismyip"package main
import (
"fmt"
"log"
"github.com/hamochi/whatismyip"
)
func main() {
ip, err := whatismyip.Get()
if err != nil {
log.Fatal(err)
}
fmt.Println(ip.String())
}The default list of IP lookup services are:
- https://checkip.amazonaws.com
- http://whatismyip.akamai.com
- https://api.ipify.org
- http://ifconfig.me/ip
- http://myexternalip.com/raw
- http://ipinfo.io/ip
- http://ipecho.net/plain
- http://icanhazip.com
- http://ifconfig.me/ip
- http://ident.me
- http://bot.whatismyipaddress.com
- http://wgetip.com
- http://ip.tyk.nu
If you want to pass your own list your can use whatismyip.GetWithCustomServices()
package main
import (
"fmt"
"github.com/hamochi/whatismyip"
"log"
)
func main() {
ip, err := whatismyip.GetWithCustomServices([]string{
"http://myexternalip.com/raw",
"http://ipinfo.io/ip",
"http://ipecho.net/plain",
"http://icanhazip.com",
})
if err != nil {
log.Fatal(err)
}
fmt.Println(ip.String())
}Please note that you need to pass at least two services into the list.
You can cast the returned error to ApiErrors and inspect the returned error for each failed service call. Please take a look at the test file for more details.
Based on https://github.com/chyeh/pubip