From 363537c6524d695a50503a7a12be5f279120a6f0 Mon Sep 17 00:00:00 2001 From: zu1k Date: Tue, 21 Jul 2020 06:19:52 +0800 Subject: [PATCH] add ipip db support --- go.mod | 1 + go.sum | 2 ++ internal/app/parse.go | 7 +++++-- internal/ipdb/db.go | 2 ++ internal/ipdb/ipdb.go | 1 + pkg/ipip/ipipfree.go | 44 +++++++++++++++++++++++++++++++++++++++++++ pkg/qqwry/qqwry.go | 1 - 7 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 pkg/ipip/ipipfree.go diff --git a/go.mod b/go.mod index 5ca394e..535a7a5 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/zu1k/nali go 1.14 require ( + github.com/ipipdotnet/ipdb-go v1.3.0 github.com/oschwald/geoip2-golang v1.4.0 github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda github.com/saracen/go7z-fixtures v0.0.0-20190623165746-aa6b8fba1d2f // indirect diff --git a/go.sum b/go.sum index fc4fa5e..0f7b0cc 100644 --- a/go.sum +++ b/go.sum @@ -44,6 +44,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/ipipdotnet/ipdb-go v1.3.0 h1:FfkSkAI1do3bZ7F35ueGuF7Phur64jmikQ1C4IPl/gc= +github.com/ipipdotnet/ipdb-go v1.3.0/go.mod h1:yZ+8puwe3R37a/3qRftXo40nZVQbxYDLqls9o5foexs= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= diff --git a/internal/app/parse.go b/internal/app/parse.go index 0f05f66..50edd53 100644 --- a/internal/app/parse.go +++ b/internal/app/parse.go @@ -5,13 +5,13 @@ import ( "path/filepath" "strings" - "github.com/zu1k/nali/pkg/zxipv6wry" - "github.com/zu1k/nali/constant" "github.com/zu1k/nali/internal/ipdb" "github.com/zu1k/nali/internal/iptools" geoip2 "github.com/zu1k/nali/pkg/geoip" + "github.com/zu1k/nali/pkg/ipip" "github.com/zu1k/nali/pkg/qqwry" + "github.com/zu1k/nali/pkg/zxipv6wry" ) var ( @@ -29,6 +29,9 @@ func InitIPDB(ipdbtype ipdb.IPDBType) { case ipdb.QQIP: db[0] = qqwry.NewQQwry(filepath.Join(constant.HomePath, "qqwry.dat")) db = append(db, zxipv6wry.NewZXwry(filepath.Join(constant.HomePath, "ipv6wry.db"))) + case ipdb.IPIP: + db[0] = ipip.NewIPIPFree(filepath.Join(constant.HomePath, "ipipfree.ipdb")) + db = append(db, zxipv6wry.NewZXwry(filepath.Join(constant.HomePath, "ipv6wry.db"))) } } diff --git a/internal/ipdb/db.go b/internal/ipdb/db.go index 2801926..5bbf826 100644 --- a/internal/ipdb/db.go +++ b/internal/ipdb/db.go @@ -18,6 +18,8 @@ func GetIPDBType() IPDBType { return GEOIP2 case "chunzhen", "qqip", "qqwry": return QQIP + case "ipip", "ipipfree", "ipip.net": + return IPIP default: return QQIP } diff --git a/internal/ipdb/ipdb.go b/internal/ipdb/ipdb.go index e9d0f65..a51b3f7 100644 --- a/internal/ipdb/ipdb.go +++ b/internal/ipdb/ipdb.go @@ -6,4 +6,5 @@ type IPDBType int const ( GEOIP2 = iota // geoip2 QQIP // chunzhen + IPIP // ipip.net ) diff --git a/pkg/ipip/ipipfree.go b/pkg/ipip/ipipfree.go new file mode 100644 index 0000000..6c8cadb --- /dev/null +++ b/pkg/ipip/ipipfree.go @@ -0,0 +1,44 @@ +package ipip + +import ( + "fmt" + "log" + "os" + + "github.com/ipipdotnet/ipdb-go" +) + +type IPIPFree struct { + *ipdb.City +} + +func NewIPIPFree(filePath string) IPIPFree { + _, err := os.Stat(filePath) + if err != nil && os.IsNotExist(err) { + log.Printf("IPIP数据库不存在,请手动下载解压后保存到本地: %s \n", filePath) + log.Println("下载链接: https://www.ipip.net/product/ip.html") + os.Exit(1) + return IPIPFree{} + } else { + db, err := ipdb.NewCity(filePath) + if err != nil { + log.Fatalln("IPIP 数据库 初始化失败") + log.Fatal(err) + os.Exit(1) + } + return IPIPFree{City: db} + } +} + +func (db IPIPFree) Find(ip string) string { + info, err := db.FindInfo(ip, "CN") + if err != nil { + log.Fatalln("IPIP 查询失败:", err.Error()) + return "" + } else { + if info.CityName == "" { + return fmt.Sprintf("%s %s", info.CountryName, info.RegionName) + } + return fmt.Sprintf("%s %s %s", info.CountryName, info.RegionName, info.CityName) + } +} diff --git a/pkg/qqwry/qqwry.go b/pkg/qqwry/qqwry.go index db493c0..2abfaca 100644 --- a/pkg/qqwry/qqwry.go +++ b/pkg/qqwry/qqwry.go @@ -22,7 +22,6 @@ func NewQQwry(filePath string) QQwry { var fileData []byte var fileInfo common.FileData - // 判断文件是否存在 _, err := os.Stat(filePath) if err != nil && os.IsNotExist(err) { log.Println("文件不存在,尝试从网络获取最新纯真 IP 库")