2022-05-09 06:48:28 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2022-10-24 01:51:19 +00:00
|
|
|
"errors"
|
2022-05-09 06:48:28 +00:00
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-05-24 07:16:48 +00:00
|
|
|
"github.com/zu1k/nali/pkg/download"
|
2022-10-24 01:53:42 +00:00
|
|
|
"github.com/zu1k/nali/pkg/qqwry"
|
2022-05-09 06:48:28 +00:00
|
|
|
"github.com/zu1k/nali/pkg/zxipv6wry"
|
|
|
|
)
|
|
|
|
|
|
|
|
func UpdateDB(dbNames ...string) {
|
|
|
|
if len(dbNames) == 0 {
|
|
|
|
dbNames = DbNameListForUpdate
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(map[string]struct{})
|
|
|
|
for _, dbName := range dbNames {
|
|
|
|
update, name := getUpdateFuncByName(dbName)
|
|
|
|
if _, found := done[name]; !found {
|
|
|
|
done[name] = struct{}{}
|
|
|
|
if err := update(); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var DbNameListForUpdate = []string{
|
|
|
|
"qqwry",
|
|
|
|
"zxipv6wry",
|
|
|
|
"ip2region",
|
|
|
|
"cdn",
|
|
|
|
}
|
|
|
|
|
2022-10-24 01:51:19 +00:00
|
|
|
var DbCheckFunc = map[Format]func([]byte) bool{
|
|
|
|
FormatQQWry: qqwry.CheckFile,
|
|
|
|
FormatZXIPv6Wry: zxipv6wry.CheckFile,
|
|
|
|
}
|
|
|
|
|
2022-05-09 06:48:28 +00:00
|
|
|
func getUpdateFuncByName(name string) (func() error, string) {
|
|
|
|
name = strings.TrimSpace(name)
|
|
|
|
if db := getDbByName(name); db != nil {
|
2022-05-24 07:16:48 +00:00
|
|
|
// direct download if download-url not null
|
|
|
|
if len(db.DownloadUrls) > 0 {
|
|
|
|
return func() error {
|
|
|
|
log.Printf("正在下载最新 %s 数据库...\n", db.Name)
|
2022-10-24 01:51:19 +00:00
|
|
|
data, err := download.Download(db.File, db.DownloadUrls...)
|
2022-05-24 07:16:48 +00:00
|
|
|
if err != nil {
|
2022-10-24 01:51:19 +00:00
|
|
|
log.Printf("%s 数据库下载失败,请手动下载解压后保存到本地: %s \n", db.Name, db.File)
|
|
|
|
log.Println("下载链接:", db.DownloadUrls)
|
2022-05-24 07:16:48 +00:00
|
|
|
log.Println("error:", err)
|
|
|
|
return err
|
|
|
|
} else {
|
2022-10-24 01:51:19 +00:00
|
|
|
if check, ok := DbCheckFunc[db.Format]; ok {
|
|
|
|
if !check(data) {
|
|
|
|
log.Printf("%s 数据库下载失败,请手动下载解压后保存到本地: %s \n", db.Name, db.File)
|
|
|
|
log.Println("下载链接:", db.DownloadUrls)
|
|
|
|
return errors.New("数据库内容出错")
|
|
|
|
}
|
|
|
|
}
|
2022-05-24 07:16:48 +00:00
|
|
|
log.Printf("%s 数据库下载成功: %s\n", db.Name, db.File)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}, string(db.Format)
|
|
|
|
}
|
|
|
|
|
|
|
|
// intenel download func
|
2022-05-09 06:48:28 +00:00
|
|
|
switch db.Format {
|
|
|
|
case FormatZXIPv6Wry:
|
|
|
|
return func() error {
|
|
|
|
log.Println("正在下载最新 ZX IPv6数据库...")
|
|
|
|
_, err := zxipv6wry.Download(getDbByName("zxipv6wry").File)
|
|
|
|
if err != nil {
|
2022-05-24 07:16:48 +00:00
|
|
|
log.Println("数据库 ZXIPv6Wry 下载失败:", err)
|
2022-05-09 06:48:28 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}, FormatZXIPv6Wry
|
|
|
|
default:
|
|
|
|
return func() error {
|
2022-05-24 07:16:48 +00:00
|
|
|
log.Println("暂不支持该类型数据库的自动更新")
|
|
|
|
log.Println("可通过指定数据库的 download-urls 从特定链接下载数据库文件")
|
2022-05-09 06:48:28 +00:00
|
|
|
return nil
|
|
|
|
}, time.Now().String()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return func() error {
|
|
|
|
log.Fatalln("该名称的数据库未找到:", name)
|
|
|
|
return nil
|
|
|
|
}, time.Now().String()
|
|
|
|
}
|
|
|
|
}
|