/
dns.go
106 lines (85 loc) · 2.19 KB
/
dns.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* SPDX-License-Identifier: Apache-2.0
* Copyright 2020 vorteil.io Pty Ltd
*/
package vorteil
import (
"fmt"
"net"
"strings"
"time"
"github.com/AdguardTeam/dnsproxy/proxy"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/log"
)
const (
defaultDNSAddr = "127.0.0.1"
)
func printDNS(dns []string) {
if len(dns) > 0 {
logAlways("dns\t\t: %s", strings.Join(dns, ", "))
} else {
logAlways("dns\t\t: none")
}
}
var dns []string
func (v *Vinitd) startDNS(dnsAddr string, verbose bool) error {
// only add config DNS if not provided by DHCP
if len(v.dns) == 0 {
for _, d := range v.vcfg.System.DNS {
// replace envs
for k, val := range v.hypervisorInfo.envs {
d = strings.ReplaceAll(d, fmt.Sprintf(replaceString, k), val)
}
ip := net.ParseIP(d)
if ip != nil {
v.dns = append(v.dns, ip)
}
}
}
// remove potential duplicates
v.dns = uniqueIPs(v.dns)
// additional loop to add a dns from dhcp if there were any
for _, d := range v.dns {
dns = append(dns, d.String())
}
if verbose {
printDNS(dns)
}
// don't start
if len(dns) == 0 {
return nil
}
config := proxy.Config{
Ratelimit: 0,
CacheEnabled: true,
CacheSizeBytes: 65536,
CacheMinTTL: 60,
CacheMaxTTL: 600,
RefuseAny: false,
EnableEDNSClientSubnet: true,
UDPBufferSize: 65536,
MaxGoroutines: 10,
UpstreamMode: proxy.UModeParallel,
}
ua := &net.UDPAddr{Port: 53, IP: net.ParseIP(defaultDNSAddr)}
config.UDPListenAddr = append(config.UDPListenAddr, ua)
ta := &net.TCPAddr{Port: 53, IP: net.ParseIP(defaultDNSAddr)}
config.TCPListenAddr = append(config.TCPListenAddr, ta)
upstreamConfig, err := proxy.ParseUpstreamsConfig(dns,
upstream.Options{
InsecureSkipVerify: false,
Bootstrap: []string{},
Timeout: 10 * time.Second,
})
// upstreamConfig, err := proxy.ParseUpstreamsConfig(dns, []string{}, 10*time.Second)
if err != nil {
logError("can not start dns: %v", err)
return err
}
config.UpstreamConfig = &upstreamConfig
log.SetLevel(log.ERROR)
dnsProxy := proxy.Proxy{Config: config}
err = dnsProxy.Start()
return err
}