可设置是否隐匿目的IP

This commit is contained in:
tsosunchia
2023-11-13 10:54:17 +08:00
parent cb63e9b61c
commit 98c0fa38ca
6 changed files with 48 additions and 11 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -245,6 +245,7 @@ func Excute() {
*port = 53
}
util.DestIP = ip.String()
var conf = trace.Config{
DN42: *dn42,
SrcAddr: *srcAddr,

View File

@@ -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)
}
}

View File

@@ -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])

View File

@@ -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"
}