-
Notifications
You must be signed in to change notification settings - Fork 5
/
iterator.go
389 lines (363 loc) · 8.71 KB
/
iterator.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package iprange
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"math/big"
"net"
"strconv"
"strings"
)
const (
CidrMode = iota // CIDR
WideMode // 1.1.1.1-1.1.2.3
NarrowMode // 1-3.1-5.4.1-7
)
// RangeParseMate Range Info
type RangeParseMate struct {
s uint8 //start
e uint8 //end
}
// RangeClassMate IP Range of every byte
type RangeClassMate []RangeParseMate
// Iter Iterator
type Iter struct {
mode int // 模式
isIpv6 bool // 是否是ipv6
isIpv4 bool // 是否是ipv4
ipStr string // 填充后的ip字符串
lastIp net.IP // ip迭代空间
classmate RangeClassMate // ip范围限制信息
ipNet *net.IPNet // cidr 模式下的网段信息
sip net.IP // 开始IP
eip net.IP // 结束IP
done bool // 结束
totalNum uint64 // IP总数 max 64bit(return 0xffffffffffffffff if overflow)
}
func NewIter(ipStr string) (it *Iter, startIp net.IP, err error) {
it = &Iter{
mode: -1,
isIpv6: false,
isIpv4: false,
ipStr: "",
lastIp: nil,
classmate: nil,
ipNet: nil,
sip: nil,
eip: nil,
done: false,
totalNum: 0,
}
// IP判断和填充
if strings.Contains(ipStr, ".") { // 分段生成IPv4
it.isIpv4 = true
} else if strings.Contains(ipStr, ":") {
it.isIpv6 = true
// 填充缩写
// :: 扩展
var fill = func(ipStr string) string {
buf := strings.Builder{}
for i := strings.Count(ipStr, ":"); i < 8; i++ {
buf.WriteString(":0000")
}
ipStr = strings.Replace(ipStr, "::", buf.String()+":", 1)
buf.Reset()
return ipStr
}
if strings.Count(ipStr, "::") == 1 {
ipStr = fill(ipStr)
} else if strings.Count(ipStr, "::") == 2 && strings.Count(ipStr, "-") == 1 {
iL := strings.Split(ipStr, "-")
iL[0] = fill(iL[0])
iL[1] = fill(iL[1])
ipStr = strings.Join(iL, "-")
}
// 补零
ipv6C := strings.Split(ipStr, ":")
for i, v := range ipv6C {
ipv6D := strings.Split(v, "-")
for i2, v2 := range ipv6D {
for len(v2) < 4 {
v2 = "0" + v2
}
ipv6D[i2] = v2
}
ipv6C[i] = strings.Join(ipv6D, "-")
}
ipStr = strings.Join(ipv6C, ":")
}
it.ipStr = ipStr
if !it.isIpv4 && !it.isIpv6 {
return nil, nil, fmt.Errorf("not is ip")
}
// CidrMode
ip, ipNet, err := net.ParseCIDR(ipStr)
if err == nil {
it.sip = ip.Mask(ipNet.Mask)
it.ipNet = ipNet
it.mode = CidrMode
}
// WideMode
if it.mode == -1 && strings.Count(ipStr, "-") == 1 {
startIpStrList := strings.Split(ipStr, "-")
if len(startIpStrList) == 2 {
sip := net.ParseIP(startIpStrList[0])
eip := net.ParseIP(startIpStrList[1])
if sip == nil || eip == nil || len(sip) != len(eip) {
err = fmt.Errorf("WideMode parse ip err: %s", ipStr)
} else {
it.mode = WideMode
it.sip = sip
it.eip = eip
}
}
}
// NarrowMode
if it.mode == -1 {
var ipClasses []string
if it.isIpv4 { // 分段生成IPv4
ipClasses = strings.Split(ipStr, ".")
} else if it.isIpv6 { // 分段生成IPv6
ipClassesV6 := strings.Split(ipStr, ":")
if len(ipClassesV6) != 8 {
err = fmt.Errorf("NarrowMode ipv6 parse err %s", ipStr)
return
}
// 2001::1112-3334
// to ipClasses
// 20,01,...,11-33,12-34 (16个)
for _, v := range ipClassesV6 {
if len(v) == 4 {
ipClasses = append(ipClasses, v[:2])
ipClasses = append(ipClasses, v[2:])
} else if len(v) == 9 && strings.Contains(v, "-") {
ipClasses = append(ipClasses, v[:2]+"-"+v[5:7])
ipClasses = append(ipClasses, v[2:4]+"-"+v[7:])
}
}
}
// ipClasses to RangeParseMate
if len(ipClasses) == 4 || len(ipClasses) == 16 {
for _, v := range ipClasses {
l0 := strings.Split(v, "-") // range
var l0s uint64
if it.isIpv4 {
l0s, err = strconv.ParseUint(l0[0], 10, 8)
} else {
// ipv6 is hex
l0s, err = strconv.ParseUint(l0[0], 16, 8)
}
l0e := l0s // The default start and end are the same
if len(l0) > 2 || err != nil {
return nil, nil, err
}
if len(l0) == 2 {
if it.isIpv4 {
l0e, err = strconv.ParseUint(l0[1], 10, 8)
} else {
l0e, err = strconv.ParseUint(l0[1], 16, 8)
}
if err != nil {
return nil, nil, err
}
}
it.classmate = append(it.classmate, RangeParseMate{
s: uint8(l0s),
e: uint8(l0e),
})
}
//
_startIp := make(net.IP, len(it.classmate))
endIp := make(net.IP, len(it.classmate))
for i, v := range it.classmate {
_startIp[i] = v.s
endIp[i] = v.e
}
it.mode = NarrowMode
it.sip = _startIp
it.eip = endIp
}
}
if it.mode == -1 {
return nil, nil, fmt.Errorf("unknow mode")
}
// Avoid long ipv4 bytes
if it.sip.To4() != nil {
it.sip = it.sip.To4()
it.eip = it.eip.To4()
}
// dup copy sip to lastIp
dup := make(net.IP, len(it.sip))
copy(dup, it.sip)
it.lastIp = dup
it.totalNum = it.getTotalNum()
return it, it.sip, nil
}
func (it *Iter) Next() net.IP {
if !it.HasNext() {
return nil
}
switch it.mode {
case CidrMode:
inc(it.lastIp)
if !it.ipNet.Contains(it.lastIp) {
it.done = true
return nil
}
case WideMode:
inc(it.lastIp)
if bytes.Compare(it.eip, it.lastIp) < 0 {
it.done = true
return nil
}
case NarrowMode:
classInc(it.lastIp, it.classmate)
// 自增后置为初始值,说明到上限了
if bytes.Compare(it.sip, it.lastIp) == 0 {
it.done = true
return nil
}
default:
it.done = true
return nil
}
dup := make(net.IP, len(it.lastIp))
copy(dup, it.lastIp)
return dup
}
func (it *Iter) HasNext() bool {
return !it.done
}
// TotalNum Calculating the Total NUMBER of IP addresses
func (it *Iter) TotalNum() uint64 {
return it.totalNum
}
func (it *Iter) getTotalNum() uint64 {
switch it.mode {
case CidrMode:
ones, bits := it.ipNet.Mask.Size()
return uint64(math.Pow(2, float64(bits-ones)))
case WideMode:
if it.eip.To4() != nil {
return uint64(binary.BigEndian.Uint32(it.eip.To4()) - binary.BigEndian.Uint32(it.lastIp.To4()) + 1)
} else {
ret := big.NewInt(1)
ret = ret.Add(ret, new(big.Int).Sub(new(big.Int).SetBytes(it.eip), new(big.Int).SetBytes(it.lastIp)))
if ret.IsUint64() {
return ret.Uint64()
} else {
return 0xffffffffffffffff
}
}
case NarrowMode:
var ret = uint64(1)
for _, v := range it.classmate {
ret = ret * (uint64(v.e-v.s) + 1)
}
return ret
}
return 0
}
// GetIpByIndex ...
func (it *Iter) GetIpByIndex(index uint64) net.IP {
if index >= it.totalNum {
return nil
}
it.incByIndex(index)
return it.lastIp
}
// Contains Check whether the IP address is included
func (it *Iter) Contains(ip net.IP) bool {
if ip.To4() != nil {
ip = ip.To4()
}
switch it.mode {
case CidrMode:
return it.ipNet.Contains(ip)
case WideMode:
return bytes.Compare(it.eip, ip) >= 0 && bytes.Compare(it.sip, ip) <= 0
case NarrowMode:
if len(it.classmate) != len(ip) {
return false
}
for i, rangeParseMate := range it.classmate {
if rangeParseMate.s > ip[i] || rangeParseMate.e < ip[i] {
return false
}
}
return true
}
return false
}
// IP increment by index
func (it *Iter) incByIndex(index uint64) {
if it.classmate == nil {
if it.isIpv4 {
it.lastIp = it.lastIp.To4()
binary.BigEndian.PutUint32(it.lastIp, binary.BigEndian.Uint32(it.sip.To4())+uint32(index))
} else {
ret := new(big.Int).SetBytes(it.sip)
ret = ret.Add(ret, new(big.Int).SetUint64(index))
it.lastIp = ret.Bytes()
}
} else {
length := len(it.classmate)
ip := make([]byte, length)
rangeSpace := make([]uint, length)
// 每一位的空间容量
for i, rangeMate := range it.classmate {
rangeSpace[i] = uint(rangeMate.e-rangeMate.s) + 1
}
// transform 进位除余
carryBit := uint64(0) // 进位
var noFirst bool
for i := length - 1; i >= 0; i-- {
if rangeSpace[i] == 1 { // 如果空间为1,则不变
ip[i] = it.sip[i]
} else {
if !noFirst {
noFirst = true
carryBit = index
}
ip[i] = it.sip[i] + uint8(carryBit%uint64(rangeSpace[i]))
carryBit = uint64(uint8(carryBit / uint64(rangeSpace[i])))
}
}
it.lastIp = ip
}
}
// GenIpSet simple generate a set of ip
func GenIpSet(ipStr string) (outs []net.IP, err error) {
it, startIp, err := NewIter(ipStr)
if err != nil {
return
}
for nit := startIp; it.HasNext(); nit = it.Next() {
outs = append(outs, nit)
}
return
}
// IP increment
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
// IP Segmented(byte) increment, 有范围的IP自增 1-4.1-4.1-4.1-4 = 1.1.1.1-4.4.4.4
func classInc(ip net.IP, classMate RangeClassMate) {
for j := len(ip) - 1; j >= 0; j-- {
// 当前分段最大限制
if ip[j] >= classMate[j].e {
ip[j] = classMate[j].s // 归初始值
continue
}
ip[j]++
if ip[j] > 0 {
break
}
}
}