mirror of
https://github.com/nxtrace/NTrace-core.git
synced 2026-11-28 07:35:37 +00:00
132 lines
2.7 KiB
Go
132 lines
2.7 KiB
Go
package printer
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/nxtrace/NTrace-core/ipgeo"
|
|
"github.com/nxtrace/NTrace-core/trace"
|
|
)
|
|
|
|
// var dataOrigin string
|
|
|
|
// func TraceroutePrinter(res *trace.Result) {
|
|
// for i, hop := range res.Hops {
|
|
// fmt.Print(i + 1)
|
|
// for _, h := range hop {
|
|
// HopPrinter(h)
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
//此文件目前仅供classic_printer使用
|
|
|
|
const (
|
|
RED_PREFIX = "\033[1;31m"
|
|
GREEN_PREFIX = "\033[1;32m"
|
|
YELLOW_PREFIX = "\033[1;33m"
|
|
BLUE_PREFIX = "\033[1;34m"
|
|
CYAN_PREFIX = "\033[1;36m"
|
|
RESET_PREFIX = "\033[0m"
|
|
)
|
|
|
|
func HopPrinter(h trace.Hop, info HopInfo) {
|
|
if h.Address == nil {
|
|
fmt.Println("\t*")
|
|
} else {
|
|
applyLangSetting(&h) // 应用语言设置
|
|
txt := "\t"
|
|
|
|
if h.Hostname == "" {
|
|
txt += fmt.Sprint(h.Address, " ", fmt.Sprintf("%.2f", h.RTT.Seconds()*1000), "ms")
|
|
} else {
|
|
txt += fmt.Sprint(h.Hostname, " (", h.Address, ") ", fmt.Sprintf("%.2f", h.RTT.Seconds()*1000), "ms")
|
|
}
|
|
|
|
if h.Geo != nil {
|
|
txt += " " + FormatIPGeoData(h.Address.String(), h.Geo)
|
|
}
|
|
for _, v := range h.MPLS {
|
|
txt += " " + v
|
|
}
|
|
switch info {
|
|
case IXP:
|
|
fmt.Print(CYAN_PREFIX)
|
|
case PoP:
|
|
fmt.Print(CYAN_PREFIX)
|
|
case Peer:
|
|
fmt.Print(YELLOW_PREFIX)
|
|
case Aboard:
|
|
fmt.Print(GREEN_PREFIX)
|
|
}
|
|
|
|
fmt.Println(txt)
|
|
|
|
if info != General {
|
|
fmt.Print(RESET_PREFIX)
|
|
}
|
|
}
|
|
}
|
|
|
|
func FormatIPGeoData(ip string, data *ipgeo.IPGeoData) string {
|
|
var res = make([]string, 0, 10)
|
|
if data.Source == "timeout" {
|
|
if data.Country != "" {
|
|
return data.Country
|
|
}
|
|
if data.CountryEn != "" {
|
|
return data.CountryEn
|
|
}
|
|
}
|
|
|
|
if data.Asnumber == "" {
|
|
res = append(res, "*")
|
|
} else {
|
|
res = append(res, "AS"+data.Asnumber)
|
|
}
|
|
if data.Whois != "" &&
|
|
data.Country == "" &&
|
|
data.CountryEn == "" &&
|
|
data.Prov == "" &&
|
|
data.ProvEn == "" &&
|
|
data.City == "" &&
|
|
data.CityEn == "" &&
|
|
data.Owner == "" &&
|
|
data.Isp == "" {
|
|
res = append(res, data.Whois)
|
|
return strings.Join(res, ", ")
|
|
}
|
|
|
|
// TODO: 判断阿里云和腾讯云内网,数据不足,有待进一步完善
|
|
// TODO: 移动IDC判断到Hop.fetchIPData函数,减少API调用
|
|
//if strings.HasPrefix(ip, "9.") {
|
|
// res = append(res, "LAN Address")
|
|
//} else if strings.HasPrefix(ip, "11.") {
|
|
// res = append(res, "LAN Address")
|
|
//} else if data.Country == "" {
|
|
// res = append(res, "LAN Address")
|
|
if false {
|
|
} else {
|
|
// 有些IP的归属信息为空,这个时候将ISP的信息填入
|
|
if data.Owner == "" {
|
|
data.Owner = data.Isp
|
|
}
|
|
if data.Country != "" {
|
|
res = append(res, data.Country)
|
|
}
|
|
|
|
if data.Prov != "" {
|
|
res = append(res, data.Prov)
|
|
}
|
|
if data.City != "" {
|
|
res = append(res, data.City)
|
|
}
|
|
|
|
if data.Owner != "" {
|
|
res = append(res, data.Owner)
|
|
}
|
|
}
|
|
|
|
return strings.Join(res, ", ")
|
|
}
|