-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
119 lines (102 loc) · 2.19 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"math"
"net"
"sync"
)
func main() {
country := flag.String("c", "", "2 letters string of the country (ISO 3166)")
ipquery := flag.String("q", "", "ip address to which to resolve country")
hostscount := flag.Bool("n", false, "given country return possible hosts count (exclude network and broadcast addresses)")
CreateCacheDir()
var data []*Records
for records := range retrieveData() {
data = append(data, records)
}
flag.Parse()
query := Query{data, *country, *ipquery, *hostscount}
if query.IsCountryQuery() {
results := query.matchOnCountry()
if query.hostscount {
var count int
for _, r := range results {
ones, size := r.Mask.Size()
mask := size - ones
if mask > 0 {
count = count + int((math.Pow(2, float64(mask)) - 2))
}
}
fmt.Println(count)
} else {
for _, r := range results {
fmt.Println(r)
}
}
} else {
for _, r := range query.matchOnIp() {
fmt.Println(r)
}
}
}
type Query struct {
data []*Records
country string
ipstring string
hostscount bool
}
func (q *Query) IsCountryQuery() bool {
return q.country != ""
}
func (q *Query) matchOnCountry() []*net.IPNet {
if q.country == "" {
flag.Usage()
}
var results []*net.IPNet
for _, region := range q.data {
for _, iprecord := range region.Ips {
if iprecord.Cc == q.country && iprecord.Type == IPv4 {
results = append(results, iprecord.Net()...)
}
}
}
return results
}
func (q *Query) matchOnIp() []string {
if q.ipstring == "" {
flag.Usage()
}
var results []string
for _, region := range q.data {
for _, iprecord := range region.Ips {
for _, ipnet := range iprecord.Net() {
if ipnet.Contains(net.ParseIP(q.ipstring)) {
results = append(results, fmt.Sprintf("%s %s", iprecord.Cc, ipnet))
}
}
}
}
return results
}
func retrieveData() chan *Records {
var wg sync.WaitGroup
ch := make(chan *Records)
wg.Add(len(AllProviders))
for _, provider := range AllProviders {
go func(p Provider) {
records, err := NewReader(p.GetData()).Read()
if err != nil {
log.Fatal(err)
}
ch <- records
wg.Done()
}(provider)
}
go func() {
wg.Wait()
close(ch)
}()
return ch
}