1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-23 05:39:03 +08:00
nali/pkg/zxipv6wry/update.go

100 lines
2.1 KiB
Go
Raw Normal View History

2020-07-18 09:52:59 +08:00
package zxipv6wry
2020-07-20 20:31:12 +08:00
import (
2022-10-24 09:51:19 +08:00
"errors"
2020-07-20 20:31:12 +08:00
"io"
"io/ioutil"
"log"
"os"
"github.com/saracen/go7z"
2021-08-11 17:52:26 +08:00
"github.com/zu1k/nali/pkg/common"
2020-07-20 20:31:12 +08:00
)
func Download(filePath ...string) (data []byte, err error) {
2020-07-22 07:02:28 +08:00
data, err = getData()
if err != nil {
log.Printf("ZX IPv6数据库下载失败请手动下载解压后保存到本地: %s \n", filePath)
2021-04-19 15:11:11 +08:00
log.Println("下载链接: https://ip.zxinc.org/ip.7z")
2020-07-22 07:02:28 +08:00
return
}
2022-10-24 09:51:19 +08:00
if !CheckFile(data) {
log.Printf("ZX IPv6数据库下载出错请手动下载解压后保存到本地: %s \n", filePath)
log.Println("下载链接: https://ip.zxinc.org/ip.7z")
return nil, errors.New("数据库下载内容出错")
}
if len(filePath) == 1 {
if err := common.SaveFile(filePath[0], data); err == nil {
log.Println("已将最新的 ZX IPv6数据库 保存到本地:", filePath)
}
2020-07-22 07:02:28 +08:00
}
return
}
const (
zx = "https://ip.zxinc.org/ip.7z"
)
2020-07-20 20:31:12 +08:00
func getData() (data []byte, err error) {
data, err = common.GetHttpClient().Get(zx)
2020-07-20 20:31:12 +08:00
file7z, err := os.CreateTemp("", "*")
2020-07-22 07:02:28 +08:00
if err != nil {
return nil, err
2020-07-20 20:31:12 +08:00
}
2020-07-22 07:02:28 +08:00
defer os.Remove(file7z.Name())
if err := os.WriteFile(file7z.Name(), data, 0644); err == nil {
2020-07-22 07:02:28 +08:00
return Un7z(file7z.Name())
}
return
2020-07-20 20:31:12 +08:00
}
2020-07-22 07:02:28 +08:00
func Un7z(filePath string) (data []byte, err error) {
2020-07-20 20:31:12 +08:00
sz, err := go7z.OpenReader(filePath)
if err != nil {
return nil, err
2020-07-20 20:31:12 +08:00
}
defer sz.Close()
fileNoNeed, err := os.CreateTemp("", "*")
2020-07-22 07:02:28 +08:00
if err != nil {
return nil, err
2020-07-22 07:02:28 +08:00
}
fileNeed, err := os.CreateTemp("", "*")
2020-07-22 07:02:28 +08:00
if err != nil {
return nil, err
2020-07-22 07:02:28 +08:00
}
2020-07-20 20:31:12 +08:00
if err != nil {
return nil, err
2020-07-20 20:31:12 +08:00
}
for {
hdr, err := sz.Next()
if err == io.EOF {
break // IdxEnd of archive
2020-07-20 20:31:12 +08:00
}
if err != nil {
return nil, err
2020-07-20 20:31:12 +08:00
}
if hdr.Name == "ipv6wry.db" {
2020-07-22 07:02:28 +08:00
if _, err := io.Copy(fileNeed, sz); err != nil {
2020-07-20 20:31:12 +08:00
log.Fatalln("ZX ipv6数据库解压出错", err.Error())
}
} else {
2020-07-22 07:02:28 +08:00
if _, err := io.Copy(fileNoNeed, sz); err != nil {
2020-07-20 20:31:12 +08:00
log.Fatalln("ZX ipv6数据库解压出错", err.Error())
}
}
}
2020-07-22 07:02:28 +08:00
err = fileNoNeed.Close()
2020-07-20 20:31:12 +08:00
if err != nil {
return nil, err
2020-07-20 20:31:12 +08:00
}
2020-07-22 07:02:28 +08:00
defer os.Remove(fileNoNeed.Name())
defer os.Remove(fileNeed.Name())
return ioutil.ReadFile(fileNeed.Name())
2020-07-18 09:52:59 +08:00
}