forked from abourget/goproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.go
239 lines (215 loc) · 6.5 KB
/
filters.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
package goproxy
import (
"net/http"
"regexp"
"strings"
)
// RespContentTypeIs is a middleware to filter apply a handler only to those requests matching a given content-type.
//
// imageHandler := HandlerFunc(func(ctx *ProxyCtx) Next {
// ... convert ctx.Resp.Body, reinject a new Body with a png, and change the Content-Type
// return FORWARD // side-steps further modifications
// // or return NEXT // to continue on with other Handlers
// }
//
// proxy.HandleRequest(RespContentTypeIs("image/jpeg", "image/gif")(imageHandler))
//
func RespContentTypeIs(types ...string) ChainedHandler {
return func(chainedHandler Handler) Handler {
return HandlerFunc(func(ctx *ProxyCtx) Next {
if ctx.Resp == nil {
return NEXT
}
contentType := ctx.Resp.Header.Get("Content-Type")
for _, typ := range types {
if contentType == typ || strings.HasPrefix(contentType, typ+";") {
return chainedHandler.Handle(ctx)
}
}
return NEXT
})
}
}
// // Pre-configured
// var RespContentIsImage = RespContentTypeIs(
// "image/gif",
// "image/jpeg",
// "image/pjpeg",
// "application/octet-stream",
// "image/png",
// )
// UrlHasPrefix is a middleware matching when the destination URL the proxy client has
// requested has the given prefix, with or without the host.
//
// For example UrlHasPrefix("host/x") will match requests of the form
// 'GET host/x', and will match requests to url 'http://host/x'
func UrlHasPrefix(prefix string) ChainedHandler {
return func(chainedHandler Handler) Handler {
return HandlerFunc(func(ctx *ProxyCtx) Next {
req := ctx.Req
if strings.HasPrefix(req.URL.Path, prefix) ||
strings.HasPrefix(req.URL.Host+"/"+req.URL.Path, prefix) ||
strings.HasPrefix(req.URL.Scheme+req.URL.Host+req.URL.Path, prefix) {
return chainedHandler.Handle(ctx)
}
return NEXT
})
}
}
// UrlIsIn is a middleware that tests if the URL matches `urls`, testing
// whether or not the request URL is one of the given strings with or
// without the host prefix.
//
// UrlIsIn("google.com/","foo") will match these three cases:
// * 'GET /' with 'Host: google.com'
// * 'GET google.com/'
// * 'GET /foo' for any host
func UrlIsIn(urls ...string) ChainedHandler {
urlSet := make(map[string]bool)
for _, u := range urls {
urlSet[u] = true
}
return func(chainedHandler Handler) Handler {
return HandlerFunc(func(ctx *ProxyCtx) Next {
req := ctx.Req
_, pathOk := urlSet[req.URL.Path]
_, hostAndPathOk := urlSet[req.URL.Host+req.URL.Path]
if pathOk || hostAndPathOk {
return chainedHandler.Handle(ctx)
} else {
return NEXT
}
})
}
}
// ReqHostMatches is a middleware that tests whether the host to which
// the request was directed to matches any of the given regular
// expressions.
func ReqHostMatches(regexps ...*regexp.Regexp) ChainedHandler {
return func(chainedHandler Handler) Handler {
return HandlerFunc(func(ctx *ProxyCtx) Next {
for _, re := range regexps {
if re.MatchString(ctx.Req.Host) {
return chainedHandler.Handle(ctx)
}
}
return NEXT
})
}
}
// RequestHostContains is a middleware that tests whether the host to which
// the request is directed to contains one of the given strings.
//
func RequestHostContains(hosts ...string) ChainedHandler {
return func(chainedHandler Handler) Handler {
return HandlerFunc(func(ctx *ProxyCtx) Next {
for _, b := range hosts {
if strings.Contains(ctx.Req.URL.Host, b) {
return chainedHandler.Handle(ctx)
}
}
return NEXT
})
}
}
// RequestHostIsIn is a middleware that tests whether the host to which
// the request is directed to equal to one of the given strings.
//
// This matcher supersedes and combines DstHostIs and ReqHostIs.
func RequestHostIsIn(hosts ...string) ChainedHandler {
hostSet := HostsToMap(hosts...)
return func(chainedHandler Handler) Handler {
return HandlerFunc(func(ctx *ProxyCtx) Next {
if MatchRequestHostMap(ctx.Req, hostSet) {
return chainedHandler.Handle(ctx)
}
return NEXT
})
}
}
func RequestHostIsNotIn(hosts ...string) ChainedHandler {
hostSet := HostsToMap(hosts...)
return func(chainedHandler Handler) Handler {
return HandlerFunc(func(ctx *ProxyCtx) Next {
if !MatchRequestHostMap(ctx.Req, hostSet) {
return chainedHandler.Handle(ctx)
}
return NEXT
})
}
}
func HostsToMap(hosts ...string) map[string]bool {
hostSet := make(map[string]bool)
for _, h := range hosts {
hostSet[h] = true
}
return hostSet
}
func MatchRequestHostMap(req *http.Request, hosts map[string]bool) bool {
_, ok := hosts[req.URL.Host]
return ok
}
var localHostIpv4 = regexp.MustCompile(`127\.0\.0\.\d+`)
// IsLocalhost checks whether the destination host is explicitly local host
// (buggy, there can be IPv6 addresses it doesn't catch)
func IsLocalhost(chainedHandler Handler) Handler {
return HandlerFunc(func(ctx *ProxyCtx) Next {
if MatchIsLocalhost(ctx.Req) {
return chainedHandler.Handle(ctx)
}
return NEXT
})
}
func IsNotLocalhost(chainedHandler Handler) Handler {
return HandlerFunc(func(ctx *ProxyCtx) Next {
if MatchIsLocalhost(ctx.Req) {
return chainedHandler.Handle(ctx)
}
return NEXT
})
}
func MatchIsLocalhost(req *http.Request) bool {
return req.URL.Host == "::1" ||
req.URL.Host == "0:0:0:0:0:0:0:1" ||
localHostIpv4.MatchString(req.URL.Host) ||
req.URL.Host == "localhost"
}
// UrlMatches returns a ReqCondition testing whether the destination URL
// of the request matches the given regexp, with or without prefix
func UrlMatches(re *regexp.Regexp) ChainedHandler {
return func(chainedHandler Handler) Handler {
return HandlerFunc(func(ctx *ProxyCtx) Next {
req := ctx.Req
if re.MatchString(req.URL.Path) ||
re.MatchString(req.URL.Host+req.URL.Path) {
return chainedHandler.Handle(ctx)
}
return NEXT
})
}
}
// MatchRemoteAddr returns a ReqCondtion testing wether the source IP of the request is the given string, Was renamed from `SrcIpIs`.
func RemoteAddrIs(ip string) ChainedHandler {
return func(chainedHandler Handler) Handler {
return HandlerFunc(func(ctx *ProxyCtx) Next {
if CondRemoteAddrIs(ctx, ip) {
return chainedHandler.Handle(ctx)
}
return NEXT
})
}
}
func RemoteAddrIsNot(ip string) ChainedHandler {
return func(chainedHandler Handler) Handler {
return HandlerFunc(func(ctx *ProxyCtx) Next {
if !CondRemoteAddrIs(ctx, ip) {
return chainedHandler.Handle(ctx)
}
return NEXT
})
}
}
func CondRemoteAddrIs(ctx *ProxyCtx, ip string) bool {
return strings.HasPrefix(ctx.Req.RemoteAddr, ip+":")
}
// TODO: implement the other "Not" conditions.