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 // init ip db content
func InitIPDB() { func InitIPDB(ipdbtype ipdb.IPDBType) {
qqip = qqwry.NewQQwry(filepath.Join(constant.HomePath, "qqwry.dat")) switch ipdbtype {
//geoip = geoip2.NewGeoIP(filepath.Join(constant.HomePath, "GeoLite2-City.mmdb"))
db = qqip
}
// set db to use
func SetDB(dbName ipdb.IPDBType) {
switch dbName {
case ipdb.GEOIP2: case ipdb.GEOIP2:
db = geoip db = geoip2.NewGeoIP(filepath.Join(constant.HomePath, "GeoLite2-City.mmdb"))
case ipdb.QQIP: 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" "log"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/zu1k/nali/internal/ipdb"
"github.com/zu1k/nali/internal/app" "github.com/zu1k/nali/internal/app"
@ -13,7 +16,7 @@ import (
func main() { func main() {
setHomePath() setHomePath()
app.InitIPDB() app.InitIPDB(getIPDBType())
cmd.Execute() 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" "fmt"
"log" "log"
"net" "net"
"os"
"github.com/oschwald/geoip2-golang" "github.com/oschwald/geoip2-golang"
) )
@ -14,12 +15,20 @@ type GeoIP struct {
} }
// new geoip from db file // new geoip from db file
func NewGeoIP(filePath string) GeoIP { func NewGeoIP(filePath string) (geoip GeoIP) {
db, err := geoip2.Open(filePath) // 判断文件是否存在
if err != nil { _, err := os.Stat(filePath)
log.Fatal(err) 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)
}
geoip = GeoIP{db: db}
} }
return GeoIP{db: db} return
} }
// find ip info // find ip info

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

@ -0,0 +1 @@
package geoip