-
Notifications
You must be signed in to change notification settings - Fork 18
/
dnsrewrite.go
170 lines (149 loc) · 3.9 KB
/
dnsrewrite.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
package rules
import (
"errors"
"fmt"
"net"
"strings"
"github.com/miekg/dns"
)
// RCode is a semantic alias for int when used as a DNS response code RCODE.
type RCode = int
// RRType is a semantic alias for uint16 when used as a DNS resource record (RR)
// type.
type RRType = uint16
// RRValue is the value of a resource record. If the coresponding RR is either
// dns.TypeA or dns.TypeAAAA, the underlying type of RRValue is net.IP. If the
// RR is dns.TypeTXT, the underlying type of Value is string. Otherwise,
// currently, it is nil. New types may be added in the future.
type RRValue = interface{}
// DNSRewrite is a DNS rewrite ($dnsrewrite) rule.
type DNSRewrite struct {
// RCode is the new DNS RCODE.
RCode RCode
// RRType is the new DNS resource record (RR) type. It is only non-zero
// if RCode is dns.RCodeSuccess.
RRType RRType
// Value is the value for the record. See the RRValue documentation for
// more details.
Value RRValue
// NewCNAME is the new CNAME. If set, clients must ignore other fields,
// resolve the CNAME, and set the new A and AAAA records accordingly.
NewCNAME string
}
// loadDNSRewrite loads the $dnsrewrite modifier.
func loadDNSRewrite(s string) (rewrite *DNSRewrite, err error) {
parts := strings.SplitN(s, ";", 3)
switch len(parts) {
case 1:
return loadDNSRewriteShort(s)
case 2:
return nil, errors.New("invalid dnsrewrite: expected zero or two delimiters")
case 3:
return loadDNSRewriteNormal(parts[0], parts[1], parts[2])
default:
// TODO(a.garipov): Use panic("unreachable") instead?
return nil, fmt.Errorf("SplitN returned %d parts", len(parts))
}
}
// allUppercaseASCII returns true if s is not empty and all characters in s are
// uppercase ASCII letters.
func allUppercaseASCII(s string) (ok bool) {
if s == "" {
return false
}
for _, r := range s {
if r < 'A' || r > 'Z' {
return false
}
}
return true
}
// loadDNSRewritesShort loads the shorthand version of the $dnsrewrite modifier.
func loadDNSRewriteShort(s string) (rewrite *DNSRewrite, err error) {
if s == "" {
// Return an empty DNSRewrite, because an empty string most
// probalby means that this is a disabling allowlist case.
return &DNSRewrite{}, nil
} else if allUppercaseASCII(s) {
if s == "REFUSED" {
return &DNSRewrite{
RCode: dns.RcodeRefused,
}, nil
}
return nil, fmt.Errorf("unknown keyword: %q", s)
}
ip := net.ParseIP(s)
if ip != nil {
if ip4 := ip.To4(); ip4 != nil {
return &DNSRewrite{
RCode: dns.RcodeSuccess,
RRType: dns.TypeA,
Value: ip,
}, nil
}
return &DNSRewrite{
RCode: dns.RcodeSuccess,
RRType: dns.TypeAAAA,
Value: ip,
}, nil
}
return &DNSRewrite{
NewCNAME: s,
}, nil
}
// loadDNSRewritesNormal loads the normal version for of the $dnsrewrite
// modifier.
func loadDNSRewriteNormal(rcodeStr, rrStr, valStr string) (rewrite *DNSRewrite, err error) {
rcode, ok := dns.StringToRcode[strings.ToUpper(rcodeStr)]
if !ok {
return nil, fmt.Errorf("unknown rcode: %q", rcodeStr)
}
if rcode != dns.RcodeSuccess {
return &DNSRewrite{
RCode: rcode,
}, nil
}
rr, err := strToRR(rrStr)
if err != nil {
return nil, err
}
switch rr {
case dns.TypeA:
ip := net.ParseIP(valStr)
if ip4 := ip.To4(); ip4 == nil {
return nil, fmt.Errorf("invalid ipv4: %q", valStr)
}
return &DNSRewrite{
RCode: rcode,
RRType: rr,
Value: ip,
}, nil
case dns.TypeAAAA:
ip := net.ParseIP(valStr)
if ip == nil {
return nil, fmt.Errorf("invalid ipv6: %q", valStr)
} else if ip4 := ip.To4(); ip4 != nil {
return nil, fmt.Errorf("want ipv6, got ipv4: %q", valStr)
}
return &DNSRewrite{
RCode: rcode,
RRType: rr,
Value: ip,
}, nil
case dns.TypeCNAME:
return &DNSRewrite{
NewCNAME: valStr,
}, nil
case dns.TypeTXT:
return &DNSRewrite{
RCode: rcode,
RRType: rr,
Value: valStr,
}, nil
default:
return &DNSRewrite{
RCode: rcode,
RRType: rr,
}, nil
}
}