geoip

package
v0.0.0-...-4fe0a07 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2022 License: MIT Imports: 9 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Providers = map[string]*Provider{
	"freegeoip.app": {
		Name:        "freegeoip.app",
		TemplateURL: "https://freegeoip.app/json/%[1]s",
		Mappings: Location{
			CountryCode: "country_code",
			Country:     "country_name",
			RegionCode:  "region_code",
			Region:      "region_name",
			City:        "city",
			TimeZone:    "time_zone",
		},
	},
	"db-ip.com": {
		Name:        "db-ip.com",
		TemplateURL: "http://api.db-ip.com/v2/free/%[1]s",
		Mappings: Location{
			CountryCode: "countryCode",
			Country:     "countryName",
			RegionCode:  "stateProvCode",
			Region:      "stateProv",
			City:        "city",
		},
	},
	"keycdn.com": {
		Name:        "keycdn.com",
		TemplateURL: "https://tools.keycdn.com/geo.json?host=%[1]s",
		Mappings: Location{
			CountryCode: "data.geo.country_code",
			Country:     "data.geo.country_name",
			RegionCode:  "data.geo.region_code",
			Region:      "data.geo.region_name",
			City:        "data.geo.city",
			TimeZone:    "data.geo.time_zone",
			ISP:         "data.geo.isp",
		},
	},
	"ip-api.com": {
		Name:        "ip-api.com",
		TemplateURL: "http://ip-api.com/json/%[1]s",
		Mappings: Location{
			CountryCode: "countryCode",
			Country:     "country",
			RegionCode:  "region",
			Region:      "regionName",
			City:        "city",
			TimeZone:    "timezone",
			ISP:         "isp",
		},
	},
	"ipinfo.io": {
		Name:        "ipinfo.io",
		TemplateURL: "http://ipinfo.io/%[1]s?token=%[2]s",
		Mappings: Location{
			Country:    "country",
			RegionCode: "region",
			City:       "city",
			TimeZone:   "timezone",
			ISP:        "org",
		},
	},
}

Providers is a map of known providers

Functions

func IsPrivateIP

func IsPrivateIP(ip net.IP) bool

IsPrivateIP returns true if the given IP address belongs to private network space

Types

type Client

type Client interface {
	Provider() string
	GetLocation(ctx context.Context, hostname string) (*Location, error)
}

Client is used to retrieve location data from a provider

func GetClients

func GetClients(providerList string) (clients []Client)

GetClients creates new clients from the comma separated list of provider names Parameters are supported too, eg: "ipinfo.io xxtokenxx, ip-api.com, freegeoip.app"

type Location

type Location struct {
	IP          string `json:"ip"`
	Hostname    string `json:"hostname,omitempty"`
	CountryCode string `json:"country_code"`
	Country     string `json:"country"`
	RegionCode  string `json:"region_code"`
	Region      string `json:"region"`
	City        string `json:"city"`
	TimeZone    string `json:"timezone"`
	ISP         string `json:"isp,omitempty"`
	Source      string `json:"source,omitempty"`
}

Location contains geographical location and other data of an IP address or hostname

func (Location) String

func (loc Location) String() string

type Provider

type Provider struct {
	Name        string
	TemplateURL string // must always contain a %[1]s for the IP/hostname
	Mappings    Location
}

Provider contains the URL template to a 3rd party IP geolocation API and the mappings from the provider's json fields to Location fields

func (*Provider) NewClient

func (p *Provider) NewClient(params ...string) Client

NewClient returns a new client to the provider

Jump to

Keyboard shortcuts

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