diff --git a/.gitignore b/.gitignore index c00f18c..f35b924 100644 --- a/.gitignore +++ b/.gitignore @@ -163,3 +163,5 @@ Temporary Items # compile target directory dist/ + +NTrace-core diff --git a/cmd/cmd.go b/cmd/cmd.go index f7c9773..3faf70e 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -35,7 +35,7 @@ func Excute() { tcp := parser.Flag("T", "tcp", &argparse.Options{Help: "Use TCP SYN for tracerouting (default port is 80)"}) udp := parser.Flag("U", "udp", &argparse.Options{Help: "Use UDP SYN for tracerouting (default port is 33494)"}) fast_trace := parser.Flag("F", "fast-trace", &argparse.Options{Help: "One-Key Fast Trace to China ISPs"}) - port := parser.Int("p", "port", &argparse.Options{Help: "Set the destination port to use. With default of 80 for \"tcp\", 33494 for \"udp\""}) + port := parser.Int("p", "port", &argparse.Options{Help: "Set the destination port to use. With default of 80 for \"tcp\", 33494 for \"udp\"", Default: 80}) numMeasurements := parser.Int("q", "queries", &argparse.Options{Default: 3, Help: "Set the number of probes per each hop"}) parallelRequests := parser.Int("", "parallel-requests", &argparse.Options{Default: 18, Help: "Set ParallelRequests number. It should be 1 when there is a multi-routing"}) maxHops := parser.Int("m", "max-hops", &argparse.Options{Default: 30, Help: "Set the max number of hops (max TTL to be reached)"}) @@ -96,16 +96,28 @@ func Excute() { os.Exit(0) } + if !*tcp && *port == 80 { + *port = 33494 + } + domain := *str - if *port == 0 { - *port = 80 + var m trace.Method + + switch { + case *tcp: + m = trace.TCPTrace + case *udp: + m = trace.UDPTrace + default: + m = trace.ICMPTrace } if *fast_trace || *file != "" { var paramsFastTrace = fastTrace.ParamsFastTrace{ SrcDev: *srcDev, SrcAddr: *srcAddr, + DestPort: *port, BeginHop: *beginHop, MaxHops: *maxHops, RDns: !*noRdns, @@ -118,7 +130,7 @@ func Excute() { Dot: *dot, } - fastTrace.FastTest(*tcp, *output, paramsFastTrace) + fastTrace.FastTest(m, *output, paramsFastTrace) if *output { fmt.Println("您的追踪日志已经存放在 /tmp/trace.log 中") } @@ -133,6 +145,7 @@ func Excute() { } if strings.Contains(domain, "/") { + domain = "n" + domain parts := strings.Split(domain, "/") if len(parts) < 3 { fmt.Println("Invalid input") @@ -240,22 +253,7 @@ func Excute() { } if !*jsonPrint { - printer.PrintTraceRouteNav(ip, domain, *dataOrigin, *maxHops, *packetSize) - } - - var m trace.Method - - switch { - case *tcp: - m = trace.TCPTrace - case *udp: - m = trace.UDPTrace - default: - m = trace.ICMPTrace - } - - if !*tcp && *port == 80 { - *port = 33494 + printer.PrintTraceRouteNav(ip, domain, *dataOrigin, *maxHops, *packetSize, *srcAddr, string(m)) } util.DestIP = ip.String() diff --git a/fast_trace/fast_trace ipv6.go b/fast_trace/fast_trace ipv6.go index b3acebe..6d31473 100644 --- a/fast_trace/fast_trace ipv6.go +++ b/fast_trace/fast_trace ipv6.go @@ -12,13 +12,14 @@ import ( "log" "os" "os/signal" + "strings" ) //var pFastTracer ParamsFastTrace func (f *FastTracer) tracert_v6(location string, ispCollection ISPCollection) { fmt.Fprintf(color.Output, "%s\n", color.New(color.FgYellow, color.Bold).Sprintf("『%s %s 』", location, ispCollection.ISPName)) - fmt.Printf("traceroute to %s, %d hops max, %d byte packets\n", ispCollection.IPv6, f.ParamsFastTrace.MaxHops, f.ParamsFastTrace.PktSize) + fmt.Printf("traceroute to %s, %d hops max, %d byte packets, %s mode\n", ispCollection.IPv6, f.ParamsFastTrace.MaxHops, f.ParamsFastTrace.PktSize, strings.ToUpper(string(f.TracerouteMethod))) // ip, err := util.DomainLookUp(ispCollection.IPv6, "6", "", true) ip, err := util.DomainLookUp(ispCollection.IPv6, "6", f.ParamsFastTrace.Dot, true) @@ -28,7 +29,7 @@ func (f *FastTracer) tracert_v6(location string, ispCollection ISPCollection) { var conf = trace.Config{ BeginHop: f.ParamsFastTrace.BeginHop, DestIP: ip, - DestPort: 80, + DestPort: f.ParamsFastTrace.DestPort, MaxHops: f.ParamsFastTrace.MaxHops, NumMeasurements: 3, ParallelRequests: 18, @@ -58,7 +59,7 @@ func (f *FastTracer) tracert_v6(location string, ispCollection ISPCollection) { log.SetOutput(fp) log.SetFlags(0) log.Printf("『%s %s 』\n", location, ispCollection.ISPName) - log.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, %s mode\n", ispCollection.IPv6, f.ParamsFastTrace.MaxHops, f.ParamsFastTrace.PktSize, strings.ToUpper(string(f.TracerouteMethod))) conf.RealtimePrinter = tracelog.RealtimePrinter } else { conf.RealtimePrinter = printer.RealtimePrinter @@ -123,7 +124,7 @@ func (f *FastTracer) testFast_v6() { //f.tracert_v6(TestIPsCollection.Beijing.Location, TestIPsCollection.Beijing.CST) } -func FastTestv6(tm bool, outEnable bool, paramsFastTrace ParamsFastTrace) { +func FastTestv6(traceMode trace.Method, outEnable bool, paramsFastTrace ParamsFastTrace) { var c string oe = outEnable @@ -147,11 +148,14 @@ func FastTestv6(tm bool, outEnable bool, paramsFastTrace ParamsFastTrace) { w.Conn.Close() }() - if !tm { + switch traceMode { + case trace.ICMPTrace: ft.TracerouteMethod = trace.ICMPTrace - fmt.Println("您将默认使用ICMP协议进行路由跟踪,如果您想使用TCP SYN进行路由跟踪,可以加入 -T 参数") - } else { + case trace.TCPTrace: ft.TracerouteMethod = trace.TCPTrace + case trace.UDPTrace: + fmt.Println("[Info] IPv6 UDP Traceroute is not supported right now.") + os.Exit(0) } switch c { diff --git a/fast_trace/fast_trace.go b/fast_trace/fast_trace.go index eda5d6d..3e9f134 100644 --- a/fast_trace/fast_trace.go +++ b/fast_trace/fast_trace.go @@ -26,6 +26,7 @@ type FastTracer struct { type ParamsFastTrace struct { SrcDev string SrcAddr string + DestPort int BeginHop int MaxHops int RDns bool @@ -48,7 +49,7 @@ var oe = false func (f *FastTracer) tracert(location string, ispCollection ISPCollection) { fmt.Fprintf(color.Output, "%s\n", color.New(color.FgYellow, color.Bold).Sprintf("『%s %s 』", location, ispCollection.ISPName)) - fmt.Printf("traceroute to %s, %d hops max, %d byte packets\n", ispCollection.IP, f.ParamsFastTrace.MaxHops, f.ParamsFastTrace.PktSize) + fmt.Printf("traceroute to %s, %d hops max, %d byte packets, %s mode\n", ispCollection.IP, f.ParamsFastTrace.MaxHops, f.ParamsFastTrace.PktSize, strings.ToUpper(string(f.TracerouteMethod))) // ip, err := util.DomainLookUp(ispCollection.IP, "4", "", true) ip, err := util.DomainLookUp(ispCollection.IP, "4", f.ParamsFastTrace.Dot, true) @@ -58,7 +59,7 @@ func (f *FastTracer) tracert(location string, ispCollection ISPCollection) { var conf = trace.Config{ BeginHop: f.ParamsFastTrace.BeginHop, DestIP: ip, - DestPort: 80, + DestPort: f.ParamsFastTrace.DestPort, MaxHops: f.ParamsFastTrace.MaxHops, NumMeasurements: 3, ParallelRequests: 18, @@ -89,7 +90,7 @@ func (f *FastTracer) tracert(location string, ispCollection ISPCollection) { log.SetOutput(fp) log.SetFlags(0) log.Printf("『%s %s 』\n", location, ispCollection.ISPName) - log.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, %s mode\n", ispCollection.IP, f.ParamsFastTrace.MaxHops, f.ParamsFastTrace.PktSize, strings.ToUpper(string(f.TracerouteMethod))) conf.RealtimePrinter = tracelog.RealtimePrinter } else { conf.RealtimePrinter = printer.RealtimePrinter @@ -103,13 +104,13 @@ func (f *FastTracer) tracert(location string, ispCollection ISPCollection) { fmt.Println() } -func FastTest(tm bool, outEnable bool, paramsFastTrace ParamsFastTrace) { +func FastTest(traceMode trace.Method, outEnable bool, paramsFastTrace ParamsFastTrace) { // tm means tcp mode var c string oe = outEnable if paramsFastTrace.File != "" { - testFile(paramsFastTrace, tm) + testFile(paramsFastTrace, traceMode) return } @@ -139,7 +140,7 @@ func FastTest(tm bool, outEnable bool, paramsFastTrace ParamsFastTrace) { } } } - FastTestv6(tm, outEnable, paramsFastTrace) + FastTestv6(traceMode, outEnable, paramsFastTrace) return } if paramsFastTrace.SrcDev != "" { @@ -180,11 +181,13 @@ func FastTest(tm bool, outEnable bool, paramsFastTrace ParamsFastTrace) { w.Conn.Close() }() - if !tm { + switch traceMode { + case trace.ICMPTrace: ft.TracerouteMethod = trace.ICMPTrace - fmt.Println("您将默认使用ICMP协议进行路由跟踪,如果您想使用TCP SYN进行路由跟踪,可以加入 -T 参数") - } else { + case trace.TCPTrace: ft.TracerouteMethod = trace.TCPTrace + case trace.UDPTrace: + ft.TracerouteMethod = trace.UDPTrace } switch c { @@ -205,7 +208,7 @@ func FastTest(tm bool, outEnable bool, paramsFastTrace ParamsFastTrace) { } } -func testFile(paramsFastTrace ParamsFastTrace, tm bool) { +func testFile(paramsFastTrace ParamsFastTrace, traceMode trace.Method) { // 建立 WebSocket 连接 w := wshandle.New() w.Interrupt = make(chan os.Signal, 1) @@ -215,11 +218,13 @@ func testFile(paramsFastTrace ParamsFastTrace, tm bool) { }() var tracerouteMethod trace.Method - if !tm { + switch traceMode { + case trace.ICMPTrace: tracerouteMethod = trace.ICMPTrace - fmt.Println("您将默认使用ICMP协议进行路由跟踪,如果您想使用TCP SYN进行路由跟踪,可以加入 -T 参数") - } else { + case trace.TCPTrace: tracerouteMethod = trace.TCPTrace + case trace.UDPTrace: + tracerouteMethod = trace.UDPTrace } filePath := paramsFastTrace.File @@ -283,9 +288,9 @@ func testFile(paramsFastTrace ParamsFastTrace, tm bool) { color.New(color.FgYellow, color.Bold).Sprint("『 "+ip.Desc+"』"), ) if util.EnableHidDstIP == "" { - fmt.Printf("traceroute to %s, %d hops max, %d bytes payload\n", ip.Ip, paramsFastTrace.MaxHops, paramsFastTrace.PktSize) + fmt.Printf("traceroute to %s, %d hops max, %d bytes payload, %s mode\n", ip.Ip, paramsFastTrace.MaxHops, paramsFastTrace.PktSize, strings.ToUpper(string(tracerouteMethod))) } else { - fmt.Printf("traceroute to %s, %d hops max, %d bytes payload\n", util.HideIPPart(ip.Ip), paramsFastTrace.MaxHops, paramsFastTrace.PktSize) + fmt.Printf("traceroute to %s, %d hops max, %d bytes payload, %s mode\n", util.HideIPPart(ip.Ip), paramsFastTrace.MaxHops, paramsFastTrace.PktSize, strings.ToUpper(string(tracerouteMethod))) } var srcAddr string if ip.Version4 { @@ -331,7 +336,7 @@ func testFile(paramsFastTrace ParamsFastTrace, tm bool) { var conf = trace.Config{ BeginHop: paramsFastTrace.BeginHop, DestIP: net.ParseIP(ip.Ip), - DestPort: 80, + DestPort: paramsFastTrace.DestPort, MaxHops: paramsFastTrace.MaxHops, NumMeasurements: 3, ParallelRequests: 18, @@ -354,7 +359,7 @@ func testFile(paramsFastTrace ParamsFastTrace, tm bool) { log.SetOutput(fp) log.SetFlags(0) log.Printf("『%s』\n", ip.Desc) - log.Printf("traceroute to %s, %d hops max, %d byte packets\n", ip.Ip, paramsFastTrace.MaxHops, paramsFastTrace.PktSize) + log.Printf("traceroute to %s, %d hops max, %d byte packets, %s mode\n", ip.Ip, paramsFastTrace.MaxHops, paramsFastTrace.PktSize, strings.ToUpper(string(tracerouteMethod))) conf.RealtimePrinter = tracelog.RealtimePrinter err = fp.Close() if err != nil { diff --git a/printer/basic.go b/printer/basic.go index ea0c195..a53da0f 100644 --- a/printer/basic.go +++ b/printer/basic.go @@ -6,6 +6,7 @@ import ( "github.com/nxtrace/NTrace-core/trace" "github.com/nxtrace/NTrace-core/util" "net" + "strings" "github.com/fatih/color" ) @@ -76,16 +77,21 @@ func sponsor() { // ) //} -func PrintTraceRouteNav(ip net.IP, domain string, dataOrigin string, maxHops int, packetSize int) { +func PrintTraceRouteNav(ip net.IP, domain string, dataOrigin string, maxHops int, packetSize int, srcAddr string, mode string) { fmt.Println("IP Geo Data Provider: " + dataOrigin) + if srcAddr == "" { + srcAddr = "traceroute to" + } else { + srcAddr += " ->" + } if util.EnableHidDstIP == "" { if ip.String() == domain { - fmt.Printf("traceroute to %s, %d hops max, %d bytes payload\n", ip.String(), maxHops, packetSize) + fmt.Printf("%s %s, %d hops max, %d bytes payload, %s mode\n", srcAddr, ip.String(), maxHops, packetSize, strings.ToUpper(mode)) } else { - fmt.Printf("traceroute to %s (%s), %d hops max, %d bytes payload\n", ip.String(), domain, maxHops, packetSize) + fmt.Printf("%s %s (%s), %d hops max, %d bytes payload, %s mode\n", srcAddr, ip.String(), domain, maxHops, packetSize, strings.ToUpper(mode)) } } else { - fmt.Printf("traceroute to %s, %d hops max, %d bytes payload\n", util.HideIPPart(ip.String()), maxHops, packetSize) + fmt.Printf("%s %s, %d hops max, %d bytes payload, %s mode\n", srcAddr, util.HideIPPart(ip.String()), maxHops, packetSize, strings.ToUpper(mode)) } }