-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fetch ranges from AS via bgp tools (#8)
* new internal util function for sources: `getRangesForAsn`, which allows to get all ranges (CIDRs) for a given ASN using <https://bgp.tools/table.txt> Following providers are now sourced from this technique * Alibaba * Ovh * Scaleway * Tencent * Ucloud
- Loading branch information
Showing
18 changed files
with
2,567 additions
and
3,459 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package source | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
|
@@ -9,7 +10,10 @@ import ( | |
"net" | ||
"net/http" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/Escape-Technologies/cloudfinder/internal/log" | ||
) | ||
|
||
const defaultHTTPTimeout = 30 * time.Second | ||
|
@@ -128,3 +132,67 @@ func isPrivateNetwork(n *net.IPNet) bool { | |
} | ||
return false | ||
} | ||
|
||
/// BGP TOOLS | ||
|
||
// ensure we fill the map only once | ||
var bgpToolsMutex = sync.Mutex{} | ||
|
||
// ASN -> CIDR | ||
var bgpToolsAsnRanges map[string][]*IPRange | ||
var bgpToolsTableURL = "https://bgp.tools/table.txt" | ||
|
||
// Fetches https://bgp.tools/table.txt and parses it into the ASN -> CIDR map | ||
func getRangesForAsn(asn string) []*IPRange { | ||
bgpToolsMutex.Lock() | ||
if bgpToolsAsnRanges == nil { | ||
bgpToolsAsnRanges = make(map[string][]*IPRange) | ||
|
||
// Fill the map | ||
log.Info("Fetching AS infos from %s", bgpToolsTableURL) | ||
req, _ := http.NewRequest(http.MethodGet, bgpToolsTableURL, nil) // nolint: noctx | ||
// bgp tools requires a descriptive user agent in case the program gets out of control | ||
req.Header.Add("user-agent", "https://github.com/Escape-Technologies/cloudfinder - [email protected]") | ||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
log.Fatal("Error getting bgp tools table", err) | ||
} | ||
if res.StatusCode != http.StatusOK { | ||
err := fmt.Errorf("status code: %d", res.StatusCode) | ||
log.Fatal("Error getting bgp tools table", err) | ||
} | ||
|
||
scanner := bufio.NewScanner(res.Body) | ||
// Read lines | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
// Line is formatted as "<CIDR> <ASN>" | ||
x := strings.Split(line, " ") | ||
if len(x) != 2 { // nolint: mnd | ||
continue | ||
} | ||
cidr, asn := x[0], x[1] | ||
|
||
n, cat := ParseCIDR(cidr) | ||
// Skip private networks | ||
if isPrivateNetwork(n) { | ||
continue | ||
} | ||
// Fill map | ||
if _, ok := bgpToolsAsnRanges[asn]; !ok { | ||
bgpToolsAsnRanges[asn] = make([]*IPRange, 0) | ||
} | ||
bgpToolsAsnRanges[asn] = append(bgpToolsAsnRanges[asn], &IPRange{ | ||
Network: n, | ||
Cat: cat, | ||
}) | ||
} | ||
res.Body.Close() | ||
log.Info("Got %d AS infos", len(bgpToolsAsnRanges)) | ||
} | ||
bgpToolsMutex.Unlock() | ||
if val, ok := bgpToolsAsnRanges[asn]; ok { | ||
return val | ||
} | ||
return []*IPRange{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
56bb287f7dbb22a8ba1ccad5e16fe86e8bd3632de2a056b37ed939cd3692ab7f | ||
47c10bda97e1961c363626868c7aa32aace0cff5557877bbdcd0ea48a506804e |
Binary file not shown.
Binary file not shown.
Oops, something went wrong.