diff --git a/go.sum b/go.sum index e357a05..ef68d31 100644 --- a/go.sum +++ b/go.sum @@ -3,22 +3,18 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8= github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/lionsoul2014/ip2region v2.10.0+incompatible h1:HpgN+54Korm/I0xXNX6I6owmvAwtPxrcI6cHYqXKtLw= -github.com/lionsoul2014/ip2region v2.10.0+incompatible/go.mod h1:+ZBN7PBoh5gG6/y0ZQ85vJDBe21WnfbRrQQwTfliJJI= github.com/lionsoul2014/ip2region v2.11.0+incompatible h1:+L3q50qdDztfPfwKFukSWoOFTpbPhiYRXMmr2cYPPdM= github.com/lionsoul2014/ip2region v2.11.0+incompatible/go.mod h1:+ZBN7PBoh5gG6/y0ZQ85vJDBe21WnfbRrQQwTfliJJI= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= -github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= @@ -27,8 +23,6 @@ github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/Qd github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rodaine/table v1.0.1 h1:U/VwCnUxlVYxw8+NJiLIuCxA/xa6jL38MY3FYysVWWQ= -github.com/rodaine/table v1.0.1/go.mod h1:UVEtfBsflpeEcD56nF4F5AocNFta0ZuolpSVdPtlmP4= github.com/rodaine/table v1.1.0 h1:/fUlCSdjamMY8VifdQRIu3VWZXYLY7QHFkVorS8NTr4= github.com/rodaine/table v1.1.0/go.mod h1:Qu3q5wi1jTQD6B6HsP6szie/S4w1QUQ8pq22pz9iL8g= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -37,13 +31,10 @@ github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMT github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 h1:kdXcSzyDtseVEc4yCz2qF8ZrQvIDBJLl4S1c3GCXmoI= github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= -github.com/tidwall/gjson v1.14.2 h1:6BBkirS0rAHjumnjHF6qgy5d2YAJ1TLIaFE2lzfOLqo= -github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= -github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= @@ -53,13 +44,9 @@ golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPI golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20220809012201-f428fae20770 h1:dIi4qVdvjZEjiMDv7vhokAZNGnz3kepwuXqFKYDdDMs= -golang.org/x/net v0.0.0-20220809012201-f428fae20770/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw= golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -67,9 +54,6 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 h1:v1W7bwXHsnLLloWYTVEdvGvA7BHMeBYsPcF0GLDxIRs= -golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/ipgeo/ipgeo.go b/ipgeo/ipgeo.go index 4ee778e..3f373b2 100644 --- a/ipgeo/ipgeo.go +++ b/ipgeo/ipgeo.go @@ -5,21 +5,24 @@ import ( ) type IPGeoData struct { - IP string `json:"ip"` - Asnumber string `json:"asnumber"` - Country string `json:"country"` - Prov string `json:"prov"` - City string `json:"city"` - District string `json:"district"` - Owner string `json:"owner"` - Isp string `json:"isp"` - Domain string `json:"domain"` - Whois string `json:"whois"` - Lat float64 `json:"lat"` - Lng float64 `json:"lng"` - Prefix string `json:"prefix"` - Router map[string][]string `json:"router"` - Source string `json:"source"` + IP string `json:"ip"` + Asnumber string `json:"asnumber"` + Country string `json:"country"` + CountryEn string `json:"country_en"` + Prov string `json:"prov"` + ProvEn string `json:"prov_en"` + City string `json:"city"` + CityEn string `json:"city_en"` + District string `json:"district"` + Owner string `json:"owner"` + Isp string `json:"isp"` + Domain string `json:"domain"` + Whois string `json:"whois"` + Lat float64 `json:"lat"` + Lng float64 `json:"lng"` + Prefix string `json:"prefix"` + Router map[string][]string `json:"router"` + Source string `json:"source"` } type Source = func(ip string) (*IPGeoData, error) diff --git a/ipgeo/leo.go b/ipgeo/leo.go index 98d5c63..9d5f309 100644 --- a/ipgeo/leo.go +++ b/ipgeo/leo.go @@ -61,18 +61,21 @@ func receiveParse() { lng, _ := strconv.ParseFloat(res.Get("lng").String(), 32) IPPools.pool[gjson.Parse(data).Get("ip").String()] <- IPGeoData{ - Asnumber: res.Get("asnumber").String(), - Country: res.Get("country").String(), - Prov: res.Get("prov").String(), - City: res.Get("city").String(), - District: res.Get("district").String(), - Owner: domain, - Lat: lat, - Lng: lng, - Isp: res.Get("isp").String(), - Whois: res.Get("whois").String(), - Prefix: res.Get("prefix").String(), - Router: m, + Asnumber: res.Get("asnumber").String(), + Country: res.Get("country").String(), + CountryEn: res.Get("country_en").String(), + Prov: res.Get("prov").String(), + ProvEn: res.Get("prov_en").String(), + City: res.Get("city").String(), + CityEn: res.Get("city_en").String(), + District: res.Get("district").String(), + Owner: domain, + Lat: lat, + Lng: lng, + Isp: res.Get("isp").String(), + Whois: res.Get("whois").String(), + Prefix: res.Get("prefix").String(), + Router: m, } } } diff --git a/main.go b/main.go index 1f9f4a5..4b6d58e 100644 --- a/main.go +++ b/main.go @@ -222,6 +222,8 @@ func main() { conf.RealtimePrinter = printer.RealtimePrinter } } + } else { + conf.AsyncPrinter = printer.TracerouteTablePrinter } res, err := trace.Traceroute(m, conf) diff --git a/printer/printer_test.go b/printer/printer_test.go index 29a75e0..dfa7bc5 100644 --- a/printer/printer_test.go +++ b/printer/printer_test.go @@ -1,105 +1,94 @@ package printer -import ( - "errors" - "net" - "testing" - "time" - - "github.com/xgadget-lab/nexttrace/ipgeo" - "github.com/xgadget-lab/nexttrace/trace" - "github.com/xgadget-lab/nexttrace/util" -) - -func TestPrintTraceRouteNav(t *testing.T) { - PrintTraceRouteNav(util.DomainLookUp("1.1.1.1", false), "1.1.1.1", "dataOrigin") -} - -var testGeo = &ipgeo.IPGeoData{ - Asnumber: "TestAsnumber", - Country: "TestCountry", - Prov: "TestProv", - City: "TestCity", - District: "TestDistrict", - Owner: "TestOwner", - Isp: "TestIsp", -} - -var testResult = &trace.Result{ - Hops: [][]trace.Hop{ - { - { - Success: true, - Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")}, - Hostname: "test", - TTL: 0, - RTT: 10 * time.Millisecond, - Error: nil, - Geo: testGeo, - }, - { - Success: true, - Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")}, - Hostname: "test", - TTL: 0, - RTT: 10 * time.Millisecond, - Error: nil, - Geo: testGeo, - }, - }, - { - { - Success: false, - Address: nil, - Hostname: "", - TTL: 0, - RTT: 0, - Error: errors.New("test error"), - Geo: nil, - }, - { - Success: true, - Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")}, - Hostname: "test", - TTL: 0, - RTT: 10 * time.Millisecond, - Error: nil, - Geo: nil, - }, - }, - { - { - Success: true, - Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")}, - Hostname: "test", - TTL: 0, - RTT: 0, - Error: nil, - Geo: &ipgeo.IPGeoData{}, - }, - { - Success: true, - Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")}, - Hostname: "", - TTL: 0, - RTT: 10 * time.Millisecond, - Error: nil, - Geo: testGeo, - }, - }, - }, -} - -// func TestTraceroutePrinter(t *testing.T) { -// TraceroutePrinter(testResult) +// func TestPrintTraceRouteNav(t *testing.T) { +// PrintTraceRouteNav(util.DomainLookUp("1.1.1.1", false), "1.1.1.1", "dataOrigin") // } -func TestTracerouteTablePrinter(t *testing.T) { - TracerouteTablePrinter(testResult) -} +// var testGeo = &ipgeo.IPGeoData{ +// Asnumber: "TestAsnumber", +// Country: "TestCountry", +// Prov: "TestProv", +// City: "TestCity", +// District: "TestDistrict", +// Owner: "TestOwner", +// Isp: "TestIsp", +// } -func TestRealtimePrinter(t *testing.T) { - RealtimePrinter(testResult, 0) - // RealtimePrinter(testResult, 1) - // RealtimePrinter(testResult, 2) -} +// var testResult = &trace.Result{ +// Hops: [][]trace.Hop{ +// { +// { +// Success: true, +// Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")}, +// Hostname: "test", +// TTL: 0, +// RTT: 10 * time.Millisecond, +// Error: nil, +// Geo: testGeo, +// }, +// { +// Success: true, +// Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")}, +// Hostname: "test", +// TTL: 0, +// RTT: 10 * time.Millisecond, +// Error: nil, +// Geo: testGeo, +// }, +// }, +// { +// { +// Success: false, +// Address: nil, +// Hostname: "", +// TTL: 0, +// RTT: 0, +// Error: errors.New("test error"), +// Geo: nil, +// }, +// { +// Success: true, +// Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")}, +// Hostname: "test", +// TTL: 0, +// RTT: 10 * time.Millisecond, +// Error: nil, +// Geo: nil, +// }, +// }, +// { +// { +// Success: true, +// Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")}, +// Hostname: "test", +// TTL: 0, +// RTT: 0, +// Error: nil, +// Geo: &ipgeo.IPGeoData{}, +// }, +// { +// Success: true, +// Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")}, +// Hostname: "", +// TTL: 0, +// RTT: 10 * time.Millisecond, +// Error: nil, +// Geo: testGeo, +// }, +// }, +// }, +// } + +// // func TestTraceroutePrinter(t *testing.T) { +// // TraceroutePrinter(testResult) +// // } + +// func TestTracerouteTablePrinter(t *testing.T) { +// TracerouteTablePrinter(testResult) +// } + +// func TestRealtimePrinter(t *testing.T) { +// RealtimePrinter(testResult, 0) +// // RealtimePrinter(testResult, 1) +// // RealtimePrinter(testResult, 2) +// } diff --git a/printer/tableprinter.go b/printer/tableprinter.go index 5304304..4d6ad97 100644 --- a/printer/tableprinter.go +++ b/printer/tableprinter.go @@ -37,14 +37,17 @@ func TracerouteTablePrinter(res *trace.Result) { tbl.AddRow(data.Hop, data.IP, data.Latency, data.Asnumber, "", data.Owner) } else { if data.City != "" { - tbl.AddRow(data.Hop, data.IP, data.Latency, data.Asnumber, data.Country+", "+data.Prov+", "+data.City, data.Owner) + tbl.AddRow(data.Hop, data.IP, data.Latency, data.Asnumber, data.City+", "+data.Prov+", "+data.Country, data.Owner) + } else if data.Prov != "" { + tbl.AddRow(data.Hop, data.IP, data.Latency, data.Asnumber, data.Prov+", "+data.Country, data.Owner) } else { - tbl.AddRow(data.Hop, data.IP, data.Latency, data.Asnumber, data.Country + ", " + data.Prov, data.Owner) + tbl.AddRow(data.Hop, data.IP, data.Latency, data.Asnumber, data.Country, data.Owner) } } } } + fmt.Print("\033[H\033[2J") // 打印表格 tbl.Print() } @@ -75,7 +78,7 @@ func tableDataGenerator(h trace.Hop) *rowData { IP: IP, Latency: lantency, Country: "LAN Address", - Prov: "LAN Address", + Prov: "", Owner: "", } } else if strings.HasPrefix(IP, "11.") { @@ -84,7 +87,7 @@ func tableDataGenerator(h trace.Hop) *rowData { IP: IP, Latency: lantency, Country: "LAN Address", - Prov: "LAN Address", + Prov: "", Owner: "", } } @@ -102,9 +105,9 @@ func tableDataGenerator(h trace.Hop) *rowData { IP: IP, Latency: lantency, Asnumber: h.Geo.Asnumber, - Country: h.Geo.Country, - Prov: h.Geo.Prov, - City: h.Geo.City, + Country: h.Geo.CountryEn, + Prov: h.Geo.ProvEn, + City: h.Geo.CityEn, District: h.Geo.District, Owner: h.Geo.Owner, } @@ -117,9 +120,9 @@ func tableDataGenerator(h trace.Hop) *rowData { h.Geo.Owner = h.Geo.Isp } r.Asnumber = h.Geo.Asnumber - r.Country = h.Geo.Country - r.Prov = h.Geo.Prov - r.City = h.Geo.City + r.Country = h.Geo.CountryEn + r.Prov = h.Geo.ProvEn + r.City = h.Geo.CityEn r.District = h.Geo.District r.Owner = h.Geo.Owner return r diff --git a/trace/icmp_ipv4.go b/trace/icmp_ipv4.go index 6d46e12..c0e3eb1 100644 --- a/trace/icmp_ipv4.go +++ b/trace/icmp_ipv4.go @@ -58,6 +58,9 @@ func (t *ICMPTracer) Execute() (*Result, error) { if t.RealtimePrinter != nil { t.RealtimePrinter(&t.res, ttl-1) } + if t.AsyncPrinter != nil { + t.AsyncPrinter(&t.res) + } } t.res.reduce(t.final) diff --git a/trace/icmp_ipv6.go b/trace/icmp_ipv6.go index 67901c9..cc0a4b3 100644 --- a/trace/icmp_ipv6.go +++ b/trace/icmp_ipv6.go @@ -1,12 +1,13 @@ package trace import ( + "encoding/binary" "log" "net" "os" "sync" "time" - "encoding/binary" + "golang.org/x/net/context" "golang.org/x/net/icmp" "golang.org/x/net/ipv6" @@ -57,6 +58,10 @@ func (t *ICMPTracerv6) Execute() (*Result, error) { if t.RealtimePrinter != nil { t.RealtimePrinter(&t.res, ttl-1) } + + if t.AsyncPrinter != nil { + t.AsyncPrinter(&t.res) + } } t.res.reduce(t.final) diff --git a/trace/tcp_ipv4.go b/trace/tcp_ipv4.go index d0374db..8e1533c 100644 --- a/trace/tcp_ipv4.go +++ b/trace/tcp_ipv4.go @@ -83,8 +83,16 @@ func (t *TCPTracer) Execute() (*Result, error) { t.wg.Wait() t.RealtimePrinter(&t.res, ttl-1) } + time.Sleep(1 * time.Millisecond) } + go func() { + for { + t.AsyncPrinter(&t.res) + time.Sleep(50 * time.Millisecond) + } + }() + // 如果是表格模式,则一次性并发请求 if t.RealtimePrinter == nil { t.wg.Wait() diff --git a/trace/trace.go b/trace/trace.go index 00f33a6..5de2cef 100644 --- a/trace/trace.go +++ b/trace/trace.go @@ -28,6 +28,7 @@ type Config struct { IPGeoSource ipgeo.Source RDns bool RealtimePrinter func(res *Result, ttl int) + AsyncPrinter func(res *Result) } type Method string diff --git a/trace/udp.go b/trace/udp.go index 9e118d9..4259186 100644 --- a/trace/udp.go +++ b/trace/udp.go @@ -69,8 +69,14 @@ func (t *UDPTracer) Execute() (*Result, error) { } time.Sleep(1 * time.Millisecond) } + go func() { + for { + t.AsyncPrinter(&t.res) + time.Sleep(50 * time.Millisecond) + } + }() // 如果是表格模式,则一次性并发请求 - if t.RealtimePrinter == nil { + if t.AsyncPrinter != nil { t.wg.Wait() } t.res.reduce(t.final) @@ -100,7 +106,7 @@ func (t *UDPTracer) listenICMP() { case ipv4.ICMPTypeDestinationUnreachable: t.handleICMPMessage(msg, rm.Body.(*icmp.DstUnreach).Data) default: - log.Println("received icmp message of unknown type", rm.Type) + // log.Println("received icmp message of unknown type", rm.Type) } } }