2020-07-17 09:05:25 +08:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-07-17 09:41:09 +08:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/zu1k/nali/constant"
|
2020-07-17 09:05:25 +08:00
|
|
|
|
|
|
|
"github.com/zu1k/nali/internal/ipdb"
|
|
|
|
|
|
|
|
geoip2 "github.com/zu1k/nali/pkg/geoip"
|
|
|
|
"github.com/zu1k/nali/pkg/qqwry"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
db ipdb.IPDB
|
|
|
|
qqip qqwry.QQwry
|
|
|
|
geoip geoip2.GeoIP
|
|
|
|
)
|
|
|
|
|
2020-07-17 09:50:06 +08:00
|
|
|
// init ip db content
|
2020-07-17 09:41:09 +08:00
|
|
|
func InitIPDB() {
|
|
|
|
qqip = qqwry.NewQQwry(filepath.Join(constant.HomePath, "qqwry.dat"))
|
|
|
|
//geoip = geoip2.NewGeoIP(filepath.Join(constant.HomePath, "GeoLite2-City.mmdb"))
|
2020-07-17 09:43:48 +08:00
|
|
|
|
2020-07-17 09:05:25 +08:00
|
|
|
db = qqip
|
|
|
|
}
|
|
|
|
|
2020-07-17 09:50:06 +08:00
|
|
|
// set db to use
|
2020-07-17 09:05:25 +08:00
|
|
|
func SetDB(dbName ipdb.IPDBType) {
|
|
|
|
switch dbName {
|
|
|
|
case ipdb.GEOIP2:
|
|
|
|
db = geoip
|
|
|
|
case ipdb.QQIP:
|
|
|
|
db = qqip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 09:50:06 +08:00
|
|
|
// parse several ips
|
2020-07-17 09:05:25 +08:00
|
|
|
func ParseIPs(ips []string) {
|
|
|
|
for _, ip := range ips {
|
|
|
|
ParseIP(ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 09:50:06 +08:00
|
|
|
// parse one ip
|
2020-07-17 09:05:25 +08:00
|
|
|
func ParseIP(ip string) {
|
|
|
|
result := db.Find(ip)
|
|
|
|
fmt.Println(formatResult(ip, result))
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatResult(ip string, result string) string {
|
|
|
|
if result == "" {
|
|
|
|
result = "未找到"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s [%s]", ip, result)
|
|
|
|
}
|