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

     1  package printer
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/nxtrace/NTrace-core/ipgeo"
     8  
     9  	"github.com/nxtrace/NTrace-core/trace"
    10  
    11  	"github.com/fatih/color"
    12  	"github.com/rodaine/table"
    13  )
    14  
    15  type rowData struct {
    16  	Hop      string
    17  	IP       string
    18  	Latency  string
    19  	Asnumber string
    20  	Country  string
    21  	Prov     string
    22  	City     string
    23  	District string
    24  	Owner    string
    25  }
    26  
    27  func TracerouteTablePrinter(res *trace.Result) {
    28  	// 初始化表格
    29  	tbl := New()
    30  	for _, hop := range res.Hops {
    31  		for k, h := range hop {
    32  			data := tableDataGenerator(h)
    33  			if k > 0 {
    34  				data.Hop = ""
    35  			}
    36  			if data.Country == "" && data.Prov == "" && data.City == "" {
    37  				tbl.AddRow(data.Hop, data.IP, data.Latency, data.Asnumber, "", data.Owner)
    38  			} else {
    39  				if data.City != "" {
    40  					tbl.AddRow(data.Hop, data.IP, data.Latency, data.Asnumber, data.City+", "+data.Prov+", "+data.Country, data.Owner)
    41  				} else if data.Prov != "" {
    42  					tbl.AddRow(data.Hop, data.IP, data.Latency, data.Asnumber, data.Prov+", "+data.Country, data.Owner)
    43  				} else {
    44  					tbl.AddRow(data.Hop, data.IP, data.Latency, data.Asnumber, data.Country, data.Owner)
    45  				}
    46  
    47  			}
    48  		}
    49  	}
    50  	fmt.Print("\033[H\033[2J")
    51  	// 打印表格
    52  	tbl.Print()
    53  }
    54  
    55  func New() table.Table {
    56  	// 初始化表格
    57  	headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
    58  	columnFmt := color.New(color.FgYellow).SprintfFunc()
    59  
    60  	tbl := table.New("Hop", "IP", "Lantency", "ASN", "Location", "Owner")
    61  	tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
    62  	return tbl
    63  }
    64  
    65  func tableDataGenerator(h trace.Hop) *rowData {
    66  	if h.Address == nil {
    67  		return &rowData{
    68  			Hop: fmt.Sprint(h.TTL),
    69  			IP:  "*",
    70  		}
    71  	} else {
    72  		lantency := fmt.Sprintf("%.2fms", h.RTT.Seconds()*1000)
    73  		IP := h.Address.String()
    74  
    75  		if strings.HasPrefix(IP, "9.") {
    76  			return &rowData{
    77  				Hop:     fmt.Sprint(h.TTL),
    78  				IP:      IP,
    79  				Latency: lantency,
    80  				Country: "LAN Address",
    81  				Prov:    "",
    82  				Owner:   "",
    83  			}
    84  		} else if strings.HasPrefix(IP, "11.") {
    85  			return &rowData{
    86  				Hop:     fmt.Sprint(h.TTL),
    87  				IP:      IP,
    88  				Latency: lantency,
    89  				Country: "LAN Address",
    90  				Prov:    "",
    91  				Owner:   "",
    92  			}
    93  		}
    94  
    95  		if h.Hostname != "" {
    96  			IP = fmt.Sprint(h.Hostname, " (", IP, ") ")
    97  		}
    98  
    99  		if h.Geo == nil {
   100  			h.Geo = &ipgeo.IPGeoData{}
   101  		}
   102  
   103  		r := &rowData{
   104  			Hop:      fmt.Sprint(h.TTL),
   105  			IP:       IP,
   106  			Latency:  lantency,
   107  			Asnumber: h.Geo.Asnumber,
   108  			Country:  h.Geo.CountryEn,
   109  			Prov:     h.Geo.ProvEn,
   110  			City:     h.Geo.CityEn,
   111  			District: h.Geo.District,
   112  			Owner:    h.Geo.Owner,
   113  		}
   114  
   115  		if h.Geo == nil {
   116  			return r
   117  		}
   118  
   119  		if h.Geo.Owner == "" {
   120  			h.Geo.Owner = h.Geo.Isp
   121  		}
   122  		r.Asnumber = h.Geo.Asnumber
   123  		r.Country = h.Geo.CountryEn
   124  		r.Prov = h.Geo.ProvEn
   125  		r.City = h.Geo.CityEn
   126  		r.District = h.Geo.District
   127  		r.Owner = h.Geo.Owner
   128  		return r
   129  	}
   130  }