-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathdns_android.go
More file actions
42 lines (37 loc) · 1.07 KB
/
Copy pathdns_android.go
File metadata and controls
42 lines (37 loc) · 1.07 KB
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
//go:build android
package main
import (
"context"
"net"
"os"
"path/filepath"
"strings"
)
// Android ships no /etc/resolv.conf, so Go's own resolver falls back to
// 127.0.0.1:53 and every lookup fails "connection refused" - which is every
// command, since even -relay has a hostname. Termux keeps its own copy under
// $PREFIX/etc, so honour that and fall back to the WARP resolver.
const androidFallbackDNS = "1.1.1.1:53"
func init() {
server := termuxNameserver()
net.DefaultResolver.Dial = func(ctx context.Context, network, _ string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, network, server)
}
}
func termuxNameserver() string {
prefix := os.Getenv("PREFIX")
if prefix == "" {
return androidFallbackDNS
}
data, err := os.ReadFile(filepath.Join(prefix, "etc", "resolv.conf"))
if err != nil {
return androidFallbackDNS
}
for _, line := range strings.Split(string(data), "\n") {
fields := strings.Fields(line)
if len(fields) >= 2 && fields[0] == "nameserver" {
return net.JoinHostPort(fields[1], "53")
}
}
return androidFallbackDNS
}