-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmatcher.go
More file actions
225 lines (183 loc) · 4.82 KB
/
matcher.go
File metadata and controls
225 lines (183 loc) · 4.82 KB
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
package proxy
import (
"github.com/gobwas/ws"
"net/http"
)
type ReqMatcher interface {
MatchReq(*http.Request, *Context) bool
RespMatcher
}
type ReqFilter struct {
cfg *Config
matcher []ReqMatcher
}
func (c *Config) WithReqMatcher(matcher ...ReqMatcher) *ReqFilter {
return &ReqFilter{cfg: c, matcher: matcher}
}
type ReqHandlerFn func(*http.Request, *Context) (*http.Request, *http.Response)
func (r *ReqFilter) Handle(handle ReqHandlerFn) {
r.cfg.reqHandlers = append(r.cfg.reqHandlers,
func(req *http.Request, ctx *Context) (*http.Request, *http.Response) {
for _, matcher := range r.matcher {
if !matcher.MatchReq(req, ctx) {
return req, nil
}
}
return handle(req, ctx)
})
}
func (c *Config) filterReq(req *http.Request, ctx *Context) (*http.Request, *http.Response) {
var resp *http.Response
for _, handle := range c.reqHandlers {
req, resp = handle(req, ctx)
}
return req, resp
}
type ReqMatchFn func(*http.Request, *Context) bool
func (f ReqMatchFn) MatchReq(req *http.Request, ctx *Context) bool {
return f(req, ctx)
}
func (f ReqMatchFn) MatchResp(resp *http.Response, ctx *Context) bool {
return f(ctx.Req, ctx)
}
func Not(matcher ReqMatcher) ReqMatchFn {
return func(req *http.Request, ctx *Context) bool {
return !matcher.MatchReq(req, ctx)
}
}
func ReqHostIs(hosts ...string) ReqMatchFn {
match := make(map[string]struct{})
for _, host := range hosts {
match[host] = struct{}{}
}
return func(req *http.Request, ctx *Context) bool {
_, ok := match[req.Host]
return ok
}
}
type RespMatcher interface {
MatchResp(*http.Response, *Context) bool
}
type RespFilter struct {
cfg *Config
matcher []RespMatcher
}
func (c *Config) WithRespMatcher(matcher ...RespMatcher) *RespFilter {
return &RespFilter{cfg: c, matcher: matcher}
}
type RespHandlerFn func(*http.Response, *Context) *http.Response
func (r *RespFilter) Handle(handle RespHandlerFn) {
r.cfg.respHandlers = append(r.cfg.respHandlers,
func(resp *http.Response, ctx *Context) *http.Response {
for _, matcher := range r.matcher {
if !matcher.MatchResp(resp, ctx) {
return resp
}
}
return handle(resp, ctx)
})
}
func (c *Config) filterResp(resp *http.Response, ctx *Context) *http.Response {
for _, handle := range c.respHandlers {
resp = handle(resp, ctx)
}
return resp
}
type RespMatchFn func(*http.Response, *Context) bool
func (f RespMatchFn) MatchResp(resp *http.Response, ctx *Context) bool {
return f(resp, ctx)
}
func StatusCodeIs(codes ...int) RespMatchFn {
match := make(map[int]bool)
for _, code := range codes {
match[code] = true
}
return func(resp *http.Response, ctx *Context) bool {
_, ok := match[resp.StatusCode]
return ok
}
}
type WsMatcher interface {
Match(frame ws.Frame, ctx *Context) bool
}
type WsFilter struct {
cfg *Config
matcher []WsMatcher
}
func (c *Config) WithWsMatcher(matcher ...WsMatcher) *WsFilter {
return &WsFilter{cfg: c, matcher: matcher}
}
type WsHandlerFn func(ws.Frame, *Context) ws.Frame
func (w *WsFilter) Handle(handle WsHandlerFn) {
w.cfg.wsHandlers = append(w.cfg.wsHandlers,
func(frame ws.Frame, ctx *Context) ws.Frame {
for _, matcher := range w.matcher {
if !matcher.Match(frame, ctx) {
return frame
}
}
return handle(frame, ctx)
})
}
func (c *Config) filterWs(frame ws.Frame, ctx *Context) ws.Frame {
for _, handle := range c.wsHandlers {
frame = handle(frame, ctx)
}
return frame
}
type WsMatchFn func(ws.Frame, *Context) bool
func (f WsMatchFn) Match(frame ws.Frame, ctx *Context) bool {
return f(frame, ctx)
}
func WsHostIs(hosts ...string) WsMatchFn {
match := make(map[string]struct{})
for _, host := range hosts {
match[host] = struct{}{}
}
return func(frame ws.Frame, ctx *Context) bool {
_, ok := match[ctx.DstHost]
return ok
}
}
type RawMatcher interface {
Match(raw []byte, ctx *Context) bool
}
type RawFilter struct {
cfg *Config
matcher []RawMatcher
}
func (c *Config) WithRawMatcher(matcher ...RawMatcher) *RawFilter {
return &RawFilter{cfg: c, matcher: matcher}
}
type RawHandlerFn func([]byte, *Context) []byte
func (r *RawFilter) Handle(handle RawHandlerFn) {
r.cfg.rawHandlers = append(r.cfg.rawHandlers,
func(raw []byte, ctx *Context) []byte {
for _, matcher := range r.matcher {
if !matcher.Match(raw, ctx) {
return raw
}
}
return handle(raw, ctx)
})
}
func (c *Config) filterRaw(raw []byte, ctx *Context) []byte {
for _, handle := range c.rawHandlers {
raw = handle(raw, ctx)
}
return raw
}
type RawMatchFn func([]byte, *Context) bool
func (f RawMatchFn) Match(raw []byte, ctx *Context) bool {
return f(raw, ctx)
}
func RawHostIs(hosts ...string) RawMatchFn {
match := make(map[string]struct{})
for _, host := range hosts {
match[host] = struct{}{}
}
return func(raw []byte, ctx *Context) bool {
_, ok := match[ctx.DstHost]
return ok
}
}