Skip to content

Commit f35357e

Browse files
committed
Simplify the forwarding syntax
1 parent 35e32b8 commit f35357e

7 files changed

Lines changed: 77 additions & 34 deletions

File tree

.travis.yml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,119 +22,119 @@ script:
2222
- env GOOS=windows GOARCH=386 go build
2323
- mkdir win32
2424
- ln dnscrypt-proxy.exe win32/
25-
- ln dnscrypt-proxy.toml forwarding-rules.txt win32/
25+
- ln dnscrypt-proxy.toml forwarding-rules.txt blacklist.txt win32/
2626
- zip -9 -r dnscrypt-proxy-win32-${TRAVIS_TAG:-dev}.zip win32
2727

2828
- go clean
2929
- env GOOS=windows GOARCH=amd64 go build
3030
- mkdir win64
3131
- ln dnscrypt-proxy.exe win64/
32-
- ln dnscrypt-proxy.toml forwarding-rules.txt win64/
32+
- ln dnscrypt-proxy.toml forwarding-rules.txt blacklist.txt win64/
3333
- zip -9 -r dnscrypt-proxy-win64-${TRAVIS_TAG:-dev}.zip win64
3434

3535
- go clean
3636
- env GOOS=openbsd GOARCH=386 go build
3737
- mkdir openbsd-i386
3838
- ln dnscrypt-proxy openbsd-i386/
39-
- ln dnscrypt-proxy.toml forwarding-rules.txt openbsd-i386/
39+
- ln dnscrypt-proxy.toml forwarding-rules.txt blacklist.txt openbsd-i386/
4040
- tar czpvf dnscrypt-proxy-openbsd_i386-${TRAVIS_TAG:-dev}.tar.gz openbsd-i386
4141

4242
- go clean
4343
- env GOOS=openbsd GOARCH=amd64 go build
4444
- mkdir openbsd-amd64
4545
- ln dnscrypt-proxy openbsd-amd64/
46-
- ln dnscrypt-proxy.toml forwarding-rules.txt openbsd-amd64/
46+
- ln dnscrypt-proxy.toml forwarding-rules.txt blacklist.txt openbsd-amd64/
4747
- tar czpvf dnscrypt-proxy-openbsd_amd64-${TRAVIS_TAG:-dev}.tar.gz openbsd-amd64
4848

4949
- go clean
5050
- env GOOS=freebsd GOARCH=386 go build
5151
- mkdir freebsd-i386
5252
- ln dnscrypt-proxy freebsd-i386/
53-
- ln dnscrypt-proxy.toml forwarding-rules.txt freebsd-i386/
53+
- ln dnscrypt-proxy.toml forwarding-rules.txt blacklist.txt freebsd-i386/
5454
- tar czpvf dnscrypt-proxy-freebsd_i386-${TRAVIS_TAG:-dev}.tar.gz freebsd-i386
5555

5656
- go clean
5757
- env GOOS=freebsd GOARCH=amd64 go build
5858
- mkdir freebsd-amd64
5959
- ln dnscrypt-proxy freebsd-amd64/
60-
- ln dnscrypt-proxy.toml forwarding-rules.txt freebsd-amd64/
60+
- ln dnscrypt-proxy.toml forwarding-rules.txt blacklist.txt freebsd-amd64/
6161
- tar czpvf dnscrypt-proxy-freebsd_amd64-${TRAVIS_TAG:-dev}.tar.gz freebsd-amd64
6262

6363
- go clean
6464
- env GOOS=dragonfly GOARCH=amd64 go build
6565
- mkdir dragonflybsd-amd64
6666
- ln dnscrypt-proxy dragonflybsd-amd64/
67-
- ln dnscrypt-proxy.toml forwarding-rules.txt dragonflybsd-amd64/
67+
- ln dnscrypt-proxy.toml forwarding-rules.txt blacklist.txt dragonflybsd-amd64/
6868
- tar czpvf dnscrypt-proxy-dragonflybsd_amd64-${TRAVIS_TAG:-dev}.tar.gz dragonflybsd-amd64
6969

7070
- go clean
7171
- env GOOS=netbsd GOARCH=386 go build
7272
- mkdir netbsd-i386
7373
- ln dnscrypt-proxy netbsd-i386/
74-
- ln dnscrypt-proxy.toml forwarding-rules.txt netbsd-i386/
74+
- ln dnscrypt-proxy.toml forwarding-rules.txt blacklist.txt netbsd-i386/
7575
- tar czpvf dnscrypt-proxy-netbsd_i386-${TRAVIS_TAG:-dev}.tar.gz netbsd-i386
7676

7777
- go clean
7878
- env GOOS=netbsd GOARCH=amd64 go build
7979
- mkdir netbsd-amd64
8080
- ln dnscrypt-proxy netbsd-amd64/
81-
- ln dnscrypt-proxy.toml forwarding-rules.txt netbsd-amd64/
81+
- ln dnscrypt-proxy.toml forwarding-rules.txt blacklist.txt netbsd-amd64/
8282
- tar czpvf dnscrypt-proxy-netbsd_amd64-${TRAVIS_TAG:-dev}.tar.gz netbsd-amd64
8383

