-
Notifications
You must be signed in to change notification settings - Fork 18
/
reader_v2.go
360 lines (314 loc) · 7.18 KB
/
reader_v2.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright 2022 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package iplibrary
import (
"bytes"
"encoding/json"
"errors"
"io"
"net"
"runtime"
"sort"
"strconv"
"strings"
)
// ReaderV2 IP库Reader V2
type ReaderV2 struct {
meta *Meta
regionMap map[string]*ipRegion // 缓存重复的区域用来节约内存
ipV4Items []ipv4ItemV2
ipV6Items []ipv6ItemV2
lastCountryId uint16
lastProvinceId uint16
lastCityId uint32
lastTownId uint32
lastProviderId uint16
}
// NewReaderV2 创建新Reader对象
func NewReaderV2(reader io.Reader) (*ReaderV2, error) {
var libReader = &ReaderV2{
regionMap: map[string]*ipRegion{},
}
if runtime.NumCPU() >= 4 /** CPU数量较多的通常有着大内存 **/ {
libReader.ipV4Items = make([]ipv4ItemV2, 0, 6_000_000)
} else {
libReader.ipV4Items = make([]ipv4ItemV2, 0, 600_000)
}
err := libReader.load(reader)
if err != nil {
return nil, err
}
return libReader, nil
}
// 从Reader中加载数据
func (this *ReaderV2) load(reader io.Reader) error {
var buf = make([]byte, 1024)
var metaLine []byte
var metaLineFound = false
var dataBuf = []byte{}
for {
n, err := reader.Read(buf)
if n > 0 {
var data = buf[:n]
dataBuf = append(dataBuf, data...)
if metaLineFound {
left, err := this.parse(dataBuf)
if err != nil {
return err
}
dataBuf = left
} else {
var index = bytes.IndexByte(dataBuf, '\n')
if index > 0 {
metaLine = dataBuf[:index]
dataBuf = dataBuf[index+1:]
metaLineFound = true
var meta = &Meta{}
err = json.Unmarshal(metaLine, &meta)
if err != nil {
return err
}
meta.Init()
this.meta = meta
left, err := this.parse(dataBuf)
if err != nil {
return err
}
dataBuf = left
}
}
}
if err != nil {
if err != io.EOF {
return err
}
break
}
}
sort.Slice(this.ipV4Items, func(i, j int) bool {
var from0 = this.ipV4Items[i].IPFrom
var to0 = this.ipV4Items[i].IPTo
var from1 = this.ipV4Items[j].IPFrom
var to1 = this.ipV4Items[j].IPTo
if from0 == from1 {
return bytes.Compare(to0[:], to1[:]) < 0
}
return bytes.Compare(from0[:], from1[:]) < 0
})
sort.Slice(this.ipV6Items, func(i, j int) bool {
var from0 = this.ipV6Items[i].IPFrom
var to0 = this.ipV6Items[i].IPTo
var from1 = this.ipV6Items[j].IPFrom
var to1 = this.ipV6Items[j].IPTo
if from0 == from1 {
return bytes.Compare(to0[:], to1[:]) < 0
}
return bytes.Compare(from0[:], from1[:]) < 0
})
// 清理内存
this.regionMap = nil
return nil
}
func (this *ReaderV2) Lookup(ip net.IP) *QueryResult {
if ip == nil {
return &QueryResult{}
}
var isV4 = ip.To4() != nil
var resultItem any
if isV4 {
sort.Search(len(this.ipV4Items), func(i int) bool {
var item = this.ipV4Items[i]
if bytes.Compare(item.IPFrom[:], ip) <= 0 {
if bytes.Compare(item.IPTo[:], ip) >= 0 {
resultItem = item
return false
}
return false
}
return true
})
} else {
sort.Search(len(this.ipV6Items), func(i int) bool {
var item = this.ipV6Items[i]
if bytes.Compare(item.IPFrom[:], ip) <= 0 {
if bytes.Compare(item.IPTo[:], ip) >= 0 {
resultItem = item
return false
}
return false
}
return true
})
}
return &QueryResult{
item: resultItem,
meta: this.meta,
}
}
func (this *ReaderV2) Meta() *Meta {
return this.meta
}
func (this *ReaderV2) IPv4Items() []ipv4ItemV2 {
return this.ipV4Items
}
func (this *ReaderV2) IPv6Items() []ipv6ItemV2 {
return this.ipV6Items
}
func (this *ReaderV2) Destroy() {
this.meta = nil
this.regionMap = nil
this.ipV4Items = nil
this.ipV6Items = nil
}
// 分析数据
func (this *ReaderV2) parse(data []byte) (left []byte, err error) {
if len(data) == 0 {
return
}
for {
if len(data) == 0 {
break
}
var offset int
if data[0] == '|' {
offset = 1 + 8 + 1
} else if data[0] == '4' {
offset = 2 + 8 + 1
} else if data[0] == '6' {
offset = 2 + 32 + 1
}
var index = bytes.IndexByte(data[offset:], '\n')
if index >= 0 {
index += offset
var line = data[:index]
err = this.parseLine(line)
if err != nil {
return nil, err
}
data = data[index+1:]
} else {
left = data
break
}
}
return
}
// 单行分析
func (this *ReaderV2) parseLine(line []byte) error {
if len(line) == 0 {
return nil
}
const maxPieces = 8
var pieces []string
var offset int
if line[0] == '|' {
offset = 1 + 8 + 1
pieces = append(pieces, "", string(line[1:5]), string(line[5:9]))
} else if line[0] == '4' {
offset = 2 + 8 + 1
pieces = append(pieces, "", string(line[2:6]), string(line[6:10]))
} else if line[0] == '6' {
offset = 2 + 32 + 1
pieces = append(pieces, "6", string(line[2:18]), string(line[18:34]))
}
pieces = append(pieces, strings.Split(string(line[offset:]), "|")...)
var countPieces = len(pieces)
if countPieces < maxPieces { // 补足一行
for i := 0; i < maxPieces-countPieces; i++ {
pieces = append(pieces, "")
}
} else if countPieces > maxPieces {
return errors.New("invalid ip definition '" + string(line) + "'")
}
var version = pieces[0]
if len(version) == 0 {
version = "4"
}
if version != "4" && version != "6" {
return errors.New("invalid ip version '" + string(line) + "'")
}
// ip range
var ipFromV4 [4]byte
var ipToV4 [4]byte
var ipFromV6 [16]byte
var ipToV6 [16]byte
if version == "6" {
ipFromV6 = [16]byte([]byte(pieces[1]))
ipToV6 = [16]byte([]byte(pieces[2]))
} else {
ipFromV4 = [4]byte([]byte(pieces[1]))
ipToV4 = [4]byte([]byte(pieces[2]))
}
// country
var countryId uint16
if pieces[3] == "+" {
countryId = this.lastCountryId
} else {
countryId = uint16(this.decodeUint64(pieces[3]))
}
this.lastCountryId = countryId
var provinceId uint16
if pieces[4] == "+" {
provinceId = this.lastProvinceId
} else {
provinceId = uint16(this.decodeUint64(pieces[4]))
}
this.lastProvinceId = provinceId
// city
var cityId uint32
if pieces[5] == "+" {
cityId = this.lastCityId
} else {
cityId = uint32(this.decodeUint64(pieces[5]))
}
this.lastCityId = cityId
// town
var townId uint32
if pieces[6] == "+" {
townId = this.lastTownId
} else {
townId = uint32(this.decodeUint64(pieces[6]))
}
this.lastTownId = townId
// provider
var providerId uint16
if pieces[7] == "+" {
providerId = this.lastProviderId
} else {
providerId = uint16(this.decodeUint64(pieces[7]))
}
this.lastProviderId = providerId
var hash = HashRegion(countryId, provinceId, cityId, townId, providerId)
region, ok := this.regionMap[hash]
if !ok {
region = &ipRegion{
CountryId: countryId,
ProvinceId: provinceId,
CityId: cityId,
TownId: townId,
ProviderId: providerId,
}
this.regionMap[hash] = region
}
if version == "4" {
this.ipV4Items = append(this.ipV4Items, ipv4ItemV2{
IPFrom: ipFromV4,
IPTo: ipToV4,
Region: region,
})
} else {
this.ipV6Items = append(this.ipV6Items, ipv6ItemV2{
IPFrom: ipFromV6,
IPTo: ipToV6,
Region: region,
})
}
return nil
}
func (this *ReaderV2) decodeUint64(s string) uint64 {
if this.meta != nil && this.meta.Version == Version2 {
i, _ := strconv.ParseUint(s, 32, 64)
return i
}
i, _ := strconv.ParseUint(s, 10, 64)
return i
}