From 31fe43c46a269ed0f36eba864f1aeb7df4d3d145 Mon Sep 17 00:00:00 2001 From: fengdelan Date: Tue, 17 May 2022 11:22:30 +0800 Subject: [PATCH] Added support for IP2Location DB3 LITE --- README_en.md | 3 ++ go.mod | 3 +- go.sum | 15 ++++---- internal/db/default.go | 7 ++++ internal/db/type.go | 4 +++ pkg/dbif/db.go | 2 ++ pkg/ip2location/ip2locationdb.go | 61 ++++++++++++++++++++++++++++++++ 7 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 pkg/ip2location/ip2locationdb.go diff --git a/README_en.md b/README_en.md index 41063c4..a3520e3 100644 --- a/README_en.md +++ b/README_en.md @@ -35,6 +35,7 @@ However the C version has too few functions, and the js version is too big and t - IPIP free database - ip2region database - db-ip database + - IP2Location DB3 LITE database - Pipeline support - Interactive query - Offline query @@ -238,6 +239,7 @@ supported database: - IPIP `['ipip']` - Ip2Resion `['ip2region', 'i2r']` - - DBIP `['dbip', 'db-ip']` +- IP2Location `['ip2location']` #### Windows @@ -312,6 +314,7 @@ export NALI_HOME=/home/nali - [IPIP数据库](https://www.ipip.net/product/ip.html) - [IPIP数据库解析](https://github.com/ipipdotnet/ipdb-go) - [ip2region数据库](https://github.com/lionsoul2014/ip2region) +- [IP2Location DB3 LITE](https://lite.ip2location.com/database/db3-ip-country-region-city) *use the IPv6 BIN as it contains both IPv4 & IPv6* - [Cobra CLI库](https://github.com/spf13/cobra) - [Nali-cli](https://github.com/SukkaW/nali-cli) diff --git a/go.mod b/go.mod index b93e9e3..24cf4f7 100644 --- a/go.mod +++ b/go.mod @@ -4,11 +4,13 @@ go 1.18 require ( github.com/fatih/color v1.13.0 + github.com/ip2location/ip2location-go/v9 v9.2.0 github.com/ipipdotnet/ipdb-go v1.3.1 github.com/lionsoul2014/ip2region v2.2.0-release+incompatible github.com/oschwald/geoip2-golang v1.7.0 github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda github.com/spf13/cobra v1.4.0 + github.com/spf13/viper v1.11.0 golang.org/x/text v0.3.7 gopkg.in/yaml.v2 v2.4.0 ) @@ -30,7 +32,6 @@ require ( github.com/spf13/cast v1.4.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect - github.com/spf13/viper v1.11.0 // indirect github.com/subosito/gotenv v1.2.0 // indirect github.com/ulikunitz/xz v0.5.10 // indirect golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect diff --git a/go.sum b/go.sum index 9525eb1..5066d69 100644 --- a/go.sum +++ b/go.sum @@ -47,8 +47,8 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -125,14 +125,18 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1: github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/ip2location/ip2location-go/v9 v9.2.0 h1:ZWURUspTpe2gxMQGtcQScocBd73gZKUEWlHCFUJYLHk= +github.com/ip2location/ip2location-go/v9 v9.2.0/go.mod h1:s5SV6YZL10TpfPpXw//7fEJC65G/yH7Oh+Tjq9JcQEQ= github.com/ipipdotnet/ipdb-go v1.3.1 h1:iMTt7a4o8r5FmTMzuHLg8XPtz8vb06gpEzJVSZzDZMY= github.com/ipipdotnet/ipdb-go v1.3.1/go.mod h1:yZ+8puwe3R37a/3qRftXo40nZVQbxYDLqls9o5foexs= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/lionsoul2014/ip2region v2.2.0-release+incompatible h1:1qp9iks+69h7IGLazAplzS9Ca14HAxuD5c0rbFdPGy4= github.com/lionsoul2014/ip2region v2.2.0-release+incompatible/go.mod h1:+ZBN7PBoh5gG6/y0ZQ85vJDBe21WnfbRrQQwTfliJJI= @@ -328,10 +332,6 @@ golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220403020550-483a9cbc67c0 h1:PgUUmg0gNMIPY2WafhL/oLyQGw+kdTNPlVWOjltpp3w= -golang.org/x/sys v0.0.0-20220403020550-483a9cbc67c0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f h1:8w7RhxzTVgUzw/AH/9mUV5q0vMgy40SQRursCcfmkCw= -golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 h1:nonptSpoQ4vQjyraW20DXPAglgQfVnM9ZC6MmNLMR60= golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -398,8 +398,8 @@ golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f h1:GGU+dLjvlC3qDwqYgL6UgRmHXhOOgns0bZu2Ty5mm6U= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= @@ -488,8 +488,8 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2 google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/ini.v1 v1.66.4 h1:SsAcf+mM7mRZo2nJNGt8mZCjG8ZRaNGMURJw7BsIST4= @@ -497,7 +497,6 @@ gopkg.in/ini.v1 v1.66.4/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/db/default.go b/internal/db/default.go index 8899a28..9a43b74 100644 --- a/internal/db/default.go +++ b/internal/db/default.go @@ -62,6 +62,13 @@ func GetDefaultDBList() List { Languages: LanguagesZH, Types: TypesIPv4, }, + &DB{ + Name: "ip2location", + Format: FormatIP2Location, + File: "IP2LOCATION-LITE-DB3.IPV6.BIN", + Languages: LanguagesAll, + Types: TypesIP, + }, &DB{ Name: "cdn", diff --git a/internal/db/type.go b/internal/db/type.go index 1aa5168..c5cf9fd 100644 --- a/internal/db/type.go +++ b/internal/db/type.go @@ -10,6 +10,7 @@ import ( "github.com/zu1k/nali/pkg/ipip" "github.com/zu1k/nali/pkg/qqwry" "github.com/zu1k/nali/pkg/zxipv6wry" + "github.com/zu1k/nali/pkg/ip2location" ) type DB struct { @@ -41,6 +42,8 @@ func (d *DB) get() (db dbif.DB) { db, err = geoip.NewGeoIP(filePath) case FormatIP2Region: db, err = ip2region.NewIp2Region(filePath) + case FormatIP2Location: + db, err = ip2locationdb.NewIP2LocationDB(filePath) case FormatCDNSkkYml: db, err = cdn.NewCDN(filePath) default: @@ -63,6 +66,7 @@ const ( FormatZXIPv6Wry = "zxipv6wry" FormatIPIP = "ipip" FormatIP2Region = "ip2region" + FormatIP2Location = "ip2location" FormatCDNSkkYml = "cdn-skk-yml" ) diff --git a/pkg/dbif/db.go b/pkg/dbif/db.go index 7239ff0..e5b8040 100644 --- a/pkg/dbif/db.go +++ b/pkg/dbif/db.go @@ -9,6 +9,7 @@ import ( "github.com/zu1k/nali/pkg/ipip" "github.com/zu1k/nali/pkg/qqwry" "github.com/zu1k/nali/pkg/zxipv6wry" + "github.com/zu1k/nali/pkg/ip2location" ) type QueryType uint @@ -29,5 +30,6 @@ var ( _ DB = &ipip.IPIPFree{} _ DB = &geoip.GeoIP{} _ DB = &ip2region.Ip2Region{} + _ DB = &ip2locationdb.IP2LocationDB{} _ DB = &cdn.CDN{} ) diff --git a/pkg/ip2location/ip2locationdb.go b/pkg/ip2location/ip2locationdb.go new file mode 100644 index 0000000..5cd1ab1 --- /dev/null +++ b/pkg/ip2location/ip2locationdb.go @@ -0,0 +1,61 @@ +package ip2locationdb + +import ( + "errors" + "fmt" + "log" + "net" + "os" + + "github.com/ip2location/ip2location-go/v9" +) + +// IP2LocationDB +type IP2LocationDB struct { + db *ip2location.DB +} + +// new IP2Location from database file +func NewIP2LocationDB(filePath string) (*IP2LocationDB, error) { + _, err := os.Stat(filePath) + if err != nil && os.IsNotExist(err) { + log.Println("文件不存在,请自行下载 IP2Location 库,并保存在", filePath) + return nil, err + } else { + db, err := ip2location.OpenDB(filePath) + + if err != nil { + log.Fatal(err) + } + return &IP2LocationDB{db: db}, nil + } +} + +func (x IP2LocationDB) Find(query string, params ...string) (result fmt.Stringer, err error) { + ip := net.ParseIP(query) + if ip == nil { + return nil, errors.New("Query should be valid IP") + } + record, err := x.db.Get_all(ip.String()) + + if err != nil { + return + } + + result = Result{ + Country: record.Country_long, + Region: record.Region, + City: record.City, + } + return +} + +type Result struct { + Country string + Region string + City string +} + +func (r Result) String() string { + return fmt.Sprintf("%s %s %s", r.Country, r.Region, r.City) +}