Skip to content

Get your public Ip by looking it up on public services

License

Notifications You must be signed in to change notification settings

hamochi/whatismyip

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

whatismyip

Get your public Ip by looking it up on public ip services.

GitHub License GoDoc Build Status

Introduction

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.

Install

$ go get -u github.com/hamochi/whatismyip"

Usage

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:

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.

Errors

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.

Credits

Based on https://github.com/chyeh/pubip

About

Get your public Ip by looking it up on public services

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages