Skip to content

Commit

Permalink
可设置是否隐匿目的IP
Browse files Browse the repository at this point in the history
  • Loading branch information
tsosunchia committed Nov 13, 2023
1 parent cb63e9b commit 98c0fa3
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ func Excute() {
*port = 53
}

util.DestIP = ip.String()
var conf = trace.Config{
DN42: *dn42,
SrcAddr: *srcAddr,
Expand Down
12 changes: 8 additions & 4 deletions printer/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}

Expand Down
26 changes: 19 additions & 7 deletions printer/realtime_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package printer

import (
"fmt"
"github.com/nxtrace/NTrace-core/util"
"net"
"strconv"
"strings"
Expand All @@ -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)
Expand Down Expand Up @@ -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])
Expand Down
16 changes: 16 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
// 如果在缓存中找到,直接返回
Expand Down Expand Up @@ -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"
}

0 comments on commit 98c0fa3

Please sign in to comment.