-
Notifications
You must be signed in to change notification settings - Fork 1
/
bgp.go
232 lines (214 loc) · 6.6 KB
/
bgp.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
// Copyright (C) 2024 Opsmate, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// Except as contained in this notice, the name(s) of the above copyright
// holders shall not be used in advertising or otherwise to promote the
// sale, use or other dealings in this Software without prior written
// authorization.
package main
import (
"context"
"encoding/csv"
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"net/http"
"net/netip"
"strconv"
"strings"
"sync"
"time"
"github.com/kentik/patricia"
"github.com/kentik/patricia/uint32_tree"
)
var bgptoolsClient = &http.Client{
Timeout: 30 * time.Second,
}
type autonomousSystem struct {
Number uint32
Name string
}
func (as *autonomousSystem) String() string {
str := fmt.Sprintf("AS%d", as.Number)
if as.Name != "" {
str += fmt.Sprintf(" (%s)", as.Name)
}
return str
}
func (as *autonomousSystem) HTML() template.HTML {
str := fmt.Sprintf(`<a href="https://bgp.tools/as/%d">AS%d</a>`, as.Number, as.Number)
if as.Name != "" {
str += fmt.Sprintf(" (%s)", template.HTMLEscapeString(as.Name))
}
return template.HTML(str)
}
var bgpData struct {
mu sync.Mutex
asNames map[uint32]string
v4prefixes *uint32_tree.TreeV4
v6prefixes *uint32_tree.TreeV6
}
func getAutonomousSystems(addrString string) []autonomousSystem {
if addr, err := netip.ParseAddr(addrString); err == nil {
return getAutonomousSystemsForAddr(addr)
} else {
return nil
}
}
func getAutonomousSystemsForAddr(addr netip.Addr) []autonomousSystem {
bgpData.mu.Lock()
defer bgpData.mu.Unlock()
var asns []uint32
if addr.Is4() && bgpData.v4prefixes != nil {
_, asns = bgpData.v4prefixes.FindDeepestTags(patricia.NewIPv4AddressFromBytes(addr.AsSlice(), 32))
} else if addr.Is6() && bgpData.v6prefixes != nil {
_, asns = bgpData.v6prefixes.FindDeepestTags(patricia.NewIPv6Address(addr.AsSlice(), 128))
}
ases := make([]autonomousSystem, len(asns))
for i, asn := range asns {
ases[i].Number = asn
ases[i].Name = bgpData.asNames[asn]
}
return ases
}
func downloadASNames(ctx context.Context) (map[uint32]string, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://bgp.tools/asns.csv", nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", userAgentString)
resp, err := bgptoolsClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
respBytes, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("%s response from %s: %s", resp.Status, req.URL, string(respBytes))
}
reader := csv.NewReader(resp.Body)
reader.ReuseRecord = true
if _, err := reader.Read(); err != nil {
return nil, fmt.Errorf("error reading CSV header from %s: %w", req.URL, err)
}
names := make(map[uint32]string, 100000)
for {
record, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("error reading CSV response from %s: %w", req.URL, err)
}
if minFields := 3; len(record) < minFields {
return nil, fmt.Errorf("error reading CSV response from %s: expected at least %d fields but got %d instead", req.URL, minFields, len(record))
}
if record[0] == "AS-1" {
continue
}
asn, err := strconv.ParseUint(strings.TrimPrefix(record[0], "AS"), 10, 32)
if err != nil {
return nil, fmt.Errorf("error parsing ASN in CSV from %s: %w", req.URL, err)
}
names[uint32(asn)] = record[1]
}
return names, nil
}
func refreshASNames(ctx context.Context) error {
names, err := downloadASNames(ctx)
if err != nil {
return fmt.Errorf("error downloading autonomous system names: %w", err)
}
bgpData.mu.Lock()
defer bgpData.mu.Unlock()
bgpData.asNames = names
return nil
}
func refreshASNamesPeriodically() {
for {
if err := refreshASNames(context.Background()); err != nil {
log.Print(err)
time.Sleep(1 * time.Hour)
} else {
time.Sleep(24 * time.Hour)
}
}
}
func downloadPrefixes(ctx context.Context) (*uint32_tree.TreeV4, *uint32_tree.TreeV6, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://bgp.tools/table.jsonl", nil)
if err != nil {
return nil, nil, err
}
req.Header.Set("User-Agent", userAgentString)
resp, err := bgptoolsClient.Do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
respBytes, _ := io.ReadAll(resp.Body)
return nil, nil, fmt.Errorf("%s response from %s: %s", resp.Status, req.URL, string(respBytes))
}
decoder := json.NewDecoder(resp.Body)
v4prefixes := uint32_tree.NewTreeV4()
v6prefixes := uint32_tree.NewTreeV6()
for {
var record struct {
CIDR netip.Prefix
ASN uint32
}
if err := decoder.Decode(&record); err == io.EOF {
break
} else if err != nil {
return nil, nil, fmt.Errorf("error decoding JSON from %s: %w", req.URL, err)
}
if record.CIDR.Addr().Is4() {
addr := patricia.NewIPv4AddressFromBytes(record.CIDR.Addr().AsSlice(), uint(record.CIDR.Bits()))
v4prefixes.Add(addr, record.ASN, nil)
} else if record.CIDR.Addr().Is6() {
addr := patricia.NewIPv6Address(record.CIDR.Addr().AsSlice(), uint(record.CIDR.Bits()))
v6prefixes.Add(addr, record.ASN, nil)
}
}
return v4prefixes, v6prefixes, nil
}
func refreshPrefixes(ctx context.Context) error {
v4prefixes, v6prefixes, err := downloadPrefixes(ctx)
if err != nil {
return fmt.Errorf("error downloading BGP prefixes: %w", err)
}
bgpData.mu.Lock()
defer bgpData.mu.Unlock()
bgpData.v4prefixes = v4prefixes
bgpData.v6prefixes = v6prefixes
return nil
}
func refreshPrefixesPeriodically() {
for {
if err := refreshPrefixes(context.Background()); err != nil {
log.Print(err)
time.Sleep(30 * time.Minute)
} else {
time.Sleep(2 * time.Hour)
}
}
}