-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanchors.go
302 lines (256 loc) · 6.99 KB
/
anchors.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
/*
(C) 2022 Robert Kisteleki & RIPE NCC
See LICENSE file for the license.
*/
package goat
import (
"encoding/json"
"fmt"
"net/netip"
"net/url"
)
// Anchor object, as it comes from the API
type Anchor struct {
ID uint `json:"id"`
Address4 *netip.Addr `json:"ip_v4"`
ASN4 *uint `json:"as_v4"`
IPv4Gateway *netip.Addr `json:"ip_v4_gateway"`
IPv4Netmask *netip.Addr `json:"ip_v4_netmask"`
Address6 *netip.Addr `json:"ip_v6"`
ASN6 *uint `json:"as_v6"`
IPv6Gateway *netip.Addr `json:"ip_v6_gateway"`
IPv6Netmask *netip.Addr `json:"ip_v6_netmask"`
FQDN string `json:"fqdn"`
ProbeID uint `json:"probe"`
CountryCode string `json:"country"`
City string `json:"city"`
Company string `json:"company"`
IPv4Only bool `json:"is_ipv4_only"`
Disabled bool `json:"is_disabled"`
NicHandle string `json:"nic_handle"`
Location Geolocation `json:"geometry"`
Type string `json:"type"`
TLSARecord string `json:"tlsa_record"`
LiveSince *uniTime `json:"date_live"`
HardwareVersion uint `json:"hardware_version"`
}
type AsyncAnchorResult struct {
Anchor Anchor
Error error
}
// Translate the anchor version (code) into something more understandable
func (anchor *Anchor) decodeHardwareVersion() string {
switch anchor.HardwareVersion {
case 1:
return "1"
case 2:
return "2"
case 3:
return "3"
case 99:
return "VM"
default:
return "?"
}
}
// ShortString produces a short textual description of the anchor
func (anchor *Anchor) ShortString() string {
text := fmt.Sprintf("%d\t%d\t%s\t%s\t%s",
anchor.ID,
anchor.ProbeID,
anchor.CountryCode,
anchor.City,
anchor.FQDN,
)
text += valueOrNA("AS", false, anchor.ASN4)
text += valueOrNA("AS", false, anchor.ASN6)
text += fmt.Sprintf("\t%v", anchor.Location.Coordinates)
return text
}
// LongString produces a longer textual description of the anchor
func (anchor *Anchor) LongString() string {
text := anchor.ShortString()
text += valueOrNA("", false, anchor.Address4)
text += valueOrNA("", false, anchor.Address6)
if anchor.NicHandle != "" {
text += "\t" + anchor.NicHandle
} else {
text += "\tN/A"
}
text += fmt.Sprintf("\t\"%s\" %v %v %s",
anchor.Company,
anchor.IPv4Only,
anchor.Disabled,
anchor.decodeHardwareVersion(),
)
return text
}
// the API paginates; this describes one such page
type anchorListingPage struct {
Count uint `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Anchors []Anchor `json:"results"`
}
// AnchorFilter struct holds specified filters and other options
type AnchorFilter struct {
params url.Values
id uint
limit uint
verbose bool
}
// NewAnchorFilter prepares a new anchor filter object
func NewAnchorFilter() AnchorFilter {
filter := AnchorFilter{}
filter.params = url.Values{}
return filter
}
// Verboe sets verbosity
func (filter *AnchorFilter) Verbose(verbose bool) {
filter.verbose = verbose
}
// FilterID filters by a particular anchor ID
func (filter *AnchorFilter) FilterID(id uint) {
filter.id = id
}
// FilterCountry filters by a country code (ISO3166-1 alpha-2)
func (filter *AnchorFilter) FilterCountry(cc string) {
filter.params.Add("country", cc)
}
// FilterSearch filters within the fields `city`, `fqdn` and `company`
func (filter *AnchorFilter) FilterSearch(text string) {
filter.params.Add("search", text)
}
// FilterASN4 filters for an ASN in IPv4 space
func (filter *AnchorFilter) FilterASN4(as uint) {
filter.params.Add("as_v4", fmt.Sprint(as))
}
// FilterASN6 filters for an ASN in IPv6 space
func (filter *AnchorFilter) FilterASN6(as uint) {
filter.params.Add("as_v6", fmt.Sprint(as))
}
// Limit limits the number of result retrieved
func (filter *AnchorFilter) Limit(max uint) {
filter.limit = max
}
// Verify sanity of applied filters
func (filter *AnchorFilter) verifyFilters() error {
if filter.params.Has("country") {
cc := filter.params.Get("country")
// TODO: properly verify country code
if len(cc) != 2 {
return fmt.Errorf("invalid country code")
}
}
return nil
}
// GetAnchorCount returns the count of anchors by filtering
func (filter *AnchorFilter) GetAnchorCount() (
count uint,
err error,
) {
// sanity checks - late in the process, but not too late
err = filter.verifyFilters()
if err != nil {
return
}
// counting needs application of the specified filters
query := apiBaseURL + "anchors/?" + filter.params.Encode()
resp, err := apiGetRequest(filter.verbose, query, nil)
if err != nil {
return 0, err
}
defer resp.Body.Close()
// grab and store the actual content
var page anchorListingPage
err = json.NewDecoder(resp.Body).Decode(&page)
if err != nil {
return 0, err
}
// the only really important data point is the count
return page.Count, nil
}
// GetAnchors returns a bunch of anchors by filtering
// Results (or an error) appear on a channel
func (filter *AnchorFilter) GetAnchors(
anchors chan AsyncAnchorResult,
) {
defer close(anchors)
// special case: a specific ID was "filtered"
if filter.id != 0 {
anchor, err := GetAnchor(filter.verbose, filter.id)
if err != nil {
anchors <- AsyncAnchorResult{Anchor{}, err}
return
}
anchors <- AsyncAnchorResult{*anchor, nil}
return
}
// sanity checks - late in the process, but not too late
err := filter.verifyFilters()
if err != nil {
anchors <- AsyncAnchorResult{Anchor{}, err}
return
}
query := apiBaseURL + "anchors/?" + filter.params.Encode()
resp, err := apiGetRequest(filter.verbose, query, nil)
// results are paginated with next= (and previous=)
var total uint = 0
for {
if err != nil {
anchors <- AsyncAnchorResult{Anchor{}, err}
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
anchors <- AsyncAnchorResult{Anchor{}, err}
return
}
// grab and store the actual content
var page anchorListingPage
err = json.NewDecoder(resp.Body).Decode(&page)
if err != nil {
anchors <- AsyncAnchorResult{Anchor{}, err}
}
// return items while observing the limit
for _, anchor := range page.Anchors {
anchors <- AsyncAnchorResult{anchor, nil}
total++
if total >= filter.limit {
return
}
}
// no next page => we're done
if page.Next == "" {
break
}
// just follow the next link
resp, err = apiGetRequest(filter.verbose, page.Next, nil)
}
}
// GetAnchor retrieves data for a single anchor, by ID
// returns anchor, _ if an anchor was found
// returns nil, _ if an anchor was not found
// returns _, err on error
func GetAnchor(
verbose bool,
id uint,
) (
anchor *Anchor,
err error,
) {
query := fmt.Sprintf("%sanchors/%d/", apiBaseURL, id)
resp, err := apiGetRequest(verbose, query, nil)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, parseAPIError(resp)
}
// grab and store the actual content
err = json.NewDecoder(resp.Body).Decode(&anchor)
if err != nil {
return
}
return
}