ipdb

package module
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2022 License: Apache-2.0 Imports: 11 Imported by: 351

README

ipdb-go

TravisCI Build Status Coverage Status IPDB Database API Document

IPIP.net officially supported IP database ipdb format parsing library

Installing

go get github.com/ipipdotnet/ipdb-go

Code Example

支持IPDB格式地级市精度IP离线库(免费版,每周高级版,每日标准版,每日高级版,每日专业版,每日旗舰版)


package main

import (
	"github.com/ipipdotnet/ipdb-go"
	"fmt"
	"log"
)

func main() {
	db, err := ipdb.NewCity("/path/to/city.ipv4.ipdb")
	if err != nil {
		log.Fatal(err)
	}

	db.Reload("/path/to/city.ipv4.ipdb") // 更新 ipdb 文件后可调用 Reload 方法重新加载内容

	fmt.Println(db.IsIPv4()) // check database support ip type
	fmt.Println(db.IsIPv6()) // check database support ip type
	fmt.Println(db.BuildTime()) // database build time
	fmt.Println(db.Languages()) // database support language
	fmt.Println(db.Fields()) // database support fields

	fmt.Println(db.FindInfo("2001:250:200::", "CN")) // return CityInfo
	fmt.Println(db.Find("1.1.1.1", "CN")) // return []string
	fmt.Println(db.FindMap("118.28.8.8", "CN")) // return map[string]string
	fmt.Println(db.FindInfo("127.0.0.1", "CN")) // return CityInfo

	fmt.Println()
}

地级市精度库数据字段说明

country_name : 国家名字 (每周高级版及其以上版本包含)
region_name  : 省名字   (每周高级版及其以上版本包含)
city_name    : 城市名字 (每周高级版及其以上版本包含)
owner_domain : 所有者   (每周高级版及其以上版本包含)
isp_domain  : 运营商 (每周高级版与每日高级版及其以上版本包含)
latitude  :  纬度   (每日标准版及其以上版本包含)
longitude : 经度    (每日标准版及其以上版本包含)
timezone : 时区     (每日标准版及其以上版本包含)
utc_offset : UTC时区    (每日标准版及其以上版本包含)
china_admin_code : 中国行政区划代码 (每日标准版及其以上版本包含)
idd_code : 国家电话号码前缀 (每日标准版及其以上版本包含)
country_code : 国家2位代码  (每日标准版及其以上版本包含)
continent_code : 大洲代码   (每日标准版及其以上版本包含)
idc : IDC |  VPN   (每日专业版及其以上版本包含)
base_station : 基站 | WIFI (每日专业版及其以上版本包含)
country_code3 : 国家3位代码 (每日专业版及其以上版本包含)
european_union : 是否为欧盟成员国: 1 | 0 (每日专业版及其以上版本包含)
currency_code : 当前国家货币代码    (每日旗舰版及其以上版本包含)
currency_name : 当前国家货币名称    (每日旗舰版及其以上版本包含)
anycast : ANYCAST       (每日旗舰版及其以上版本包含)

适用于IPDB格式的中国地区 IPv4 区县库

db, err := ipdb.NewDistrict("/path/to/quxian.ipdb")
if err != nil {
	log.Fatal(err)
}
fmt.Println(db.IsIPv4())    // check database support ip type
fmt.Println(db.IsIPv6())    // check database support ip type
fmt.Println(db.Languages()) // database support language
fmt.Println(db.Fields())    // database support fields

fmt.Println(db.Find("1.12.7.255", "CN"))
fmt.Println(db.FindMap("2001:250:200::", "CN"))
fmt.Println(db.FindInfo("1.12.7.255", "CN"))

fmt.Println()

适用于IPDB格式的基站 IPv4 库

db, err := ipdb.NewBaseStation("/path/to/station_ip.ipdb")
if err != nil {
	log.Fatal(err)
}

fmt.Println(db.FindMap("223.220.223.255", "CN"))

Documentation

Index

Constants

