Skip to content

Commit

Permalink
Merge pull request nxtrace#122 from sjlleo/resovle_v4_v6_only
Browse files Browse the repository at this point in the history
增加指定解析IPv4/IPv6的功能
  • Loading branch information
tsosunchia authored May 29, 2023
2 parents 23d20de + b38fc18 commit 86b8172
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down Expand Up @@ -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 != "" {
Expand Down
2 changes: 1 addition & 1 deletion fast_trace/fast_trace ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion fast_trace/fast_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 16 additions & 2 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net"
"os"
"strings"

"github.com/fatih/color"
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 86b8172

Please sign in to comment.