From b38fc18bb4dbb7f448a1a45ad7d613e1d9de6900 Mon Sep 17 00:00:00 2001 From: tsosunchia <59512455+tsosunchia@users.noreply.github.com> Date: Mon, 29 May 2023 20:53:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8C=87=E5=AE=9A=E8=A7=A3?= =?UTF-8?q?=E6=9E=90IPv4/IPv6=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++++++ README_zh_CN.md | 6 ++++++ cmd/cmd.go | 16 ++++++++++++++-- fast_trace/fast_trace ipv6.go | 2 +- fast_trace/fast_trace.go | 2 +- util/util.go | 18 ++++++++++++++++-- 6 files changed, 44 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9285299..60dd073 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,10 @@ nexttrace --table 1.0.0.1 nexttrace --raw 1.0.0.1 nexttrace --json 1.0.0.1 +# IPv4/IPv6 Resolve Only +nexttrace --ipv4 g.co +nexttrace --ipv6 g.co + # IPv6 ICMP Trace nexttrace 2606:4700:4700::1111 @@ -190,6 +194,8 @@ Usage: nexttrace [-h|--help] [-T|--tcp] [-U|--udp] [-F|--fast-trace] [-p|--port Arguments: -h --help Print help information + -4 --ipv4 Use IPv4 only + -6 --ipv6 Use IPv6 only -T --tcp Use TCP SYN for tracerouting (default port is 80) -U --udp Use UDP SYN for tracerouting (default port diff --git a/README_zh_CN.md b/README_zh_CN.md index dd957f8..57733ff 100644 --- a/README_zh_CN.md +++ b/README_zh_CN.md @@ -66,6 +66,10 @@ nexttrace --table 1.0.0.1 nexttrace --raw 1.0.0.1 nexttrace --json 1.0.0.1 +# 只进行IPv4/IPv6解析 +nexttrace --ipv4 g.co +nexttrace --ipv6 g.co + # IPv6 ICMP Trace nexttrace 2606:4700:4700::1111 @@ -197,6 +201,8 @@ Usage: nexttrace [-h|--help] [-T|--tcp] [-U|--udp] [-F|--fast-trace] [-p|--port Arguments: -h --help Print help information + -4 --ipv4 Use IPv4 only + -6 --ipv6 Use IPv6 only -T --tcp Use TCP SYN for tracerouting (default port is 80) -U --udp Use UDP SYN for tracerouting (default port diff --git a/cmd/cmd.go b/cmd/cmd.go index b39e4c7..9992563 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -28,6 +28,8 @@ import ( func Excute() { parser := argparse.NewParser("nexttrace", "An open source visual route tracking CLI tool") // Create string flag + ipv4Only := parser.Flag("4", "ipv4", &argparse.Options{Help: "Use IPv4 only"}) + ipv6Only := parser.Flag("6", "ipv6", &argparse.Options{Help: "Use IPv6 only"}) tcp := parser.Flag("T", "tcp", &argparse.Options{Help: "Use TCP SYN for tracerouting (default port is 80)"}) udp := parser.Flag("U", "udp", &argparse.Options{Help: "Use UDP SYN for tracerouting (default port is 53)"}) fast_trace := parser.Flag("F", "fast-trace", &argparse.Options{Help: "One-Key Fast Trace to China ISPs"}) @@ -120,9 +122,19 @@ func Excute() { } if *udp { - ip = util.DomainLookUp(domain, true, *dot, *jsonPrint) + if *ipv6Only { + fmt.Println("[Info] IPv6 UDP Traceroute is not supported right now.") + os.Exit(0) + } + ip = util.DomainLookUp(domain, "4", *dot, *jsonPrint) } else { - ip = util.DomainLookUp(domain, false, *dot, *jsonPrint) + if *ipv6Only { + ip = util.DomainLookUp(domain, "6", *dot, *jsonPrint) + } else if *ipv4Only { + ip = util.DomainLookUp(domain, "4", *dot, *jsonPrint) + } else { + ip = util.DomainLookUp(domain, "all", *dot, *jsonPrint) + } } if *src_dev != "" { diff --git a/fast_trace/fast_trace ipv6.go b/fast_trace/fast_trace ipv6.go index a0881b1..41b2642 100644 --- a/fast_trace/fast_trace ipv6.go +++ b/fast_trace/fast_trace ipv6.go @@ -33,7 +33,7 @@ func (f *FastTracer) tracert_v6(location string, ispCollection ISPCollection) { log.Printf("『%s %s 』\n", location, ispCollection.ISPName) fmt.Printf("traceroute to %s, 30 hops max, 32 byte packets\n", ispCollection.IPv6) log.Printf("traceroute to %s, 30 hops max, 32 byte packets\n", ispCollection.IPv6) - ip := util.DomainLookUp(ispCollection.IP, false, "", true) + ip := util.DomainLookUp(ispCollection.IP, "6", "", true) var conf = trace.Config{ BeginHop: 1, DestIP: ip, diff --git a/fast_trace/fast_trace.go b/fast_trace/fast_trace.go index 5f2719e..7fc86e3 100644 --- a/fast_trace/fast_trace.go +++ b/fast_trace/fast_trace.go @@ -39,7 +39,7 @@ func (f *FastTracer) tracert(location string, ispCollection ISPCollection) { log.Printf("『%s %s 』\n", location, ispCollection.ISPName) fmt.Printf("traceroute to %s, 30 hops max, 32 byte packets\n", ispCollection.IP) log.Printf("traceroute to %s, 30 hops max, 32 byte packets\n", ispCollection.IP) - ip := util.DomainLookUp(ispCollection.IP, true, "", true) + ip := util.DomainLookUp(ispCollection.IP, "4", "", true) var conf = trace.Config{ BeginHop: 1, DestIP: ip, diff --git a/util/util.go b/util/util.go index e2c00ae..557fd91 100644 --- a/util/util.go +++ b/util/util.go @@ -6,6 +6,7 @@ import ( "log" "net" "os" + "strings" "github.com/fatih/color" ) @@ -45,8 +46,8 @@ func LocalIPPortv6(dstip net.IP) (net.IP, int) { return nil, -1 } -func DomainLookUp(host string, ipv4Only bool, dotServer string, disableOutput bool) net.IP { - // TODO: IPv4Only功能 +func DomainLookUp(host string, ipVersion string, dotServer string, disableOutput bool) net.IP { + // ipVersion: 4, 6, all var ( r *net.Resolver ips []net.IP @@ -84,6 +85,19 @@ func DomainLookUp(host string, ipv4Only bool, dotServer string, disableOutput bo // } //} + // Filter by IPv4/IPv6 + if ipVersion != "all" { + var filteredIPs []net.IP + for _, ip := range ips { + if ipVersion == "4" && ip.To4() != nil { + filteredIPs = append(filteredIPs, ip) + } else if ipVersion == "6" && strings.Contains(ip.String(), ":") { + filteredIPs = append(filteredIPs, ip) + } + } + ips = filteredIPs + } + if (len(ips) == 1) || (disableOutput) { return ips[0] } else {