feat: strengthen three-carrier return route detection
Sync ASN metadata / sync (push) Has been cancelled

This commit is contained in:
spiritlhl committed 2026-07-31 00:31:56 +08:00
1 parent c350b4e1f8
commit 5369aa16f7
23 files changed
+1187 -136

No files matched your search

+36 -2
View File
@@ -40,7 +40,9 @@ type cliOptions struct {
help bool
ipv6 bool
jsonOutput bool
routeJSON bool
deep bool
routeTries int
specifiedIP string
timeout time.Duration
}
@@ -55,8 +57,10 @@ func newBacktraceFlagSet(options *cliOptions) *flag.FlagSet {
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.routeJSON, "route-json", false, "Output structured return-route report as JSON")
set.BoolVar(&options.deep, "deep", false, "Fetch geofeed and enable WHOIS fallback")
set.DurationVar(&options.timeout, "timeout", 15*time.Second, "Structured report timeout")
set.IntVar(&options.routeTries, "route-attempts", 3, "Traceroute attempts per target (1-5)")
set.DurationVar(&options.timeout, "timeout", 15*time.Second, "Structured or route report timeout")
return set
}
@@ -83,7 +87,7 @@ func main() {
if err := backtraceFlag.Parse(os.Args[1:]); err != nil {
os.Exit(2)
}
if !options.jsonOutput {
if !options.jsonOutput && !options.routeJSON {
fmt.Println(Green("Repo:"), Yellow("https://github.com/oneclickvirt/backtrace"))
}
if options.help {
@@ -114,6 +118,18 @@ func main() {
}
return
}
if options.routeJSON {
report := backtrace.RunRouteReport(context.Background(), backtrace.RouteReportConfig{
EnableIPv6: options.ipv6,
Attempts: options.routeTries,
Timeout: options.timeout,
})
if err := writeStructuredRouteReport(os.Stdout, report); err != nil {
fmt.Fprintf(os.Stderr, "route report failed: %s\n", sanitizeErrorText(err.Error()))
os.Exit(1)
}
return
}
info := IpInfo{}
if options.showIPInfo {
rsp, err := http.Get("http://ipinfo.io")
@@ -200,9 +216,21 @@ func main() {
}
func validateStructuredOptions(options cliOptions) error {
if options.jsonOutput && options.routeJSON {
return fmt.Errorf("-json and -route-json are mutually exclusive")
}
if options.deep && !options.jsonOutput {
return fmt.Errorf("-deep requires -json or -structured")
}
if options.routeJSON {
if options.timeout <= 0 {
return fmt.Errorf("route report timeout must be positive")
}
if options.routeTries < 1 || options.routeTries > 5 {
return fmt.Errorf("route attempts must be between 1 and 5")
}
return nil
}
if !options.jsonOutput {
return nil
}
@@ -215,6 +243,12 @@ func validateStructuredOptions(options cliOptions) error {
return nil
}
func writeStructuredRouteReport(output io.Writer, report backtrace.RouteReport) error {
encoder := json.NewEncoder(output)
encoder.SetIndent("", " ")
return encoder.Encode(report)
}
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 {