Skip to content

Commit d233e0e

Browse files
committed
fasttrace和filetrace支持udpmode,且增加fasttrace和filetrace的port自定义参数
1 parent 183516b commit d233e0e

File tree

3 files changed

+41
-36
lines changed

3 files changed

+41
-36
lines changed

cmd/cmd.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func Excute() {
3535
tcp := parser.Flag("T", "tcp", &argparse.Options{Help: "Use TCP SYN for tracerouting (default port is 80)"})
3636
udp := parser.Flag("U", "udp", &argparse.Options{Help: "Use UDP SYN for tracerouting (default port is 33494)"})
3737
fast_trace := parser.Flag("F", "fast-trace", &argparse.Options{Help: "One-Key Fast Trace to China ISPs"})
38-
port := parser.Int("p", "port", &argparse.Options{Help: "Set the destination port to use. With default of 80 for \"tcp\", 33494 for \"udp\""})
38+
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})
3939
numMeasurements := parser.Int("q", "queries", &argparse.Options{Default: 3, Help: "Set the number of probes per each hop"})
4040
parallelRequests := parser.Int("", "parallel-requests", &argparse.Options{Default: 18, Help: "Set ParallelRequests number. It should be 1 when there is a multi-routing"})
4141
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() {
9696
os.Exit(0)
9797
}
9898

99+
if !*tcp && *port == 80 {
100+
*port = 33494
101+
}
102+
99103
domain := *str
100104

