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

100 lines
1.9 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 (
"io"
"io/ioutil"
"log"
"net/http"
"os"
2020-07-22 07:25:01 +08:00
"github.com/zu1k/nali/pkg/common"
2020-07-20 20:31:12 +08:00
"github.com/saracen/go7z"
)
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)
log.Println("下载链接: https://www.zxinc.org/ip.7z")
return
}
2020-07-22 07:25:01 +08:00
common.ExistThenRemove(filePath)
2020-07-22 07:02:28 +08:00
if err = ioutil.WriteFile(filePath, data, 0644); err == nil {
log.Printf("已将最新的 ZX IPv6数据库 保存到本地: %s ", filePath)
}
return
}
func getData() (data []byte, err error) {
2020-07-20 20:31:12 +08:00
resp, err := http.Get("https://www.zxinc.org/ip.7z")
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
2020-07-22 07:02:28 +08:00
file7z, err := ioutil.TempFile("", "*")
if err != nil {
panic(err)
2020-07-20 20:31:12 +08:00
}
2020-07-22 07:02:28 +08:00
defer os.Remove(file7z.Name())
2020-07-20 20:31:12 +08:00
2020-07-22 07:02:28 +08:00
if err := ioutil.WriteFile(file7z.Name(), body, 0644); err == nil {
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 {
panic(err)
}
defer sz.Close()
2020-07-22 07:02:28 +08:00
fileNoNeed, err := ioutil.TempFile("", "*")
if err != nil {
panic(err)
}
fileNeed, err := ioutil.TempFile("", "*")
if err != nil {
panic(err)
}
2020-07-20 20:31:12 +08:00
if err != nil {
panic(err)
}
for {
hdr, err := sz.Next()
if err == io.EOF {
break // End of archive
}
if err != nil {
panic(err)
}
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 {
panic(err)
}
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
}