Skip to content

Commit e894404

Browse files
committed
Merge remote-tracking branch 'upstream/master' into multiple_relays
2 parents e6e876b + 9b61b73 commit e894404

File tree

2,008 files changed

+8466
-331441
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,008 files changed

+8466
-331441
lines changed

.github/workflows/releases.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
run: echo "VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_OUTPUT
2929

3030
- name: Set up Go
31-
uses: actions/setup-go@v3
31+
uses: actions/setup-go@v4
3232
with:
3333
go-version: 1
3434
id: go
@@ -78,7 +78,7 @@ jobs:
7878
prerelease: false
7979

8080
- name: Upload release assets
81-
uses: softprops/action-gh-release@d4e8205d7e959a9107da6396278b2f1f07af0f9b
81+
uses: softprops/action-gh-release@c9b46fe7aad9f02afd89b12450b780f52dacfb2d
8282
if: startsWith(github.ref, 'refs/tags/')
8383
env:
8484
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

dnscrypt-proxy/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ func (config *Config) loadSource(proxy *Proxy, cfgSourceName string, cfgSource *
927927
cfgSource.Prefix,
928928
)
929929
if err != nil {
930-
if len(source.in) <= 0 {
930+
if len(source.bin) <= 0 {
931931
dlog.Criticalf("Unable to retrieve source [%s]: [%s]", cfgSourceName, err)
932932
return err
933933
}

dnscrypt-proxy/example-dnscrypt-proxy.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,17 @@ cert_refresh_delay = 240
256256
bootstrap_resolvers = ['9.9.9.11:53', '8.8.8.8:53']
257257

258258

259-
## Always use the bootstrap resolver before the system DNS settings.
259+
## When internal DNS resolution is required, for example to retrieve
260+
## the resolvers list:
261+
##
262+
## - queries will be sent to dnscrypt-proxy itself, if it is already
263+
## running with active servers (*)
264+
## - or else, queries will be sent to fallback servers
265+
## - finally, if `ignore_system_dns` is `false`, queries will be sent
266+
## to the system DNS
267+
##
268+
## (*) this is incompatible with systemd sockets.
269+
## `listen_addrs` must not be empty.
260270

261271
ignore_system_dns = true
262272

dnscrypt-proxy/example-forwarding-rules.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@
2323
# 192.in-addr.arpa 192.168.1.1
2424

2525
## Forward queries for example.com and *.example.com to 9.9.9.9 and 8.8.8.8
26-
# example.com 9.9.9.9,8.8.8.8
26+
# example.com 9.9.9.9,8.8.8.8
27+
28+
## Forward queries for .onion names to a local Tor client
29+
## Tor must be configured with the following in the torrc file:
30+
## DNSPort 9053
31+
## AutomapHostsOnResolve 1
32+
33+
# onion 127.0.0.1:9053

dnscrypt-proxy/proxy.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ func (proxy *Proxy) StartProxy() {
248248
dlog.Fatal(err)
249249
}
250250
}
251+
proxy.xTransport.internalResolverReady = false
252+
proxy.xTransport.internalResolvers = proxy.listenAddresses
251253
liveServers, err := proxy.serversInfo.refresh(proxy)
252254
if liveServers > 0 {
253255
proxy.certIgnoreTimestamp = false

dnscrypt-proxy/serversInfo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ func (serversInfo *ServersInfo) refresh(proxy *Proxy) (int, error) {
242242
for _, registeredServer := range registeredServers {
243243
if err = serversInfo.refreshServer(proxy, registeredServer.name, registeredServer.stamp); err == nil {
244244
liveServers++
245+
proxy.xTransport.internalResolverReady = true
245246
}
246247
}
247248
serversInfo.Lock()

dnscrypt-proxy/sources.go

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package main
33
import (
44
"bytes"
55
"fmt"
6+
"math"
67
"math/rand"
78
"net/url"
89
"os"
9-
"path/filepath"
1010
"strings"
1111
"time"
1212

@@ -31,13 +31,13 @@ const (
3131
type Source struct {
3232
name string
3333
urls []*url.URL
34-
format SourceFormat
35-
in []byte
34+
bin []byte // copy of the file content - there's something wrong in our logic, we shouldn't need to keep that in memory
3635
minisignKey *minisign.PublicKey
3736
cacheFile string
37+
prefix string
3838
cacheTTL, prefetchDelay time.Duration
3939
refresh time.Time
40-
prefix string
40+
format SourceFormat
4141
}
4242

4343
func (source *Source) checkSignature(bin, sig []byte) (err error) {
@@ -48,10 +48,10 @@ func (source *Source) checkSignature(bin, sig []byte) (err error) {
4848
return err
4949
}
5050

51-
// timeNow can be replaced by tests to provide a static value
51+
// timeNow() can be replaced by tests to provide a static value
5252
var timeNow = time.Now
5353

54-
func (source *Source) fetchFromCache(now time.Time) (delay time.Duration, err error) {
54+
func (source *Source) fetchFromCache(now time.Time) (remaining time.Duration, err error) {
5555
var bin, sig []byte
5656
if bin, err = os.ReadFile(source.cacheFile); err != nil {
5757
return
@@ -62,14 +62,14 @@ func (source *Source) fetchFromCache(now time.Time) (delay time.Duration, err er
6262
if err = source.checkSignature(bin, sig); err != nil {
6363
return
6464
}
65-
source.in = bin
65+
source.bin = bin
6666
var fi os.FileInfo
6767
if fi, err = os.Stat(source.cacheFile); err != nil {
6868
return
6969
}
7070
if elapsed := now.Sub(fi.ModTime()); elapsed < source.cacheTTL {
71-
delay = source.prefetchDelay - elapsed
72-
dlog.Debugf("Source [%s] cache file [%s] is still fresh, next update: %v", source.name, source.cacheFile, delay)
71+
remaining = source.prefetchDelay - elapsed
72+
dlog.Debugf("Source [%s] cache file [%s] is still fresh, next update: %v min", source.name, source.cacheFile, math.Round(remaining.Minutes()))
7373
} else {
7474
dlog.Debugf("Source [%s] cache file [%s] needs to be refreshed", source.name, source.cacheFile)
7575
}
@@ -98,25 +98,24 @@ func writeSource(f string, bin, sig []byte) (err error) {
9898
return fSig.Commit()
9999
}
100100

101-
func (source *Source) writeToCache(bin, sig []byte, now time.Time) {
101+
func (source *Source) updateCache(bin, sig []byte, now time.Time) error {
102102
f := source.cacheFile
103-
var writeErr error // an error writing cache isn't fatal
104-
defer func() {
105-
source.in = bin
106-
if writeErr == nil {
107-
return
108-
}
109-
if absPath, absErr := filepath.Abs(f); absErr == nil {
110-
f = absPath
111-
}
112-
dlog.Warnf("%s: %s", f, writeErr)
113-
}()
114-
if !bytes.Equal(source.in, bin) {
115-
if writeErr = writeSource(f, bin, sig); writeErr != nil {
116-
return
117-
}
103+
// If the data is unchanged, update the files timestamps only
104+
if bytes.Equal(source.bin, bin) {
105+
_ = os.Chtimes(f, now, now)
106+
_ = os.Chtimes(f+".minisig", now, now)
107+
return nil
108+
}
109+
// Otherwise, write the new data and signature
110+
if err := writeSource(f, bin, sig); err != nil {
111+
dlog.Warnf("Source [%s] failed to update cache file [%s]: %v", source.name, f, err)
112+
return err
118113
}
119-
writeErr = os.Chtimes(f, now, now)
114+
source.bin = bin // In-memory copy of the cache file content
115+
// The tests require the timestamps to be updated, no idea why
116+
_ = os.Chtimes(f, now, now)
117+
_ = os.Chtimes(f+".minisig", now, now)
118+
return nil
120119
}
121120

122121
func (source *Source) parseURLs(urls []string) {
@@ -134,23 +133,23 @@ func fetchFromURL(xTransport *XTransport, u *url.URL) (bin []byte, err error) {
134133
return bin, err
135134
}
136135

137-
func (source *Source) fetchWithCache(xTransport *XTransport, now time.Time) (delay time.Duration, err error) {
138-
if delay, err = source.fetchFromCache(now); err != nil {
136+
func (source *Source) fetchWithCache(xTransport *XTransport, now time.Time) (time.Duration, error) {
137+
remaining, err := source.fetchFromCache(now)
138+
if err != nil {
139139
if len(source.urls) == 0 {
140-
dlog.Errorf("Source [%s] cache file [%s] not present and no valid URL", source.name, source.cacheFile)
141-
return
140+
dlog.Fatalf("Source [%s] cache file [%s] not present and no valid URL", source.name, source.cacheFile)
141+
return 0, err
142142
}
143143
dlog.Debugf("Source [%s] cache file [%s] not present", source.name, source.cacheFile)
144144
}
145-
if len(source.urls) > 0 {
146-
defer func() {
147-
source.refresh = now.Add(delay)
148-
}()
145+
if len(source.urls) == 0 {
146+
dlog.Debugf("No URL to update [%s]", source.name)
147+
return 24 * time.Hour, nil
149148
}
150-
if len(source.urls) == 0 || delay > 0 {
151-
return
149+
if remaining > 0 {
150+
source.refresh = now.Add(remaining)
151+
return remaining, nil
152152
}
153-
delay = MinimumPrefetchInterval
154153
var bin, sig []byte
155154
for _, srcURL := range source.urls {
156155
dlog.Infof("Source [%s] loading from URL [%s]", source.name, srcURL)
@@ -171,11 +170,13 @@ func (source *Source) fetchWithCache(xTransport *XTransport, now time.Time) (del
171170
dlog.Debugf("Source [%s] failed signature check using URL [%s]", source.name, srcURL)
172171
}
173172
if err != nil {
174-
return
173+
source.refresh = now.Add(MinimumPrefetchInterval)
174+
return MinimumPrefetchInterval, err
175175
}
176-
source.writeToCache(bin, sig, now)
177-
delay = source.prefetchDelay
178-
return
176+
source.updateCache(bin, sig, now)
177+
remaining = source.prefetchDelay
178+
source.refresh = now.Add(remaining)
179+
return remaining, nil
179180
}
180181

181182
// NewSource loads a new source using the given cacheFile and urls, ensuring it has a valid signature
@@ -229,7 +230,7 @@ func PrefetchSources(xTransport *XTransport, sources []*Source) time.Duration {
229230
if delay, err := source.fetchWithCache(xTransport, now); err != nil {
230231
dlog.Infof("Prefetching [%s] failed: %v, will retry in %v", source.name, err, interval)
231232
} else {
232-
dlog.Debugf("Prefetching [%s] succeeded, next update: %v", source.name, delay)
233+
dlog.Debugf("Prefetching [%s] succeeded, next update: %v min", source.name, math.Round(delay.Minutes()))
233234
if delay >= MinimumPrefetchInterval && (interval == MinimumPrefetchInterval || interval > delay) {
234235
interval = delay
235236
}
@@ -254,8 +255,8 @@ func (source *Source) parseV2() ([]RegisteredServer, error) {
254255
stampErrs = append(stampErrs, stampErr)
255256
dlog.Warn(stampErr)
256257
}
257-
in := string(source.in)
258-
parts := strings.Split(in, "## ")
258+
bin := string(source.bin)
259+
parts := strings.Split(bin, "## ")
259260
if len(parts) < 2 {
260261
return registeredServers, fmt.Errorf("Invalid format for source at [%v]", source.urls)
261262
}

dnscrypt-proxy/sources_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,9 @@ func prepSourceTestCache(t *testing.T, d *SourceTestData, e *SourceTestExpect, s
284284
e.cache = []SourceFixture{d.fixtures[state][source], d.fixtures[state][source+".minisig"]}
285285
switch state {
286286
case TestStateCorrect:
287-
e.Source.in, e.success = e.cache[0].content, true
287+
e.Source.bin, e.success = e.cache[0].content, true
288288
case TestStateExpired:
289-
e.Source.in = e.cache[0].content
289+
e.Source.bin = e.cache[0].content
290290
case TestStatePartial, TestStatePartialSig:
291291
e.err = "signature"
292292
case TestStateMissing, TestStateMissingSig, TestStateOpenErr, TestStateOpenSigErr:
@@ -339,7 +339,7 @@ func prepSourceTestDownload(
339339
switch state {
340340
case TestStateCorrect:
341341
e.cache = []SourceFixture{d.fixtures[state][source], d.fixtures[state][source+".minisig"]}
342-
e.Source.in, e.success = e.cache[0].content, true
342+
e.Source.bin, e.success = e.cache[0].content, true
343343
fallthrough
344344
case TestStateMissingSig, TestStatePartial, TestStatePartialSig, TestStateReadSigErr:
345345
d.reqExpect[path+".minisig"]++
@@ -477,7 +477,7 @@ func TestPrefetchSources(t *testing.T) {
477477
e.mtime = d.timeUpd
478478
s := &Source{}
479479
*s = *e.Source
480-
s.in = nil
480+
s.bin = nil
481481
sources = append(sources, s)
482482
expects = append(expects, e)
483483
}

0 commit comments

Comments
 (0)