/
qqwry.go
327 lines (280 loc) · 6.78 KB
/
qqwry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package custom
// forked from https://github.com/freshcn/qqwry
import (
"bytes"
"compress/zlib"
"encoding/binary"
"fmt"
"github.com/hanc00l/nemo_go/pkg/logging"
"io"
"net"
"net/http"
"os"
"strings"
"golang.org/x/text/encoding/simplifiedchinese"
)
const (
// IndexLen 索引长度
IndexLen = 7
// RedirectMode1 国家的类型, 指向另一个指向
RedirectMode1 = 0x01
// RedirectMode2 国家的类型, 指向一个指向
RedirectMode2 = 0x02
)
// ResultQQwry 归属地信息
type ResultQQwry struct {
IP string `json:"ip"`
Country string `json:"country"`
Area string `json:"area"`
}
type fileData struct {
Data []byte
FilePath string
Path *os.File
IPNum int64
}
// QQwry 纯真ip库
type QQwry struct {
Data *fileData
Offset int64
}
// Response 向客户端返回数据的
type Response struct {
r *http.Request
w http.ResponseWriter
}
// IPData IP库的数据
var IPData fileData
// @ref https://zhangzifan.com/update-qqwry-dat.html
func getKey() (uint32, error) {
resp, err := http.Get("http://update.cz88.net/ip/copywrite.rar")
if err != nil {
return 0, err
}
defer resp.Body.Close()
if body, err := io.ReadAll(resp.Body); err != nil {
return 0, err
} else {
// @see https://stackoverflow.com/questions/34078427/how-to-read-packed-binary-data-in-go
return binary.LittleEndian.Uint32(body[5*4:]), nil
}
}
func GetOnline() ([]byte, error) {
resp, err := http.Get("http://update.cz88.net/ip/qqwry.rar")
if err != nil {
return nil, err
}
defer resp.Body.Close()
if body, err := io.ReadAll(resp.Body); err != nil {
return nil, err
} else {
if key, err := getKey(); err != nil {
return nil, err
} else {
for i := 0; i < 0x200; i++ {
key = key * 0x805
key++
key = key & 0xff
body[i] = byte(uint32(body[i]) ^ key)
}
reader, err := zlib.NewReader(bytes.NewReader(body))
if err != nil {
return nil, err
}
return io.ReadAll(reader)
}
}
}
// InitIPData 初始化ip库数据到内存中
func (f *fileData) InitIPData() (rs interface{}) {
var tmpData []byte
// 判断文件是否存在
_, err := os.Stat(f.FilePath)
if err != nil && os.IsNotExist(err) {
msg := "文件不存在,尝试从网络获取最新纯真 Domain 库"
logging.RuntimeLog.Info(msg)
logging.CLILog.Info(msg)
tmpData, err = GetOnline()
if err != nil {
logging.RuntimeLog.Info(err)
logging.CLILog.Info(err)
rs = err
return
} else {
if err := os.WriteFile(f.FilePath, tmpData, 0644); err == nil {
msg = fmt.Sprintf("已将最新的纯真 Domain 库保存到本地 %s ", f.FilePath)
logging.RuntimeLog.Info(msg)
logging.CLILog.Info(msg)
}
}
} else {
// 打开文件句柄
// logging.RuntimeLog.Infof("从本地数据库文件 %s 打开\n", f.FilePath)
f.Path, err = os.OpenFile(f.FilePath, os.O_RDONLY, 0400)
if err != nil {
logging.RuntimeLog.Info(err)
logging.CLILog.Info(err)
rs = err
return
}
defer f.Path.Close()
tmpData, err = io.ReadAll(f.Path)
if err != nil {
logging.RuntimeLog.Info(err)
logging.CLILog.Info(err)
rs = err
return
}
}
f.Data = tmpData
buf := f.Data[0:8]
start := binary.LittleEndian.Uint32(buf[:4])
end := binary.LittleEndian.Uint32(buf[4:])
f.IPNum = int64((end-start)/IndexLen + 1)
return true
}
// NewQQwry 新建 qqwry 类型
func NewQQwry() QQwry {
return QQwry{
Data: &IPData,
}
}
// ReadData 从文件中读取数据
func (q *QQwry) ReadData(num int, offset ...int64) (rs []byte) {
if len(offset) > 0 {
q.SetOffset(offset[0])
}
nums := int64(num)
end := q.Offset + nums
dataNum := int64(len(q.Data.Data))
if q.Offset > dataNum {
return nil
}
if end > dataNum {
end = dataNum
}
rs = q.Data.Data[q.Offset:end]
q.Offset = end
return
}
// SetOffset 设置偏移量
func (q *QQwry) SetOffset(offset int64) {
q.Offset = offset
}
// Find ip地址查询对应归属地信息
func (q *QQwry) Find(ip string) (res ResultQQwry) {
res = ResultQQwry{}
res.IP = ip
if strings.Count(ip, ".") != 3 {
return res
}
offset := q.searchIndex(binary.BigEndian.Uint32(net.ParseIP(ip).To4()))
if offset <= 0 {
return
}
var country []byte
var area []byte
mode := q.readMode(offset + 4)
if mode == RedirectMode1 {
countryOffset := q.readUInt24()
mode = q.readMode(countryOffset)
if mode == RedirectMode2 {
c := q.readUInt24()
country = q.readString(c)
countryOffset += 4
} else {
country = q.readString(countryOffset)
countryOffset += uint32(len(country) + 1)
}
area = q.readArea(countryOffset)
} else if mode == RedirectMode2 {
countryOffset := q.readUInt24()
country = q.readString(countryOffset)
area = q.readArea(offset + 8)
} else {
country = q.readString(offset + 4)
area = q.readArea(offset + uint32(5+len(country)))
}
enc := simplifiedchinese.GBK.NewDecoder()
res.Country, _ = enc.String(string(country))
res.Area, _ = enc.String(string(area))
return
}
// readMode 获取偏移值类型
func (q *QQwry) readMode(offset uint32) byte {
mode := q.ReadData(1, int64(offset))
return mode[0]
}
// readArea 读取区域
func (q *QQwry) readArea(offset uint32) []byte {
mode := q.readMode(offset)
if mode == RedirectMode1 || mode == RedirectMode2 {
areaOffset := q.readUInt24()
if areaOffset == 0 {
return []byte("")
}
return q.readString(areaOffset)
}
return q.readString(offset)
}
// readString 获取字符串
func (q *QQwry) readString(offset uint32) []byte {
q.SetOffset(int64(offset))
data := make([]byte, 0, 30)
buf := make([]byte, 1)
for {
buf = q.ReadData(1)
if buf[0] == 0 {
break
}
data = append(data, buf[0])
}
return data
}
// searchIndex 查找索引位置
func (q *QQwry) searchIndex(ip uint32) uint32 {
header := q.ReadData(8, 0)
start := binary.LittleEndian.Uint32(header[:4])
end := binary.LittleEndian.Uint32(header[4:])
buf := make([]byte, IndexLen)
mid := uint32(0)
_ip := uint32(0)
for {
mid = q.getMiddleOffset(start, end)
buf = q.ReadData(IndexLen, int64(mid))
_ip = binary.LittleEndian.Uint32(buf[:4])
if end-start == IndexLen {
offset := byteToUInt32(buf[4:])
buf = q.ReadData(IndexLen)
if ip < binary.LittleEndian.Uint32(buf[:4]) {
return offset
}
return 0
}
// 找到的比较大,向前移
if _ip > ip {
end = mid
} else if _ip < ip { // 找到的比较小,向后移
start = mid
} else if _ip == ip {
return byteToUInt32(buf[4:])
}
}
}
// readUInt24
func (q *QQwry) readUInt24() uint32 {
buf := q.ReadData(3)
return byteToUInt32(buf)
}
// getMiddleOffset
func (q *QQwry) getMiddleOffset(start uint32, end uint32) uint32 {
records := ((end - start) / IndexLen) >> 1
return start + records*IndexLen
}
// byteToUInt32 将 byte 转换为uint32
func byteToUInt32(data []byte) uint32 {
i := uint32(data[0]) & 0xff
i |= (uint32(data[1]) << 8) & 0xff00
i |= (uint32(data[2]) << 16) & 0xff0000
return i
}