Skip to content

Commit

Permalink
update: fix channel closed, printer imrove
Browse files Browse the repository at this point in the history
  • Loading branch information
sjlleo committed May 22, 2022
1 parent ead46de commit 5f99396
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion ipgeo/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ type tokenData struct {

var token = tokenData{
ipinsight: "",
ipinfo: "42764a944dabd0",
ipinfo: "",
ipleo: "NextTraceDemo",
}
4 changes: 0 additions & 4 deletions printer/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import (
"net"
)

func PrintCopyRight() {
fmt.Println("NextTrace v0.1.0 Alpha \nxgadget-lab zhshch (xzhsh.ch) & leo (leo.moe)")
}

func PrintTraceRouteNav(ip net.IP, domain string, dataOrigin string) {
fmt.Println("IP Geo Data Provider: " + dataOrigin)

Expand Down
3 changes: 2 additions & 1 deletion printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package printer

import (
"fmt"
"github.com/xgadget-lab/nexttrace/trace"
"strings"

"github.com/xgadget-lab/nexttrace/trace"

"github.com/xgadget-lab/nexttrace/ipgeo"
)

Expand Down
16 changes: 13 additions & 3 deletions printer/tableprinter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package printer

import (
"fmt"
"github.com/xgadget-lab/nexttrace/trace"
"strings"

"github.com/xgadget-lab/nexttrace/trace"

"github.com/fatih/color"
"github.com/rodaine/table"
)
Expand All @@ -30,7 +31,16 @@ func TracerouteTablePrinter(res *trace.Result) {
if k > 0 {
data.Hop = ""
}
tbl.AddRow(data.Hop, data.IP, data.Latency, data.Asnumber, data.Country, data.Prov, data.City, data.Owner)
if data.Country == "" && data.Prov == "" && data.City == "" {
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)
} else {
tbl.AddRow(data.Hop, data.IP, data.Latency, data.Asnumber, data.Country, data.Owner)
}

}
}
}
// 打印表格
Expand All @@ -42,7 +52,7 @@ func New() table.Table {
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()

tbl := table.New("Hop", "IP", "Lantency", "ASN", "Country", "Province", "City", "Owner")
tbl := table.New("Hop", "IP", "Lantency", "ASN", "Location", "Owner")
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
return tbl
}
Expand Down
12 changes: 6 additions & 6 deletions trace/icmp_ipv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ func (t *ICMPTracer) send(fork workFork) error {
t.inflightRequest[fork.num] = hopCh
t.inflightRequestLock.Unlock()

defer func() {
t.inflightRequestLock.Lock()
close(hopCh)
delete(t.inflightRequest, fork.ttl)
t.inflightRequestLock.Unlock()
}()
// defer func() {
// t.inflightRequestLock.Lock()
// close(hopCh)
// delete(t.inflightRequest, fork.ttl)
// t.inflightRequestLock.Unlock()
// }()

if fork.num == 0 && t.Config.RoutePath {
fmt.Print(strconv.Itoa(fork.ttl))
Expand Down
12 changes: 6 additions & 6 deletions trace/icmp_ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ func (t *ICMPTracerv6) send(fork workFork) error {
t.inflightRequest[fork.num] = hopCh
t.inflightRequestLock.Unlock()

defer func() {
t.inflightRequestLock.Lock()
close(hopCh)
delete(t.inflightRequest, fork.ttl)
t.inflightRequestLock.Unlock()
}()
// defer func() {
// t.inflightRequestLock.Lock()
// close(hopCh)
// delete(t.inflightRequest, fork.ttl)
// t.inflightRequestLock.Unlock()
// }()

if fork.num == 0 && t.Config.RoutePath {
fmt.Print(strconv.Itoa(fork.ttl))
Expand Down
4 changes: 2 additions & 2 deletions trace/tcp_ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (t *TCPTracerv6) Execute() (*Result, error) {
}

t.SrcIP, _ = util.LocalIPPort(t.DestIP)

log.Println(util.LocalIPPort(t.DestIP))
var err error
t.tcp, err = net.ListenPacket("ip6:tcp", t.SrcIP.String())
if err != nil {
Expand Down Expand Up @@ -207,7 +207,7 @@ func (t *TCPTracerv6) send(ttl int) error {
return err
}

ipv6.NewPacketConn(t.tcp)
ipv6.NewPacketConn(t.tcp).SetHopLimit(ttl)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func Traceroute(method Method, config Config) (*Result, error) {
if config.DestIP.To4() != nil {
tracer = &TCPTracer{Config: config}
} else {
// tracer = &TCPTracerv6{Config: config}
return nil, errors.New("IPv6 TCP Traceroute is not supported")
}
default:
Expand Down

0 comments on commit 5f99396

Please sign in to comment.