1
0
mirror of https://github.com/zu1k/nali.git synced 2025-02-02 10:22:41 +08:00

add db choose

This commit is contained in:
zu1k 2020-07-17 11:37:03 +08:00
parent 6a9b901077
commit 69d5fd5eea
4 changed files with 36 additions and 18 deletions

View File

@ -22,20 +22,12 @@ var (
)
// init ip db content
func InitIPDB() {
qqip = qqwry.NewQQwry(filepath.Join(constant.HomePath, "qqwry.dat"))
//geoip = geoip2.NewGeoIP(filepath.Join(constant.HomePath, "GeoLite2-City.mmdb"))
db = qqip
}
// set db to use
func SetDB(dbName ipdb.IPDBType) {
switch dbName {
func InitIPDB(ipdbtype ipdb.IPDBType) {
switch ipdbtype {
case ipdb.GEOIP2:
db = geoip
db = geoip2.NewGeoIP(filepath.Join(constant.HomePath, "GeoLite2-City.mmdb"))
case ipdb.QQIP:
db = qqip
db = qqwry.NewQQwry(filepath.Join(constant.HomePath, "qqwry.dat"))
}
}

18
main.go
View File

@ -4,6 +4,9 @@ import (
"log"
"os"
"path/filepath"
"strings"
"github.com/zu1k/nali/internal/ipdb"
"github.com/zu1k/nali/internal/app"
@ -13,7 +16,7 @@ import (
func main() {
setHomePath()
app.InitIPDB()
app.InitIPDB(getIPDBType())
cmd.Execute()
}
@ -30,3 +33,16 @@ func setHomePath() {
}
}
}
func getIPDBType() ipdb.IPDBType {
dbname := os.Getenv("NALI_DB")
dbname = strings.ToLower(dbname)
switch dbname {
case "geo", "geoip", "geoip2":
return ipdb.GEOIP2
case "chunzhen", "qqip", "qqwry":
return ipdb.QQIP
default:
return ipdb.QQIP
}
}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net"
"os"
"github.com/oschwald/geoip2-golang"
)
@ -14,12 +15,20 @@ type GeoIP struct {
}
// new geoip from db file
func NewGeoIP(filePath string) GeoIP {
func NewGeoIP(filePath string) (geoip GeoIP) {
// 判断文件是否存在
_, err := os.Stat(filePath)
if err != nil && os.IsNotExist(err) {
log.Println("文件不存在,请自行下载 Geoip2 City库并保存在", filePath)
os.Exit(1)
} else {
db, err := geoip2.Open(filePath)
if err != nil {
log.Fatal(err)
}
return GeoIP{db: db}
geoip = GeoIP{db: db}
}
return
}
// find ip info

1
pkg/geoip/update.go Normal file
View File

@ -0,0 +1 @@
package geoip