From dcce91614f1f891ebe17dc86c3e7d438167ef09e Mon Sep 17 00:00:00 2001 From: tsosunchia <59512455+tsosunchia@users.noreply.github.com> Date: Sun, 4 Jun 2023 04:12:57 +0800 Subject: [PATCH] =?UTF-8?q?fix=20bugs:ft=E4=B8=8Bmaxhops=E3=80=81pktsize?= =?UTF-8?q?=E4=B8=8D=E6=AD=A3=E5=B8=B8=E6=98=BE=E7=A4=BA=E3=80=81tracemap?= =?UTF-8?q?=E5=9C=A8=E9=85=8D=E7=BD=AE=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E6=97=B6=E4=B8=8D=E8=83=BD=E6=AD=A3=E5=B8=B8=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E3=80=81trace=E6=97=B6=E6=97=A0=E6=B3=95=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E8=B7=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/cmd.go | 80 ++++++++++++++++++----------------- fast_trace/fast_trace ipv6.go | 28 ++++++------ fast_trace/fast_trace.go | 18 ++++---- printer/basic.go | 6 +-- tracemap/tracemap.go | 1 - 5 files changed, 70 insertions(+), 63 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 930688c..58384b1 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -9,7 +9,6 @@ import ( "os/signal" "runtime" "strings" - "sync" "time" "github.com/akamensky/argparse" @@ -99,6 +98,7 @@ func Excute() { AlwaysWaitRDNS: *alwaysRdns, Lang: *lang, PktSize: *packetSize, + Timeout: time.Duration(*timeout) * time.Millisecond, } fastTrace.FastTest(*tcp, *output, paramsFastTrace) @@ -144,46 +144,50 @@ func Excute() { *disableMaptrace = true } - var wg sync.WaitGroup - wg.Add(2) - - go func() { - defer wg.Done() - if strings.ToUpper(*dataOrigin) == "LEOMOEAPI" { - val, ok := os.LookupEnv("NEXTTRACE_DATAPROVIDER") - if ok { - *dataOrigin = val - } else { - w := wshandle.New() - w.Interrupt = make(chan os.Signal, 1) - signal.Notify(w.Interrupt, os.Interrupt) - defer func() { - w.Conn.Close() - }() - } + /** + * 此处若使用goroutine同时运行ws的建立与nslookup, + * 会导致第一跳的IP信息无法获取,原因不明。 + */ + //var wg sync.WaitGroup + //wg.Add(2) + // + //go func() { + // defer wg.Done() + if strings.ToUpper(*dataOrigin) == "LEOMOEAPI" { + val, ok := os.LookupEnv("NEXTTRACE_DATAPROVIDER") + if ok { + *dataOrigin = val + } else { + w := wshandle.New() + w.Interrupt = make(chan os.Signal, 1) + signal.Notify(w.Interrupt, os.Interrupt) + defer func() { + w.Conn.Close() + }() } - }() - - go func() { - defer wg.Done() - if *udp { - if *ipv6Only { - fmt.Println("[Info] IPv6 UDP Traceroute is not supported right now.") - os.Exit(0) - } + } + //}() + // + //go func() { + // defer wg.Done() + if *udp { + if *ipv6Only { + fmt.Println("[Info] IPv6 UDP Traceroute is not supported right now.") + os.Exit(0) + } + ip = util.DomainLookUp(domain, "4", *dot, *jsonPrint) + } else { + if *ipv6Only { + ip = util.DomainLookUp(domain, "6", *dot, *jsonPrint) + } else if *ipv4Only { ip = util.DomainLookUp(domain, "4", *dot, *jsonPrint) } else { - if *ipv6Only { - ip = util.DomainLookUp(domain, "6", *dot, *jsonPrint) - } else if *ipv4Only { - ip = util.DomainLookUp(domain, "4", *dot, *jsonPrint) - } else { - ip = util.DomainLookUp(domain, "all", *dot, *jsonPrint) - } + ip = util.DomainLookUp(domain, "all", *dot, *jsonPrint) } - }() - - wg.Wait() + } + //}() + // + //wg.Wait() if *srcDev != "" { dev, _ := net.InterfaceByName(*srcDev) @@ -198,7 +202,7 @@ func Excute() { } if !*jsonPrint { - printer.PrintTraceRouteNav(ip, domain, *dataOrigin, *maxHops) + printer.PrintTraceRouteNav(ip, domain, *dataOrigin, *maxHops, *packetSize) } var m trace.Method = "" diff --git a/fast_trace/fast_trace ipv6.go b/fast_trace/fast_trace ipv6.go index 41b2642..c719a30 100644 --- a/fast_trace/fast_trace ipv6.go +++ b/fast_trace/fast_trace ipv6.go @@ -2,17 +2,15 @@ package fastTrace import ( "fmt" - "github.com/xgadget-lab/nexttrace/util" - "log" - "os" - "os/signal" - "time" - "github.com/xgadget-lab/nexttrace/ipgeo" "github.com/xgadget-lab/nexttrace/printer" "github.com/xgadget-lab/nexttrace/trace" "github.com/xgadget-lab/nexttrace/tracelog" + "github.com/xgadget-lab/nexttrace/util" "github.com/xgadget-lab/nexttrace/wshandle" + "log" + "os" + "os/signal" ) func (f *FastTracer) tracert_v6(location string, ispCollection ISPCollection) { @@ -31,19 +29,25 @@ func (f *FastTracer) tracert_v6(location string, ispCollection ISPCollection) { log.SetFlags(0) fmt.Printf("%s『%s %s 』%s\n", printer.YELLOW_PREFIX, location, ispCollection.ISPName, printer.RESET_PREFIX) log.Printf("『%s %s 』\n", location, ispCollection.ISPName) - fmt.Printf("traceroute to %s, 30 hops max, 32 byte packets\n", ispCollection.IPv6) - log.Printf("traceroute to %s, 30 hops max, 32 byte packets\n", ispCollection.IPv6) + fmt.Printf("traceroute to %s, %d hops max, %d byte packets\n", ispCollection.IPv6, f.ParamsFastTrace.MaxHops, f.ParamsFastTrace.PktSize) + log.Printf("traceroute to %s, %d hops max, %d byte packets\n", ispCollection.IPv6, f.ParamsFastTrace.MaxHops, f.ParamsFastTrace.PktSize) ip := util.DomainLookUp(ispCollection.IP, "6", "", true) var conf = trace.Config{ - BeginHop: 1, + BeginHop: f.ParamsFastTrace.BeginHop, DestIP: ip, DestPort: 80, - MaxHops: 30, + MaxHops: f.ParamsFastTrace.MaxHops, NumMeasurements: 3, ParallelRequests: 18, - RDns: true, + RDns: f.ParamsFastTrace.RDns, + AlwaysWaitRDNS: f.ParamsFastTrace.AlwaysWaitRDNS, + PacketInterval: 100, + TTLInterval: 500, IPGeoSource: ipgeo.GetSource("LeoMoeAPI"), - Timeout: 1 * time.Second, + Timeout: f.ParamsFastTrace.Timeout, + SrcAddr: f.ParamsFastTrace.SrcAddr, + PktSize: f.ParamsFastTrace.PktSize, + Lang: f.ParamsFastTrace.Lang, } if oe { diff --git a/fast_trace/fast_trace.go b/fast_trace/fast_trace.go index 84e461f..b6ef254 100644 --- a/fast_trace/fast_trace.go +++ b/fast_trace/fast_trace.go @@ -2,18 +2,17 @@ package fastTrace import ( "fmt" + "github.com/xgadget-lab/nexttrace/ipgeo" + "github.com/xgadget-lab/nexttrace/printer" + "github.com/xgadget-lab/nexttrace/trace" + "github.com/xgadget-lab/nexttrace/tracelog" "github.com/xgadget-lab/nexttrace/util" + "github.com/xgadget-lab/nexttrace/wshandle" "log" "net" "os" "os/signal" "time" - - "github.com/xgadget-lab/nexttrace/ipgeo" - "github.com/xgadget-lab/nexttrace/printer" - "github.com/xgadget-lab/nexttrace/trace" - "github.com/xgadget-lab/nexttrace/tracelog" - "github.com/xgadget-lab/nexttrace/wshandle" ) type FastTracer struct { @@ -30,6 +29,7 @@ type ParamsFastTrace struct { AlwaysWaitRDNS bool Lang string PktSize int + Timeout time.Duration } var oe = false @@ -50,8 +50,8 @@ func (f *FastTracer) tracert(location string, ispCollection ISPCollection) { log.SetFlags(0) fmt.Printf("%s『%s %s 』%s\n", printer.YELLOW_PREFIX, location, ispCollection.ISPName, printer.RESET_PREFIX) log.Printf("『%s %s 』\n", location, ispCollection.ISPName) - fmt.Printf("traceroute to %s, 30 hops max, 32 byte packets\n", ispCollection.IP) - log.Printf("traceroute to %s, 30 hops max, 32 byte packets\n", ispCollection.IP) + fmt.Printf("traceroute to %s, %d hops max, %d byte packets\n", ispCollection.IP, f.ParamsFastTrace.MaxHops, f.ParamsFastTrace.PktSize) + log.Printf("traceroute to %s, %d hops max, %d byte packets\n", ispCollection.IP, f.ParamsFastTrace.MaxHops, f.ParamsFastTrace.PktSize) ip := util.DomainLookUp(ispCollection.IP, "4", "", true) var conf = trace.Config{ BeginHop: f.ParamsFastTrace.BeginHop, @@ -65,7 +65,7 @@ func (f *FastTracer) tracert(location string, ispCollection ISPCollection) { PacketInterval: 100, TTLInterval: 500, IPGeoSource: ipgeo.GetSource("LeoMoeAPI"), - Timeout: 1 * time.Second, + Timeout: f.ParamsFastTrace.Timeout, SrcAddr: f.ParamsFastTrace.SrcAddr, PktSize: f.ParamsFastTrace.PktSize, Lang: f.ParamsFastTrace.Lang, diff --git a/printer/basic.go b/printer/basic.go index 60a8586..93c0c64 100644 --- a/printer/basic.go +++ b/printer/basic.go @@ -63,13 +63,13 @@ func PluginCopyRight() { ) } -func PrintTraceRouteNav(ip net.IP, domain string, dataOrigin string, maxHops int) { +func PrintTraceRouteNav(ip net.IP, domain string, dataOrigin string, maxHops int, packetSize int) { fmt.Println("IP Geo Data Provider: " + dataOrigin) if ip.String() == domain { - fmt.Printf("traceroute to %s, %d hops max, 32 byte packets\n", ip.String(), maxHops) + fmt.Printf("traceroute to %s, %d hops max, %d bytes packets\n", ip.String(), maxHops, packetSize) } else { - fmt.Printf("traceroute to %s (%s), %d hops max, 32 byte packets\n", ip.String(), domain, maxHops) + fmt.Printf("traceroute to %s (%s), %d hops max, %d bytes packets\n", ip.String(), domain, maxHops, packetSize) } } diff --git a/tracemap/tracemap.go b/tracemap/tracemap.go index a6031b1..9a9489f 100644 --- a/tracemap/tracemap.go +++ b/tracemap/tracemap.go @@ -26,7 +26,6 @@ func GetMapUrl(r string) (string, error) { } u := url.URL{Scheme: "https", Host: fastIp + ":" + port, Path: "/tracemap/api"} tracemapUrl := u.String() - tracemapUrl = "https://api.leo.moe/tracemap/api" client := &http.Client{ Timeout: 5 * time.Second,