-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipv6.go
49 lines (41 loc) · 1.04 KB
/
ipv6.go
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
package resolver
import (
"fmt"
"net"
"sync"
"sync/atomic"
"time"
)
var ipv6Check sync.Once
var ipv6Answered atomic.Bool
var ipv6Available atomic.Bool
// IPv6Available return true if IPv6 Internet connectivity is found.
// If the check has not been performed, it won't block, and (initially) will return false.
func IPv6Available() bool {
if ipv6Answered.Load() {
return ipv6Available.Load()
}
if ipv6Available.Load() {
return true
}
go ipv6Check.Do(UpdateIPv6Availability)
return false
}
func UpdateIPv6Availability() {
defer ipv6Answered.Store(true)
// TODO: This needs to be better. I need to make sure I can actually talk to the server.
// Tries:
// k.root-servers.net
// e.root-servers.net.
// a.root-servers.net.
for _, address := range []string{"2001:7fd::1", "2001:500:a8::e", "2001:503:ba3e::2:30"} {
ipv6Address := fmt.Sprintf("[%s]:53", address)
timeout := 1 * time.Second
conn, err := net.DialTimeout("udp6", ipv6Address, timeout)
ipv6Available.Store(err == nil)
if err == nil {
conn.Close()
return
}
}
}