-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
matchers.go
334 lines (299 loc) · 9.03 KB
/
matchers.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2020 Matthew Holt
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package layer4
import (
"encoding/json"
"fmt"
"net"
"net/netip"
"strings"
"github.com/caddyserver/caddy/v2"
"go.uber.org/zap"
)
func init() {
caddy.RegisterModule(MatchIP{})
caddy.RegisterModule(MatchLocalIP{})
caddy.RegisterModule(MatchNot{})
}
// ConnMatcher is a type that can match a connection.
type ConnMatcher interface {
// Match returns true if the given connection matches.
// It should read from the connection as little as possible:
// only as much as necessary to determine a match.
Match(*Connection) (bool, error)
}
// MatcherSet is a set of matchers which
// must all match in order for the request
// to be matched successfully.
type MatcherSet []ConnMatcher
// Match returns true if the connection matches all matchers in mset
// or if there are no matchers. Any error terminates matching.
func (mset MatcherSet) Match(cx *Connection) (matched bool, err error) {
for _, m := range mset {
cx.freeze()
matched, err = m.Match(cx)
cx.unfreeze()
if cx.Logger.Core().Enabled(zap.DebugLevel) {
matcher := "unknown"
if cm, ok := m.(caddy.Module); ok {
matcher = cm.CaddyModule().String()
}
cx.Logger.Debug("matching",
zap.String("remote", cx.RemoteAddr().String()),
zap.Error(err),
zap.String("matcher", matcher),
zap.Bool("matched", matched),
)
}
if !matched || err != nil {
return
}
}
matched = true
return
}
// RawMatcherSets is a group of matcher sets in their
// raw JSON form.
type RawMatcherSets []caddy.ModuleMap
// MatcherSets is a group of matcher sets capable of checking
// whether a connection matches any of the sets.
type MatcherSets []MatcherSet
// AnyMatch returns true if the connection matches any of the matcher sets
// in mss or if there are no matchers, in which case the request always
// matches. Any error terminates matching.
func (mss MatcherSets) AnyMatch(cx *Connection) (matched bool, err error) {
for _, m := range mss {
matched, err = m.Match(cx)
if matched || err != nil {
return
}
}
matched = len(mss) == 0
return
}
// FromInterface fills ms from an interface{} value obtained from LoadModule.
func (mss *MatcherSets) FromInterface(matcherSets interface{}) error {
for _, matcherSetIfaces := range matcherSets.([]map[string]interface{}) {
var matcherSet MatcherSet
for _, matcher := range matcherSetIfaces {
connMatcher, ok := matcher.(ConnMatcher)
if !ok {
return fmt.Errorf("decoded module is not a ConnMatcher: %#v", matcher)
}
matcherSet = append(matcherSet, connMatcher)
}
*mss = append(*mss, matcherSet)
}
return nil
}
// MatchIP matches requests by remote IP (or CIDR range).
type MatchIP struct {
Ranges []string `json:"ranges,omitempty"`
cidrs []netip.Prefix
}
// CaddyModule returns the Caddy module information.
func (MatchIP) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "layer4.matchers.ip",
New: func() caddy.Module { return new(MatchIP) },
}
}
// Provision parses m's IP ranges, either from IP or CIDR expressions.
func (m *MatchIP) Provision(_ caddy.Context) (err error) {
m.cidrs, err = ParseNetworks(m.Ranges)
if err != nil {
return err
}
return nil
}
// Match returns true if the connection is from one of the designated IP ranges.
func (m MatchIP) Match(cx *Connection) (bool, error) {
clientIP, err := m.getClientIP(cx)
if err != nil {
return false, fmt.Errorf("getting client IP: %v", err)
}
for _, ipRange := range m.cidrs {
if ipRange.Contains(clientIP) {
return true, nil
}
}
return false, nil
}
func (m MatchIP) getClientIP(cx *Connection) (netip.Addr, error) {
remote := cx.Conn.RemoteAddr().String()
ipStr, _, err := net.SplitHostPort(remote)
if err != nil {
ipStr = remote // OK; probably didn't have a port
}
ip, err := netip.ParseAddr(ipStr)
if err != nil {
return netip.Addr{}, fmt.Errorf("invalid client IP address: %s", ipStr)
}
return ip, nil
}
// MatchLocalIP matches requests by local IP (or CIDR range).
type MatchLocalIP struct {
Ranges []string `json:"ranges,omitempty"`
cidrs []netip.Prefix
}
// CaddyModule returns the Caddy module information.
func (MatchLocalIP) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "layer4.matchers.local_ip",
New: func() caddy.Module { return new(MatchLocalIP) },
}
}
// Provision parses m's IP ranges, either from IP or CIDR expressions.
func (m *MatchLocalIP) Provision(ctx caddy.Context) error {
ipnets, err := ParseNetworks(m.Ranges)
if err != nil {
return err
}
m.cidrs = ipnets
return nil
}
// Match returns true if the connection is from one of the designated IP ranges.
func (m MatchLocalIP) Match(cx *Connection) (bool, error) {
localIP, err := m.getLocalIP(cx)
if err != nil {
return false, fmt.Errorf("getting local IP: %v", err)
}
for _, ipRange := range m.cidrs {
if ipRange.Contains(localIP) {
return true, nil
}
}
return false, nil
}
func (m MatchLocalIP) getLocalIP(cx *Connection) (netip.Addr, error) {
remote := cx.Conn.LocalAddr().String()
ipStr, _, err := net.SplitHostPort(remote)
if err != nil {
ipStr = remote // OK; probably didn't have a port
}
ip, err := netip.ParseAddr(ipStr)
if err != nil {
return netip.Addr{}, fmt.Errorf("invalid local IP address: %s", ipStr)
}
return ip, nil
}
// MatchNot matches requests by negating the results of its matcher
// sets. A single "not" matcher takes one or more matcher sets. Each
// matcher set is OR'ed; in other words, if any matcher set returns
// true, the final result of the "not" matcher is false. Individual
// matchers within a set work the same (i.e. different matchers in
// the same set are AND'ed).
//
// NOTE: The generated docs which describe the structure of this
// module are wrong because of how this type unmarshals JSON in a
// custom way. The correct structure is:
//
// ```json
// [
//
// {},
// {}
//
// ]
// ```
//
// where each of the array elements is a matcher set, i.e. an
// object keyed by matcher name.
type MatchNot struct {
MatcherSetsRaw []caddy.ModuleMap `json:"-" caddy:"namespace=layer4.matchers"`
MatcherSets []MatcherSet `json:"-"`
}
// CaddyModule implements caddy.Module.
func (MatchNot) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "layer4.matchers.not",
New: func() caddy.Module { return new(MatchNot) },
}
}
// UnmarshalJSON satisfies json.Unmarshaler. It puts the JSON
// bytes directly into m's MatcherSetsRaw field.
func (m *MatchNot) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &m.MatcherSetsRaw)
}
// MarshalJSON satisfies json.Marshaler by marshaling
// m's raw matcher sets.
func (m MatchNot) MarshalJSON() ([]byte, error) {
return json.Marshal(m.MatcherSetsRaw)
}
// Provision loads the matcher modules to be negated.
func (m *MatchNot) Provision(ctx caddy.Context) error {
matcherSets, err := ctx.LoadModule(m, "MatcherSetsRaw")
if err != nil {
return fmt.Errorf("loading matcher sets: %v", err)
}
for _, modMap := range matcherSets.([]map[string]any) {
var ms MatcherSet
for _, modIface := range modMap {
ms = append(ms, modIface.(ConnMatcher))
}
m.MatcherSets = append(m.MatcherSets, ms)
}
return nil
}
// Match returns true if r matches m. Since this matcher negates
// the embedded matchers, false is returned if any of its matcher
// sets return true.
func (m MatchNot) Match(r *Connection) (bool, error) {
for _, ms := range m.MatcherSets {
match, err := ms.Match(r)
if err != nil {
return false, err
}
if match {
return false, nil
}
}
return true, nil
}
// Interface guards
var (
_ caddy.Module = (*MatchIP)(nil)
_ ConnMatcher = (*MatchIP)(nil)
_ caddy.Provisioner = (*MatchIP)(nil)
_ caddy.Module = (*MatchLocalIP)(nil)
_ ConnMatcher = (*MatchLocalIP)(nil)
_ caddy.Provisioner = (*MatchLocalIP)(nil)
_ caddy.Module = (*MatchNot)(nil)
_ caddy.Provisioner = (*MatchNot)(nil)
_ ConnMatcher = (*MatchNot)(nil)
)
// ParseNetworks parses a list of string IP addresses or CDIR subnets into a slice of net.IPNet's.
// It accepts for example ["127.0.0.1", "127.0.0.0/8", "::1", "2001:db8::/32"].
func ParseNetworks(networks []string) (ipNets []netip.Prefix, err error) {
for _, str := range networks {
if strings.Contains(str, "/") {
ipNet, err := netip.ParsePrefix(str)
if err != nil {
return nil, fmt.Errorf("parsing CIDR expression: %v", err)
}
ipNets = append(ipNets, ipNet)
continue
}
addr, err := netip.ParseAddr(str)
if err != nil {
return nil, err
}
bits := 32
if addr.Is6() {
bits = 128
}
ipNets = append(ipNets, netip.PrefixFrom(addr, bits))
}
return ipNets, nil
}