- 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
91 lines
2.0 KiB
Go
91 lines
2.0 KiB
Go
// 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)
|
|
}
|
|
} |