geo

package
v0.0.0-...-8aeb8a1 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2023 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxmindType = "maxmind"
	DummyType   = "dummy"

	UKCountry = "UK"
)
View Source
const (
	MaxmindPrefix = "maxmind://"
)

Variables

View Source
var (
	ErrInvalidLicenseKey   = errors.New("Invalid license key")
	ErrMaxmindFileNotFound = fmt.Errorf("MaxMind DB (file with %s suffix) wasn't found", mmdbSuffix)
)
View Source
var (
	EmptyIP = errors.New("IP is empty")

	EUCountries = map[string]bool{
		"BE": true,
		"EL": true,
		"LT": true,
		"PT": true,
		"BG": true,
		"ES": true,
		"LU": true,
		"RO": true,
		"CZ": true,
		"FR": true,
		"HU": true,
		"SI": true,
		"DK": true,
		"HR": true,
		"MT": true,
		"SK": true,
		"DE": true,
		"IT": true,
		"NL": true,
		"FI": true,
		"EE": true,
		"CY": true,
		"AT": true,
		"SE": true,
		"IE": true,
		"LV": true,
		"PL": true,
	}
)

Functions

func ParseConfigAsLink(config *ResolverConfig) (string, error)

ParseConfigAsLink works only with MaxMindType returns MaxMind URL or error

Types

type Data

type Data struct {
	Continent   string  `mapstructure:"continent,omitempty" json:"continent,omitempty"`
	Country     string  `mapstructure:"country,omitempty" json:"country,omitempty"`
	CountryName string  `mapstructure:"country_name,omitempty" json:"country_name,omitempty"`
	City        string  `mapstructure:"city,omitempty" json:"city,omitempty"`
	Lat         float64 `mapstructure:"latitude,omitempty" json:"latitude,omitempty"`
	Lon         float64 `mapstructure:"longitude,omitempty" json:"longitude,omitempty"`
	Zip         string  `mapstructure:"zip,omitempty" json:"zip,omitempty"`
	Region      string  `mapstructure:"region,omitempty" json:"region,omitempty"`

	ASN          uint   `mapstructure:"autonomous_system_number,omitempty" json:"autonomous_system_number,omitempty"`
	ASO          string `mapstructure:"autonomous_system_organization,omitempty" json:"autonomous_system_organization,omitempty"`
	ISP          string `mapstructure:"isp,omitempty" json:"isp,omitempty"`
	Organization string `mapstructure:"organization,omitempty" json:"organization,omitempty"`
	Domain       string `mapstructure:"domain,omitempty" json:"domain,omitempty"`
}

Data is a geo location data dto

type DummyResolver

type DummyResolver struct{}

DummyResolver is a dummy resolver that does nothing and returns empty geo data

func (*DummyResolver) Close

func (dr *DummyResolver) Close() error

func (*DummyResolver) Resolve

func (dr *DummyResolver) Resolve(ip string) (*Data, error)

func (*DummyResolver) Type

func (dr *DummyResolver) Type() string

type Edition

type Edition string
const (
	//paid editions
	GeoIP2CountryEdition Edition = "GeoIP2-Country"
	GeoIP2CityEdition    Edition = "GeoIP2-City"
	GeoIP2ISPEdition     Edition = "GeoIP2-ISP"
	GeoIP2DomainEdition  Edition = "GeoIP2-Domain"

	//free editions
	GeoLite2CityEdition    Edition = "GeoLite2-City"
	GeoLite2CountryEdition Edition = "GeoLite2-Country"
	GeoLite2ASNEdition     Edition = "GeoLite2-ASN"

	Unknown     Edition = ""
	NotRequired Edition = "NotRequired"
)

func (Edition) FreeAnalog

func (e Edition) FreeAnalog() Edition

FreeAnalog returns free database analog of the current add this to EditionRules as well

func (Edition) String

func (e Edition) String() string

type EditionData

type EditionData struct {
	Name    Edition       `json:"name"`
	Status  EditionStatus `json:"status"`
	Message string        `json:"message"`
}

EditionData is a dto for describing edition status

type EditionRule

type EditionRule struct {
	Main   *EditionData `json:"main"`
	Analog *EditionData `json:"analog"`
}

EditionRule is a dto for returning edition statuses

type EditionStatus

