Categorygithub.com/chyeh/pubip
modulepackage
0.0.0-20170203095919-b7e679cf541c
Repository: https://github.com/chyeh/pubip.git
Documentation: pkg.go.dev

# 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.

# Functions

Get queries several APIs to retrieve a `net.IP` of this machine's public IP address.
GetIPBy queries an API to retrieve a `net.IP` of this machine's public IP address.
GetIPStrBy queries an API to retrieve a `string` of this machine's public IP address.
GetStr queries several APIs to retrieve a `string` of this machine's public IP address.

# Constants

MaxTries is the maximum amount of tries to attempt to one service.
Version indicates the version of this package.

# Variables

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