mirror of
https://github.com/oneclickvirt/backtrace.git
synced 2026-11-24 23:39:41 +08:00
feat: strengthen three-carrier return route detection
Sync ASN metadata / sync (push) Has been cancelled
Sync ASN metadata / sync (push) Has been cancelled
This commit is contained in:
1 parent
c350b4e1f8
commit
5369aa16f7
23 files changed
+1187
-136
No files matched your search
+36
-2
@@ -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 {
|
||||
|
||||
+44
-1
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/oneclickvirt/backtrace/bgptools"
|
||||
backtrace "github.com/oneclickvirt/backtrace/bk"
|
||||
)
|
||||
|
||||
func TestBacktraceStructuredFlagParsing(t *testing.T) {
|
||||
@@ -26,11 +27,53 @@ func TestBacktraceDefaultsPreserveLegacyMode(t *testing.T) {
|
||||
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 {
|
||||
if !options.showIPInfo || options.jsonOutput || options.routeJSON || options.deep || options.ipv6 || options.specifiedIP != "" || options.timeout != 15*time.Second || options.routeTries != 3 {
|
||||
t.Fatalf("legacy defaults changed: %+v", options)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBacktraceRouteStructuredFlagParsing(t *testing.T) {
|
||||
var options cliOptions
|
||||
set := newBacktraceFlagSet(&options)
|
||||
if err := set.Parse([]string{"-route-json", "-ipv6", "-route-attempts", "4", "-timeout", "9s"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !options.routeJSON || !options.ipv6 || options.routeTries != 4 || options.timeout != 9*time.Second {
|
||||
t.Fatalf("unexpected route options: %+v", options)
|
||||
}
|
||||
if err := validateStructuredOptions(options); err != nil {
|
||||
t.Fatalf("valid route options rejected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRouteStructuredOptions(t *testing.T) {
|
||||
for _, options := range []cliOptions{
|
||||
{jsonOutput: true, routeJSON: true, timeout: time.Second, routeTries: 3},
|
||||
{routeJSON: true, timeout: 0, routeTries: 3},
|
||||
{routeJSON: true, timeout: time.Second, routeTries: 0},
|
||||
{routeJSON: true, timeout: time.Second, routeTries: 6},
|
||||
} {
|
||||
if err := validateStructuredOptions(options); err == nil {
|
||||
t.Fatalf("expected invalid route options: %+v", options)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteStructuredRouteReportKeepsStdoutJSONOnly(t *testing.T) {
|
||||
var output bytes.Buffer
|
||||
report := backtrace.RouteReport{SchemaVersion: backtrace.RouteReportSchema, Targets: []backtrace.RouteTargetReport{}}
|
||||
if err := writeStructuredRouteReport(&output, report); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var decoded backtrace.RouteReport
|
||||
if err := json.Unmarshal(output.Bytes(), &decoded); err != nil {
|
||||
t.Fatalf("stdout is not route JSON: %v (%q)", err, output.String())
|
||||
}
|
||||
if decoded.SchemaVersion != backtrace.RouteReportSchema {
|
||||
t.Fatalf("unexpected route report: %+v", decoded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateStructuredOptionsRequiresExplicitModeAndTarget(t *testing.T) {
|
||||
tests := []cliOptions{
|
||||
{deep: true, timeout: time.Second},
|
||||
|
||||
@@ -20,7 +20,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var knownASNs = []string{"AS23764", "AS4134", "AS4809", "AS4837", "AS58453", "AS58807", "AS9808", "AS9929"}
|
||||
var knownASNs = []string{"AS10099", "AS23764", "AS4134", "AS4809", "AS4837", "AS58453", "AS58807", "AS9808", "AS9929"}
|
||||
var errPrefixCountDrop = errors.New("prefix count dropped beyond protection threshold")
|
||||
|
||||
type updateConfig struct {
|
||||
|
||||
Reference in new issue
Block a user