1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-22 21:29:02 +08:00
nali/pkg/zxipv6wry/update.go
2020-07-20 20:31:12 +08:00

85 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package zxipv6wry
import (
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"github.com/zu1k/nali/constant"
"github.com/saracen/go7z"
)
func Download(filePath string) (data []byte, err error) {
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
}
file7z := filepath.Join(constant.HomePath, "ip.7z")
_, err = os.Stat(file7z)
if err != nil && os.IsNotExist(err) {
if err := ioutil.WriteFile(file7z, body, 0644); err == nil {
Un7z(file7z)
err = os.Remove(file7z)
}
} else {
Un7z(file7z)
err = os.Remove(file7z)
}
return ioutil.ReadFile(filePath)
}
func Un7z(filePath string) {
sz, err := go7z.OpenReader(filePath)
if err != nil {
panic(err)
}
defer sz.Close()
f, err := os.Create("tmp")
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" {
f, err := os.Create(filepath.Join(constant.HomePath, "ipv6wry.db"))
if err != nil {
panic(err)
}
defer f.Close()
if _, err := io.Copy(f, sz); err != nil {
log.Fatalln("ZX ipv6数据库解压出错", err.Error())
}
} else {
if _, err := io.Copy(f, sz); err != nil {
log.Fatalln("ZX ipv6数据库解压出错", err.Error())
}
}
}
err = f.Close()
if err != nil {
panic(err)
}
_ = os.Remove("tmp")
}