8484
- go clean
8585
- env GOOS=linux GOARCH=386 go build
8686
- mkdir linux-i386
8787
- ln dnscrypt-proxy linux-i386/
88-
- ln dnscrypt-proxy.toml forwarding-rules.txt linux-i386/
88+
- ln dnscrypt-proxy.toml forwarding-rules.txt blacklist.txt linux-i386/
8989
- tar czpvf dnscrypt-proxy-linux_i386-${TRAVIS_TAG:-dev}.tar.gz linux-i386
9090

9191
- go clean
9292
- env GOOS=linux GOARCH=amd64 go build
9393
- mkdir linux-x86_64
9494
- ln dnscrypt-proxy linux-x86_64/
95-
- ln dnscrypt-proxy.toml forwarding-rules.txt linux-x86_64/
95+
- ln dnscrypt-proxy.toml forwarding-rules.txt blacklist.txt linux-x86_64/
9696
- tar czpvf dnscrypt-proxy-linux_x86_64-${TRAVIS_TAG:-dev}.tar.gz linux-x86_64
9797

9898
- go clean
9999
- env GOOS=linux GOARCH=arm go build
100100
- mkdir linux-arm
101101
- ln dnscrypt-proxy linux-arm/
102-
- ln dnscrypt-proxy.toml forwarding-rules.txt linux-arm/
102+
- ln dnscrypt-proxy.toml forwarding-rules.txt blacklist.txt linux-arm/
103103
- tar czpvf dnscrypt-proxy-linux_arm-${TRAVIS_TAG:-dev}.tar.gz linux-arm
104104

105105
- go clean
106106
- env GOOS=linux GOARCH=arm64 go build
107107
- mkdir linux-arm64
108108
- ln dnscrypt-proxy linux-arm64/
109-
- ln dnscrypt-proxy.toml forwarding-rules.txt linux-arm64/
109+
- ln dnscrypt-proxy.toml forwarding-rules.txt blacklist.txt linux-arm64/
110110
- tar czpvf dnscrypt-proxy-linux_arm64-${TRAVIS_TAG:-dev}.tar.gz linux-arm64
111111

112112
- go clean
113113
- env GOOS=linux GOARCH=mips go build
114114
- mkdir linux-mips
115115
- ln dnscrypt-proxy linux-mips/
116-
- ln dnscrypt-proxy.toml forwarding-rules.txt linux-mips/
116+
- ln dnscrypt-proxy.toml forwarding-rules.txt blacklist.txt linux-mips/
117117
- tar czpvf dnscrypt-proxy-linux_mips-${TRAVIS_TAG:-dev}.tar.gz linux-mips
118118

119119
- go clean
120120
- env GOOS=linux GOARCH=mips64 go build
121121
- mkdir linux-mips64
122122
- ln dnscrypt-proxy linux-mips64/
123-
- ln dnscrypt-proxy.toml forwarding-rules.txt linux-mips64/
123+
- ln dnscrypt-proxy.toml forwarding-rules.txt blacklist.txt linux-mips64/
124124
- tar czpvf dnscrypt-proxy-linux_mips64-${TRAVIS_TAG:-dev}.tar.gz linux-mips64
125125

126126
- go clean
127127
- env GOOS=linux GOARCH=mips64le go build
128128
- mkdir linux-mips64le
129129
- ln dnscrypt-proxy linux-mips64le/
130-
- ln dnscrypt-proxy.toml forwarding-rules.txt linux-mips64le/
130+
- ln dnscrypt-proxy.toml forwarding-rules.txt blacklist.txt linux-mips64le/
131131
- tar czpvf dnscrypt-proxy-linux_mips64le-${TRAVIS_TAG:-dev}.tar.gz linux-mips64le
132132

133133
- go clean
134134
- env GOOS=darwin GOARCH=amd64 go build
135135
- mkdir macos
136136
- ln dnscrypt-proxy macos/
137-
- ln dnscrypt-proxy.toml forwarding-rules.txt macos/
137+
- ln dnscrypt-proxy.toml forwarding-rules.txt blacklist.txt macos/
138138
- tar czpvf dnscrypt-proxy-macos-${TRAVIS_TAG:-dev}.tar.gz macos
139139

140140
- ls -l dnscrypt-proxy-*.tar.gz dnscrypt-proxy-*.zip

dnscrypt-proxy/blacklist.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
###########################
3+
# Blacklist #
4+
###########################
5+
6+
## Rules for name-based query blocking, one per line
7+
##
8+
## Example of valid patterns:
9+
##
10+
## ads.* | matches anything with an "ads." prefix
11+
## *.example.com | matches example.com and all names within that zone such as www.example.com
12+
## example.com | identical to the above
13+
## *sex* | matches any name containing that substring
14+
## ads[0-9]* | matches "ads" followed by one or more digits
15+
## ads*.example* | *, ? and [] can be used anywhere, but prefixes/suffixes are faster
16+
17+
ad.*
18+
ads.*
19+
banner.*
20+
banners.*
21+
creatives.*
22+
oas.*
23+
oascentral.*
24+
stats.*
25+
tag.*
26+
telemetry.*
27+
tracker.*
28+
*.local
29+
eth0.me
30+
*.workgroup

