-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdns_android.go
66 lines (57 loc) · 1.34 KB
/
dns_android.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//go:build android && !cgo
// +build android,!cgo
package main
import (
"context"
"net"
"sync"
"time"
)
func init() {
// On Android, when not using cgo, we need to manually set up the default DNS resolver.
// This resolver will attempt Cloudflare's DNS over both IPv4 and IPv6.
var dialer net.Dialer
dnsServers := []string{
"[2606:4700:4700::1111]:53", // Cloudflare IPv6
"[2606:4700:4700::1001]:53", // Cloudflare IPv6
"1.1.1.1:53", // Cloudflare IPv4
"1.0.0.1:53", // Cloudflare IPv4
}
net.DefaultResolver = &net.Resolver{
PreferGo: false,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var wg sync.WaitGroup
result := make(chan net.Conn, 1)
errChan := make(chan error, len(dnsServers))
for _, ip := range dnsServers {
wg.Add(1)
go func(ip string) {
defer wg.Done()
conn, err := dialer.DialContext(ctx, "udp", ip)
if err == nil {
select {
case result <- conn:
cancel()
default:
}
} else {
errChan <- err
}
}(ip)
}
go func() {
wg.Wait()
close(result)
close(errChan)
}()
select {
case conn := <-result:
return conn, nil
case <-time.After(2 * time.Second):
return nil, net.ErrClosed
}
},
}
}