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

update all db

This commit is contained in:
zu1k 2020-07-22 07:25:01 +08:00
parent 628c998333
commit d5d766f536
7 changed files with 63 additions and 44 deletions

View File

@ -1,11 +1,12 @@
package cmd package cmd
import ( import (
"io/ioutil"
"log" "log"
"os"
"path/filepath" "path/filepath"
"github.com/zu1k/nali/pkg/cdn"
"github.com/zu1k/nali/pkg/zxipv6wry"
"github.com/zu1k/nali/constant" "github.com/zu1k/nali/constant"
"github.com/zu1k/nali/pkg/qqwry" "github.com/zu1k/nali/pkg/qqwry"
@ -18,27 +19,31 @@ var updateCmd = &cobra.Command{
Short: "update chunzhen ip database", Short: "update chunzhen ip database",
Long: `update chunzhen ip database`, Long: `update chunzhen ip database`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// Chunzhen ipv4
filePath := filepath.Join(constant.HomePath, "qqwry.dat") filePath := filepath.Join(constant.HomePath, "qqwry.dat")
log.Println("正在下载最新 纯真 IPv4数据库...")
log.Println("正在下载最新纯真 IP 库...") _, err := qqwry.Download(filePath)
tmpData, err := qqwry.Download(filePath)
if err != nil { if err != nil {
log.Fatalln("下载失败", err.Error()) log.Fatalln("下载失败", err.Error())
return return
} }
// 文件存在就删除 // ZX ipv6
_, err = os.Stat(filePath) filePath = filepath.Join(constant.HomePath, "ipv6wry.db")
if err == nil { log.Println("正在下载最新 ZX IPv6数据库...")
err = os.Remove(filePath) _, err = zxipv6wry.Download(filePath)
if err != nil { if err != nil {
log.Fatalln("旧文件删除失败", err.Error()) log.Fatalln("下载失败", err.Error())
os.Exit(1) return
}
} }
if err := ioutil.WriteFile(filePath, tmpData, 0644); err == nil { // cdn
log.Printf("已将最新的纯真 IP 库保存到本地 %s ", filePath) filePath = filepath.Join(constant.HomePath, "cdn.json")
log.Println("正在下载最新 CDN服务提供商数据库...")
_, err = cdn.Download(filePath)
if err != nil {
log.Fatalln("下载失败", err.Error())
return
} }
}, },
} }

View File

