ip

package module
v0.0.0-...-4e25b52 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2024 License: MIT Imports: 7 Imported by: 0

README

简介

高性能的离线IP归属地查询库,数据源来自纯真IP数据库

使用

go get github.com/ryanexo/QQWry-Reader

下载数据库文件

安全原因,库不提供远程下载功能,请点此下载数据库文件或复制下方链接

https://github.com/ryanexo/QQWry-Reader/releases/tag/DB20240221

示例

func main() {
    q, err := ip.New(ip.WithMemoryMode("./qqwry.dat"))
    if err != nil {
        panic(err)
    }
    r, err := q.Query("1.0.0.1")
    if err != nil {
        panic(err)
    }
    
    fmt.Printf("国家: %s\n地区: %s\n合并: %s\n", r.Country, r.Region, r.Location)
    fmt.Printf("IP库版本号: %s", q.Version())
}

并发安全

使用goroutine模拟并发未发生竟态

测试

内存模式下单次查询为10微秒级别,文件模式下单次查询为100微秒级别

=== RUN   TestFileMode_Query
--- PASS: TestFileMode_Query (0.00s)
=== RUN   TestFileMode_Query_Concurrency_Times10000
--- PASS: TestFileMode_Query_Concurrency_Times10000 (1.46s)
=== RUN   TestMemMode_Query
--- PASS: TestMemMode_Query (0.00s)
=== RUN   TestMemMode_Query_Concurrency_Times10000
--- PASS: TestMemMode_Query_Concurrency_Times10000 (0.06s)
goos: windows
goarch: amd64
pkg: github.com/ryanexo/QQWry-Reader
cpu: 12th Gen Intel(R) Core(TM) i5-12400F
BenchmarkData_Query
BenchmarkData_Query-12             16491             72876 ns/op            8675 B/op         44 allocs/op
BenchmarkData_Query_Mem
BenchmarkData_Query_Mem-12        527658              2158 ns/op            8675 B/op         44 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	VersionIPBegin uint32 = 4294967040
	VersionIPEnd   uint32 = 4294967295

	UnknownText          = []byte("未知")
	DefaultUnknownRegion = []byte{32, 67, 90, 56, 56, 46, 78, 69, 84} // 未知地区为[ CZ88.NET]

	ErrIP       = errors.New("IP不正确")
	ErrNotFound = errors.New("未找到IP记录")
	ErrDecode   = errors.New("已找到记录,编码解析错误")
)
View Source
var ErrOutOfRange = errors.New("偏移量超出范围")

Functions

This section is empty.

Types

type Builder

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

func WithFileMode

func WithFileMode(filename string) Builder

func WithMemoryMode

func WithMemoryMode(filename string) Builder

type Data

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

func New

func New(builder Builder) (*Data, error)

func (*Data) Close

func (data *Data) Close() error

func (*Data) Query

func (data *Data) Query(ip string) (Record, error)

func (*Data) Version

func (data *Data) Version() string

type Record

type Record struct {
	Country  []byte
	Region   []byte
	Location []byte
}

type Source

type Source interface {
	io.Reader
	io.ReaderAt
	io.Closer
}

Jump to

Keyboard shortcuts

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