-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
491 lines (420 loc) · 11.7 KB
/
proxy.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// Package webproxy is a simple http proxy tool.
//
// Refer:
// https://github.com/nodejitsu/node-http-proxy
// https://github.com/chimurai/http-proxy-middleware
// "golang.org/x/net/proxy" -> proxy.SOCKS5()
//
// 使用说明:
//
// 1. 作为中间件 - 匹配成功就代理请求,否则跳过当前请求。
//
// proxy.New("/api", some options)
// proxy.New([]string{"/api", "/v1/api"}, some options)
//
// 2. 直接作为路由path handler - 代理此路由下的所有请求(相当于作为中间件时,ctx为一个url path)
//
// router.GET("/api", proxy.All(some options))
// http.HandleFunc("/api", proxy.All(some options))
//
// 参数说明:
//
// proxy.New() 接收两个参数:第一个是要匹配的信息,第二个是一些选项设置
//
// 关于参数 'ctx',它允许为:
// empty:
// nil - matches any path, all requests will be proxied.
// path matching(string - a URL path, support wildcard):
// "/", "**" - matches any path, all requests will be proxied.
// "/api" - matches paths starting with /api
// "**/*.html" - matches any path which ends with .html
// "/*.html" - matches paths directly under path-absolute
// "!**/bad.json" - exclusion
// multiple path matching([]string - multi URL path, support wildcard).
// []string{"/api", "/v1/api", "/some/**/special-api"}
// custom validate func:
// FilterFunc - must be type of FilterFunc. return True, proxy current request
package webproxy
import (
"fmt"
"io"
"log"
"math/rand"
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"strings"
)
// the Proxy ctx data type name
const (
CtxIsEmpty = "empty"
CtxIsString = "string"
CtxIsStrings = "strings"
CtxIsFilter = "func"
)
// Proxy definition
type Proxy struct {
// the ReverseProxy instance
rp *httputil.ReverseProxy
// options for proxy
opts *Options
// internal. parsed from opts.Target
target *url.URL
// context use for match request.
ctx interface{}
// the ctx data type. allow in: "empty", "string", "strings", "func"
ctxType string
// compiled ctx matchers
ctxMatchers map[string]*regexp.Regexp
// logger
logger *log.Logger
}
// Options for proxy.
type Options struct {
// open debug
Debug bool
// WS enable webSocket proxy
WS bool
// Target url string. eg. "http://www.example.org"
// Notice:
// Target and Forward cannot be both missing
Target string
// Forward url string.
Forward string
// IgnorePath specify whether you want to ignore the proxy path of the incoming request. Default: false
IgnorePath bool
// ChangeOrigin changes the origin of the host header to the target URL. Default: false
// for vhosted sites, changes host header to match to target's host
ChangeOrigin bool
// Auth is basic authentication i.e. 'user:password' to compute an Authorization header.
Auth string
// WS bool
// PathRewrite url path rewrite
// {
// '^/api/old-path' : '/api/new-path', // rewrite path
// '^/api/remove/path' : '/path' // remove base path
// '^/' : '/basePath/' // add base path
// },
PathRewrite map[string]string
LogLevel int
// LogOutput
// Example:
// LogOutput = os.Stdout
// LogOutput = new(bytes.Buffer)
// LogOutput, _ = os.OpenFile("proxy.log", os.O_RDWR|os.O_CREATE, os.ModePerm)
LogOutput io.Writer
//
Events map[string]func(args ...interface{}) error
// Routes table, if match success, will override Target.
//
// Example:
// {
// // when request.headers.host == 'dev.localhost:3000',
// // override target 'http://www.example.org' to 'http://localhost:8000'
// "dev.localhost:3000" : "http://localhost:8000"
// }
Routes map[string]string
}
const (
anyMatch = `[^/]+`
allMatch = `.+`
)
// FilterFunc custom filter to check if it should be proxy or not
type FilterFunc func(path string, r *http.Request) bool
var (
matchAll = map[string]uint8{"": 1, "/": 1, "**": 1}
// proxyRes -> ModifyResponse
proxyEvents = []string{"error", "proxyReq", "proxyReqWs", "proxyRes", "open", "close"}
)
// New a proxy instance.
func New(ctx interface{}, opts Options) *Proxy {
opts.Target = strings.TrimSpace(opts.Target)
if opts.Target == "" {
panic("target url cannot be empty")
}
var err error
p := &Proxy{ctx: ctx, opts: &opts}
p.checkCtxType()
// parse target url to url.URL instance
p.target, err = url.Parse(p.opts.Target)
if err != nil {
panic(err)
}
p.createReverseProxy(p.target)
return p
}
// All requests will be proxy
func All(opts Options) *Proxy {
return New(nil, opts)
}
// Target requests will be proxy to the target URL
func Target(url string, opts ...Options) *Proxy {
var opt Options
if len(opts) > 0 {
opt = opts[0]
} else {
opt = Options{}
}
opt.Target = url
return New(nil, opt)
}
func (p *Proxy) init(opts *Options) {
if opts.LogOutput != nil {
p.logger = log.New(opts.LogOutput, "httpProxy", log.Lshortfile)
}
}
func (p *Proxy) checkCtxType() {
if p.ctx == nil {
p.ctxType = CtxIsEmpty
return
}
switch p.ctx.(type) {
case string:
path := strings.TrimSpace(p.ctx.(string))
p.ctx = path
p.ctxType = CtxIsString
p.compilePathMatchers([]string{path})
case []string:
p.ctxType = CtxIsStrings
p.compilePathMatchers(p.ctx.([]string))
case FilterFunc:
p.ctxType = CtxIsFilter
default:
panic("invalid data type of the 'ctx', allow: string, strings, func")
}
}
func (p *Proxy) compilePathMatchers(paths []string) {
p.ctxMatchers = make(map[string]*regexp.Regexp, 0)
for _, path := range paths {
// eg "", "**", "/"
if _, ok := matchAll[path]; ok {
continue
}
raw := path
// eg "!/api/users"
if path[0] == '!' {
path = path[1:]
}
// don't need compile regex. eg "/api"
if p.isFixedPath(path) {
continue
}
// ".html" -> "\.html"
path = quotePointChar(path)
// has wildcard "*"
if strings.IndexByte(path, '*') > -1 {
// has match all wildcard "**"
regex := strings.Replace(path, "**", allMatch, -1)
// has match any wildcard "*"
regex = strings.Replace(regex, "*", anyMatch, -1)
p.ctxMatchers[raw] = regexp.MustCompile("^" + regex)
} else {
p.ctxMatchers[raw] = regexp.MustCompile("^" + path)
}
}
}
func (p *Proxy) createReverseProxy(target *url.URL) {
targetQuery := target.RawQuery
p.rp = &httputil.ReverseProxy{
ErrorLog: p.logger,
// you can modify request data before request target host.
Director: func(req *http.Request) {
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
if _, ok := req.Header["User-Agent"]; !ok {
// explicitly disable User-Agent so it's not set to default value
req.Header.Set("User-Agent", "")
}
// for vhosted sites, changes host header to match to target's host
if p.opts.ChangeOrigin {
req.Header.Set("Host", target.Host) // add Port: target.Port
}
//
p.emit("proxyReq", req)
},
// you can modify response data before respond to client
ModifyResponse: func(res *http.Response) error {
//
p.emit("proxyRes", res)
return nil
},
}
}
// eg "!/api/users"
func (p *Proxy) isExclusion(path string) bool {
return path[0] == '!'
}
// isFixedPath. eg "/api"
func (p *Proxy) isFixedPath(path string) bool {
return strings.IndexByte(path, '*') == -1
}
/*************************************************************
* request handle
*************************************************************/
// Middleware of the interface http.Handler
func (p *Proxy) Middleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if p.opts.Debug {
log.Printf("Received request [%s] %s %s\n", r.Method, r.Host, r.RemoteAddr)
}
// match
if p.shouldProxy(r) {
p.ServeHTTP(w, r)
} else {
h.ServeHTTP(w, r) // skip
}
})
}
// HandlerFunc return http.HandlerFunc
func (p *Proxy) HandlerFunc() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p.ServeHTTP(w, r)
})
}
// ServeHTTP handle request
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if p.opts.Debug {
log.Printf(
"Proxy the request %s %s %s to %s\n",
r.Method, r.Host, r.RemoteAddr,
p.opts.Target,
)
}
p.rp.ServeHTTP(w, r)
}
/*************************************************************
* match request
*************************************************************/
// shouldProxy check
func (p *Proxy) shouldProxy(r *http.Request) bool {
switch p.ctxType {
case CtxIsEmpty:
return true
case CtxIsString:
return p.matchSingleString(p.ctx.(string), r)
case CtxIsStrings:
return p.matchMultiStrings(p.ctx.([]string), r)
case CtxIsFilter: // custom filter check
filter := p.ctx.(FilterFunc)
return filter(r.URL.Path, r)
}
return false
}
// match single URL path string.
// Rules:
// path matching(string - a URL path, support wildcard):
// "/", "**" - matches any path, all requests will be proxied.
// "/api" - matches paths starting with /api
// "**/*.html" - matches any path which ends with .html
// "/*.html" - matches paths directly under path-absolute
// "!**/bad.json" - exclusion
func (p *Proxy) matchSingleString(path string, r *http.Request) bool {
if _, ok := matchAll[path]; ok {
return true
}
raw := path
reqPath := r.URL.Path
okReturn := true
// eg "!/api/users"
if path[0] == '!' {
path = path[1:]
okReturn = false
}
// eg "/api"
if p.isFixedPath(path) && strings.HasPrefix(reqPath, path) {
return okReturn
}
matcher, ok := p.ctxMatchers[raw]
if ok && matcher.MatchString(reqPath) {
return okReturn
}
return false
}
// match multi URL path strings
func (p *Proxy) matchMultiStrings(paths []string, r *http.Request) bool {
for _, path := range paths {
if p.matchSingleString(path, r) {
return true
}
}
return false
}
// WEB request proxy
func (p *Proxy) WEB() {
}
// WS request proxy
func (p *Proxy) WS() {
}
// emit a event
func (p *Proxy) emit(event string, args ...interface{}) {
}
// Options get
func (p *Proxy) Options() Options {
return *p.opts
}
func quotePointChar(path string) string {
if strings.IndexByte(path, '.') > 0 {
// "about.html" -> "about\.html"
return strings.Replace(path, ".", `\.`, -1)
}
return path
}
func singleJoiningSlash(a, b string) string {
aAlash := strings.HasSuffix(a, "/")
bAlash := strings.HasPrefix(b, "/")
switch {
case aAlash && bAlash:
return a + b[1:]
case !aAlash && !bAlash:
return a + "/" + b
}
return a + b
}
// MultiHostReverseProxy create a global reverse proxy.
// usage:
// rp := MultiHostReverseProxy(&url.URL{
// Scheme: "http",
// Host: "localhost:9091",
// }, &url.URL{
// Scheme: "http",
// Host: "localhost:9092",
// })
// log.Fatal(http.ListenAndServe(":9090", rp))
func MultiHostReverseProxy(targets ...*url.URL) *httputil.ReverseProxy {
if len(targets) == 0 {
panic("Please add at least one remote target server")
}
var target *url.URL
// if only one target
if len(targets) == 1 {
target = targets[0]
}
director := func(req *http.Request) {
if len(targets) > 1 {
target = targets[rand.Int()%len(targets)]
}
fmt.Printf("Received request %s %s %s\n", req.Method, req.Host, req.RemoteAddr)
targetQuery := target.RawQuery
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.URL.Path = target.Path
// req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
if _, ok := req.Header["User-Agent"]; !ok {
// explicitly disable User-Agent so it's not set to default value
req.Header.Set("User-Agent", "")
}
}
return &httputil.ReverseProxy{Director: director}
}