View Source
const IPv4 = 0x01
View Source
const IPv6 = 0x02

Variables

View Source
var (
	ErrFileSize = errors.New("IP Database file size error.")
	ErrMetaData = errors.New("IP Database metadata error.")
	ErrReadFull = errors.New("IP Database ReadFull error.")

	ErrDatabaseError = errors.New("database error")

	ErrIPFormat = errors.New("Query IP Format error.")

	ErrNoSupportLanguage = errors.New("language not support")
	ErrNoSupportIPv4     = errors.New("IPv4 not support")
	ErrNoSupportIPv6     = errors.New("IPv6 not support")

	ErrDataNotExists = errors.New("data is not exists")
)

Functions

This section is empty.

Types

type ASNInfo added in v1.3.0

type ASNInfo struct {
	ASN      int    `json:"asn"`
	Registry string `json:"reg"`
	Country  string `json:"cc"`
	Net      string `json:"net"`
	Org      string `json:"org"`
	Type     string `json:"type"`
	Domain   string `json:"domain"`
}

type BaseStation

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

func NewBaseStation

func NewBaseStation(name string) (*BaseStation, error)

func (*BaseStation) BuildTime

func (db *BaseStation) BuildTime() time.Time

func (*BaseStation) Fields

func (db *BaseStation) Fields() []string

func (*BaseStation) Find

func (db *BaseStation) Find(addr, language string) ([]string, error)

func (*BaseStation) FindInfo

func (db *BaseStation) FindInfo(addr, language string) (*BaseStationInfo, error)

func (*BaseStation) FindMap

func (db *BaseStation) FindMap(addr, language string) (map[string]string, error)

func (*BaseStation) IsIPv4

func (db *BaseStation) IsIPv4() bool

func (*BaseStation) IsIPv6

func (db *BaseStation) IsIPv6() bool

func (*BaseStation) Languages

func (db *BaseStation) Languages() []string

func (*BaseStation) Reload added in v1.2.0

func (db *BaseStation) Reload(name string) error

type BaseStationInfo

type BaseStationInfo struct {
	CountryName string `json:"country_name"`
	RegionName  string `json:"region_name"`
	CityName    string `json:"city_name"`
	OwnerDomain string `json:"owner_domain"`
	IspDomain   string `json:"isp_domain"`
	BaseStation string `json:"base_station"`
}

type City

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

City struct

func NewCity

func NewCity(name string) (*City, error)

NewCity initialize

func NewCityFromBytes added in v1.3.3

func NewCityFromBytes(bs []byte) (*City, error)

func (*City) BuildTime

func (db *City) BuildTime() time.Time

BuildTime return database build Time

func (*City) Fields

func (db *City) Fields() []string

Fields return support fields

func (*City) Find

func (db *City) Find(addr, language string) ([]string, error)

Find query with addr

func (*City) FindInfo

func (db *City) FindInfo(addr, language string) (*CityInfo, error)

FindInfo query with addr

func (*City) FindMap

func (db *City) FindMap(addr, language string) (map[string]string, error)

FindMap query with addr

func (*City) IsIPv4

func (db *City) IsIPv4() bool

IsIPv4 whether support ipv4

func (*City) IsIPv6

func (db *City) IsIPv6() bool

IsIPv6 whether support ipv6

func (*City) Languages

func (db *City) Languages() []string

Languages return support languages

func (*City) Reload added in v1.2.0

func (db *City) Reload(name string) error

Reload the database

type CityInfo

