pubip

package module
v0.0.0-...-b7e679c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2017 License: Unlicense Imports: 10 Imported by: 20

README

pubip

A simple package for getting your public IP address by several services. It's inspired by go-ipify.

GitHub License GoDoc Build Status

Introduction

In short, It validates the results from several services and returns the IP address if a valid one is found. If you have ever tried to deploy services in China, you would understand what the [fallacies of distributed computing](fallacies of distributed computing) are. Based on the assumption that the services your program depends on are not always available, it's better to have more backups services. This package gives you the public IP address from several APIs that I found.

Installation

To install pubip, simply run:

$ go get -u github.com/chyeh/pubip

This will install the latest version of the package automatically.

Usage

Here's a simple example:

package main

import (
    "fmt"
    "github.com/chyeh/pubip"
)

func main() {
    ip, err := pubip.Get()
    if err != nil {
        fmt.Println("Couldn't get my IP address:", err)
    } else {
        fmt.Println("My IP address is:", ip)
    }
}

For more details, please take a look at the GoDoc.

Error handling

It returns an error when the followings happen:

  • It fails to get at least 3 results from the services
  • The results from different services are not identical

Contributing

Just send me a PR or open an issue. Please include tests for your changes.

Documentation

Overview

Package pubip gets your public IP address from several services

Index

Constants

View Source
const MaxTries = 3

MaxTries is the maximum amount of tries to attempt to one service.

View Source
const Version = "1.0.0"

Version indicates the version of this package.

Variables

View Source
var APIURIs = []string{
	"https://api.ipify.org",
	"http://myexternalip.com/raw",
	"http://ipinfo.io/ip",
	"http://ipecho.net/plain",
	"http://icanhazip.com",
	"http://ifconfig.me/ip",
	"http://ident.me",
	"http://checkip.amazonaws.com",
	"http://bot.whatismyipaddress.com",
	"http://whatismyip.akamai.com",
	"http://wgetip.com",
	"http://ip.appspot.com",
	"http://ip.tyk.nu",
	"https://shtuff.it/myip/short",
}

APIURIs is the URIs of the services.

View Source
var Timeout = 2 * time.Second

Timeout sets the time limit of collecting results from different services.

Functions

func Get

func Get() (net.IP, error)

Get queries several APIs to retrieve a `net.IP` of this machine's public IP address.

Usage:

package main

import (
	"fmt"
	"github.com/chyeh/pubip"
)

func main() {
	ip, err := pubip.Get()
	if err != nil {
		fmt.Println("Couldn't get my IP address:", err)
	} else {
		fmt.Println("My IP address is:", ip)
	}
}

func GetIPBy

func GetIPBy(dest string) (net.IP, error)

GetIPBy queries an API to retrieve a `net.IP` of this machine's public IP address.

Usage:

package main

import (
	"fmt"
	"github.com/chyeh/pubip"
)

func main() {
	ip, err := pubip.GetIPBy("https://api.ipify.org")
	if err != nil {
		fmt.Println("Couldn't get my IP address:", err)
	} else {
		fmt.Println("My IP address is:", ip)
	}
}

func GetIPStrBy

func GetIPStrBy(dest string) (string, error)

GetIPStrBy queries an API to retrieve a `string` of this machine's public IP address.

Usage:

package main

import (
	"fmt"
	"github.com/chyeh/pubip"
)

func main() {
	ip, err := pubip.GetIPBy("https://api.ipify.org")
	if err != nil {
		fmt.Println("Couldn't get my IP address:", err)
	} else {
		fmt.Println("My IP address is:", ip)
	}
}

func GetStr

func GetStr() (string, error)

GetStr queries several APIs to retrieve a `string` of this machine's public IP address.

Usage:

package main

import (
	"fmt"
	"github.com/chyeh/pubip"
)

func main() {
	ip, err := pubip.Get()
	if err != nil {
		fmt.Println("Couldn't get my IP address:", err)
	} else {
		fmt.Println("My IP address is:", ip)
	}
}

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL