sxgeo

package module
v0.0.2-beta.1 Latest Latest
Warning

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

Go to latest
Published: May 21, 2021 License: MIT Imports: 12 Imported by: 1

README

sxgeo

A tool to work with Sypex Geo database, which helps detect a country and a city by IP

Надстройка над базой данных Sypex Geo версии 2.2 (https://sypexgeo.net/ru/docs/)

Настройка

В базу записаны коды, зависящие от машинного порядка записи байтов (LittleEndian, BigEndian). Для начала работы нужно определить порядок на рабочей машине. По умолчанию в переменную hbo установлена LittleEndian.

var hbo = binary.LittleEndian

Кодировку определяйте с помощью функции DetectEndian(), задавайте SetEndian(sxgeo.LITTLE || sxgeo.BIG).

Использование

Cчитайте файл SxGeoCity.dat в память:

	if _, err := sxgeo.ReadDBToMemory(dbPath); err != nil {
		log.Fatalf("error: cannot read database file: %v", err)
	}

IP строкой вида 8.8.8.8 передайте в функцию GetCityFull:

		city, err := sxgeo.GetCityFull(ip)
		if err != nil {
			fmt.Printf("error: %v", err)
			os.Exit(1)
		}

Теперь можно преобразовать полученную структуру, например, в json.

		enc, err := json.Marshal(city)
		if err != nil {
			fmt.Printf("error: %v", err)
			os.Exit(1)
		}

		fmt.Printf("%s\n", enc)

Documentation

Index

Constants

View Source
const (
	PACKTYPECOUNTRY = 0
	PACKTYPEREGION  = 1
	PACKTYPECITY    = 2
)
View Source
const BIG = false
View Source
const LITTLE = true

Variables

View Source
var (
	DB      []byte
	Regions []byte
	Cities  []byte
)

Functions

func Endian

func Endian() string

Endian sets host endian

func ReadDBToMemory

func ReadDBToMemory(path string) (bool, error)

ReadDBToMemory Reads the whole DB to the memory

func Seek

func Seek(ip string) (uint32, error)

Seek seeks an IP index in the DB It's a reflect of PHP get_num($ip)

func SetEndian

func SetEndian(order bool)

SetEndian sets host endian

func ToMemory

func ToMemory(f *bytes.Reader) (bool, error)

ToMemory Reads the whole DB to the memory without temp file

Types

type City

type City struct {
	ID         int     `json:"id"`
	NameRu     string  `json:"name_ru"`
	NameEn     string  `json:"name_en"`
	Latitude   float64 `json:"lat"`
	Longitude  float64 `json:"lon"`
	RegionSeek float64 `json:"region_seek"`
}

type Country

type Country struct {
	ID     uint8  `json:"id"`
	ISO    string `json:"iso"`
	NameRu string `json:"name_ru"`
	NameEn string `json:"name_en"`
}

type Full

type Full struct {
	City    *City    `json:"city"`
	Country *Country `json:"country"`
	Region  *Region  `json:"region"`
}

func GetCityFull

func GetCityFull(ip string) (*Full, error)
type Header struct {
	Name     string
	Selector string
	Length   int
	PowExp   float64
}

Header распакованный заголовок города, региона или страны

type Info

type Info struct {
	Ver         byte   // C
	Time        uint32 // N
	Type        byte
	Charset     byte
	BIdxLen     byte
	MIdxLen     uint16 // n
	Range       uint16
	DbItems     uint32
	IdLen       byte
	MaxRegion   uint16
	MaxCity     uint16
	RegionSize  uint32
	CitySize    uint32
	MaxCountry  uint16
	CountrySize uint32
	PackSize    uint16
}
var I Info

func (*Info) String

func (i *Info) String() string

String распечатает информацию о БД

type Meta

type Meta struct {
	BlockLen byte
	BIdxStr  []byte
	MIdxStr  []byte

	Pack        [][]byte
	PackHeaders [][]Header

	DbBegin      int64
	BIdxArr      []uint32
	MIdxArr      [][]byte
	RegionsBegin int64
	CitiesBegin  int64
}
var M Meta

type Region

type Region struct {
	ID     int    `json:"id"`
	ISO    string `json:"iso"`
	NameRu string `json:"name_ru"`
	NameEn string `json:"name_en"`
}

Jump to

Keyboard shortcuts

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