type CityInfo struct {
	CountryName     string `json:"country_name"`
	RegionName      string `json:"region_name"`
	CityName        string `json:"city_name"`
	DistrictName    string `json:"district_name"`
	OwnerDomain     string `json:"owner_domain"`
	IspDomain       string `json:"isp_domain"`
	Latitude        string `json:"latitude"`
	Longitude       string `json:"longitude"`
	Timezone        string `json:"timezone"`
	UtcOffset       string `json:"utc_offset"`
	ChinaRegionCode string `json:"china_region_code"`
	ChinaCityCode   string `json:"china_city_code"`
	ChinaAdminCode  string `json:"china_admin_code"`
	IddCode         string `json:"idd_code"`
	CountryCode     string `json:"country_code"`
	ContinentCode   string `json:"continent_code"`
	IDC             string `json:"idc"`
	BaseStation     string `json:"base_station"`
	CountryCode3    string `json:"country_code3"`
	EuropeanUnion   string `json:"european_union"`
	CurrencyCode    string `json:"currency_code"`
	CurrencyName    string `json:"currency_name"`
	Anycast         string `json:"anycast"`

	Line string `json:"line"`

	DistrictInfo DistrictInfo `json:"district_info"`

	Route   string    `json:"route"`
	ASN     string    `json:"asn"`
	ASNInfo []ASNInfo `json:"asn_info"`

	AreaCode string `json:"area_code"`

	UsageType string `json:"usage_type"`
}

CityInfo is City Database Content

type District

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

func NewDistrict

func NewDistrict(name string) (*District, error)

func (*District) BuildTime

func (db *District) BuildTime() time.Time

func (*District) Fields

func (db *District) Fields() []string

func (*District) Find

func (db *District) Find(addr, language string) ([]string, error)

func (*District) FindInfo

func (db *District) FindInfo(addr, language string) (*DistrictInfo, error)

func (*District) FindMap

func (db *District) FindMap(addr, language string) (map[string]string, error)

func (*District) IsIPv4

func (db *District) IsIPv4() bool

func (*District) IsIPv6

func (db *District) IsIPv6() bool

func (*District) Languages

func (db *District) Languages() []string

func (*District) Reload added in v1.2.0

func (db *District) Reload(name string) error

type DistrictInfo

type DistrictInfo struct {
	CountryName    string `json:"country_name"`
	RegionName     string `json:"region_name"`
	CityName       string `json:"city_name"`
	DistrictName   string `json:"district_name"`
	ChinaAdminCode string `json:"china_admin_code"`
	CoveringRadius string `json:"covering_radius"`
	Latitude       string `json:"latitude"`
	Longitude      string `json:"longitude"`
}

type Download added in v1.3.2

type Download struct {
	URL *url.URL
}

func NewDownload added in v1.3.2

func NewDownload(httpUrl string) (*Download, error)

func (*Download) SaveToFile added in v1.3.2

func (dl *Download) SaveToFile(fn string) error

type IDC

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

func NewIDC

func NewIDC(name string) (*IDC, error)

func (*IDC) BuildTime

func (db *IDC) BuildTime() time.Time

func (*IDC) Fields

func (db *IDC) Fields() []string

func (*IDC) Find

func (db *IDC) Find(addr, language string) ([]string, error)

func (*IDC) FindInfo

func (db *IDC) FindInfo(addr, language string) (*IDCInfo, error)

func (*IDC) FindMap

func (db *IDC) FindMap(addr, language string) (map[string]string, error)

func (*IDC) IsIPv4

func (db *IDC) IsIPv4() bool

func (*IDC) IsIPv6

func (db *IDC) IsIPv6() bool

func (*IDC) Languages

func (db *IDC) Languages() []string

func (*IDC) Reload added in v1.2.0

func (db *IDC) Reload(name string) error

type IDCInfo

type IDCInfo struct {
	CountryName string `json:"country_name"`
	RegionName  string `json:"region_name"`
	CityName    string `json:"city_name"`
	OwnerDomain string `json:"owner_domain"`
	IspDomain   string `json:"isp_domain"`
	IDC         string `json:"idc"`
}

type MetaData

type MetaData struct {
	Build     int64          `json:"build"`
	IPVersion uint16         `json:"ip_version"`
	Languages map[string]int `json:"languages"`
	NodeCount int            `json:"node_count"`
	TotalSize int            `json:"total_size"`
	Fields    []string       `json:"fields"`
}

Jump to

Keyboard shortcuts

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