summaryrefslogtreecommitdiff
path: root/ipv6test.go.new
blob: c9d814e28b27f0e68fd462ea06a89eda35e4ed64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main

import (
	"context"
	"fmt"
	"net"
	"os"
	"time"
)

const (
	ipv6TestHost = "ipv6.google.com"
	testPort     = "443" // HTTPS port
)

func main() {
	// --- Step 1: Fast DNS Lookup ---
	// We'll use a separate, short timeout just for the DNS resolution.
	// If DNS is broken, we'll know in a second or two.
	dnsTimeout := 2 * time.Second
	ctx, cancel := context.WithTimeout(context.Background(), dnsTimeout)
	defer cancel()

	// net.DefaultResolver.LookupIPAddr forces the resolver to look up the host.
	// We specify "ip6" to ensure we only get AAAA records.
	addrs, err := net.DefaultResolver.LookupIPAddr(ctx, "ip6", ipv6TestHost)
	if err != nil {
		fmt.Printf("IPv6 DNS resolution failed: %v\n", err)
		fmt.Println("This likely means your network cannot resolve IPv6 addresses.")
		os.Exit(1)
	}

	if len(addrs) == 0 {
		fmt.Printf("No IPv6 addresses found for %s\n", ipv6TestHost)
		os.Exit(1)
	}

	// If we get here, DNS is working. Let's use the first address we found.
	targetIP := addrs[0].IP.String()
	fmt.Printf("Successfully resolved %s to IPv6 address: %s\n", ipv6TestHost, targetIP)

	// --- Step 2: Connection Attempt ---
	// Now we try to connect to the IP we found. This tests routing and firewalls.
	connectionTimeout := 3 * time.Second
	conn, err := net.DialTimeout("tcp6", net.JoinHostPort(targetIP, testPort), connectionTimeout)
	if err != nil {
		fmt.Printf("IPv6 connection to %s failed: %v\n", targetIP, err)
		fmt.Println("This likely means your network has an IPv6 routing or firewall issue.")
		os.Exit(1)
	}
	defer conn.Close()

	fmt.Println("IPv6 connectivity test successful.")
	os.Exit(0)
}