@ -2,9 +2,7 @@ package app
import ( import (
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -79,23 +77,9 @@ func UpdateDB() {
filePath := filepath.Join(constant.HomePath, "cdn.json") filePath := filepath.Join(constant.HomePath, "cdn.json")
log.Println("正在下载最新 CDN数据库...") log.Println("正在下载最新 CDN数据库...")
tmpData, err := cdn.Download() _, err := cdn.Download(filePath)
if err != nil { if err != nil {
log.Fatalln("下载失败", err.Error()) log.Fatalln("下载失败", err.Error())
return return
} }
// 文件存在就删除
_, err = os.Stat(filePath)
if err == nil {
err = os.Remove(filePath)
if err != nil {
log.Fatalln("旧文件删除失败", err.Error())
os.Exit(1)
}
}
if err := ioutil.WriteFile(filePath, tmpData, 0644); err == nil {
log.Printf("已将最新的CDN数据库保存到本地 %s ", filePath)
}
} }

View File

@ -22,22 +22,14 @@ func NewCDN(filePath string) CDN {
cdnDist := make(CDNDist) cdnDist := make(CDNDist)
cdnData := make([]byte, 0) cdnData := make([]byte, 0)
// 判断文件是否存在
_, err := os.Stat(filePath) _, err := os.Stat(filePath)
if err != nil && os.IsNotExist(err) { if err != nil && os.IsNotExist(err) {
log.Println("文件不存在尝试从网络获取最新CDN数据库") log.Println("文件不存在尝试从网络获取最新CDN数据库")
cdnData, err = Download() cdnData, err = Download(filePath)
if err != nil { if err != nil {
log.Printf("CDN数据库下载失败请手动下载解压后保存到本地: %s \n", filePath)
log.Println("下载链接: https://cdn.jsdelivr.net/gh/SukkaLab/cdn/dist/cdn.json")
os.Exit(1) os.Exit(1)
} else {
if err := ioutil.WriteFile(filePath, cdnData, 0644); err == nil {
log.Printf("已将最新的 CDN数据库 保存到本地: %s \n", filePath)
}
} }
} else { } else {
// 打开文件句柄
cdnFile, err := os.OpenFile(filePath, os.O_RDONLY, 0400) cdnFile, err := os.OpenFile(filePath, os.O_RDONLY, 0400)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -2,10 +2,28 @@ package cdn
import ( import (
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"github.com/zu1k/nali/pkg/common"
) )
func Download() (data []byte, err error) { func Download(filePath string) (data []byte, err error) {
data, err = getData()
if err != nil {
log.Printf("CDN数据库下载失败请手动下载解压后保存到本地: %s \n", filePath)
log.Println("下载链接: https://cdn.jsdelivr.net/gh/SukkaLab/cdn/dist/cdn.json")
return
}
common.ExistThenRemove(filePath)
if err := ioutil.WriteFile(filePath, data, 0644); err == nil {
log.Printf("已将最新的 CDN数据库 保存到本地: %s \n", filePath)
}
return
}
func getData() (data []byte, err error) {
//resp, err := http.Get("https://raw.githubusercontent.com/SukkaLab/cdn/master/dist/cdn.json") //resp, err := http.Get("https://raw.githubusercontent.com/SukkaLab/cdn/master/dist/cdn.json")
resp, err := http.Get("https://cdn.jsdelivr.net/gh/SukkaLab/cdn/dist/cdn.json") resp, err := http.Get("https://cdn.jsdelivr.net/gh/SukkaLab/cdn/dist/cdn.json")
if err != nil { if err != nil {

View File

@ -1,8 +1,24 @@
package common package common
import (
"log"
"os"
)
func ByteToUInt32(data []byte) uint32 { func ByteToUInt32(data []byte) uint32 {
i := uint32(data[0]) & 0xff i := uint32(data[0]) & 0xff
i |= (uint32(data[1]) << 8) & 0xff00 i |= (uint32(data[1]) << 8) & 0xff00
i |= (uint32(data[2]) << 16) & 0xff0000 i |= (uint32(data[2]) << 16) & 0xff0000
return i return i
} }
func ExistThenRemove(filePath string) {
_, err := os.Stat(filePath)
if err == nil {
err = os.Remove(filePath)
if err != nil {
log.Fatalln("旧文件删除失败", err.Error())
os.Exit(1)
}
}
}

View File

@ -7,6 +7,8 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"github.com/zu1k/nali/pkg/common"
) )
func Download(filePath string) (data []byte, err error) { func Download(filePath string) (data []byte, err error) {
@ -16,7 +18,7 @@ func Download(filePath string) (data []byte, err error) {
log.Println("下载链接: https://qqwry.mirror.noc.one/qqwry.rar") log.Println("下载链接: https://qqwry.mirror.noc.one/qqwry.rar")
return return
} }
common.ExistThenRemove(filePath)
if err = ioutil.WriteFile(filePath, data, 0644); err == nil { if err = ioutil.WriteFile(filePath, data, 0644); err == nil {
log.Printf("已将最新的 纯真IP库 保存到本地: %s ", filePath) log.Printf("已将最新的 纯真IP库 保存到本地: %s ", filePath)
} }

View File

@ -7,6 +7,8 @@ import (
"net/http" "net/http"
"os" "os"
"github.com/zu1k/nali/pkg/common"
"github.com/saracen/go7z" "github.com/saracen/go7z"
) )
@ -17,7 +19,7 @@ func Download(filePath string) (data []byte, err error) {
log.Println("下载链接: https://www.zxinc.org/ip.7z") log.Println("下载链接: https://www.zxinc.org/ip.7z")
return return
} }
common.ExistThenRemove(filePath)
if err = ioutil.WriteFile(filePath, data, 0644); err == nil { if err = ioutil.WriteFile(filePath, data, 0644); err == nil {
log.Printf("已将最新的 ZX IPv6数据库 保存到本地: %s ", filePath) log.Printf("已将最新的 ZX IPv6数据库 保存到本地: %s ", filePath)
} }