Add asinfo package for AS number information lookups
- Created pkg/asinfo with embedded AS data from ipverse/asn-info - Provides fast lookups by ASN with GetDescription() and GetHandle() - Includes Search() functionality for finding AS by name/handle - Added asinfo-gen tool to fetch and convert CSV data to JSON - Added 'make asupdate' target to refresh AS data - Embedded JSON data contains 130k+ AS entries - Added comprehensive tests and examples
This commit is contained in:
parent
14e85f042b
commit
ee80311ba1
5
.gitignore
vendored
5
.gitignore
vendored
@ -25,4 +25,7 @@ go.work.sum
|
|||||||
# Database files
|
# Database files
|
||||||
*.db
|
*.db
|
||||||
*.db-journal
|
*.db-journal
|
||||||
*.db-wal
|
*.db-wal
|
||||||
|
|
||||||
|
# Temporary files
|
||||||
|
*.tmp
|
7
Makefile
7
Makefile
@ -1,6 +1,6 @@
|
|||||||
export DEBUG = routewatch
|
export DEBUG = routewatch
|
||||||
|
|
||||||
.PHONY: test fmt lint build clean run
|
.PHONY: test fmt lint build clean run asupdate
|
||||||
|
|
||||||
all: test
|
all: test
|
||||||
|
|
||||||
@ -22,3 +22,8 @@ clean:
|
|||||||
|
|
||||||
run: build
|
run: build
|
||||||
./bin/routewatch
|
./bin/routewatch
|
||||||
|
|
||||||
|
asupdate:
|
||||||
|
@echo "Updating AS info data..."
|
||||||
|
@go run cmd/asinfo-gen/main.go > pkg/asinfo/asdata.json.tmp && \
|
||||||
|
mv pkg/asinfo/asdata.json.tmp pkg/asinfo/asdata.json
|
||||||
|
91
cmd/asinfo-gen/main.go
Normal file
91
cmd/asinfo-gen/main.go
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
// Package main generates the embedded AS info data for the asinfo package
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/csv"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const asCsvURL = "https://github.com/ipverse/asn-info/raw/refs/heads/master/as.csv"
|
||||||
|
|
||||||
|
// ASInfo represents information about an Autonomous System
|
||||||
|
type ASInfo struct {
|
||||||
|
ASN int `json:"asn"`
|
||||||
|
Handle string `json:"handle"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Configure logger to write to stderr
|
||||||
|
log.SetOutput(os.Stderr)
|
||||||
|
|
||||||
|
// Fetch the CSV data
|
||||||
|
log.Println("Fetching AS CSV data...")
|
||||||
|
resp, err := http.Get(asCsvURL)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to fetch CSV: %v", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
log.Fatalf("Failed to fetch CSV: HTTP %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse CSV
|
||||||
|
reader := csv.NewReader(resp.Body)
|
||||||
|
|
||||||
|
// Read header
|
||||||
|
header, err := reader.Read()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to read CSV header: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(header) != 3 || header[0] != "asn" || header[1] != "handle" || header[2] != "description" {
|
||||||
|
log.Fatalf("Unexpected CSV header: %v", header)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read all records
|
||||||
|
var asInfos []ASInfo
|
||||||
|
for {
|
||||||
|
record, err := reader.Read()
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error reading CSV record: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(record) != 3 {
|
||||||
|
log.Printf("Skipping invalid record: %v", record)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
asn, err := strconv.Atoi(record[0])
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Invalid ASN %q: %v", record[0], err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
asInfos = append(asInfos, ASInfo{
|
||||||
|
ASN: asn,
|
||||||
|
Handle: strings.TrimSpace(record[1]),
|
||||||
|
Description: strings.TrimSpace(record[2]),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Parsed %d AS records", len(asInfos))
|
||||||
|
|
||||||
|
// Convert to JSON and output to stdout
|
||||||
|
encoder := json.NewEncoder(os.Stdout)
|
||||||
|
encoder.SetIndent("", "\t")
|
||||||
|
if err := encoder.Encode(asInfos); err != nil {
|
||||||
|
log.Fatalf("Failed to encode JSON: %v", err)
|
||||||
|
}
|
||||||
|
}
|
652012
pkg/asinfo/asdata.json
Normal file
652012
pkg/asinfo/asdata.json
Normal file
File diff suppressed because it is too large
Load Diff
139
pkg/asinfo/asinfo.go
Normal file
139
pkg/asinfo/asinfo.go
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
// Package asinfo provides information about Autonomous Systems (AS)
|
||||||
|
package asinfo
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"encoding/json"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed asdata.json
|
||||||
|
var asDataJSON []byte
|
||||||
|
|
||||||
|
// Info represents information about an Autonomous System
|
||||||
|
type Info struct {
|
||||||
|
ASN int `json:"asn"`
|
||||||
|
Handle string `json:"handle"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Registry provides access to AS information
|
||||||
|
type Registry struct {
|
||||||
|
mu sync.RWMutex
|
||||||
|
byASN map[int]*Info
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
defaultRegistry *Registry
|
||||||
|
once sync.Once
|
||||||
|
)
|
||||||
|
|
||||||
|
// initRegistry initializes the default registry with embedded data
|
||||||
|
func initRegistry() {
|
||||||
|
defaultRegistry = &Registry{
|
||||||
|
byASN: make(map[int]*Info),
|
||||||
|
}
|
||||||
|
|
||||||
|
var asInfos []Info
|
||||||
|
if err := json.Unmarshal(asDataJSON, &asInfos); err != nil {
|
||||||
|
panic("failed to unmarshal embedded AS data: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range asInfos {
|
||||||
|
info := &asInfos[i]
|
||||||
|
defaultRegistry.byASN[info.ASN] = info
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns AS information for the given ASN
|
||||||
|
func Get(asn int) (*Info, bool) {
|
||||||
|
once.Do(initRegistry)
|
||||||
|
|
||||||
|
defaultRegistry.mu.RLock()
|
||||||
|
defer defaultRegistry.mu.RUnlock()
|
||||||
|
|
||||||
|
info, ok := defaultRegistry.byASN[asn]
|
||||||
|
if !ok {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a copy to prevent mutation
|
||||||
|
copy := *info
|
||||||
|
return ©, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDescription returns just the description for an ASN
|
||||||
|
func GetDescription(asn int) string {
|
||||||
|
info, ok := Get(asn)
|
||||||
|
if !ok {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return info.Description
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetHandle returns just the handle for an ASN
|
||||||
|
func GetHandle(asn int) string {
|
||||||
|
info, ok := Get(asn)
|
||||||
|
if !ok {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return info.Handle
|
||||||
|
}
|
||||||
|
|
||||||
|
// Total returns the total number of AS entries in the registry
|
||||||
|
func Total() int {
|
||||||
|
once.Do(initRegistry)
|
||||||
|
|
||||||
|
defaultRegistry.mu.RLock()
|
||||||
|
defer defaultRegistry.mu.RUnlock()
|
||||||
|
|
||||||
|
return len(defaultRegistry.byASN)
|
||||||
|
}
|
||||||
|
|
||||||
|
// All returns all AS information as a slice
|
||||||
|
// Note: This creates a copy of all data, use sparingly
|
||||||
|
func All() []Info {
|
||||||
|
once.Do(initRegistry)
|
||||||
|
|
||||||
|
defaultRegistry.mu.RLock()
|
||||||
|
defer defaultRegistry.mu.RUnlock()
|
||||||
|
|
||||||
|
result := make([]Info, 0, len(defaultRegistry.byASN))
|
||||||
|
for _, info := range defaultRegistry.byASN {
|
||||||
|
result = append(result, *info)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search searches for AS entries where the handle or description contains the query
|
||||||
|
// This is a simple case-sensitive substring search
|
||||||
|
func Search(query string) []Info {
|
||||||
|
once.Do(initRegistry)
|
||||||
|
|
||||||
|
defaultRegistry.mu.RLock()
|
||||||
|
defer defaultRegistry.mu.RUnlock()
|
||||||
|
|
||||||
|
var results []Info
|
||||||
|
for _, info := range defaultRegistry.byASN {
|
||||||
|
if contains(info.Handle, query) || contains(info.Description, query) {
|
||||||
|
results = append(results, *info)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
// contains is a simple substring search
|
||||||
|
func contains(s, substr string) bool {
|
||||||
|
return len(substr) > 0 && len(s) >= len(substr) && containsImpl(s, substr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func containsImpl(s, substr string) bool {
|
||||||
|
for i := 0; i <= len(s)-len(substr); i++ {
|
||||||
|
if s[i:i+len(substr)] == substr {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
189
pkg/asinfo/asinfo_test.go
Normal file
189
pkg/asinfo/asinfo_test.go
Normal file
@ -0,0 +1,189 @@
|
|||||||
|
package asinfo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGet(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
asn int
|
||||||
|
wantOK bool
|
||||||
|
wantDesc string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "AS1 Level 3",
|
||||||
|
asn: 1,
|
||||||
|
wantOK: true,
|
||||||
|
wantDesc: "Level 3 Parent, LLC",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "AS15169 Google",
|
||||||
|
asn: 15169,
|
||||||
|
wantOK: true,
|
||||||
|
wantDesc: "Google LLC",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Non-existent AS",
|
||||||
|
asn: 999999999,
|
||||||
|
wantOK: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "AS0 IANA Reserved",
|
||||||
|
asn: 0,
|
||||||
|
wantOK: true,
|
||||||
|
wantDesc: "Internet Assigned Numbers Authority",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
info, ok := Get(tt.asn)
|
||||||
|
if ok != tt.wantOK {
|
||||||
|
t.Errorf("Get(%d) ok = %v, want %v", tt.asn, ok, tt.wantOK)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok && info.Description != tt.wantDesc {
|
||||||
|
t.Errorf("Get(%d) description = %q, want %q", tt.asn, info.Description, tt.wantDesc)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetDescription(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
asn int
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{1, "Level 3 Parent, LLC"},
|
||||||
|
{999999999, ""},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run("", func(t *testing.T) {
|
||||||
|
if got := GetDescription(tt.asn); got != tt.want {
|
||||||
|
t.Errorf("GetDescription(%d) = %q, want %q", tt.asn, got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetHandle(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
asn int
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{1, "LVLT"},
|
||||||
|
{0, "IANA-RSVD"},
|
||||||
|
{999999999, ""},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run("", func(t *testing.T) {
|
||||||
|
if got := GetHandle(tt.asn); got != tt.want {
|
||||||
|
t.Errorf("GetHandle(%d) = %q, want %q", tt.asn, got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTotal(t *testing.T) {
|
||||||
|
total := Total()
|
||||||
|
if total < 100000 {
|
||||||
|
t.Errorf("Total() = %d, expected > 100000", total)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify it's consistent
|
||||||
|
if total2 := Total(); total2 != total {
|
||||||
|
t.Errorf("Total() returned different values: %d vs %d", total, total2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSearch(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
query string
|
||||||
|
wantMin int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Search for Google",
|
||||||
|
query: "Google",
|
||||||
|
wantMin: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Search for University",
|
||||||
|
query: "University",
|
||||||
|
wantMin: 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Empty search",
|
||||||
|
query: "",
|
||||||
|
wantMin: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Non-existent org",
|
||||||
|
query: "XYZABC123456",
|
||||||
|
wantMin: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
results := Search(tt.query)
|
||||||
|
if len(results) < tt.wantMin {
|
||||||
|
t.Errorf("Search(%q) returned %d results, want at least %d", tt.query, len(results), tt.wantMin)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify all results contain the query
|
||||||
|
if tt.query != "" {
|
||||||
|
for _, r := range results {
|
||||||
|
if !contains(r.Handle, tt.query) && !contains(r.Description, tt.query) {
|
||||||
|
t.Errorf("Search result ASN %d doesn't contain query %q", r.ASN, tt.query)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDataIntegrity(t *testing.T) {
|
||||||
|
// Verify no duplicate ASNs
|
||||||
|
all := All()
|
||||||
|
seen := make(map[int]bool)
|
||||||
|
for _, info := range all {
|
||||||
|
if seen[info.ASN] {
|
||||||
|
t.Errorf("Duplicate ASN found: %d", info.ASN)
|
||||||
|
}
|
||||||
|
seen[info.ASN] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify all entries have required fields
|
||||||
|
for _, info := range all {
|
||||||
|
if info.Handle == "" && info.ASN != 0 {
|
||||||
|
t.Errorf("ASN %d has empty handle", info.ASN)
|
||||||
|
}
|
||||||
|
if info.Description == "" {
|
||||||
|
t.Errorf("ASN %d has empty description", info.ASN)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkGet(b *testing.B) {
|
||||||
|
// Common ASNs to lookup
|
||||||
|
asns := []int{1, 15169, 13335, 32934, 8075, 16509}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
Get(asns[i%len(asns)])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSearch(b *testing.B) {
|
||||||
|
queries := []string{"Google", "Amazon", "Microsoft", "University"}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
Search(queries[i%len(queries)])
|
||||||
|
}
|
||||||
|
}
|
28
pkg/asinfo/doc.go
Normal file
28
pkg/asinfo/doc.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
Package asinfo provides information about Autonomous Systems (AS).
|
||||||
|
|
||||||
|
The package embeds a comprehensive database of AS numbers, handles, and descriptions
|
||||||
|
sourced from https://github.com/ipverse/asn-info. The data is embedded at compile
|
||||||
|
time and provides fast, offline lookups.
|
||||||
|
|
||||||
|
Basic usage:
|
||||||
|
|
||||||
|
// Get information about an AS
|
||||||
|
info, ok := asinfo.Get(15169)
|
||||||
|
if ok {
|
||||||
|
fmt.Printf("AS%d: %s - %s\n", info.ASN, info.Handle, info.Description)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get just the description
|
||||||
|
desc := asinfo.GetDescription(15169) // "Google LLC"
|
||||||
|
|
||||||
|
// Search for AS entries
|
||||||
|
results := asinfo.Search("University")
|
||||||
|
for _, as := range results {
|
||||||
|
fmt.Printf("AS%d: %s\n", as.ASN, as.Description)
|
||||||
|
}
|
||||||
|
|
||||||
|
The data is loaded lazily on first access and cached in memory for the lifetime
|
||||||
|
of the program. All getter methods are safe for concurrent use.
|
||||||
|
*/
|
||||||
|
package asinfo
|
29
pkg/asinfo/example_test.go
Normal file
29
pkg/asinfo/example_test.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package asinfo_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/routewatch/pkg/asinfo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleGet() {
|
||||||
|
info, ok := asinfo.Get(15169)
|
||||||
|
if ok {
|
||||||
|
fmt.Printf("AS%d: %s - %s\n", info.ASN, info.Handle, info.Description)
|
||||||
|
}
|
||||||
|
// Output: AS15169: GOOGLE - Google LLC
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleGetDescription() {
|
||||||
|
desc := asinfo.GetDescription(13335)
|
||||||
|
fmt.Println(desc)
|
||||||
|
// Output: Cloudflare, Inc.
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleSearch() {
|
||||||
|
results := asinfo.Search("MIT-GATEWAY")
|
||||||
|
for _, info := range results {
|
||||||
|
fmt.Printf("AS%d: %s - %s\n", info.ASN, info.Handle, info.Description)
|
||||||
|
}
|
||||||
|
// Output: AS3: MIT-GATEWAYS - Massachusetts Institute of Technology
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user