-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
ipv6.go
46 lines (41 loc) · 1006 Bytes
/
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
package support
import (
"context"
"fmt"
"net"
"net/netip"
"strings"
"time"
)
func IPv6(ctx context.Context, ipv6AddrPort netip.AddrPort) (
ipv6Supported bool, err error) {
if !ipv6AddrPort.IsValid() {
const cloudflareIPv6AddrPort = "[2606:4700:4700::1111]:443"
ipv6AddrPort = netip.MustParseAddrPort(cloudflareIPv6AddrPort)
}
dialer := net.Dialer{
Timeout: time.Second,
}
conn, err := dialer.DialContext(ctx, "tcp", ipv6AddrPort.String())
if err != nil {
if ctxErr := ctx.Err(); ctxErr != nil {
return false, ctxErr
}
errMessage := err.Error()
ipv6ErrorMessages := []string{
"connect: network is unreachable",
"cannot assign requested address",
}
for _, ipv6ErrorMessage := range ipv6ErrorMessages {
if strings.Contains(errMessage, ipv6ErrorMessage) {
return false, nil
}
}
return false, fmt.Errorf("unknown error: %w", err)
}
err = conn.Close()
if err != nil {
return false, fmt.Errorf("closing connection: %w", err)
}
return true, nil
}