diff --git a/README.md b/README.md index b7bc3ee..8760ab2 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,8 @@ nexttrace --parallel-requests 1 www.hkix.net # Start Trace with TTL of 5, end at TTL of 10 nexttrace --first 5 --max-hops 10 www.decix.net +# In addition, an ENV is provided to set whether to hide the destination IP +export NEXTTRACE_ENABLEHIDDENDSTIP=1 # Turn off the IP reverse parsing function nexttrace --no-rdns www.bbix.net diff --git a/README_zh_CN.md b/README_zh_CN.md index f03fae3..ca77169 100644 --- a/README_zh_CN.md +++ b/README_zh_CN.md @@ -243,6 +243,8 @@ nexttrace --parallel-requests 1 www.hkix.net # 从TTL为5开始发送探测包,直到TTL为10结束 nexttrace --first 5 --max-hops 10 www.decix.net +# 此外还提供了一个ENV,可以设置是否隐匿目的IP +export NEXTTRACE_ENABLEHIDDENDSTIP=1 # 关闭IP反向解析功能 nexttrace --no-rdns www.bbix.net diff --git a/cmd/cmd.go b/cmd/cmd.go index b380ffd..8c2954b 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -245,6 +245,7 @@ func Excute() { *port = 53 } + util.DestIP = ip.String() var conf = trace.Config{ DN42: *dn42, SrcAddr: *srcAddr, diff --git a/printer/basic.go b/printer/basic.go index abfe223..85b92f6 100644 --- a/printer/basic.go +++ b/printer/basic.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/nxtrace/NTrace-core/config" "github.com/nxtrace/NTrace-core/trace" + "github.com/nxtrace/NTrace-core/util" "net" "github.com/fatih/color" @@ -78,11 +79,14 @@ func sponsor() { func PrintTraceRouteNav(ip net.IP, domain string, dataOrigin string, maxHops int, packetSize int) { fmt.Println("IP Geo Data Provider: " + dataOrigin) - - if ip.String() == domain { - fmt.Printf("traceroute to %s, %d hops max, %d bytes packets\n", ip.String(), maxHops, packetSize) + if util.EnableHidDstIP == "" { + if ip.String() == domain { + fmt.Printf("traceroute to %s, %d hops max, %d bytes packets\n", ip.String(), maxHops, packetSize) + } else { + fmt.Printf("traceroute to %s (%s), %d hops max, %d bytes packets\n", ip.String(), domain, maxHops, packetSize) + } } else { - fmt.Printf("traceroute to %s (%s), %d hops max, %d bytes packets\n", ip.String(), domain, maxHops, packetSize) + fmt.Printf("traceroute to %s, %d hops max, %d bytes packets\n", util.HideIPPart(ip.String()), maxHops, packetSize) } } diff --git a/printer/realtime_printer.go b/printer/realtime_printer.go index f7bf110..9b7566a 100644 --- a/printer/realtime_printer.go +++ b/printer/realtime_printer.go @@ -2,6 +2,7 @@ package printer import ( "fmt" + "github.com/nxtrace/NTrace-core/util" "net" "strconv" "strings" @@ -12,7 +13,6 @@ import ( func RealtimePrinter(res *trace.Result, ttl int) { fmt.Printf("%s ", color.New(color.FgHiYellow, color.Bold).Sprintf("%-2d", ttl+1)) - // 去重 var latestIP string tmpMap := make(map[string][]string) @@ -51,13 +51,25 @@ func RealtimePrinter(res *trace.Result, ttl int) { fmt.Printf("%4s", "") } if net.ParseIP(ip).To4() == nil { - fmt.Fprintf(color.Output, "%s", - color.New(color.FgWhite, color.Bold).Sprintf("%-25s", ip), - ) + if util.EnableHidDstIP == "" || ip != util.DestIP { + fmt.Fprintf(color.Output, "%s", + color.New(color.FgWhite, color.Bold).Sprintf("%-25s", ip), + ) + } else { + fmt.Fprintf(color.Output, "%s", + color.New(color.FgWhite, color.Bold).Sprintf("%-25s", util.HideIPPart(ip)), + ) + } } else { - fmt.Fprintf(color.Output, "%s", - color.New(color.FgWhite, color.Bold).Sprintf("%-15s", ip), - ) + if util.EnableHidDstIP == "" || ip != util.DestIP { + fmt.Fprintf(color.Output, "%s", + color.New(color.FgWhite, color.Bold).Sprintf("%-15s", ip), + ) + } else { + fmt.Fprintf(color.Output, "%s", + color.New(color.FgWhite, color.Bold).Sprintf("%-15s", util.HideIPPart(ip)), + ) + } } i, _ := strconv.Atoi(v[0]) diff --git a/util/util.go b/util/util.go index 81c7fcc..15a0839 100644 --- a/util/util.go +++ b/util/util.go @@ -22,6 +22,8 @@ var UserAgent = fmt.Sprintf("NextTrace %s/%s/%s", config.Version, runtime.GOOS, var RdnsCache sync.Map var PowProviderParam = "" var DisableMPLS = GetenvDefault("NEXTTRACE_DISABLEMPLS", "") +var EnableHidDstIP = GetenvDefault("NEXTTRACE_ENABLEHIDDENDSTIP", "") +var DestIP string func LookupAddr(addr string) ([]string, error) { // 如果在缓存中找到,直接返回 @@ -226,3 +228,17 @@ func StringInSlice(val string, list []string) bool { } return false } + +func HideIPPart(ip string) string { + parsedIP := net.ParseIP(ip) + if parsedIP == nil { + return "" + } + + if parsedIP.To4() != nil { + // IPv4: 隐藏后16位 + return strings.Join(strings.Split(ip, ".")[:2], ".") + ".0.0/16" + } + // IPv6: 隐藏后96位 + return parsedIP.Mask(net.CIDRMask(32, 128)).String() + "/32" +}