forked from mellowdrifter/go-bgpstuff.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbgpstuff.go
331 lines (273 loc) · 6.77 KB
/
bgpstuff.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
322
323
324
325
326
327
328
329
330
331
package bgpstuff
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"strconv"
"strings"
"time"
"github.com/mellowdrifter/bogons"
"golang.org/x/time/rate"
)
const (
version = "1.0.0"
liveapi = "https://bgpstuff.net"
testapi = "https://test.bgpstuff.net"
)
var (
errInvalidIP = errors.New("invalid IP")
errInvalidASN = errors.New("invalid AS Number")
rpm = 30 // requests per minute
)
// Client is a client to the bgpstuff.net REST API
type Client struct {
Loc string
limiter *rate.Limiter
api string
ASNames map[int]string
Invalids map[int][]*net.IPNet
}
// NewBGPClient return a pointer to a new client
// TODO: Hate setting testing here...
func NewBGPClient(testing bool) *Client {
r := rate.Every(time.Minute / time.Duration(rpm))
limit := rate.NewLimiter(r, rpm)
var api string
if testing {
api = testapi
} else {
api = liveapi
}
return &Client{
limiter: limit,
api: api,
}
}
func newHTTPClient(timeout time.Duration) *http.Client {
return &http.Client{
Timeout: timeout,
}
}
func (c *Client) getURI(urls []string) string {
var uri strings.Builder
uri.WriteString(c.api)
for _, v := range urls {
uri.WriteString("/")
uri.WriteString(v)
}
return uri.String()
}
// getRequest will take a handler and any arugments and request
// a response from the bgpstuff.net API. Timeouts are set to 5 seconds
// to prevent hanging connections.
func (c Client) getRequest(urls ...string) (*response, error) {
if err := c.limiter.Wait(context.Background()); err != nil {
return nil, err
}
client := newHTTPClient(time.Second * 8)
uri := c.getURI(urls)
re, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
re.Header.Set("Content-Type", "application/json")
re.Header.Set("User-Agent", fmt.Sprintf("go-bgpstuff.net/%s", version))
res, err := client.Do(re)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("received status: %s (%d)", http.StatusText(res.StatusCode), res.StatusCode)
}
defer res.Body.Close()
var resp response
if err := resp.decodeJSON(res.Body); err != nil {
return &resp, err
}
return &resp, nil
}
// GetRoute uses the /route handler
func (c *Client) GetRoute(ip string) (*net.IPNet, error) {
if !bogons.ValidPublicIP(ip) {
return nil, errInvalidIP
}
p := net.ParseIP(ip)
resp, err := c.getRequest("route", p.String())
if err != nil {
return nil, err
}
// Response could be no route.
if resp.Data.Route == "" {
return nil, nil
}
// TODO: stop returning "/0"
if resp.Data.Route == "/0" {
return nil, err
}
_, ipnet, err := net.ParseCIDR(resp.Data.Route)
if err != nil {
return nil, err
}
return ipnet, nil
}
// GetOrigin uses the /origin handler.
func (c *Client) GetOrigin(ip string) (int, error) {
if !bogons.ValidPublicIP(ip) {
return 0, errInvalidIP
}
p := net.ParseIP(ip)
resp, err := c.getRequest("origin", p.String())
if err != nil {
return 0, err
}
return resp.Data.Origin, nil
}
func getASPathFromResponse(res *response) ([]int, []int) {
if len(res.Data.ASPath) == 0 {
return nil, nil
}
path := make([]int, 0, len(res.Data.ASPath))
for _, v := range res.Data.ASPath {
i, _ := strconv.Atoi(v)
path = append(path, i)
}
if len(res.Data.ASSet) > 0 {
as := make([]int, 0, len(res.Data.ASSet))
for _, v := range res.Data.ASSet {
i, _ := strconv.Atoi(v)
as = append(as, i)
}
return path, as
}
return path, nil
}
// GetASPath uses the /aspath handler.
func (c *Client) GetASPath(ip string) ([]int, []int, error) {
if !bogons.ValidPublicIP(ip) {
return nil, nil, errInvalidIP
}
p := net.ParseIP(ip)
resp, err := c.getRequest("aspath", p.String())
if err != nil {
return nil, nil, err
}
paths, sets := getASPathFromResponse(resp)
return paths, sets, nil
}
// GetROA uses the /roa handler.
func (c *Client) GetROA(ip string) (string, error) {
if !bogons.ValidPublicIP(ip) {
return "", errInvalidIP
}
p := net.ParseIP(ip)
resp, err := c.getRequest("roa", p.String())
if err != nil {
return "", err
}
// If there is no origin, there is no prefix ROA to check.
if resp.Data.Origin == 0 {
return "", nil
}
return resp.Data.ROA, nil
}
// GetASName uses the /asname handler
func (c *Client) GetASName(asn int) (string, error) {
if !bogons.ValidPublicASN(uint32(asn)) {
return "", errInvalidASN
}
// Check asnames if it has the entry
if len(c.ASNames) > 1 {
if name, ok := c.ASNames[asn]; ok {
return name, nil
}
return "", nil
}
resp, err := c.getRequest("asname", fmt.Sprint(asn))
if err != nil {
return "", err
}
return resp.Data.ASName, nil
}
// GetASNames uses the /asnames handler
func (c *Client) GetASNames() error {
c.ASNames = make(map[int]string)
resp, err := c.getRequest("asnames")
if err != nil {
return err
}
for _, v := range resp.Data.ASNames {
c.ASNames[int(v.ASN)] = v.ASName
}
return nil
}
// GetInvalids grabs all current invalids and populates c.Invalids
func (c *Client) GetInvalids() error {
c.Invalids = make(map[int][]*net.IPNet)
resp, err := c.getRequest("invalids")
if err != nil {
return err
}
for _, v := range resp.Data.Invalids {
prefixes := make([]*net.IPNet, 0, len(v.Prefixes))
for _, prefix := range v.Prefixes {
_, ipnet, err := net.ParseCIDR(prefix)
if err != nil {
return err
}
prefixes = append(prefixes, ipnet)
}
c.Invalids[int(v.ASN)] = prefixes
}
return nil
}
// GetInvalid implements the /invalid handler
func (c *Client) GetInvalid(asn int) ([]*net.IPNet, error) {
if !bogons.ValidPublicASN(uint32(asn)) {
return nil, errInvalidASN
}
if c.Invalids == nil {
return nil, fmt.Errorf("invalids is empty, run GetInvalids() first")
}
return c.Invalids[asn], nil
}
// GetSourced implements the /sourced handler
func (c *Client) GetSourced(asn int) ([]*net.IPNet, int, int, error) {
if !bogons.ValidPublicASN(uint32(asn)) {
return nil, 0, 0, errInvalidASN
}
resp, err := c.getRequest("sourced", fmt.Sprint(asn))
if err != nil {
return nil, 0, 0, err
}
prefixes := make([]*net.IPNet, 0, len(resp.Data.Sourced.Prefixes))
for _, v := range resp.Data.Sourced.Prefixes {
_, prefix, err := net.ParseCIDR(v)
if err != nil {
return nil, 0, 0, err
}
prefixes = append(prefixes, prefix)
}
return prefixes, resp.Data.Sourced.Ipv4, resp.Data.Sourced.Ipv6, nil
}
// GetTotals implements the /totals handler
func (c *Client) GetTotals() (int, int, error) {
resp, err := c.getRequest("totals")
if err != nil {
return 0, 0, err
}
return resp.Data.Totals.Ipv4, resp.Data.Totals.Ipv6, nil
}
// GetGeoIP uses the /geoip handler.
func (c *Client) GetGeoIP(ip string) (*Location, error) {
if !bogons.ValidPublicIP(ip) {
return nil, errInvalidIP
}
p := net.ParseIP(ip)
resp, err := c.getRequest("geoip", p.String())
if err != nil {
return nil, err
}
return &resp.Data.GeoIP, nil
}