2020-07-17 09:05:25 +08:00
|
|
|
package qqwry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2021-08-02 12:01:25 +08:00
|
|
|
"errors"
|
2020-07-17 09:05:25 +08:00
|
|
|
"fmt"
|
2022-10-20 15:54:20 +08:00
|
|
|
"io"
|
2020-07-17 09:05:25 +08:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
2022-07-21 14:14:08 +08:00
|
|
|
"github.com/zu1k/nali/pkg/download"
|
2022-10-20 15:54:20 +08:00
|
|
|
"github.com/zu1k/nali/pkg/wry"
|
2020-07-17 09:05:25 +08:00
|
|
|
)
|
|
|
|
|
2022-07-21 14:11:27 +08:00
|
|
|
var DownloadUrls = []string{
|
2023-04-24 16:39:25 +08:00
|
|
|
"https://gh-release.zu1k.com/HMBSbige/qqwry/qqwry.dat", // redirect to HMBSbige/qqwry
|
2023-01-08 09:30:59 +08:00
|
|
|
// Other repo:
|
|
|
|
// https://github.com/HMBSbige/qqwry
|
|
|
|
// https://github.com/metowolf/qqwry.dat
|
2022-07-21 14:11:27 +08:00
|
|
|
}
|
|
|
|
|
2020-07-17 09:05:25 +08:00
|
|
|
type QQwry struct {
|
2022-10-20 15:54:20 +08:00
|
|
|
wry.IPDB[uint32]
|
2020-07-17 09:05:25 +08:00
|
|
|
}
|
|
|
|
|
2021-08-02 12:01:25 +08:00
|
|
|
// NewQQwry new database from path
|
2022-03-02 12:34:11 +08:00
|
|
|
func NewQQwry(filePath string) (*QQwry, error) {
|
2020-07-18 10:48:41 +08:00
|
|
|
var fileData []byte
|
2020-07-17 09:05:25 +08:00
|
|
|
|
|
|
|
_, err := os.Stat(filePath)
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
log.Println("文件不存在,尝试从网络获取最新纯真 IP 库")
|
2022-07-21 14:11:27 +08:00
|
|
|
fileData, err = download.Download(filePath, DownloadUrls...)
|
2020-07-17 09:05:25 +08:00
|
|
|
if err != nil {
|
2022-03-02 12:34:11 +08:00
|
|
|
return nil, err
|
2020-07-17 09:05:25 +08:00
|
|
|
}
|
|
|
|
} else {
|
2022-10-20 15:54:20 +08:00
|
|
|
fileBase, err := os.OpenFile(filePath, os.O_RDONLY, 0400)
|
2020-07-17 09:05:25 +08:00
|
|
|
if err != nil {
|
2022-03-02 12:34:11 +08:00
|
|
|
return nil, err
|
2020-07-17 09:05:25 +08:00
|
|
|
}
|
2022-10-20 15:54:20 +08:00
|
|
|
defer fileBase.Close()
|
2020-07-17 09:05:25 +08:00
|
|
|
|
2022-10-20 15:54:20 +08:00
|
|
|
fileData, err = io.ReadAll(fileBase)
|
2020-07-17 09:05:25 +08:00
|
|
|
if err != nil {
|
2022-03-02 12:34:11 +08:00
|
|
|
return nil, err
|
2020-07-17 09:05:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 09:51:19 +08:00
|
|
|
if !CheckFile(fileData) {
|
2022-10-20 16:05:02 +08:00
|
|
|
log.Fatalln("纯真 IP 库存在错误,请重新下载")
|
|
|
|
}
|
|
|
|
|
2022-10-20 15:54:20 +08:00
|
|
|
header := fileData[0:8]
|
|
|
|
start := binary.LittleEndian.Uint32(header[:4])
|
|
|
|
end := binary.LittleEndian.Uint32(header[4:])
|
2020-07-17 09:05:25 +08:00
|
|
|
|
2022-03-02 12:34:11 +08:00
|
|
|
return &QQwry{
|
2022-10-20 15:54:20 +08:00
|
|
|
IPDB: wry.IPDB[uint32]{
|
|
|
|
Data: fileData,
|
|
|
|
|
|
|
|
OffLen: 3,
|
|
|
|
IPLen: 4,
|
|
|
|
IPCnt: (end-start)/7 + 1,
|
|
|
|
IdxStart: start,
|
|
|
|
IdxEnd: end,
|
2020-07-18 09:52:59 +08:00
|
|
|
},
|
2022-03-02 12:34:11 +08:00
|
|
|
}, nil
|
2020-07-17 09:05:25 +08:00
|
|
|
}
|
|
|
|
|
2021-08-02 12:01:25 +08:00
|
|
|
func (db QQwry) Find(query string, params ...string) (result fmt.Stringer, err error) {
|
|
|
|
ip := net.ParseIP(query)
|
|
|
|
if ip == nil {
|
2022-10-20 15:54:20 +08:00
|
|
|
return nil, errors.New("query should be IPv4")
|
2020-07-17 09:05:25 +08:00
|
|
|
}
|
2021-08-02 12:01:25 +08:00
|
|
|
ip4 := ip.To4()
|
|
|
|
if ip4 == nil {
|
2022-10-20 15:54:20 +08:00
|
|
|
return nil, errors.New("query should be IPv4")
|
2021-08-02 12:01:25 +08:00
|
|
|
}
|
|
|
|
ip4uint := binary.BigEndian.Uint32(ip4)
|
2020-07-18 09:19:27 +08:00
|
|
|
|
2022-10-20 15:54:20 +08:00
|
|
|
offset := db.SearchIndexV4(ip4uint)
|
2020-07-17 09:05:25 +08:00
|
|
|
if offset <= 0 {
|
2022-10-20 15:54:20 +08:00
|
|
|
return nil, errors.New("query not valid")
|
2020-07-17 09:05:25 +08:00
|
|
|
}
|
|
|
|
|
2022-10-20 15:54:20 +08:00
|
|
|
reader := wry.NewReader(db.Data)
|
|
|
|
reader.Parse(offset + 4)
|
|
|
|
return reader.Result.DecodeGBK(), nil
|
2020-07-17 09:05:25 +08:00
|
|
|
}
|
2022-10-24 09:51:19 +08:00
|
|
|
|
|
|
|
func CheckFile(data []byte) bool {
|
|
|
|
if len(data) < 8 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
header := data[0:8]
|
|
|
|
start := binary.LittleEndian.Uint32(header[:4])
|
|
|
|
end := binary.LittleEndian.Uint32(header[4:])
|
|
|
|
|
|
|
|
if start >= end || uint32(len(data)) < end+7 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|