-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsource.go
More file actions
87 lines (72 loc) · 1.6 KB
/
source.go
File metadata and controls
87 lines (72 loc) · 1.6 KB
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
package main
type IPSource interface {
IPs() <-chan string
Count() int
}
type FileSource struct {
path string
ips []string
}
func NewFileSource(path string) (*FileSource, error) {
ips, err := loadLines(path)
if err != nil {
return nil, err
}
return &FileSource{path: path, ips: ips}, nil
}
func (s *FileSource) IPs() <-chan string {
return sliceToChannel(s.ips)
}
func (s *FileSource) Count() int {
return len(s.ips)
}
type DNSListSource struct {
country string
ips []string
}
func NewDNSListSource(dataDir, country string) (*DNSListSource, error) {
ips, err := LoadKnownDNS(dataDir, country)
if err != nil {
return nil, err
}
return &DNSListSource{country: country, ips: ips}, nil
}
func (s *DNSListSource) IPs() <-chan string {
return sliceToChannel(s.ips)
}
func (s *DNSListSource) Count() int {
return len(s.ips)
}
type CIDRSource struct {
country string
mode string
blocks []string
}
func NewCIDRSource(dataDir, country, mode string) (*CIDRSource, error) {
if !CIDRBlocksExist(dataDir, country) {
if err := DownloadCIDRBlocks(dataDir, country); err != nil {
return nil, err
}
}
blocks, err := LoadCIDRBlocks(dataDir, country)
if err != nil {
return nil, err
}
return &CIDRSource{country: country, mode: mode, blocks: blocks}, nil
}
func (s *CIDRSource) IPs() <-chan string {
return ExpandCIDR(s.blocks, s.mode)
}
func (s *CIDRSource) Count() int {
return CountCIDRIPs(s.blocks, s.mode)
}
func sliceToChannel(ips []string) <-chan string {
ch := make(chan string, len(ips))
go func() {
defer close(ch)
for _, ip := range ips {
ch <- ip
}
}()
return ch
}