Skip to content
/ iploc Public
forked from kayon/iploc

每秒百万高性能IP查询库,使用纯真IP库,国内省、市、县,qqwry.dat转换工具:GBK转为UTF-8

License

Notifications You must be signed in to change notification settings

bufanyun/iploc

This branch is up to date with kayon/iploc:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

bda3e96 · Mar 12, 2020

History

27 Commits
Mar 11, 2020
May 15, 2018
Mar 12, 2020
May 15, 2018
Mar 19, 2019
Mar 12, 2020
May 20, 2018
Mar 3, 2019
May 15, 2018
May 15, 2018
May 15, 2018
May 15, 2018
May 15, 2018

Repository files navigation

iploc

Build Status

使用纯真IP库 qqwry.dat,高性能,线程安全,并对国内数据格式化到省、市、县

需要 go 1.9 或更高

附带的 qqwry.datUTF-8 编码 2018-05-15版本

不再提供 qqwry.dat, 新增命令行工具 iploc-fetch, 可在线获取官方最新版本的 qqwry.dat

安装

go get -u github.com/kayon/iploc/...

无法安装 golang.org/x/text 包,没有梯子使用下面方法

$ mkdir -P $GOPATH/src/golang.org/x
cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/text.git

获取&更新 qqwry.dat

1. 下载 qqwry.dat

方法一:使用命令行工具 iploc-fetch

由于服务器限制国外IP,只能使用国内网络。

下载到当前目录,保存为 qqwry.gbk.dat

$ iploc-fetch qqwry.gbk.dat
方法二:手动下载

纯真官网下载并安装,复制安装目录中的 qqwry.dat

2. 转换为 UTF-8

使用命令行工具 iploc-conv 将刚刚下载的 qqwry.gbk.dat 转换为 UTF-8 保存为 qqwry.dat

iploc-conv -s qqwry.gbk.dat -d qqwry.dat

Benchmarks

// 缓存索引
BenchmarkFind-8            	 2000000	       735 ns/op               136万/秒
// 无索引
BenchmarkFindUnIndexed-8   	   20000	     78221 ns/op               1.2万/秒
// 缓存索引,并发
BenchmarkFindParallel-8    	10000000	       205 ns/op               487万/秒

使用

func main() {
	loc, err := iploc.Open("qqwry.dat")
	if err != nil {
		panic(err)
	}
	detail := loc.Find("8.8.8") // 补全为8.8.0.8, 参考 ping 工具
	fmt.Printf("IP:%s; 网段:%s - %s; %s\n", detail.IP, detail.Start, detail.End, detail)
	
	detail2 := loc.Find("8.8.3.1")
	fmt.Printf("%t %t\n", detail.In(detail2.IP.String()), detail.String() == detail2.String())

	// output
	// IP:8.8.0.8; 网段: 8.7.245.0 - 8.8.3.255; 美国 科罗拉多州布隆菲尔德市Level 3通信股份有限公司
	// true true
	
	detail = loc.Find("1.24.41.0")
	fmt.Println(detail.String())
	fmt.Println(detail.Country, detail.Province, detail.City, detail.County)
	
	// output
	// 内蒙古锡林郭勒盟苏尼特右旗 联通
	// 中国 内蒙古 锡林郭勒盟 苏尼特右旗
	
}	

快捷方法

Find(qqwrySrc, ip string) (*Detail, error)

iploc.Find 使用 OpenWithoutIndexes

初始化

Open(qqwrySrc string) (*Locator, error)

iploc.Open 缓存并索引,生成索引需要耗时500毫秒左右,但会带来更高的查询性能

OpenWithoutIndexes(qqwrySrc string) (*Locator, error)

iploc.OpenWithoutIndexes 只读取文件头做简单检查,无索引

查询

(*Locator).Find(ip string) *Detail

如果IP不合法,返回 nil

命令行工具

iploc-conv

将原版 qqwry.datGBK 转换为 UTF-8

$ iploc-conv -s src.gbk.dat -d dst.utf8.dat

新生成的DAT文件保留原数据结构,由于编码原因,文件会增大一点

修正原 qqwry.dat 中几处错误的重定向 (qqwry.dat 2018-05-10),并将 "CZ88.NET" 替换为 "N/A"

iploc-fetch

从纯真官网下载最新 qqwry.dat

由于服务器限制国外IP,只能使用国内网络。

$ iploc-fetch qqwry.gbk.dat

下载后别忘了使用 iploc-conv 转换为 UTF-8

iploc-gen

创建静态版本的 iploc 集成到你的项目中

iploc-gen 会在当前目录创建 iploc-binary.go 文件,拷贝到你的项目中,通过变量名 IPLoc 直接可以使用

$ iploc-gen path/qqwry.dat

--pkg 指定 package name, 默认 main

-n 使用 OpenWithoutIndexes 初始化,无索引

静态编译 iploc 和 qqwry.dat 并集成到你的项目中

编译后的二进制没有 qqwry.dat 依赖,不需要再带着 qqwry.dat 一起打包了

示例

到项目目录 $GOPATH/src/myproject/

$ mkdir myloc && cd myloc
$ iploc-gen path/qqwry.dat --pkg myloc

$GOPATH/src/myproject/main.go

package main
	
import (
	"fmt"
	
	"myproject/myloc"
)
	
func main() {
	fmt.Println(myloc.IPLoc.Find("8.8.8.8"))
}

About

每秒百万高性能IP查询库,使用纯真IP库,国内省、市、县,qqwry.dat转换工具:GBK转为UTF-8

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 98.5%
  • Makefile 1.5%