101-
if *port == 0 {
102-
*port = 80
105+
var m trace.Method
106+
107+
switch {
108+
case *tcp:
109+
m = trace.TCPTrace
110+
case *udp:
111+
m = trace.UDPTrace
112+
default:
113+
m = trace.ICMPTrace
103114
}
104115

105116
if *fast_trace || *file != "" {
106117
var paramsFastTrace = fastTrace.ParamsFastTrace{
107118
SrcDev: *srcDev,
108119
SrcAddr: *srcAddr,
120+
DestPort: *port,
109121
BeginHop: *beginHop,
110122
MaxHops: *maxHops,
111123
RDns: !*noRdns,
@@ -118,7 +130,7 @@ func Excute() {
118130
Dot: *dot,
119131
}
120132

121-
fastTrace.FastTest(*tcp, *output, paramsFastTrace)
133+
fastTrace.FastTest(m, *output, paramsFastTrace)
122134
if *output {
123135
fmt.Println("您的追踪日志已经存放在 /tmp/trace.log 中")
124136
}
@@ -240,25 +252,10 @@ func Excute() {
240252
}
241253
}
242254

243-
var m trace.Method
244-
245-
switch {
246-
case *tcp:
247-
m = trace.TCPTrace
248-
case *udp:
249-
m = trace.UDPTrace
250-
default:
251-
m = trace.ICMPTrace
252-
}
253-
254255
if !*jsonPrint {
255256
printer.PrintTraceRouteNav(ip, domain, *dataOrigin, *maxHops, *packetSize, *srcAddr, string(m))
256257
}
257258

258-
if !*tcp && *port == 80 {
259-
*port = 33494
260-
}
261-
262259
util.DestIP = ip.String()
263260
var conf = trace.Config{
264261
DN42: *dn42,

fast_trace/fast_trace ipv6.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (f *FastTracer) tracert_v6(location string, ispCollection ISPCollection) {
2929
var conf = trace.Config{
3030
BeginHop: f.ParamsFastTrace.BeginHop,
3131
DestIP: ip,
32-
DestPort: 80,
32+
DestPort: f.ParamsFastTrace.DestPort,
3333
MaxHops: f.ParamsFastTrace.MaxHops,
3434
NumMeasurements: 3,
3535
ParallelRequests: 18,
@@ -124,7 +124,7 @@ func (f *FastTracer) testFast_v6() {
124124
//f.tracert_v6(TestIPsCollection.Beijing.Location, TestIPsCollection.Beijing.CST)
125125
}
126126

127-
func FastTestv6(tm bool, outEnable bool, paramsFastTrace ParamsFastTrace) {
127+
func FastTestv6(traceMode trace.Method, outEnable bool, paramsFastTrace ParamsFastTrace) {
128128
var c string
129129

130130
oe = outEnable
@@ -148,11 +148,14 @@ func FastTestv6(tm bool, outEnable bool, paramsFastTrace ParamsFastTrace) {
148148
w.Conn.Close()
149149
}()
150150

151-
if !tm {
151+
switch traceMode {
152+
case trace.ICMPTrace:
152153
ft.TracerouteMethod = trace.ICMPTrace
153-
fmt.Println("您将默认使用ICMP协议进行路由跟踪,如果您想使用TCP SYN进行路由跟踪,可以加入 -T 参数")
154-
} else {
154+
case trace.TCPTrace:
155155
ft.TracerouteMethod = trace.TCPTrace
156+
case trace.UDPTrace:
157+
fmt.Println("[Info] IPv6 UDP Traceroute is not supported right now.")
158+
os.Exit(0)
156159
}
157160

158161
switch c {

fast_trace/fast_trace.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type FastTracer struct {
2626
type ParamsFastTrace struct {
2727
SrcDev string
2828
SrcAddr string
29+
DestPort int
2930
BeginHop int
3031
MaxHops int
3132
RDns bool
@@ -58,7 +59,7 @@ func (f *FastTracer) tracert(location string, ispCollection ISPCollection) {
5859
var conf = trace.Config{
5960
BeginHop: f.ParamsFastTrace.BeginHop,
6061
DestIP: ip,
61-
DestPort: 80,
62+
DestPort: f.ParamsFastTrace.DestPort,
6263
MaxHops: f.ParamsFastTrace.MaxHops,
6364
NumMeasurements: 3,
6465
ParallelRequests: 18,
@@ -103,13 +104,13 @@ func (f *FastTracer) tracert(location string, ispCollection ISPCollection) {
103104
fmt.Println()
104105
}
105106

106-
func FastTest(tm bool, outEnable bool, paramsFastTrace ParamsFastTrace) {
107+
func FastTest(traceMode trace.Method, outEnable bool, paramsFastTrace ParamsFastTrace) {
107108
// tm means tcp mode
108109
var c string
109110
oe = outEnable
110111

111112
if paramsFastTrace.File != "" {
112-
testFile(paramsFastTrace, tm)
113+
testFile(paramsFastTrace, traceMode)
113114
return
114115
}
115116

@@ -139,7 +140,7 @@ func FastTest(tm bool, outEnable bool, paramsFastTrace ParamsFastTrace) {
139140
}
140141
}
141142
}
142-
FastTestv6(tm, outEnable, paramsFastTrace)
143+
FastTestv6(traceMode, outEnable, paramsFastTrace)
143144
return
144145
}
145146
if paramsFastTrace.SrcDev != "" {
@@ -180,11 +181,13 @@ func FastTest(tm bool, outEnable bool, paramsFastTrace ParamsFastTrace) {
180181
w.Conn.Close()
181182
}()
182183

183-
if !tm {
184+
switch traceMode {
185+
case trace.ICMPTrace:
184186
ft.TracerouteMethod = trace.ICMPTrace
185-
fmt.Println("您将默认使用ICMP协议进行路由跟踪,如果您想使用TCP SYN进行路由跟踪,可以加入 -T 参数")
186-
} else {
187+
case trace.TCPTrace:
187188
ft.TracerouteMethod = trace.TCPTrace
189+
case trace.UDPTrace:
190+
ft.TracerouteMethod = trace.UDPTrace
188191
}
189192

190193
switch c {
@@ -205,7 +208,7 @@ func FastTest(tm bool, outEnable bool, paramsFastTrace ParamsFastTrace) {
205208
}
206209
}
207210

208-
func testFile(paramsFastTrace ParamsFastTrace, tm bool) {
211+
func testFile(paramsFastTrace ParamsFastTrace, traceMode trace.Method) {
209212
// 建立 WebSocket 连接
210213
w := wshandle.New()
211214
w.Interrupt = make(chan os.Signal, 1)
@@ -215,11 +218,13 @@ func testFile(paramsFastTrace ParamsFastTrace, tm bool) {
215218
}()
216219

217220
var tracerouteMethod trace.Method
218-
if !tm {
221+
switch traceMode {
222+
case trace.ICMPTrace:
219223
tracerouteMethod = trace.ICMPTrace
220-
fmt.Println("您将默认使用ICMP协议进行路由跟踪,如果您想使用TCP SYN进行路由跟踪,可以加入 -T 参数")
221-
} else {
224+
case trace.TCPTrace:
222225
tracerouteMethod = trace.TCPTrace
226+
case trace.UDPTrace:
227+
tracerouteMethod = trace.UDPTrace
223228
}
224229

225230
filePath := paramsFastTrace.File
@@ -331,7 +336,7 @@ func testFile(paramsFastTrace ParamsFastTrace, tm bool) {
331336
var conf = trace.Config{
332337
BeginHop: paramsFastTrace.BeginHop,
333338
DestIP: net.ParseIP(ip.Ip),
334-
DestPort: 80,
339+
DestPort: paramsFastTrace.DestPort,
335340
MaxHops: paramsFastTrace.MaxHops,
336341
NumMeasurements: 3,
337342
ParallelRequests: 18,

0 commit comments

Comments
 (0)