feat: refresh ASN prefixes with bounded Go loader

This commit is contained in:
spiritlhl committed 2026-07-21 00:42:30 +08:00
1 parent dd4fdcdaea
commit a32a1067d3
11 files changed
+632 -42

No files matched your search

+87 -17
View File
@@ -1,9 +1,11 @@
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"runtime"
@@ -32,6 +34,32 @@ type ConcurrentResults struct {
// backtraceError error
}
type cliOptions struct {
showVersion bool
showIPInfo bool
help bool
ipv6 bool
jsonOutput bool
deep bool
specifiedIP string
timeout time.Duration
}
func newBacktraceFlagSet(options *cliOptions) *flag.FlagSet {
set := flag.NewFlagSet("backtrace", flag.ContinueOnError)
set.BoolVar(&options.help, "h", false, "Show help information")
set.BoolVar(&options.showVersion, "v", false, "Show version")
set.BoolVar(&options.showIPInfo, "s", true, "Disabe show ip info")
set.BoolVar(&model.EnableLoger, "log", false, "Enable logging")
set.BoolVar(&options.ipv6, "ipv6", false, "Enable ipv6 testing")
set.StringVar(&options.specifiedIP, "ip", "", "Specify IP address for bgptools")
set.BoolVar(&options.jsonOutput, "json", false, "Output structured RDAP/BGP report as JSON")
set.BoolVar(&options.jsonOutput, "structured", false, "Alias for -json")
set.BoolVar(&options.deep, "deep", false, "Fetch geofeed and enable WHOIS fallback")
set.DurationVar(&options.timeout, "timeout", 15*time.Second, "Structured report timeout")
return set
}
func safeGo(wg *sync.WaitGroup, fn func()) {
go func() {
defer wg.Done()
@@ -50,28 +78,44 @@ func main() {
resp.Body.Close()
}
}()
fmt.Println(Green("Repo:"), Yellow("https://github.com/oneclickvirt/backtrace"))
var showVersion, showIpInfo, help, ipv6 bool
var specifiedIP string
backtraceFlag := flag.NewFlagSet("backtrace", flag.ContinueOnError)
backtraceFlag.BoolVar(&help, "h", false, "Show help information")
backtraceFlag.BoolVar(&showVersion, "v", false, "Show version")
backtraceFlag.BoolVar(&showIpInfo, "s", true, "Disabe show ip info")
backtraceFlag.BoolVar(&model.EnableLoger, "log", false, "Enable logging")
backtraceFlag.BoolVar(&ipv6, "ipv6", false, "Enable ipv6 testing")
backtraceFlag.StringVar(&specifiedIP, "ip", "", "Specify IP address for bgptools")
backtraceFlag.Parse(os.Args[1:])
if help {
var options cliOptions
backtraceFlag := newBacktraceFlagSet(&options)
if err := backtraceFlag.Parse(os.Args[1:]); err != nil {
os.Exit(2)
}
if !options.jsonOutput {
fmt.Println(Green("Repo:"), Yellow("https://github.com/oneclickvirt/backtrace"))
}
if options.help {
fmt.Printf("Usage: %s [options]\n", os.Args[0])
backtraceFlag.PrintDefaults()
return
}
if showVersion {
if options.showVersion {
fmt.Println(model.BackTraceVersion)
return
}
if err := validateStructuredOptions(options); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
if options.jsonOutput {
config := bgptools.IPBGPReportConfig{
Timeout: options.timeout,
FetchGeofeed: options.deep,
EnableWHOISFallback: options.deep,
ResolveASN: func(ctx context.Context, ip string) (string, error) {
return bgptools.ResolveOriginASNWithConfig(ctx, ip, bgptools.OriginASNConfig{Timeout: options.timeout})
},
}
if err := writeStructuredReport(context.Background(), os.Stdout, options.specifiedIP, config, bgptools.QueryIPBGPReport); err != nil {
fmt.Fprintf(os.Stderr, "structured report failed: %v\n", err)
os.Exit(1)
}
return
}
info := IpInfo{}
if showIpInfo {
if options.showIPInfo {
rsp, err := http.Get("http://ipinfo.io")
if err != nil {
fmt.Printf("get ip info err %v \n", err.Error())
@@ -98,7 +142,7 @@ func main() {
var useIPv6 bool
switch preCheck.StackType {
case "DualStack":
useIPv6 = ipv6
useIPv6 = options.ipv6
case "IPv4":
useIPv6 = false
case "IPv6":
@@ -114,8 +158,8 @@ func main() {
results := ConcurrentResults{}
var wg sync.WaitGroup
var targetIP string
if specifiedIP != "" {
targetIP = specifiedIP
if options.specifiedIP != "" {
targetIP = options.specifiedIP
} else if info.Ip != "" {
targetIP = info.Ip
}
@@ -154,3 +198,29 @@ func main() {
fmt.Scanln()
}
}
func validateStructuredOptions(options cliOptions) error {
if options.deep && !options.jsonOutput {
return fmt.Errorf("-deep requires -json or -structured")
}
if !options.jsonOutput {
return nil
}
if options.specifiedIP == "" {
return fmt.Errorf("structured report requires -ip")
}
if options.timeout <= 0 {
return fmt.Errorf("structured report timeout must be positive")
}
return nil
}
func writeStructuredReport(ctx context.Context, output io.Writer, ip string, config bgptools.IPBGPReportConfig, query func(context.Context, string, bgptools.IPBGPReportConfig) (*bgptools.IPBGPReport, error)) error {
report, err := query(ctx, ip, config)
if err != nil {
return err
}
encoder := json.NewEncoder(output)
encoder.SetIndent("", " ")
return encoder.Encode(report)
}
+65
View File
@@ -0,0 +1,65 @@
package main
import (
"bytes"
"context"
"encoding/json"
"testing"
"time"
"github.com/oneclickvirt/backtrace/bgptools"
)
func TestBacktraceStructuredFlagParsing(t *testing.T) {
var options cliOptions
set := newBacktraceFlagSet(&options)
if err := set.Parse([]string{"-structured", "-deep", "-ip", "192.0.2.1", "-timeout", "900ms"}); err != nil {
t.Fatal(err)
}
if !options.jsonOutput || !options.deep || options.specifiedIP != "192.0.2.1" || options.timeout != 900*time.Millisecond {
t.Fatalf("unexpected options: %+v", options)
}
}
func TestBacktraceDefaultsPreserveLegacyMode(t *testing.T) {
var options cliOptions
if err := newBacktraceFlagSet(&options).Parse(nil); err != nil {
t.Fatal(err)
}
if !options.showIPInfo || options.jsonOutput || options.deep || options.ipv6 || options.specifiedIP != "" || options.timeout != 15*time.Second {
t.Fatalf("legacy defaults changed: %+v", options)
}
}
func TestValidateStructuredOptionsRequiresExplicitModeAndTarget(t *testing.T) {
tests := []cliOptions{
{deep: true, timeout: time.Second},
{jsonOutput: true, timeout: time.Second},
{jsonOutput: true, specifiedIP: "192.0.2.1"},
}
for _, options := range tests {
if err := validateStructuredOptions(options); err == nil {
t.Fatalf("expected invalid structured options: %+v", options)
}
}
if err := validateStructuredOptions(cliOptions{jsonOutput: true, deep: true, specifiedIP: "192.0.2.1", timeout: time.Second}); err != nil {
t.Fatalf("valid structured options rejected: %v", err)
}
}
func TestWriteStructuredReportKeepsStdoutJSONOnly(t *testing.T) {
var output bytes.Buffer
err := writeStructuredReport(context.Background(), &output, "192.0.2.1", bgptools.IPBGPReportConfig{Timeout: time.Second}, func(_ context.Context, ip string, _ bgptools.IPBGPReportConfig) (*bgptools.IPBGPReport, error) {
return &bgptools.IPBGPReport{IP: ip, Status: bgptools.ReportAvailable, Sources: []bgptools.IPBGPSourceStatus{}}, nil
})
if err != nil {
t.Fatal(err)
}
var report bgptools.IPBGPReport
if err := json.Unmarshal(output.Bytes(), &report); err != nil {
t.Fatalf("stdout is not JSON: %v (%q)", err, output.String())
}
if report.IP != "192.0.2.1" {
t.Fatalf("unexpected report: %+v", report)
}
}
+186
View File
@@ -0,0 +1,186 @@
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net/http"
"net/netip"
"net/url"
"os"
"path/filepath"
"sort"
"strings"
"time"
)
var knownASNs = []string{"AS23764", "AS4134", "AS4809", "AS4837", "AS58453", "AS58807", "AS9808", "AS9929"}
var errPrefixCountDrop = errors.New("prefix count dropped beyond protection threshold")
type updateConfig struct {
SourceBase string
OutputDir string
Timeout time.Duration
}
func main() {
config := updateConfig{}
flag.StringVar(&config.SourceBase, "source-base", "https://stat.ripe.net/data/announced-prefixes/data.json", "upstream announced-prefixes API")
flag.StringVar(&config.OutputDir, "output-dir", "bk/prefix", "prefix snapshot directory")
flag.DurationVar(&config.Timeout, "timeout", 30*time.Second, "upstream request timeout")
flag.Parse()
if err := updatePrefixes(context.Background(), http.DefaultClient, config); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func updatePrefixes(ctx context.Context, client *http.Client, config updateConfig) error {
if ctx == nil {
ctx = context.Background()
}
if client == nil {
client = http.DefaultClient
}
if config.SourceBase == "" || config.OutputDir == "" || config.Timeout <= 0 {
return errors.New("source-base, output-dir, and timeout must be valid")
}
valuesByASN := make(map[string][]string, len(knownASNs))
for _, asn := range knownASNs {
requestCtx, cancel := context.WithTimeout(ctx, config.Timeout)
endpoint, err := url.Parse(config.SourceBase)
if err != nil {
cancel()
return fmt.Errorf("parse source URL: %w", err)
}
query := endpoint.Query()
query.Set("resource", asn)
endpoint.RawQuery = query.Encode()
request, err := http.NewRequestWithContext(requestCtx, http.MethodGet, endpoint.String(), nil)
if err != nil {
cancel()
return err
}
request.Header.Set("User-Agent", "oneclickvirt-backtrace-prefix-sync/1")
response, err := client.Do(request)
if err != nil {
cancel()
return fmt.Errorf("fetch %s: %w", asn, err)
}
body, readErr := io.ReadAll(io.LimitReader(response.Body, 8<<20))
response.Body.Close()
cancel()
if readErr != nil {
return readErr
}
if response.StatusCode != http.StatusOK {
return fmt.Errorf("fetch %s: HTTP %d", asn, response.StatusCode)
}
values, err := extractIPv6Prefixes(body)
if err != nil {
return fmt.Errorf("parse %s: %w", asn, err)
}
valuesByASN[asn] = values
}
for _, asn := range knownASNs {
if err := writePrefixFile(filepath.Join(config.OutputDir, strings.ToLower(asn)+".txt"), valuesByASN[asn]); err != nil {
if errors.Is(err, errPrefixCountDrop) {
fmt.Fprintf(os.Stderr, "skip %s: %v\n", asn, err)
continue
}
return err
}
}
return nil
}
func extractIPv6Prefixes(data []byte) ([]string, error) {
var response struct {
Status string `json:"status"`
Data struct {
Resource string `json:"resource"`
Prefixes []struct {
Prefix string `json:"prefix"`
} `json:"prefixes"`
} `json:"data"`
}
if err := json.Unmarshal(data, &response); err != nil {
return nil, fmt.Errorf("decode announced-prefixes response: %w", err)
}
if response.Status != "ok" || response.Data.Resource == "" || response.Data.Prefixes == nil {
return nil, errors.New("announced-prefixes response is missing required fields")
}
seen := make(map[string]struct{})
for _, record := range response.Data.Prefixes {
prefix, err := netip.ParsePrefix(strings.TrimSpace(record.Prefix))
if err != nil || !prefix.Addr().Is6() || prefix.Bits() < 16 || prefix.Bits() > 128 {
continue
}
seen[prefix.String()] = struct{}{}
}
values := make([]string, 0, len(seen))
for value := range seen {
values = append(values, value)
}
sort.Strings(values)
if len(values) == 0 {
return nil, errors.New("no IPv6 prefixes found")
}
return values, nil
}
func writePrefixFile(output string, values []string) error {
if err := os.MkdirAll(filepath.Dir(output), 0o755); err != nil {
return err
}
candidate := []byte(strings.Join(values, "\n") + "\n")
current, readErr := os.ReadFile(output)
if readErr == nil {
currentCount := countNonemptyLines(current)
if currentCount > 0 && len(values)*100 < currentCount*65 {
return fmt.Errorf("%w: prefix count dropped from %d to %d for %s", errPrefixCountDrop, currentCount, len(values), filepath.Base(output))
}
if bytes.Equal(current, candidate) {
return nil
}
}
if readErr != nil && !errors.Is(readErr, os.ErrNotExist) {
return readErr
}
temporary, err := os.CreateTemp(filepath.Dir(output), ".prefix-*")
if err != nil {
return err
}
temporaryName := temporary.Name()
defer os.Remove(temporaryName)
if err := temporary.Chmod(0o644); err != nil {
_ = temporary.Close()
return err
}
if _, err := temporary.Write(candidate); err != nil {
_ = temporary.Close()
return err
}
if err := temporary.Sync(); err != nil {
_ = temporary.Close()
return err
}
if err := temporary.Close(); err != nil {
return err
}
return os.Rename(temporaryName, output)
}
func countNonemptyLines(data []byte) int {
count := 0
for _, line := range strings.Split(string(data), "\n") {
if strings.TrimSpace(line) != "" {
count++
}
}
return count
}
+32
View File
@@ -0,0 +1,32 @@
package main
import (
"errors"
"os"
"path/filepath"
"testing"
)
func TestExtractIPv6Prefixes(t *testing.T) {
values, err := extractIPv6Prefixes([]byte(`{"status":"ok","data":{"resource":"64500","prefixes":[{"prefix":"2409:8000::/32"},{"prefix":"2409:8000::/32"},{"prefix":"192.0.2.0/24"}]}}`))
if err != nil || len(values) != 1 || values[0] != "2409:8000::/32" {
t.Fatalf("unexpected prefixes: %#v, %v", values, err)
}
}
func TestExtractIPv6PrefixesRejectsSchemaDrift(t *testing.T) {
if _, err := extractIPv6Prefixes([]byte(`{"status":"ok","data":{"resource":"64500"}}`)); err == nil {
t.Fatal("missing prefixes unexpectedly accepted")
}
}
func TestWritePrefixFileProtectsLargeCountDrop(t *testing.T) {
path := filepath.Join(t.TempDir(), "as64500.txt")
if err := os.WriteFile(path, []byte("2001:db8:1::/48\n2001:db8:2::/48\n2001:db8:3::/48\n2001:db8:4::/48\n"), 0o644); err != nil {
t.Fatal(err)
}
err := writePrefixFile(path, []string{"2001:db8:1::/48"})
if !errors.Is(err, errPrefixCountDrop) {
t.Fatalf("count drop error = %v", err)
}
}