github.com/nxtrace/NTrace-core@v1.3.1-0.20240513132635-39169291e8c9/printer/printer.go (about)

     1  package printer
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/nxtrace/NTrace-core/trace"
     8  
     9  	"github.com/nxtrace/NTrace-core/ipgeo"
    10  )
    11  
    12  // var dataOrigin string
    13  
    14  // func TraceroutePrinter(res *trace.Result) {
    15  // 	for i, hop := range res.Hops {
    16  // 		fmt.Print(i + 1)
    17  // 		for _, h := range hop {
    18  // 			HopPrinter(h)
    19  // 		}
    20  // 	}
    21  // }
    22  
    23  //此文件目前仅供classic_printer使用
    24  
    25  const (
    26  	RED_PREFIX    = "\033[1;31m"
    27  	GREEN_PREFIX  = "\033[1;32m"
    28  	YELLOW_PREFIX = "\033[1;33m"
    29  	BLUE_PREFIX   = "\033[1;34m"
    30  	CYAN_PREFIX   = "\033[1;36m"
    31  	RESET_PREFIX  = "\033[0m"
    32  )
    33  
    34  func HopPrinter(h trace.Hop, info HopInfo) {
    35  	if h.Address == nil {
    36  		fmt.Println("\t*")
    37  	} else {
    38  		applyLangSetting(&h) // 应用语言设置
    39  		txt := "\t"
    40  
    41  		if h.Hostname == "" {
    42  			txt += fmt.Sprint(h.Address, " ", fmt.Sprintf("%.2f", h.RTT.Seconds()*1000), "ms")
    43  		} else {
    44  			txt += fmt.Sprint(h.Hostname, " (", h.Address, ") ", fmt.Sprintf("%.2f", h.RTT.Seconds()*1000), "ms")
    45  		}
    46  
    47  		if h.Geo != nil {
    48  			txt += " " + formatIpGeoData(h.Address.String(), h.Geo)
    49  		}
    50  		for _, v := range h.MPLS {
    51  			txt += " " + v
    52  		}
    53  		switch info {
    54  		case IXP:
    55  			fmt.Print(CYAN_PREFIX)
    56  		case PoP:
    57  			fmt.Print(CYAN_PREFIX)
    58  		case Peer:
    59  			fmt.Print(YELLOW_PREFIX)
    60  		case Aboard:
    61  			fmt.Print(GREEN_PREFIX)
    62  		}
    63  
    64  		fmt.Println(txt)
    65  
    66  		if info != General {
    67  			fmt.Print(RESET_PREFIX)
    68  		}
    69  	}
    70  }
    71  
    72  func formatIpGeoData(ip string, data *ipgeo.IPGeoData) string {
    73  	var res = make([]string, 0, 10)
    74  
    75  	if data.Asnumber == "" {
    76  		res = append(res, "*")
    77  	} else {
    78  		res = append(res, "AS"+data.Asnumber)
    79  	}
    80  
    81  	// TODO: 判断阿里云和腾讯云内网,数据不足,有待进一步完善
    82  	// TODO: 移动IDC判断到Hop.fetchIPData函数,减少API调用
    83  	//if strings.HasPrefix(ip, "9.") {
    84  	//	res = append(res, "LAN Address")
    85  	//} else if strings.HasPrefix(ip, "11.") {
    86  	//	res = append(res, "LAN Address")
    87  	//} else if data.Country == "" {
    88  	//	res = append(res, "LAN Address")
    89  	if false {
    90  	} else {
    91  		// 有些IP的归属信息为空,这个时候将ISP的信息填入
    92  		if data.Owner == "" {
    93  			data.Owner = data.Isp
    94  		}
    95  		if data.Prov == "" && data.City == "" {
    96  			// anyCast或是骨干网数据不应该有国家信息
    97  			data.Owner = data.Owner + ", " + data.Owner
    98  		} else {
    99  			// 非骨干网正常填入IP的国家信息数据
   100  			res = append(res, data.Country)
   101  		}
   102  
   103  		if data.Prov != "" {
   104  			res = append(res, data.Prov)
   105  		}
   106  		if data.City != "" {
   107  			res = append(res, data.City)
   108  		}
   109  
   110  		if data.Owner != "" {
   111  			res = append(res, data.Owner)
   112  		}
   113  	}
   114  
   115  	return strings.Join(res, ", ")
   116  }