Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SYNC #279

Merged
merged 2 commits into from
Dec 21, 2024
Merged

SYNC #279

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,5 @@ Temporary Items

# compile target directory
dist/

NTrace-core
38 changes: 18 additions & 20 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)"})
Expand Down Expand Up @@ -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,
Expand All @@ -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 中")
}
Expand All @@ -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")
Expand Down Expand Up @@ -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()
Expand Down
18 changes: 11 additions & 7 deletions fast_trace/fast_trace ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
39 changes: 22 additions & 17 deletions fast_trace/fast_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type FastTracer struct {
type ParamsFastTrace struct {
SrcDev string
SrcAddr string
DestPort int
BeginHop int
MaxHops int
RDns bool
Expand All @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -139,7 +140,7 @@ func FastTest(tm bool, outEnable bool, paramsFastTrace ParamsFastTrace) {
}
}
}
FastTestv6(tm, outEnable, paramsFastTrace)
FastTestv6(traceMode, outEnable, paramsFastTrace)
return
}
if paramsFastTrace.SrcDev != "" {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
14 changes: 10 additions & 4 deletions printer/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/nxtrace/NTrace-core/trace"
"github.com/nxtrace/NTrace-core/util"
"net"
"strings"

"github.com/fatih/color"
)
Expand Down Expand Up @@ -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))
}
}

Expand Down
Loading