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

mod zxipv6 download

This commit is contained in:
zu1k 2020-07-22 07:02:28 +08:00
parent 2609f7c512
commit a5395db5fb
3 changed files with 39 additions and 36 deletions

View File

@ -27,8 +27,6 @@ func NewQQwry(filePath string) QQwry {
log.Println("文件不存在,尝试从网络获取最新纯真 IP 库") log.Println("文件不存在,尝试从网络获取最新纯真 IP 库")
fileData, err = Download(filePath) fileData, err = Download(filePath)
if err != nil { if err != nil {
log.Printf("纯真IP库下载失败请手动下载解压后保存到本地: %s \n", filePath)
log.Println("下载链接: https://qqwry.mirror.noc.one/qqwry.rar")
os.Exit(1) os.Exit(1)
} }
} else { } else {

View File

@ -6,14 +6,26 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"path/filepath"
"github.com/zu1k/nali/constant"
"github.com/saracen/go7z" "github.com/saracen/go7z"
) )
func Download(filePath string) (data []byte, err error) { func Download(filePath string) (data []byte, err error) {
data, err = getData()
if err != nil {
log.Printf("ZX IPv6数据库下载失败请手动下载解压后保存到本地: %s \n", filePath)
log.Println("下载链接: https://www.zxinc.org/ip.7z")
return
}
if err = ioutil.WriteFile(filePath, data, 0644); err == nil {
log.Printf("已将最新的 ZX IPv6数据库 保存到本地: %s ", filePath)
}
return
}
func getData() (data []byte, err error) {
resp, err := http.Get("https://www.zxinc.org/ip.7z") resp, err := http.Get("https://www.zxinc.org/ip.7z")
if err != nil { if err != nil {
return nil, err return nil, err
@ -25,29 +37,34 @@ func Download(filePath string) (data []byte, err error) {
return nil, err return nil, err
} }
file7z := filepath.Join(constant.HomePath, "ip.7z") file7z, err := ioutil.TempFile("", "*")
_, err = os.Stat(file7z) if err != nil {
if err != nil && os.IsNotExist(err) { panic(err)
if err := ioutil.WriteFile(file7z, body, 0644); err == nil {
Un7z(file7z)
err = os.Remove(file7z)
} }
} else { defer os.Remove(file7z.Name())
Un7z(file7z)
err = os.Remove(file7z) if err := ioutil.WriteFile(file7z.Name(), body, 0644); err == nil {
return Un7z(file7z.Name())
}
return
} }
return ioutil.ReadFile(filePath) func Un7z(filePath string) (data []byte, err error) {
}
func Un7z(filePath string) {
sz, err := go7z.OpenReader(filePath) sz, err := go7z.OpenReader(filePath)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer sz.Close() defer sz.Close()
f, err := os.Create("tmp") fileNoNeed, err := ioutil.TempFile("", "*")
if err != nil {
panic(err)
}
fileNeed, err := ioutil.TempFile("", "*")
if err != nil {
panic(err)
}
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -61,24 +78,20 @@ func Un7z(filePath string) {
} }
if hdr.Name == "ipv6wry.db" { if hdr.Name == "ipv6wry.db" {
f, err := os.Create(filepath.Join(constant.HomePath, "ipv6wry.db")) if _, err := io.Copy(fileNeed, sz); err != nil {
if err != nil {
panic(err)
}
defer f.Close()
if _, err := io.Copy(f, sz); err != nil {
log.Fatalln("ZX ipv6数据库解压出错", err.Error()) log.Fatalln("ZX ipv6数据库解压出错", err.Error())
} }
} else { } else {
if _, err := io.Copy(f, sz); err != nil { if _, err := io.Copy(fileNoNeed, sz); err != nil {
log.Fatalln("ZX ipv6数据库解压出错", err.Error()) log.Fatalln("ZX ipv6数据库解压出错", err.Error())
} }
} }
} }
err = f.Close() err = fileNoNeed.Close()
if err != nil { if err != nil {
panic(err) panic(err)
} }
_ = os.Remove("tmp") defer os.Remove(fileNoNeed.Name())
defer os.Remove(fileNeed.Name())
return ioutil.ReadFile(fileNeed.Name())
} }

View File

@ -21,22 +21,14 @@ func NewZXwry(filePath string) ZXwry {
var fileData []byte var fileData []byte
var fileInfo common.FileData var fileInfo common.FileData
// 判断文件是否存在
_, err := os.Stat(filePath) _, err := os.Stat(filePath)
if err != nil && os.IsNotExist(err) { if err != nil && os.IsNotExist(err) {
log.Println("文件不存在尝试从网络获取最新ZX IPv6数据库") log.Println("文件不存在尝试从网络获取最新ZX IPv6数据库")
fileData, err = Download(filePath) fileData, err = Download(filePath)
if err != nil { if err != nil {
log.Printf("ZX IPv6数据库下载失败请手动下载解压后保存到本地: %s \n", filePath)
log.Println("下载链接: https://www.zxinc.org/ip.7z")
os.Exit(1) os.Exit(1)
} else {
if err := ioutil.WriteFile(filePath, fileData, 0644); err == nil {
log.Printf("已将最新的 ZX IPv6数据库 保存到本地: %s ", filePath)
}
} }
} else { } else {
// 打开文件句柄
fileInfo.FileBase, err = os.OpenFile(filePath, os.O_RDONLY, 0400) fileInfo.FileBase, err = os.OpenFile(filePath, os.O_RDONLY, 0400)
if err != nil { if err != nil {
panic(err) panic(err)