Skip to content

Commit

Permalink
fasttrace和filetrace支持udpmode,且增加fasttrace和filetrace的port自定义参数
Browse files Browse the repository at this point in the history
  • Loading branch information
tsosunchia committed Dec 21, 2024
1 parent 183516b commit d233e0e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 36 deletions.
35 changes: 16 additions & 19 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 Down Expand Up @@ -240,25 +252,10 @@ func Excute() {
}
}

var m trace.Method

switch {
case *tcp:
m = trace.TCPTrace
case *udp:
m = trace.UDPTrace
default:
m = trace.ICMPTrace
}

if !*jsonPrint {
printer.PrintTraceRouteNav(ip, domain, *dataOrigin, *maxHops, *packetSize, *srcAddr, string(m))
}

if !*tcp && *port == 80 {
*port = 33494
}

util.DestIP = ip.String()
var conf = trace.Config{
DN42: *dn42,
Expand Down
13 changes: 8 additions & 5 deletions fast_trace/fast_trace ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,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 @@ -124,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 @@ -148,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
29 changes: 17 additions & 12 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 Down Expand Up @@ -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 @@ -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 @@ -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 Down

0 comments on commit d233e0e

Please sign in to comment.