mirror of
https://github.com/nxtrace/NTrace-core.git
synced 2025-08-12 06:26:39 +00:00
add: realtime and table output mode
Co-authored-by: sjlleo <sjlleo@users.noreply.github.com>
This commit is contained in:
2
go.mod
2
go.mod
@@ -5,13 +5,13 @@ go 1.18
|
||||
require (
|
||||
github.com/google/gopacket v1.1.19
|
||||
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4
|
||||
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/mattn/go-colorable v0.1.9 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/panjf2000/ants/v2 v2.5.0 // indirect
|
||||
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
21
main.go
21
main.go
@@ -21,9 +21,10 @@ var numMeasurements = flag.Int("q", 3, "Set the number of probes per each hop.")
|
||||
var parallelRequests = flag.Int("r", 18, "Set ParallelRequests number. It should be 1 when there is a multi-routing.")
|
||||
var maxHops = flag.Int("m", 30, "Set the max number of hops (max TTL to be reached).")
|
||||
var dataOrigin = flag.String("d", "LeoMoeAPI", "Choose IP Geograph Data Provider [LeoMoeAPI, IP.SB, IPInfo, IPInsight]")
|
||||
var displayMode = flag.String("displayMode", "table", "Choose The Display Mode [table, classic]")
|
||||
var rdnsenable = flag.Bool("rdns", false, "Set whether rDNS will be display")
|
||||
var routePath = flag.Bool("report", false, "Route Path")
|
||||
var realtimePrint = flag.Bool("realtime", false, "Output trace results in runtime")
|
||||
var tablePrint = flag.Bool("table", false, "Output trace results as table")
|
||||
|
||||
func flagApply() string {
|
||||
flag.Parse()
|
||||
@@ -68,8 +69,10 @@ func main() {
|
||||
RDns: *rdnsenable,
|
||||
IPGeoSource: ipgeo.GetSource(*dataOrigin),
|
||||
Timeout: 2 * time.Second,
|
||||
RoutePath: *routePath,
|
||||
//Quic: false,
|
||||
}
|
||||
|
||||
if m == trace.ICMPTrace && !*tablePrint {
|
||||
conf.RealtimePrinter = printer.RealtimePrinter
|
||||
}
|
||||
|
||||
res, err := trace.Traceroute(m, conf)
|
||||
@@ -84,9 +87,15 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
if *displayMode == "table" {
|
||||
if m == trace.ICMPTrace && *tablePrint {
|
||||
printer.TracerouteTablePrinter(res)
|
||||
} else {
|
||||
printer.TraceroutePrinter(res)
|
||||
}
|
||||
|
||||
if m == trace.TCPTrace || m == trace.UDPTrace {
|
||||
if *realtimePrint {
|
||||
printer.TraceroutePrinter(res)
|
||||
} else {
|
||||
printer.TracerouteTablePrinter(res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,12 +15,12 @@ func TraceroutePrinter(res *trace.Result) {
|
||||
for i, hop := range res.Hops {
|
||||
fmt.Print(i + 1)
|
||||
for _, h := range hop {
|
||||
hopPrinter(h)
|
||||
HopPrinter(h)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func hopPrinter(h trace.Hop) {
|
||||
func HopPrinter(h trace.Hop) {
|
||||
if h.Address == nil {
|
||||
fmt.Println("\t*")
|
||||
} else {
|
||||
|
||||
15
printer/realtime_printer.go
Normal file
15
printer/realtime_printer.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package printer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/xgadget-lab/nexttrace/trace"
|
||||
)
|
||||
|
||||
func RealtimePrinter(res *trace.Result, ttl int) {
|
||||
fmt.Print(ttl)
|
||||
for i := range res.Hops[ttl] {
|
||||
HopPrinter(res.Hops[ttl][i])
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -65,6 +63,9 @@ func (t *ICMPTracer) Execute() (*Result, error) {
|
||||
}
|
||||
// 一组TTL全部退出(收到应答或者超时终止)以后,再进行下一个TTL的包发送
|
||||
t.wg.Wait()
|
||||
if t.RealtimePrinter != nil {
|
||||
t.RealtimePrinter(&t.res, t.workFork.ttl-1)
|
||||
}
|
||||
t.workFork.num = 0
|
||||
}
|
||||
t.res.reduce(t.final)
|
||||
@@ -162,10 +163,6 @@ func (t *ICMPTracer) send(fork workFork) error {
|
||||
// t.inflightRequestLock.Unlock()
|
||||
// }()
|
||||
|
||||
if fork.num == 0 && t.Config.RoutePath {
|
||||
fmt.Print(strconv.Itoa(fork.ttl))
|
||||
}
|
||||
|
||||
select {
|
||||
case <-t.ctx.Done():
|
||||
return nil
|
||||
@@ -192,9 +189,6 @@ func (t *ICMPTracer) send(fork workFork) error {
|
||||
h.RTT = rtt
|
||||
|
||||
h.fetchIPData(t.Config)
|
||||
if t.Config.RoutePath {
|
||||
HopPrinter(h)
|
||||
}
|
||||
|
||||
t.res.add(h)
|
||||
|
||||
@@ -210,9 +204,6 @@ func (t *ICMPTracer) send(fork workFork) error {
|
||||
RTT: 0,
|
||||
Error: ErrHopLimitTimeout,
|
||||
})
|
||||
if t.Config.RoutePath {
|
||||
fmt.Println("\t" + "*")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -98,7 +96,6 @@ func (t *ICMPTracerv6) listenICMP() {
|
||||
}
|
||||
|
||||
func (t *ICMPTracerv6) handleICMPMessage(msg ReceivedMessage, icmpType int8, data []byte) {
|
||||
|
||||
t.inflightRequestLock.Lock()
|
||||
defer t.inflightRequestLock.Unlock()
|
||||
ch, ok := t.inflightRequest[t.workFork.num]
|
||||
@@ -162,10 +159,6 @@ func (t *ICMPTracerv6) send(fork workFork) error {
|
||||
// t.inflightRequestLock.Unlock()
|
||||
// }()
|
||||
|
||||
if fork.num == 0 && t.Config.RoutePath {
|
||||
fmt.Print(strconv.Itoa(fork.ttl))
|
||||
}
|
||||
|
||||
select {
|
||||
case <-t.ctx.Done():
|
||||
return nil
|
||||
@@ -192,9 +185,6 @@ func (t *ICMPTracerv6) send(fork workFork) error {
|
||||
h.RTT = rtt
|
||||
|
||||
h.fetchIPData(t.Config)
|
||||
if t.Config.RoutePath {
|
||||
HopPrinter(h)
|
||||
}
|
||||
|
||||
t.res.add(h)
|
||||
|
||||
@@ -210,9 +200,6 @@ func (t *ICMPTracerv6) send(fork workFork) error {
|
||||
RTT: 0,
|
||||
Error: ErrHopLimitTimeout,
|
||||
})
|
||||
if t.Config.RoutePath {
|
||||
fmt.Println("\t" + "*")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -25,7 +25,7 @@ type Config struct {
|
||||
Quic bool
|
||||
IPGeoSource ipgeo.Source
|
||||
RDns bool
|
||||
RoutePath bool
|
||||
RealtimePrinter func(res *Result, ttl int)
|
||||
}
|
||||
|
||||
type Method string
|
||||
|
||||
Reference in New Issue
Block a user