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

75 lines
1.4 KiB
Go
Raw Normal View History

2020-07-17 09:05:25 +08:00
package qqwry
import (
"bytes"
2020-07-17 09:41:09 +08:00
"compress/zlib"
"encoding/binary"
"io/ioutil"
2020-07-22 06:40:02 +08:00
"log"
2020-07-17 09:41:09 +08:00
"net/http"
2020-07-17 09:05:25 +08:00
)
2020-07-22 06:40:02 +08:00
func Download(filePath string) (data []byte, err error) {
data, err = getData()
if err != nil {
log.Printf("纯真IP库下载失败请手动下载解压后保存到本地: %s \n", filePath)
log.Println("下载链接: https://qqwry.mirror.noc.one/qqwry.rar")
return
}
if err = ioutil.WriteFile(filePath, data, 0644); err == nil {
log.Printf("已将最新的 纯真IP库 保存到本地: %s ", filePath)
}
return
}
func getData() (data []byte, err error) {
2020-07-18 10:48:41 +08:00
resp, err := http.Get("https://qqwry.mirror.noc.one/qqwry.rar")
2020-07-17 09:05:25 +08:00
if err != nil {
2020-07-22 06:40:02 +08:00
return
2020-07-17 09:05:25 +08:00
}
defer resp.Body.Close()
2020-07-18 10:48:41 +08:00
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
2020-07-22 06:40:02 +08:00
return
2020-07-17 09:05:25 +08:00
}
2020-07-18 10:48:41 +08:00
key, err := getCopyWriteKey()
if err != nil {
2020-07-22 06:40:02 +08:00
return
2020-07-18 10:48:41 +08:00
}
return unRar(body, key)
2020-07-17 09:05:25 +08:00
}
2020-07-18 10:48:41 +08:00
func unRar(data []byte, key uint32) ([]byte, error) {
for i := 0; i < 0x200; i++ {
key = key * 0x805
key++
key = key & 0xff
data[i] = byte(uint32(data[i]) ^ key)
}
reader, err := zlib.NewReader(bytes.NewReader(data))
2020-07-17 09:05:25 +08:00
if err != nil {
return nil, err
}
2020-07-18 10:48:41 +08:00
return ioutil.ReadAll(reader)
}
func getCopyWriteKey() (uint32, error) {
resp, err := http.Get("https://qqwry.mirror.noc.one/copywrite.rar")
if err != nil {
return 0, err
}
2020-07-17 09:05:25 +08:00
defer resp.Body.Close()
if body, err := ioutil.ReadAll(resp.Body); err != nil {
2020-07-18 10:48:41 +08:00
return 0, err
2020-07-17 09:05:25 +08:00
} else {
2020-07-18 10:48:41 +08:00
return binary.LittleEndian.Uint32(body[5*4:]), nil
2020-07-17 09:05:25 +08:00
}
}