dnscrypt-proxy/common.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"encoding/binary"
55
"errors"
66
"net"
7+
"strings"
8+
"unicode"
79
)
810

911
type CryptoConstruction uint16
@@ -80,3 +82,18 @@ func StringReverse(s string) string {
8082
}
8183
return string(r)
8284
}
85+
86+
func StringTwoFields(str string) (string, string, bool) {
87+
if len(str) < 3 {
88+
return "", "", false
89+
}
90+
pos := strings.IndexFunc(str, unicode.IsSpace)
91+
if pos == -1 {
92+
return "", "", false
93+
}
94+
a, b := strings.TrimFunc(str[:pos], unicode.IsSpace), strings.TrimFunc(str[pos+1:], unicode.IsSpace)
95+
if len(a) == 0 || len(b) == 0 {
96+
return a, b, false
97+
}
98+
return a, b, true
99+
}

dnscrypt-proxy/dnscrypt-proxy.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ block_ipv6 = false
6060
##################################################################################
6161

6262
## Example map entries (one entry per line):
63-
## example.com: 9.9.9.9
64-
## example.net: 9.9.9.9,8.8.8.8
63+
## example.com 9.9.9.9
64+
## example.net 9.9.9.9,8.8.8.8
6565

6666
# forwarding_rules = "forwarding-rules.txt"
6767

@@ -120,6 +120,7 @@ format = "tsv"
120120
######################################################
121121

122122
## Blacklists are made of one pattern per line. Example of valid patterns:
123+
##
123124
## example.com
124125
## *sex*
125126
## ads.*

dnscrypt-proxy/forwarding-rules.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
##################################
44

55
## This is used to route specific domain names to specific servers.
6-
## The general format is: <domain>: <server address> [, <server address>...]
6+
## The general format is: <domain> <server address> [, <server address>...]
77
## Adresses can be IPv4 and IPv6, and include a non-standard port number.
88

99
## In order to enable this feature, the "forwarding_rules" property needs to
1010
## be set to that file name in the main configuration file.
1111

12-
example.com: 9.9.9.9,8.8.8.8
12+
example.com 9.9.9.9,8.8.8.8
1313

dnscrypt-proxy/plugin_block_name.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"io/ioutil"
55
"path/filepath"
66
"strings"
7+
"unicode"
78

89
"github.com/hashicorp/go-immutable-radix"
910
"github.com/jedisct1/dlog"
@@ -44,7 +45,7 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
4445
plugin.blockedPrefixes = iradix.New()
4546
plugin.blockedSuffixes = iradix.New()
4647
for lineNo, line := range strings.Split(string(bin), "\n") {
47-
line = strings.Trim(line, " \t\r\n")
48+
line = strings.TrimFunc(line, unicode.IsSpace)
4849
if len(line) == 0 || strings.HasPrefix(line, "#") {
4950
continue
5051
}

dnscrypt-proxy/plugin_forward.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"math/rand"
77
"net"
88
"strings"
9+
"unicode"
910

1011
"github.com/jedisct1/dlog"
1112
"github.com/miekg/dns"
@@ -35,25 +36,18 @@ func (plugin *PluginForward) Init(proxy *Proxy) error {
3536
return err
3637
}
3738
for lineNo, line := range strings.Split(string(bin), "\n") {
38-
line = strings.Trim(line, " \t\r")
39+
line = strings.TrimFunc(line, unicode.IsSpace)
3940
if len(line) == 0 || strings.HasPrefix(line, "#") {
4041
continue
4142
}
42-
parts := strings.SplitN(line, ":", 2)
43-
if len(parts) == 0 {
44-
continue
45-
}
46-
if len(parts) != 2 {
43+
domain, serversStr, ok := StringTwoFields(line)
44+
if !ok {
4745
return fmt.Errorf("Syntax error for a forwarding rule at line %d. Expected syntax: example.com: 9.9.9.9,8.8.8.8", 1+lineNo)
4846
}
49-
domain := strings.ToLower(strings.Trim(parts[0], " \t\r"))
50-
serversStr := strings.Trim(parts[1], " \t\r")
51-
if len(domain) == 0 || len(serversStr) == 0 {
52-
continue
53-
}
47+
domain = strings.ToLower(domain)
5448
var servers []string
5549
for _, server := range strings.Split(serversStr, ",") {
56-
server = strings.Trim(server, " \t\r")
50+
server = strings.TrimFunc(server, unicode.IsSpace)
5751
if net.ParseIP(server) != nil {
5852
server = fmt.Sprintf("%s:%d", server, 53)
5953
}

0 commit comments

Comments
 (0)