Update: reporter 适配新的数据结构

This commit is contained in:
sjlleo
2022-05-18 20:57:31 +08:00
parent b688a1ae99
commit 14730bb489
4 changed files with 59 additions and 48 deletions

17
main.go
View File

@@ -3,13 +3,15 @@ package main
import (
"flag"
"fmt"
"github.com/xgadget-lab/nexttrace/ipgeo"
"github.com/xgadget-lab/nexttrace/printer"
"github.com/xgadget-lab/nexttrace/trace"
"github.com/xgadget-lab/nexttrace/util"
"log"
"os"
"time"
"github.com/xgadget-lab/nexttrace/ipgeo"
"github.com/xgadget-lab/nexttrace/printer"
"github.com/xgadget-lab/nexttrace/reporter"
"github.com/xgadget-lab/nexttrace/trace"
"github.com/xgadget-lab/nexttrace/util"
)
var tcpSYNFlag = flag.Bool("T", false, "Use TCP SYN for tracerouting (default port is 80 in TCP, 53 in UDP)")
@@ -20,6 +22,7 @@ var maxHops = flag.Int("m", 30, "Set the max number of hops (max TTL to be reach
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")
func flagApply() string {
flag.Parse()
@@ -70,6 +73,12 @@ func main() {
log.Fatalln(err)
}
if *routePath {
r := reporter.New(res, ip.String())
r.Print()
return
}
if *displayMode == "table" {
printer.TracerouteTablePrinter(res)
} else {

View File

@@ -7,14 +7,14 @@ import (
"strings"
"github.com/xgadget-lab/nexttrace/ipgeo"
"github.com/xgadget-lab/nexttrace/methods"
"github.com/xgadget-lab/nexttrace/trace"
)
type Reporter interface {
Print()
}
func New(rs map[uint16][]methods.TracerouteHop, ip string) Reporter {
func New(rs *trace.Result, ip string) Reporter {
experimentTag()
r := reporter{
routeResult: rs,
@@ -26,7 +26,7 @@ func New(rs map[uint16][]methods.TracerouteHop, ip string) Reporter {
type reporter struct {
targetIP string
routeReport map[uint16][]routeReportNode
routeResult map[uint16][]methods.TracerouteHop
routeResult *trace.Result
}
type routeReportNode struct {
@@ -37,10 +37,10 @@ type routeReportNode struct {
}
func experimentTag() {
fmt.Println("Route-Path是一个实验性功能我们的IP库不能很好的支持我们提供骨干网的地理位置信息所以IP位置有时候会异常")
fmt.Println("Route-Path 功能实验室")
}
func reduceRouteReportNode(ip string, ipGeoData ipgeo.IPGeoData) (routeReportNode, error) {
func (r *reporter) generateRouteReportNode(ip string, ipGeoData ipgeo.IPGeoData) (routeReportNode, error) {
rpn := routeReportNode{}
ptr, err := net.LookupAddr(ip)
if err == nil {
@@ -59,11 +59,15 @@ func reduceRouteReportNode(ip string, ipGeoData ipgeo.IPGeoData) (routeReportNod
} else {
rpn.asn = ipGeoData.Asnumber
}
if ipGeoData.Country == "" || ipGeoData.City == "" {
// 无论最后一跳是否为存在地理位置信息AnyCast都应该给予显示
if ipGeoData.Country == "" || ipGeoData.City == "" && ip != r.targetIP {
return rpn, errors.New("GeoData Search Failed")
} else {
rpn.geo = []string{ipGeoData.Country, ipGeoData.City}
if ipGeoData.City == "" {
rpn.geo = []string{ipGeoData.Country, ipGeoData.Country}
} else {
rpn.geo = []string{ipGeoData.Country, ipGeoData.City}
}
}
if ipGeoData.Isp == "" {
rpn.isp = ipGeoData.Owner
@@ -76,12 +80,11 @@ func reduceRouteReportNode(ip string, ipGeoData ipgeo.IPGeoData) (routeReportNod
func (r *reporter) InitialBaseData() Reporter {
var nodeIndex uint16 = 1
reportNodes := map[uint16][]routeReportNode{}
for i := uint16(1); int(i) < len(r.routeResult)+1; i++ {
traceHop := r.routeResult[i][0]
for i := uint16(0); int(i) < len(r.routeResult.Hops); i++ {
traceHop := r.routeResult.Hops[i][0]
if traceHop.Success {
currentIP := traceHop.Address.String()
ipGeoData, _ := ipgeo.LeoIP(currentIP)
rpn, err := reduceRouteReportNode(currentIP, *ipGeoData)
rpn, err := r.generateRouteReportNode(currentIP, *traceHop.Geo)
if err == nil {
reportNodes[nodeIndex] = append(reportNodes[nodeIndex], rpn)
nodeIndex += 1
@@ -94,7 +97,6 @@ func (r *reporter) InitialBaseData() Reporter {
func (r *reporter) Print() {
r.InitialBaseData()
//fmt.Println(r.routeReport)
for i := uint16(1); int(i) < len(r.routeReport)+1; i++ {
nodeReport := r.routeReport[i][0]
if i == 1 {

31
reporter/reporter_test.go Normal file
View File

@@ -0,0 +1,31 @@
package reporter
import (
"testing"
"time"
"github.com/xgadget-lab/nexttrace/ipgeo"
"github.com/xgadget-lab/nexttrace/trace"
"github.com/xgadget-lab/nexttrace/util"
)
func TestPrint(t *testing.T) {
ip := util.DomainLookUp("213.226.68.73")
var m trace.Method = "tcp"
var conf = trace.Config{
DestIP: ip,
DestPort: 80,
MaxHops: 30,
NumMeasurements: 1,
ParallelRequests: 1,
RDns: true,
IPGeoSource: ipgeo.GetSource("LeoMoeAPI"),
Timeout: 2 * time.Second,
//Quic: false,
}
res, _ := trace.Traceroute(m, conf)
r := New(res, ip.String())
r.Print()
}

View File

@@ -1,31 +0,0 @@
package reporter
import (
"testing"
"time"
"github.com/xgadget-lab/nexttrace/methods"
"github.com/xgadget-lab/nexttrace/methods/tcp"
"github.com/xgadget-lab/nexttrace/util"
)
func TestPrint(t *testing.T) {
ip := util.DomainLookUp("213.226.68.73")
tcpTraceroute := tcp.New(ip, methods.TracerouteConfig{
MaxHops: uint16(30),
NumMeasurements: uint16(1),
ParallelRequests: uint16(12),
Port: 80,
Timeout: time.Second / 2,
})
res, _ := tcpTraceroute.Start()
util.Printer(&util.PrinterConfig{
IP: ip,
DisplayMode: "classic",
DataOrigin: "LeoMoeAPI",
Rdnsenable: true,
Results: *res,
})
r := New(*res, ip.String())
r.Print()
}