forked from Psiphon-Labs/psiphon-tunnel-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
243 lines (197 loc) · 6.62 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright (c) 2016, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package server
import (
"bufio"
"bytes"
"errors"
"math/rand"
"net"
"strings"
"sync/atomic"
"time"
"github.com/Psiphon-Labs/goarista/monotime"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
)
const (
DNS_SYSTEM_CONFIG_FILENAME = "/etc/resolv.conf"
DNS_SYSTEM_CONFIG_RELOAD_PERIOD = 5 * time.Second
DNS_RESOLVER_PORT = 53
)
// DNSResolver maintains fresh DNS resolver values, monitoring
// "/etc/resolv.conf" on platforms where it is available; and
// otherwise using a default value.
type DNSResolver struct {
// Note: 64-bit ints used with atomic operations are placed
// at the start of struct to ensure 64-bit alignment.
// (https://golang.org/pkg/sync/atomic/#pkg-note-BUG)
lastReloadTime int64
common.ReloadableFile
isReloading int32
resolvers []net.IP
}
// NewDNSResolver initializes a new DNSResolver, loading it with
// fresh resolver values. The load must succeed, so either
// "/etc/resolv.conf" must contain valid "nameserver" lines with
// a DNS server IP address, or a valid "defaultResolver" default
// value must be provided.
// On systems without "/etc/resolv.conf", "defaultResolver" is
// required.
//
// The resolver is considered stale and reloaded if last checked
// more than 5 seconds before the last Get(), which is similar to
// frequencies in other implementations:
//
// - https://golang.org/src/net/dnsclient_unix.go,
// resolverConfig.tryUpdate: 5 seconds
//
// - https://github.com/ambrop72/badvpn/blob/master/udpgw/udpgw.c,
// maybe_update_dns: 2 seconds
//
func NewDNSResolver(defaultResolver string) (*DNSResolver, error) {
dns := &DNSResolver{
lastReloadTime: int64(monotime.Now()),
}
dns.ReloadableFile = common.NewReloadableFile(
DNS_SYSTEM_CONFIG_FILENAME,
func(fileContent []byte) error {
resolvers, err := parseResolveConf(fileContent)
if err != nil {
// On error, state remains the same
return common.ContextError(err)
}
dns.resolvers = resolvers
log.WithContextFields(
LogFields{
"resolvers": resolvers,
}).Debug("loaded system DNS resolvers")
return nil
})
_, err := dns.Reload()
if err != nil {
if defaultResolver == "" {
return nil, common.ContextError(err)
}
log.WithContextFields(
LogFields{"err": err}).Info(
"failed to load system DNS resolver; using default")
resolver, err := parseResolver(defaultResolver)
if err != nil {
return nil, common.ContextError(err)
}
dns.resolvers = []net.IP{resolver}
}
return dns, nil
}
// Get returns one of the cached resolvers, selected at random,
// after first updating the cached values if they're stale. If
// reloading fails, the previous values are used.
//
// Randomly selecting any one of the configured resolvers is
// expected to be more resiliant to failure; e.g., if one of
// the resolvers becomes unavailable.
func (dns *DNSResolver) Get() net.IP {
dns.reloadWhenStale()
dns.ReloadableFile.RLock()
defer dns.ReloadableFile.RUnlock()
return dns.resolvers[rand.Intn(len(dns.resolvers))]
}
func (dns *DNSResolver) reloadWhenStale() {
// Every UDP DNS port forward frequently calls Get(), so this code
// is intended to minimize blocking. Most callers will hit just the
// atomic.LoadInt64 reload time check and the RLock (an atomic.AddInt32
// when no write lock is pending). An atomic.CompareAndSwapInt32 is
// used to ensure only one goroutine enters Reload() and blocks on
// its write lock. Finally, since since ReloadableFile.Reload
// checks whether the underlying file has changed _before_ acquiring a
// write lock, we only incur write lock blocking when "/etc/resolv.conf"
// has actually changed.
lastReloadTime := monotime.Time(atomic.LoadInt64(&dns.lastReloadTime))
stale := monotime.Now().After(lastReloadTime.Add(DNS_SYSTEM_CONFIG_RELOAD_PERIOD))
if stale {
isReloader := atomic.CompareAndSwapInt32(&dns.isReloading, 0, 1)
if isReloader {
// Unconditionally set last reload time. Even on failure only
// want to retry after another DNS_SYSTEM_CONFIG_RELOAD_PERIOD.
atomic.StoreInt64(&dns.lastReloadTime, time.Now().Unix())
_, err := dns.Reload()
if err != nil {
log.WithContextFields(
LogFields{"err": err}).Info(
"failed to reload system DNS resolver")
}
atomic.StoreInt32(&dns.isReloading, 0)
}
}
}
// GetAllIPv4 returns a list of all IPv4 DNS resolver addresses.
// Cached values are updated if they're stale. If reloading fails,
// the previous values are used.
func (dns *DNSResolver) GetAllIPv4() []net.IP {
return dns.getAll(false)
}
// GetAllIPv6 returns a list of all IPv6 DNS resolver addresses.
// Cached values are updated if they're stale. If reloading fails,
// the previous values are used.
func (dns *DNSResolver) GetAllIPv6() []net.IP {
return dns.getAll(true)
}
func (dns *DNSResolver) getAll(wantIPv6 bool) []net.IP {
dns.reloadWhenStale()
dns.ReloadableFile.RLock()
defer dns.ReloadableFile.RUnlock()
resolvers := make([]net.IP, 0)
for _, resolver := range dns.resolvers {
if (resolver.To4() == nil) == wantIPv6 {
resolvers = append(resolvers, resolver)
}
}
return resolvers
}
func parseResolveConf(fileContent []byte) ([]net.IP, error) {
scanner := bufio.NewScanner(bytes.NewReader(fileContent))
var resolvers []net.IP
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, ";") || strings.HasPrefix(line, "#") {
continue
}
fields := strings.Fields(line)
if len(fields) == 2 && fields[0] == "nameserver" {
resolver, err := parseResolver(fields[1])
if err == nil {
resolvers = append(resolvers, resolver)
}
}
}
if err := scanner.Err(); err != nil {
return nil, common.ContextError(err)
}
if len(resolvers) == 0 {
return nil, common.ContextError(errors.New("no nameservers found"))
}
return resolvers, nil
}
func parseResolver(resolver string) (net.IP, error) {
ipAddress := net.ParseIP(resolver)
if ipAddress == nil {
return nil, common.ContextError(errors.New("invalid IP address"))
}
return ipAddress, nil
}