/
pdb.go
227 lines (209 loc) · 4.87 KB
/
pdb.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
package peeringdb
import (
"encoding/json"
"fmt"
"github.com/olekukonko/tablewriter"
"io/ioutil"
"net/http"
"os"
"regexp"
"strconv"
"time"
"github.com/mehrdadrad/mylg/cli"
)
const (
// APINetIXLAN holds net ix lan API
APINetIXLAN = "https://www.peeringdb.com/api/netixlan"
// APINet holds net API
APINet = "https://www.peeringdb.com/api/net"
)
// A Peer represents peeringdb record
type Peer struct {
Name string `json:"name"`
ASN int `json:"asn"`
Status string `json:"status"`
Speed int `json:"speed"`
IPAddr4 string `json:"ipaddr4"`
IPAddr6 string `json:"ipaddr6"`
}
// A Net represents peeringdb net record
type Net struct {
Name string `json:"name"`
ASN int `json:"asn"`
WWW string `json:"website"`
Traffic string `json:"info_traffic"`
InfoType string `json:"info_type"`
Note string `json:"notes"`
}
// Peers represents peeringdb records
type Peers struct {
Data []Peer `json:"data"`
}
// Nets represents network information data
type Nets struct {
Data []Net `json:"data"`
}
// GetNetIXLAN fetchs netixlan data from peeringdb
func GetNetIXLAN() (interface{}, error) {
var peers Peers
resp, err := http.Get(APINetIXLAN)
if err != nil {
return peers, fmt.Errorf("peeringdb.com is unreachable (1)")
}
if resp.StatusCode != 200 {
return peers, fmt.Errorf("peeringdb.com returns none 200 HTTP code")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return peers, fmt.Errorf("peeringdb.com is unreachable (2) %s", err.Error())
}
json.Unmarshal(body, &peers)
return peers, nil
}
// GetNet fetchs net information from peeringdb
func GetNet() (interface{}, error) {
var (
nets Nets
res = make(map[string]Net)
)
resp, err := http.Get(APINet)
if err != nil {
return nil, fmt.Errorf("peeringdb.com is unreachable (1)")
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("peeringdb.com returns none 200 HTTP code")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("peeringdb.com is unreachable (2) %s", err.Error())
}
json.Unmarshal(body, &nets)
for _, n := range nets.Data {
res[strconv.Itoa(n.ASN)] = n
}
return res, nil
}
// cache manage caching for peeringdb data
func cache(r, typ string, data interface{}) (interface{}, bool) {
switch r {
case "write":
b, err := json.Marshal(data)
if err != nil {
return nil, false
}
err = ioutil.WriteFile("/tmp/mylg.pdb."+typ, b, 0644)
if err != nil {
return nil, false
}
case "read":
b, err := ioutil.ReadFile("/tmp/mylg.pdb." + typ)
if err != nil {
return nil, false
}
if typ == "ix" {
var res Peers
err := json.Unmarshal(b, &res)
if err != nil {
return nil, false
}
return res, true
}
// net type
var res map[string]Net
err = json.Unmarshal(b, &res)
if err != nil {
return nil, false
}
return res, true
case "validate":
f, err := os.Stat("/tmp/mylg.pdb." + typ)
if err != nil {
return nil, false
}
d := time.Since(f.ModTime())
if d.Hours() > 96 {
return nil, false
}
}
return nil, true
}
// printTable prints peeringdb data as table
func printTable(net Net, ixLan [][]string) {
println("The data provided from www.peeringdb.com")
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Traffic", "Type", "Web site", "Note"})
table.Append([]string{net.Name, net.Traffic, net.InfoType, net.WWW, net.Note})
table.Render()
table = tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Status", "Speed", "IPv4 Addr", "IPv6 Addr"})
for _, v := range ixLan {
table.Append(v)
}
table.Render()
}
// Search find a key through the records
func Search(key string) {
var (
result [][]string
ASN string
peers interface{}
nets interface{}
err error
)
key, flag := cli.Flag(key)
// help
if _, ok := flag["help"]; ok {
help()
return
}
if _, ok := cache("validate", "ix", nil); ok {
peers, _ = cache("read", "ix", nil)
} else {
peers, err = GetNetIXLAN()
cache("write", "ix", peers)
}
if _, ok := cache("validate", "net", nil); ok {
nets, _ = cache("read", "net", nil)
} else {
nets, err = GetNet()
cache("write", "net", nets)
}
if err != nil {
println(err.Error())
return
}
switch {
case IsASN(key):
ASN = key
for _, p := range peers.(Peers).Data {
if strconv.Itoa(p.ASN) == key {
result = append(result, []string{p.Name, p.Status, strconv.Itoa(p.Speed), p.IPAddr4, p.IPAddr6})
}
}
}
if len(result) > 0 {
n := nets.(map[string]Net)
printTable(n[ASN], result)
} else {
println("there is not any information @ peeringdb")
}
}
// IsASN checks if the key is number
func IsASN(key string) bool {
m, err := regexp.MatchString(`^(?i)\d{2,5}`, key)
if err != nil {
return false
}
return m
}
// help represents guide to user
func help() {
println(`
usage:
peering AS.Number
example:
peering 577
`)
}