package module
v0.0.10 Latest Latest

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

Go to latest
Published: Mar 30, 2023 License: MIT Imports: 13 Imported by: 4


getmyip reference

Package getmyip provides methods to get public & outbound IP address of the current node.


go get -d



Package getmyip returns public and outbound IP addresses of current machine.



This section is empty.


View Source
var (
	// TimeOutGeolocation is the timeout for querying Geolocation API.
	TimeOutGeolocation = 15 * time.Second

	// TimeOutPublicIP is the timeout for querying Public IP API.
	TimeOutPublicIP = 10 * time.Second

	// TimeOutOutboundIP is the timeout for querying Outbound IP.
	TimeOutOutboundIP = 10 * time.Millisecond
View Source
var DefaultGeolocationToken = GeolocationAccessTokens{
	ServiceProviderIpapiCo:  "",
	ServiceProviderIpApiCom: "",

DefaultGeolocationToken contains access tokens for various Geolocation API, you may bring your own license by overriding this variable, leave it value empty if it requires no license.


func GetOutboundIP

func GetOutboundIP() (net.IP, error)

GetOutboundIP retrieves preferred outbound IP address of current machine.

func GetPublicIP

func GetPublicIP() (net.IP, error)

GetPublicIP retrieves public IPv4 address of current machine by calling various online services.

func GetPublicIPWithClient added in v0.0.4

func GetPublicIPWithClient(cli *http.Client, timeout time.Duration) (net.IP, error)

GetPublicIPWithClient retrieves public IPv4 address of current machine by calling various online services with given client and timeout.

func IsPublicIP added in v0.0.10

func IsPublicIP(ip net.IP) bool

IsPublicIP checks if the given IP is a public IP, i.e. not loopback, link-local, interface-local, unspecified, or multicast.


type Geolocation added in v0.0.3

type Geolocation struct {
	CountryCode string `json:"country_code,omitempty"`
	CountryFlag string `json:"country_flag,omitempty"`
	Country     string `json:"country,omitempty"`
	Region      string `json:"region,omitempty"`
	City        string `json:"city,omitempty"`
	Latitude    string `json:"latitude,omitempty"`
	Longitude   string `json:"longitude,omitempty"`
	Carrier     string `json:"carrier,omitempty"`
	ISP         string `json:"isp,omitempty"`
	ASN         string `json:"asn,omitempty"`

Geolocation describes the deduced geographic position and ISP info of an IP address.

type GeolocationAccessTokens added in v0.0.3

type GeolocationAccessTokens map[GeolocationServiceProvider]string

GeolocationAccessTokens represents a mapping between geolocation service provider and access token to its service.

type GeolocationResult added in v0.0.3

type GeolocationResult map[GeolocationServiceProvider]*Geolocation

GeolocationResult represents a collection of geolocation result from various service providers.

func GetGeolocation added in v0.0.3

func GetGeolocation(ip net.IP, tokens ...GeolocationAccessTokens) (GeolocationResult, error)

GetGeolocation returns deduced geographic position of a given IP address from licensed service providers.

func (GeolocationResult) GetCity added in v0.0.10

func (g GeolocationResult) GetCity() string

GetCity returns the most frequent city name in the result.

func (GeolocationResult) GetCountryCode added in v0.0.10

func (g GeolocationResult) GetCountryCode() string

GetCountryCode returns the most frequent country code in the result.

func (GeolocationResult) String added in v0.0.3

func (g GeolocationResult) String() string

type GeolocationServiceProvider added in v0.0.3

type GeolocationServiceProvider string

GeolocationServiceProvider represents the name of service provider of Geolocation API.

const (
	ServiceProviderIpregistry GeolocationServiceProvider = "ipregistry"
	ServiceProviderIpify      GeolocationServiceProvider = "ipify"
	ServiceProviderIpapiCo    GeolocationServiceProvider = "ipapico"
	ServiceProviderIpApiCom   GeolocationServiceProvider = "ipapicom"
	ServiceProviderIpdata     GeolocationServiceProvider = "ipdata"

Name of service provider of Geolocation API

Jump to

Keyboard shortcuts

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