diff --git a/go.mod b/go.mod index eb95aef..5ca394e 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,12 @@ go 1.14 require ( github.com/oschwald/geoip2-golang v1.4.0 + github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda + github.com/saracen/go7z-fixtures v0.0.0-20190623165746-aa6b8fba1d2f // indirect + github.com/saracen/solidblock v0.0.0-20190426153529-45df20abab6f // indirect github.com/spf13/cobra v1.0.0 github.com/spf13/pflag v1.0.5 // indirect + github.com/ulikunitz/xz v0.5.7 // indirect golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect golang.org/x/text v0.3.3 gopkg.in/yaml.v2 v2.3.0 // indirect diff --git a/go.sum b/go.sum index 373347b..fc4fa5e 100644 --- a/go.sum +++ b/go.sum @@ -84,6 +84,12 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda h1:h+YpzUB/bGVJcLqW+d5GghcCmE/A25KbzjXvWJQi/+o= +github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda/go.mod h1:MSotTrCv1PwoR8QgU1JurEx+lNNbtr25I+m0zbLyAGw= +github.com/saracen/go7z-fixtures v0.0.0-20190623165746-aa6b8fba1d2f h1:PF9WV5j/x6MT+x/sauUHd4objCvJbZb0wdxZkHSdd5A= +github.com/saracen/go7z-fixtures v0.0.0-20190623165746-aa6b8fba1d2f/go.mod h1:6Ff0ADODZ6S3gYepgZ2w7OqFrTqtFcfwDUhmm8jsUhs= +github.com/saracen/solidblock v0.0.0-20190426153529-45df20abab6f h1:1cJITU3JUI8qNS5T0BlXwANsVdyoJQHQ4hvOxbunPCw= +github.com/saracen/solidblock v0.0.0-20190426153529-45df20abab6f/go.mod h1:LyBTue+RWeyIfN3ZJ4wVxvDuvlGJtDgCLgCb6HCPgps= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= @@ -109,6 +115,8 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= +github.com/ulikunitz/xz v0.5.7 h1:YvTNdFzX6+W5m9msiYg/zpkSURPPtOlzbqYjrFn7Yt4= +github.com/ulikunitz/xz v0.5.7/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= @@ -146,6 +154,7 @@ golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxb golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd h1:/e+gpKk9r3dJobndpTytxS2gOy6m5uvpg+ISQoEcusQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc= diff --git a/pkg/zxipv6wry/update.go b/pkg/zxipv6wry/update.go index 9d8947f..f4a5ca0 100644 --- a/pkg/zxipv6wry/update.go +++ b/pkg/zxipv6wry/update.go @@ -1,5 +1,84 @@ package zxipv6wry -func GetOnline() ([]byte, error) { - return nil, nil +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") } diff --git a/pkg/zxipv6wry/zxipv6wry.go b/pkg/zxipv6wry/zxipv6wry.go index 441b3db..a7eb94b 100644 --- a/pkg/zxipv6wry/zxipv6wry.go +++ b/pkg/zxipv6wry/zxipv6wry.go @@ -18,15 +18,23 @@ type ZXwry struct { } func NewZXwry(filePath string) ZXwry { - var tmpData []byte + var fileData []byte var fileInfo common.FileData // 判断文件是否存在 _, err := os.Stat(filePath) if err != nil && os.IsNotExist(err) { - log.Println("文件不存在,请自行下载 ZX IPV6库,解压并保存在", filePath) - log.Println("下载链接: https://www.zxinc.org/ip.7z") - os.Exit(1) + log.Println("文件不存在,尝试从网络获取最新ZX IPv6数据库") + fileData, err = Download(filePath) + if err != nil { + log.Printf("ZX IPv6数据库下载失败,请手动下载解压后保存到本地: %s \n", filePath) + log.Println("下载链接: https://www.zxinc.org/ip.7z") + os.Exit(1) + } else { + if err := ioutil.WriteFile(filePath, fileData, 0644); err == nil { + log.Printf("已将最新的 ZX IPv6数据库 保存到本地: %s ", filePath) + } + } } else { // 打开文件句柄 fileInfo.FileBase, err = os.OpenFile(filePath, os.O_RDONLY, 0400) @@ -35,13 +43,13 @@ func NewZXwry(filePath string) ZXwry { } defer fileInfo.FileBase.Close() - tmpData, err = ioutil.ReadAll(fileInfo.FileBase) + fileData, err = ioutil.ReadAll(fileInfo.FileBase) if err != nil { panic(err) } } - fileInfo.Data = tmpData + fileInfo.Data = fileData return ZXwry{ IPDB: common.IPDB{