Skip to content

Commit

Permalink
update: 架构整理
Browse files Browse the repository at this point in the history
  • Loading branch information
sjlleo committed May 13, 2022
1 parent f765dba commit 971d68f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 33 deletions.
31 changes: 16 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ 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, Besttrace]")
var displayMode = flag.String("displayMode", "table", "Choose The Display Mode [table, classic]")
var rdnsenable = flag.Bool("rdns", false, "Set whether rDNS will be display")

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

} else {
Expand All @@ -65,13 +66,13 @@ func main() {
if err != nil {
fmt.Println("请赋予 sudo (root) 权限运行本程序")
} else {
if *displayMode == "Besttrace" {
printer.TraceroutePrinter(ip, *res, *dataOrigin)
} else if *displayMode == "table" {
printer.TracerouteTablePrinter(ip, *res, *dataOrigin)
} else {
printer.TracerouteTablePrinter(ip, *res, *dataOrigin)
}
util.Printer(&util.PrinterConfig{
IP: ip,
DisplayMode: *displayMode,
DataOrigin: *dataOrigin,
Rdnsenable: *rdnsenable,
Results: *res,
})
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions util/methods.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package util

import (
"net"

"github.com/xgadget-lab/nexttrace/methods"
"github.com/xgadget-lab/nexttrace/util/printer"
)

type IPGeoData struct {
Asnumber string `json:"asnumber"`
Country string `json:"country"`
Expand All @@ -9,3 +16,24 @@ type IPGeoData struct {
Owner string `json:"owner"`
Isp string `json:"isp"`
}

type PrinterConfig struct {
IP net.IP
DataOrigin string
DisplayMode string
Rdnsenable bool
Results map[uint16][]methods.TracerouteHop
}

func Printer(config *PrinterConfig) {
switch config.DisplayMode {
case "table":
printer.TracerouteTablePrinter(config.IP, config.Results, config.DataOrigin, config.Rdnsenable)
case "classic":
printer.TraceroutePrinter(config.IP, config.Results, config.DataOrigin, config.Rdnsenable)
case "json":
//TracerouteJSONPrinter(config.Results, config.DataOrigin)
default:
printer.TraceroutePrinter(config.IP, config.Results, config.DataOrigin, config.Rdnsenable)
}
}
21 changes: 13 additions & 8 deletions util/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import (

var dataOrigin string

func TraceroutePrinter(ip net.IP, res map[uint16][]methods.TracerouteHop, dataOrigin string) {
func TraceroutePrinter(ip net.IP, res map[uint16][]methods.TracerouteHop, dataOrigin string, rdnsenable bool) {
for hi := uint16(1); hi < 30; hi++ {
fmt.Print(hi)
for _, v := range res[hi] {
hopPrinter(v)
hopPrinter(v, rdnsenable)
if v.Address != nil && ip.String() == v.Address.String() {
hi = 31
}
}
}
}

func hopPrinter(v2 methods.TracerouteHop) {
func hopPrinter(v2 methods.TracerouteHop, rdnsenable bool) {
if v2.Address == nil {
fmt.Println("\t*")
} else {
Expand Down Expand Up @@ -52,14 +52,19 @@ func hopPrinter(v2 methods.TracerouteHop) {
geo = formatIpGeoData(ipStr, iPGeoData)
}

ptr, err := net.LookupAddr(ipStr)

txt := "\t"
if err != nil {
txt += fmt.Sprint(ipStr, " ", fmt.Sprintf("%.2f", v2.RTT.Seconds()*1000), "ms ", geo)

if rdnsenable {
ptr, err := net.LookupAddr(ipStr)
if err != nil {
txt += fmt.Sprint(ipStr, " ", fmt.Sprintf("%.2f", v2.RTT.Seconds()*1000), "ms ", geo)
} else {
txt += fmt.Sprint(ptr[0], " (", ipStr, ") ", fmt.Sprintf("%.2f", v2.RTT.Seconds()*1000), "ms ", geo)
}
} else {
txt += fmt.Sprint(ptr[0], " (", ipStr, ") ", fmt.Sprintf("%.2f", v2.RTT.Seconds()*1000), "ms ", geo)
txt += fmt.Sprint(ipStr, " ", fmt.Sprintf("%.2f", v2.RTT.Seconds()*1000), "ms ", geo)
}

fmt.Println(txt)
}
}
Expand Down
23 changes: 13 additions & 10 deletions util/printer/tableprinter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ type rowData struct {
Owner string
}

func TracerouteTablePrinter(ip net.IP, res map[uint16][]methods.TracerouteHop, dataOrigin string) {
func TracerouteTablePrinter(ip net.IP, res map[uint16][]methods.TracerouteHop, dataOrigin string, rdnsenable bool) {
// 初始化表格
tbl := New()
for hi := uint16(1); hi < 30; hi++ {
for _, v := range res[hi] {
data := tableDataGenerator(v)
data := tableDataGenerator(v, rdnsenable)
tbl.AddRow(data.Hop, data.IP, data.Latency, data.Asnumber, data.Country, data.Prov, data.City, data.Owner)
if v.Address != nil && ip.String() == v.Address.String() {
hi = 31
Expand All @@ -48,7 +48,7 @@ func New() table.Table {
return tbl
}

func tableDataGenerator(v2 methods.TracerouteHop) *rowData {
func tableDataGenerator(v2 methods.TracerouteHop, rdnsenable bool) *rowData {
if v2.Address == nil {
return &rowData{
Hop: int64(v2.TTL),
Expand All @@ -75,16 +75,19 @@ func tableDataGenerator(v2 methods.TracerouteHop) *rowData {
iPGeoData, err = ipgeo.LeoIP(ipStr)
}

ptr, err_LookupAddr := net.LookupAddr(ipStr)

lantency = fmt.Sprintf("%.2fms", v2.RTT.Seconds()*1000)

if err_LookupAddr != nil {
IP = fmt.Sprint(ipStr)
if rdnsenable {
ptr, err_LookupAddr := net.LookupAddr(ipStr)
if err_LookupAddr != nil {
IP = fmt.Sprint(ipStr)
} else {
IP = fmt.Sprint(ptr[0], " (", ipStr, ") ")
}
} else {
IP = fmt.Sprint(ptr[0], " (", ipStr, ") ")
IP = fmt.Sprint(ipStr)
}

lantency = fmt.Sprintf("%.2fms", v2.RTT.Seconds()*1000)

if iPGeoData.Owner == "" {
iPGeoData.Owner = iPGeoData.Isp
}
Expand Down

0 comments on commit 971d68f

Please sign in to comment.