type EditionStatus string
const (
	StatusOK      EditionStatus = "ok"
	StatusError   EditionStatus = "error"
	StatusUnknown EditionStatus = "unknown"
)

type MaxMindConfig

type MaxMindConfig struct {
	MaxMindURL string `mapstructure:"maxmind_url" json:"maxmind_url,omitempty" yaml:"maxmind_url,omitempty"`
}

MaxMindConfig is a dto for MaxMind configuration serialization

type MaxMindFactory

type MaxMindFactory struct {
	// contains filtered or unexported fields
}

MaxMindFactory is responsible for creation geo resolvers from

func NewMaxmindFactory

func NewMaxmindFactory(officialDownloadURLTemplate string) *MaxMindFactory

func (*MaxMindFactory) Create

func (f *MaxMindFactory) Create(path string) (Resolver, error)

Create creates Resolver from: 1. URL in format: maxmind://<license_key>?editions=GeoIP2-City,GeoIP2-ASN (and others) 2. direct URL for download DB 3. file path to DB 4. dir path where there is a file (DB) with mmdbSuffix

func (*MaxMindFactory) Test

func (f *MaxMindFactory) Test(maxmindURL string) ([]*EditionRule, error)

Test tries to download all MaxMind databases and returns all available Edition or error if no editions are available

type MaxMindResolver

type MaxMindResolver struct {
	// contains filtered or unexported fields
}

MaxMindResolver is a geo location data Resolver that is based on MaxMind DB data

func (*MaxMindResolver) Close

func (mmr *MaxMindResolver) Close() (multiErr error)

Close closes all parsers

func (*MaxMindResolver) Resolve

func (mmr *MaxMindResolver) Resolve(ip string) (*Data, error)

Resolve returns location geo data (city, asn, domain) parsed from client ip address

func (*MaxMindResolver) Type

func (mmr *MaxMindResolver) Type() string

type Mock

type Mock map[string]*Data

func (Mock) Close

func (m Mock) Close() error

func (Mock) Resolve

func (m Mock) Resolve(ip string) (*Data, error)

func (Mock) Type

func (m Mock) Type() string

type Payload

type Payload struct {
	GeoResolvers map[string]*ResolverConfig `json:"geo_data_resolvers,omitempty"`
}

type Resolver

type Resolver interface {
	Resolve(ip string) (*Data, error)
	Type() string
	Close() error
}

Resolver is a geo based location data resolver

type ResolverConfig

type ResolverConfig struct {
	Type   string      `mapstructure:"type" json:"type,omitempty" yaml:"type,omitempty"`
	Config interface{} `mapstructure:"config" json:"config,omitempty" yaml:"config,omitempty"`
}

ResolverConfig is a dto for geo data resolver config serialization

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service keep up-to-date geo data resolvers

func NewService

func NewService(ctx context.Context, geoURL, globalGeoMaxmindPath, officialDownloadURLTemplate string) *Service

NewService returns initialized Service instance

func NewTestService

func NewTestService(globalResolver Resolver) *Service

NewTestService returns test instance. It is used only for tests

func (*Service) Close

func (s *Service) Close() (multiErr error)

func (*Service) GetGeoResolver

func (s *Service) GetGeoResolver(id string) Resolver

func (*Service) GetGlobalGeoResolver

func (s *Service) GetGlobalGeoResolver() Resolver

func (*Service) GetPaidEditions

func (s *Service) GetPaidEditions() []Edition

GetPaidEditions returns paidEditions

func (*Service) TestGeoResolver

func (s *Service) TestGeoResolver(url string) ([]*EditionRule, error)

TestGeoResolver proxies request to the factory

type Unit

type Unit struct {
	// contains filtered or unexported fields
}

type UpdatableProxy

type UpdatableProxy struct {
	// contains filtered or unexported fields
}

UpdatableProxy creates Resolver and re-create it every 24 hours with retry (if create fails e.g. because of connection issue)

func (*UpdatableProxy) Close

func (up *UpdatableProxy) Close() error

func (*UpdatableProxy) Resolve

func (up *UpdatableProxy) Resolve(ip string) (*Data, error)

func (*UpdatableProxy) Type

func (up *UpdatableProxy) Type() string

Jump to

Keyboard shortcuts

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