forked from mehrdadrad/mylg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ripe.go
321 lines (293 loc) · 8.8 KB
/
ripe.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
// Package ripe provides ASN and IP information
package ripe
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"sort"
"strings"
"sync"
"github.com/olekukonko/tablewriter"
"github.com/xianwangs/mylg/data"
)
const (
// RIPEAPI holds RIPE API URL
RIPEAPI = "https://stat.ripe.net"
// RIPEPrefixURL holds RIPE prefix path
RIPEPrefixURL = "/data/prefix-overview/data.json?max_related=50&resource="
// RIPEASNURL holds RIPE ASN path
RIPEASNURL = "/data/as-overview/data.json?resource=AS"
// RIPEGeoURL holds Geo path
RIPEGeoURL = "/data/geoloc/data.json?resource="
// RIPEMyIPURL holds myIP path
RIPEMyIPURL = "/data/whats-my-ip/data.json"
)
// Geo represents RIPE NCC Geo format
type Geo struct {
Status string
Data struct {
Locations []struct {
City string
Country string
Longitude float64
Latitude float64
}
}
}
// ASN represents ASN information
type ASN struct {
Number string
Data map[string]interface{}
GeoData map[string]interface{}
}
// Prefix represents prefix information
type Prefix struct {
Resource string
Data map[string]interface{}
GeoData Geo
}
// kv represents key/value(float64) in sort func
type kv struct {
key string
value float64
}
// location represents location information
type location struct {
City string `json:"city"`
Country string `json:"country"`
}
// Set sets the resource value
func (p *Prefix) Set(r string) {
p.Resource = r
}
// GetData gets prefix information from RIPE NCC
func (p *Prefix) GetData() bool {
if len(p.Resource) < 6 {
println("error: prefix invalid")
return false
}
resp, err := http.Get(RIPEAPI + RIPEPrefixURL + p.Resource)
if err != nil {
println(err.Error())
return false
}
if resp.StatusCode != 200 {
println("error: check your prefix")
return false
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &p.Data)
return true
}
// GetGeoData gets Geo information from RIPE NCC
func (p *Prefix) GetGeoData() error {
if len(p.Resource) < 6 {
return fmt.Errorf("prefix invalid")
}
resp, err := http.Get(RIPEAPI + RIPEGeoURL + p.Resource)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("check your prefix (HTTP code: %d) ", resp.StatusCode)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &p.GeoData)
return nil
}
// PrettyPrint print ASN information (holder)
func (p *Prefix) PrettyPrint() {
data, ok := p.Data["data"].(map[string]interface{})
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Prefix", "ASN", "Holder"})
if ok {
resource := data["resource"].(string)
asns := data["asns"].([]interface{})
for _, h := range asns {
holder := h.(map[string]interface{})["holder"].(string)
asn := h.(map[string]interface{})["asn"].(float64)
table.Append([]string{resource, fmt.Sprintf("%.0f", asn), holder})
}
}
table.Render()
}
// Set ASN
func (a *ASN) Set(r string) {
a.Number = r
}
// GetData gets ASN information from RIPE NCC
func (a *ASN) GetData() bool {
var (
wg sync.WaitGroup
rOV, rGeo bool
)
wg.Add(1)
go func() {
defer wg.Done()
rOV = a.GetOVData()
}()
wg.Add(1)
go func() {
defer wg.Done()
rGeo = a.GetGeoData()
}()
wg.Wait()
return rOV || rGeo
}
// GetOVData gets ASN overview information from RIPE NCC
func (a *ASN) GetOVData() bool {
if len(a.Number) < 2 {
println("error: AS number invalid")
return false
}
resp, err := http.Get(RIPEAPI + RIPEASNURL + a.Number)
if err != nil {
println(err.Error())
return false
}
if resp.StatusCode != 200 {
return false
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &a.Data)
return true
}
// GetGeoData gets Geo information from RIPE NCC
func (a *ASN) GetGeoData() bool {
if len(a.Number) < 2 {
println("error: AS number invalid")
return false
}
resp, err := http.Get(RIPEAPI + RIPEGeoURL + "AS" + a.Number)
if err != nil {
println(err.Error())
return false
}
if resp.StatusCode != 200 {
println("error: check your AS number")
return false
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &a.GeoData)
return true
}
// PrettyPrint print ASN information (holder)
func (a *ASN) PrettyPrint() {
var cols = make(map[string]float64)
overviewData, ok := a.Data["data"].(map[string]interface{})
if ok {
println(string(overviewData["holder"].(string)))
}
geoLocData, ok := a.GeoData["data"].(map[string]interface{})
if !ok {
return
}
locs := geoLocData["locations"].([]interface{})
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Location", "Covered %"})
for _, loc := range locs {
geoInfo := loc.(map[string]interface{})
cols[geoInfo["country"].(string)] = geoInfo["covered_percentage"].(float64)
}
for _, v := range sortMapFloat(cols) {
name := v.key
percent := v.value
uc := strings.Split(name, "-")
if country, ok := data.Country[uc[0]]; ok {
name = country
}
if len(uc) == 2 {
name = fmt.Sprintf("%s - %s", name, uc[1])
}
table.Append([]string{name, fmt.Sprintf("%.4f", percent)})
}
table.Render()
}
// IsASN checks if the key is a number
func IsASN(key string) bool {
m, err := regexp.MatchString(`^\d+$`, key)
if err != nil {
return false
}
return m
}
// IsIP return true if key is IPv4/6
func IsIP(key string) bool {
var regex = map[string]string{
"IPv4": `^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`,
"IPv6": `^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*`,
}
for _, rgx := range regex {
m, _ := regexp.MatchString(rgx, key)
if m {
return m
}
}
return false
}
// IsPrefix evaluates IPv(4/6) CIDR
func IsPrefix(key string) bool {
var regex = map[string]string{
"IPv4CIDR": `^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))$`,
"IPv6CIDR": `^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/(d|dd|1[0-1]d|12[0-8]))$`,
}
for _, rgx := range regex {
m, _ := regexp.MatchString(rgx, key)
if m {
return m
}
}
return false
}
// sortMapFloat sorts map[string]float64 w/ value
func sortMapFloat(m map[string]float64) []kv {
n := map[float64][]string{}
var (
a []float64
r []kv
)
for k, v := range m {
n[v] = append(n[v], k)
}
for k := range n {
a = append(a, k)
}
sort.Sort(sort.Reverse(sort.Float64Slice(a)))
for _, k := range a {
for _, s := range n[k] {
r = append(r, kv{s, k})
}
}
return r
}
// MyIPAddr gets public ip address
func MyIPAddr() (string, error) {
var (
d struct {
Status string
Data struct {
IP string
}
}
myIP string
)
resp, err := http.Get(RIPEAPI + RIPEMyIPURL)
if err != nil {
return myIP, err
}
if resp.StatusCode != 200 {
return myIP, fmt.Errorf("HTTP code: %d returned", resp.StatusCode)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &d)
myIP = d.Data.IP
return myIP, nil
}