mirror of
https://github.com/zu1k/nali.git
synced 2025-01-22 13:19:02 +08:00
add ipip db support
This commit is contained in:
parent
0e0df1396d
commit
363537c652
1
go.mod
1
go.mod
@ -3,6 +3,7 @@ module github.com/zu1k/nali
|
|||||||
go 1.14
|
go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/ipipdotnet/ipdb-go v1.3.0
|
||||||
github.com/oschwald/geoip2-golang v1.4.0
|
github.com/oschwald/geoip2-golang v1.4.0
|
||||||
github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda
|
github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda
|
||||||
github.com/saracen/go7z-fixtures v0.0.0-20190623165746-aa6b8fba1d2f // indirect
|
github.com/saracen/go7z-fixtures v0.0.0-20190623165746-aa6b8fba1d2f // indirect
|
||||||
|
2
go.sum
2
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/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 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
||||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
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/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
|
||||||
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
|
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
|
||||||
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
|
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
|
||||||
|
@ -5,13 +5,13 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/zu1k/nali/pkg/zxipv6wry"
|
|
||||||
|
|
||||||
"github.com/zu1k/nali/constant"
|
"github.com/zu1k/nali/constant"
|
||||||
"github.com/zu1k/nali/internal/ipdb"
|
"github.com/zu1k/nali/internal/ipdb"
|
||||||
"github.com/zu1k/nali/internal/iptools"
|
"github.com/zu1k/nali/internal/iptools"
|
||||||
geoip2 "github.com/zu1k/nali/pkg/geoip"
|
geoip2 "github.com/zu1k/nali/pkg/geoip"
|
||||||
|
"github.com/zu1k/nali/pkg/ipip"
|
||||||
"github.com/zu1k/nali/pkg/qqwry"
|
"github.com/zu1k/nali/pkg/qqwry"
|
||||||
|
"github.com/zu1k/nali/pkg/zxipv6wry"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -29,6 +29,9 @@ func InitIPDB(ipdbtype ipdb.IPDBType) {
|
|||||||
case ipdb.QQIP:
|
case ipdb.QQIP:
|
||||||
db[0] = qqwry.NewQQwry(filepath.Join(constant.HomePath, "qqwry.dat"))
|
db[0] = qqwry.NewQQwry(filepath.Join(constant.HomePath, "qqwry.dat"))
|
||||||
db = append(db, zxipv6wry.NewZXwry(filepath.Join(constant.HomePath, "ipv6wry.db")))
|
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")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,8 @@ func GetIPDBType() IPDBType {
|
|||||||
return GEOIP2
|
return GEOIP2
|
||||||
case "chunzhen", "qqip", "qqwry":
|
case "chunzhen", "qqip", "qqwry":
|
||||||
return QQIP
|
return QQIP
|
||||||
|
case "ipip", "ipipfree", "ipip.net":
|
||||||
|
return IPIP
|
||||||
default:
|
default:
|
||||||
return QQIP
|
return QQIP
|
||||||
}
|
}
|
||||||
|
@ -6,4 +6,5 @@ type IPDBType int
|
|||||||
const (
|
const (
|
||||||
GEOIP2 = iota // geoip2
|
GEOIP2 = iota // geoip2
|
||||||
QQIP // chunzhen
|
QQIP // chunzhen
|
||||||
|
IPIP // ipip.net
|
||||||
)
|
)
|
||||||
|
44
pkg/ipip/ipipfree.go
Normal file
44
pkg/ipip/ipipfree.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -22,7 +22,6 @@ func NewQQwry(filePath string) QQwry {
|
|||||||
var fileData []byte
|
var fileData []byte
|
||||||
var fileInfo common.FileData
|
var fileInfo common.FileData
|
||||||
|
|
||||||
// 判断文件是否存在
|
|
||||||
_, err := os.Stat(filePath)
|
_, err := os.Stat(filePath)
|
||||||
if err != nil && os.IsNotExist(err) {
|
if err != nil && os.IsNotExist(err) {
|
||||||
log.Println("文件不存在,尝试从网络获取最新纯真 IP 库")
|
log.Println("文件不存在,尝试从网络获取最新纯真 IP 库")
|
||||||
|
Loading…
Reference in New Issue
Block a user