1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-24 06:09:01 +08:00
nali/internal/app/parse.go

108 lines
2.4 KiB
Go
Raw Normal View History

2020-07-17 09:05:25 +08:00
package app
import (
"fmt"
2020-07-17 09:41:09 +08:00
"path/filepath"
2020-07-17 10:52:36 +08:00
2020-07-17 09:41:09 +08:00
"github.com/zu1k/nali/constant"
2020-07-17 09:05:25 +08:00
"github.com/zu1k/nali/internal/ipdb"
2020-07-21 17:23:38 +08:00
"github.com/zu1k/nali/internal/tools"
2020-07-17 09:05:25 +08:00
geoip2 "github.com/zu1k/nali/pkg/geoip"
2020-07-21 06:19:52 +08:00
"github.com/zu1k/nali/pkg/ipip"
2020-07-17 09:05:25 +08:00
"github.com/zu1k/nali/pkg/qqwry"
2020-07-21 06:19:52 +08:00
"github.com/zu1k/nali/pkg/zxipv6wry"
2020-07-17 09:05:25 +08:00
)
var (
2020-07-18 09:52:59 +08:00
db []ipdb.IPDB
2020-07-17 09:05:25 +08:00
qqip qqwry.QQwry
geoip geoip2.GeoIP
)
2020-07-17 09:50:06 +08:00
// init ip db content
2020-07-17 11:37:03 +08:00
func InitIPDB(ipdbtype ipdb.IPDBType) {
2020-07-18 09:52:59 +08:00
db = make([]ipdb.IPDB, 1)
2020-07-17 11:37:03 +08:00
switch ipdbtype {
2020-07-17 09:05:25 +08:00
case ipdb.GEOIP2:
2020-07-18 09:52:59 +08:00
db[0] = geoip2.NewGeoIP(filepath.Join(constant.HomePath, "GeoLite2-City.mmdb"))
2020-07-17 09:05:25 +08:00
case ipdb.QQIP:
2020-07-18 09:52:59 +08:00
db[0] = qqwry.NewQQwry(filepath.Join(constant.HomePath, "qqwry.dat"))
db = append(db, zxipv6wry.NewZXwry(filepath.Join(constant.HomePath, "ipv6wry.db")))
2020-07-21 06:19:52 +08:00
case ipdb.IPIP:
db[0] = ipip.NewIPIPFree(filepath.Join(constant.HomePath, "ipipfree.ipdb"))
db = append(db, zxipv6wry.NewZXwry(filepath.Join(constant.HomePath, "ipv6wry.db")))
2020-07-17 09:05:25 +08:00
}
}
2020-07-17 09:50:06 +08:00
// parse several ips
2020-07-17 09:05:25 +08:00
func ParseIPs(ips []string) {
2020-07-18 09:52:59 +08:00
db0 := db[0]
var db1 ipdb.IPDB
if len(db) > 1 {
db1 = db[1]
} else {
db1 = nil
}
2020-07-17 09:05:25 +08:00
for _, ip := range ips {
2020-07-21 17:23:38 +08:00
if tools.ValidIP4(ip) {
2020-07-18 09:52:59 +08:00
result := db0.Find(ip)
fmt.Println(formatResult(ip, result))
2020-07-21 17:23:38 +08:00
} else if tools.ValidIP6(ip) && db1 != nil {
2020-07-18 09:52:59 +08:00
result := db1.Find(ip)
2020-07-17 10:52:36 +08:00
fmt.Println(formatResult(ip, result))
} else {
2020-07-19 07:24:25 +08:00
fmt.Println(ReplaceIPInString(ip))
2020-07-17 10:52:36 +08:00
}
2020-07-17 09:05:25 +08:00
}
}
func RemoveRepeatedElement(arr []string) (newArr []string) {
newArr = make([]string, 0)
for i := 0; i < len(arr); i++ {
repeat := false
for j := i + 1; j < len(arr); j++ {
if arr[i] == arr[j] {
repeat = true
break
}
}
if !repeat {
newArr = append(newArr, arr[i])
}
}
return
}
2020-07-19 07:24:25 +08:00
func ReplaceIPInString(str string) (result string) {
2020-07-18 09:52:59 +08:00
db0 := db[0]
var db1 ipdb.IPDB
if len(db) > 1 {
db1 = db[1]
} else {
db1 = nil
}
2020-07-17 10:52:36 +08:00
result = str
2020-07-21 17:23:38 +08:00
ip4s := tools.GetIP4FromString(str)
ip4s = RemoveRepeatedElement(ip4s)
2020-07-18 09:52:59 +08:00
for _, ip := range ip4s {
info := db0.Find(ip)
result = tools.AddInfoIp4(result, ip, info)
2020-07-18 09:52:59 +08:00
}
2020-07-21 17:23:38 +08:00
ip6s := tools.GetIP6FromString(str)
ip6s = RemoveRepeatedElement(ip6s)
2020-07-18 09:52:59 +08:00
for _, ip := range ip6s {
info := db1.Find(ip)
result = tools.AddInfoIp6(result, ip, info)
2020-07-17 10:52:36 +08:00
}
return
2020-07-17 09:05:25 +08:00
}
func formatResult(ip string, result string) string {
if result == "" {
result = "未找到"
}
return fmt.Sprintf("%s [%s]", ip, result)
}