-
Notifications
You must be signed in to change notification settings - Fork 46
/
hosts_parser.go
154 lines (129 loc) · 2.54 KB
/
hosts_parser.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
package hostsparser
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/yaklang/yaklang/common/utils"
"net"
"strings"
)
type hostsBlock interface {
Size() int
Contains(raw string) bool
Hosts() chan string
}
type stringBlock struct {
ctx context.Context
hostsBlock
data string
}
func newStringBlock(ctx context.Context, raw string) *stringBlock {
return &stringBlock{
ctx: ctx,
data: raw,
}
}
func (s *stringBlock) Size() int {
return 1
}
func (s *stringBlock) Contains(raw string) bool {
return raw == s.data
}
func (s *stringBlock) Hosts() chan string {
c := make(chan string)
go func() {
defer close(c)
select {
case <-s.ctx.Done():
case c <- s.data:
}
}()
return c
}
func newCIDRBlock(ctx context.Context, raw string) (*ipRangeBlock, error) {
start, netBlock, err := net.ParseCIDR(raw)
if err != nil {
return nil, utils.Errorf("parse cidr"+
"[%v] failed: %s", raw, err)
}
if start.To4() == nil {
return nil, errors.Errorf("ipv6 is not implemented")
}
low, err := utils.IPv4ToUint32(netBlock.IP)
if err != nil {
return nil, errors.Errorf("parse ip[%v] to int failed: %s", netBlock.IP, err)
}
ones, lent := netBlock.Mask.Size()
if lent < ones {
return nil, errors.Errorf("BUG: mask invalid: %s", raw)
}
var size = (1 << uint(lent-ones)) - 1
return newIPRangeBlock(ctx, fmt.Sprintf("%v-%v", netBlock.IP, utils.InetNtoA(int64(low)+int64(size))))
}
type HostsParser struct {
hostsBlock
ctx context.Context
Blocks []hostsBlock
}
func NewHostsParser(ctx context.Context, raw string) *HostsParser {
var blocks []hostsBlock
for _, i := range strings.Split(raw, ",") {
b, _ := newIPRangeBlock(ctx, i)
if b != nil {
blocks = append(blocks, b)
continue
}
b, _ = newCIDRBlock(ctx, i)
if b != nil {
blocks = append(blocks, b)
continue
}
s := newStringBlock(ctx, i)
blocks = append(blocks, s)
}
return &HostsParser{
ctx: ctx,
Blocks: blocks,
}
}
func (h *HostsParser) Size() int {
ret := 0
for _, b := range h.Blocks {
ret += b.Size()
}
return ret
}
func (h *HostsParser) Contains(raw string) bool {
for _, b := range h.Blocks {
if b.Contains(raw) {
return true
}
}
return false
}
func (h *HostsParser) Hosts() chan string {
c := make(chan string)
go func() {
defer close(c)
GEN:
for _, b := range h.Blocks {
outC := b.Hosts()
GEN2:
for {
select {
case <-h.ctx.Done():
break GEN
default:
}
select {
case data, ok := <-outC:
if !ok {
break GEN2
}
c <- data
}
}
}
}()
return c
}