Skip to content

Commit

Permalink
update: 新增 route-path 模块
Browse files Browse the repository at this point in the history
  • Loading branch information
sjlleo committed May 15, 2022
1 parent 1542cb4 commit 433c865
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 20 deletions.
40 changes: 26 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/xgadget-lab/nexttrace/methods/udp"
"github.com/xgadget-lab/nexttrace/util"
"github.com/xgadget-lab/nexttrace/util/printer"
"github.com/xgadget-lab/nexttrace/util/reporter"
)

var tcpSYNFlag = flag.Bool("T", false, "Use TCP SYN for tracerouting (default port is 80 in TCP, 53 in UDP)")
Expand All @@ -21,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 routeReport = flag.Bool("report", false, "Auto-Generate a Route-Path Report by TCPTraceroute")

func main() {
printer.PrintCopyRight()
Expand All @@ -41,13 +43,18 @@ func main() {
if err != nil {
fmt.Println("请赋予 sudo (root) 权限运行本程序")
} else {
util.Printer(&util.PrinterConfig{
IP: ip,
DisplayMode: *displayMode,
DataOrigin: *dataOrigin,
Rdnsenable: *rdnsenable,
Results: *res,
})
if *routeReport {
r := reporter.New(*res, ip.String())
r.Print()
} else {
util.Printer(&util.PrinterConfig{
IP: ip,
DisplayMode: *displayMode,
DataOrigin: *dataOrigin,
Rdnsenable: *rdnsenable,
Results: *res,
})
}
}

} else {
Expand All @@ -66,13 +73,18 @@ func main() {
if err != nil {
fmt.Println("请赋予 sudo (root) 权限运行本程序")
} else {
util.Printer(&util.PrinterConfig{
IP: ip,
DisplayMode: *displayMode,
DataOrigin: *dataOrigin,
Rdnsenable: *rdnsenable,
Results: *res,
})
if *routeReport {
r := reporter.New(*res, ip.String())
r.Print()
} else {
util.Printer(&util.PrinterConfig{
IP: ip,
DisplayMode: *displayMode,
DataOrigin: *dataOrigin,
Rdnsenable: *rdnsenable,
Results: *res,
})
}
}
}
}
Expand Down
106 changes: 103 additions & 3 deletions util/reporter/reporter.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,127 @@
package reporter

import (
"errors"
"fmt"
"net"
"strings"

"github.com/xgadget-lab/nexttrace/ipgeo"
"github.com/xgadget-lab/nexttrace/methods"
)

type Reporter interface {
Print()
}

func New(rs map[uint16][]methods.TracerouteHop) Reporter {
func New(rs map[uint16][]methods.TracerouteHop, ip string) Reporter {
experimentTag()
r := reporter{
routeResult: rs,
targetIP: ip,
}
fmt.Println(r)
return &r
}

type reporter struct {
targetIP string
routeReport map[uint16][]routeReportNode
routeResult map[uint16][]methods.TracerouteHop
}

type routeReportNode struct {
asn string
isp string
geo []string
ix bool
}

func experimentTag() {
fmt.Println("Route-Path是一个实验性功能,我们的IP库不能很好的支持我们提供骨干网的地理位置信息,所以IP位置有时候会异常")
}

func reduceRouteReportNode(ip string, ipGeoData ipgeo.IPGeoData) (routeReportNode, error) {
rpn := routeReportNode{}
ptr, err := net.LookupAddr(ip)
if err == nil {
if strings.Contains(strings.ToLower(ptr[0]), "ix") {
rpn.ix = true
} else {
rpn.ix = false
}
}

if strings.Contains(strings.ToLower(ipGeoData.Isp), "exchange") || strings.Contains(strings.ToLower(ipGeoData.Isp), "ix") || strings.Contains(strings.ToLower(ipGeoData.Owner), "exchange") || strings.Contains(strings.ToLower(ipGeoData.Owner), "ix") {
rpn.ix = true
}
if strings.HasPrefix(ip, "59.43") {
rpn.asn = "4809"
} else {
rpn.asn = ipGeoData.Asnumber
}

if ipGeoData.Country == "" || ipGeoData.City == "" {
return rpn, errors.New("GeoData Search Failed")
} else {
rpn.geo = []string{ipGeoData.Country, ipGeoData.City}
}
if ipGeoData.Isp == "" {
rpn.isp = ipGeoData.Owner
} else {
rpn.isp = ipGeoData.Isp
}
return rpn, nil
}

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]
if traceHop.Success {
currentIP := traceHop.Address.String()
ipGeoData, _ := ipgeo.LeoIP(currentIP)
rpn, err := reduceRouteReportNode(currentIP, *ipGeoData)
if err == nil {
reportNodes[nodeIndex] = append(reportNodes[nodeIndex], rpn)
nodeIndex += 1
}
}
}
r.routeReport = reportNodes
return r
}

func (r *reporter) Print() {
fmt.Println(r)
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 {
fmt.Printf("AS%s %s「%s『%s", nodeReport.asn, nodeReport.isp, nodeReport.geo[0], nodeReport.geo[1])
} else {
nodeReportBefore := r.routeReport[i-1][0]
if nodeReportBefore.asn == nodeReport.asn {
// Same ASN but Coutry or City Changed
if nodeReportBefore.geo[0] != nodeReport.geo[0] {
fmt.Printf("』→ %s『%s", nodeReport.geo[0], nodeReport.geo[1])
} else {
if nodeReportBefore.geo[1] != nodeReport.geo[1] {
fmt.Printf(" → %s", nodeReport.geo[1])
}
}
} else {
fmt.Printf("』」")
if int(i) != len(r.routeReport)+1 {
fmt.Printf("\n ╭╯\n ╰")
}
if nodeReport.ix {
fmt.Printf("AS%s \033[42;37mIXP\033[0m %s「%s『%s", nodeReport.asn, nodeReport.isp, nodeReport.geo[0], nodeReport.geo[1])
} else {
fmt.Printf("AS%s %s「%s『%s", nodeReport.asn, nodeReport.isp, nodeReport.geo[0], nodeReport.geo[1])
}
}
}
}
fmt.Println("』」")
}
13 changes: 10 additions & 3 deletions util/reporter/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ import (
)

func TestPrint(t *testing.T) {
ip := util.DomainLookUp("1.1.1.1")
ip := util.DomainLookUp("213.226.68.73")
tcpTraceroute := tcp.New(ip, methods.TracerouteConfig{
MaxHops: uint16(30),
NumMeasurements: uint16(1),
ParallelRequests: uint16(1),
ParallelRequests: uint16(12),
Port: 80,
Timeout: time.Second / 2,
})
res, _ := tcpTraceroute.Start()
r := New(*res)
util.Printer(&util.PrinterConfig{
IP: ip,
DisplayMode: "classic",
DataOrigin: "LeoMoeAPI",
Rdnsenable: true,
Results: *res,
})
r := New(*res, ip.String())
r.Print()
}

0 comments on commit 433c865

Please sign in to comment.