ipparser

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

README

ipparser

ipparser格式将全面升级为ipdb格式 IPDB格式解析代码

ipdb 格式优点

  • 可同时支持IPv4与IPv6
  • 可同时支持中文与英文
  • 查询性能大幅度提高
  • 支持嵌入式文件系统embed.FS

安装 Installation

go get -u github.com/coolstina/ipparser

示例 Usage

Example1: 非嵌入式文件
package main

import (
	"fmt"

	"github.com/coolstina/ipparser"
)

func main() {
	// For City Level IP Database
	city, err := ipparser.NewCity("/path/to/17monipdb.datx")
	if err == nil {
		fmt.Println(city.Find("8.8.8.8"))
		fmt.Println(city.Find("128.8.8.8"))
		fmt.Println(city.Find("255.255.255.255"))
		loc, err := city.FindLocation("27.190.252.103")
		if err == nil {
			fmt.Println(string(loc.ToJSON()))
			// Output:
			/*
			   {
			       "Country": "China",
			       "Province": "Hebei",
			       "City": "Tangshan",
			       "Organization": "",
			       "ISP": "ChinaTelecom",
			       "Latitude": "39.635113",
			       "Longitude": "118.175393",
			       "TimeZone": "Asia/Shanghai",
			       "TimeZone2": "UTC+8",
			       "CityCode": "130200",
			       "PhonePrefix": "86",
			       "CountryCode": "CN",
			       "ContinentCode": "AP",
			       "IDC": "",
			       "BaseStation": "",
			       "Anycast": false
			   }
			*/
		}
	}

	// Only China District IP Database
	dis, err := ipparser.NewDistrict("/path/to/quxian.datx")
	if err == nil {
		fmt.Println(dis.Find("1.12.46.0"))
		fmt.Println(dis.Find("223.255.127.0"))
	}

	// Only China Base Station IP Database
	bst, err := ipparser.NewBaseStation("/path/to/station_ip.datx")
	if err == nil {
		fmt.Println(bst.Find("1.30.6.0"))
		fmt.Println(bst.Find("223.221.121.0"))
		fmt.Println(bst.Find("223.221.121.255"))
	}
}

Example2: 嵌入式文件
package main

import (
	"embed"
	"fmt"

	"github.com/coolstina/embedsfs"
	"github.com/coolstina/ipparser"
)

//go:embed embeds
var embeds embed.FS

func main() {
	embeds := embedsfs.NewEmbedsFS(embeds, "embeds")

	// For City Level IP Database
	city, err := ipparser.NewCity("embeds/path/to/17monipdb.datx", ipparser.WithEmbedsFS(embeds))
	if err == nil {
		fmt.Println(city.Find("8.8.8.8"))
		fmt.Println(city.Find("128.8.8.8"))
		fmt.Println(city.Find("255.255.255.255"))
		loc, err := city.FindLocation("27.190.252.103")
		if err == nil {
			fmt.Println(string(loc.ToJSON()))
			// Output:
			/*
			   {
			       "Country": "China",
			       "Province": "Hebei",
			       "City": "Tangshan",
			       "Organization": "",
			       "ISP": "ChinaTelecom",
			       "Latitude": "39.635113",
			       "Longitude": "118.175393",
			       "TimeZone": "Asia/Shanghai",
			       "TimeZone2": "UTC+8",
			       "CityCode": "130200",
			       "PhonePrefix": "86",
			       "CountryCode": "CN",
			       "ContinentCode": "AP",
			       "IDC": "",
			       "BaseStation": "",
			       "Anycast": false
			   }
			*/
		}
	}

	// Only China District IP Database
	dis, err := ipparser.NewDistrict("embeds/path/to/quxian.datx", ipparser.WithEmbedsFS(embeds))
	if err == nil {
		fmt.Println(dis.Find("1.12.46.0"))
		fmt.Println(dis.Find("223.255.127.0"))
	}

	// Only China Base Station IP Database
	bst, err := ipparser.NewBaseStation("embeds/path/to/station_ip.datx", ipparser.WithEmbedsFS(embeds))
	if err == nil {
		fmt.Println(bst.Find("1.30.6.0"))
		fmt.Println(bst.Find("223.221.121.0"))
		fmt.Println(bst.Find("223.221.121.255"))
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrIPv4Format = errors.New("ipv4 format error")
View Source
var ErrNotFound = errors.New("not found")

Functions

This section is empty.

Types

type BaseStation

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

BaseStation ...

func NewBaseStation

func NewBaseStation(fn string, ops ...Option) (*BaseStation, error)

NewBaseStation ...

func (*BaseStation) Find

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

Find ...

type City

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

City ...

func NewCity

func NewCity(filename string, ops ...Option) (*City, error)

NewCity ...

func (*City) Find

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

Find ...

func (*City) FindLocation

func (db *City) FindLocation(s string) (Location, error)

type District

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

District ...

func NewDistrict

func NewDistrict(fn string, ops ...Option) (*District, error)

NewDistrict ...

func (*District) Find

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

Find ...

type Location

type Location struct {
	Country       string
	Province      string
	City          string
	Organization  string
	ISP           string
	Latitude      string
	Longitude     string
	TimeZone      string
	TimeZone2     string
	CityCode      string
	PhonePrefix   string
	CountryCode   string
	ContinentCode string
	IDC           string // IDC | VPN
	BaseStation   string // WIFI | BS (Base Station)
	CountryCode3  string
	EuropeanUnion bool
	CurrencyCode  string
	CurrencyName  string
	Anycast       bool
}

func (Location) ToJSON

func (l Location) ToJSON() []byte

type Option

type Option interface {
	// contains filtered or unexported methods
}

type OptionFunc

type OptionFunc func(option *option)

func WithEmbedsFS

func WithEmbedsFS(fs *embedsfs.EmbedsFS) OptionFunc

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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