forked from pydio/cells
/
proxy.go
420 lines (381 loc) · 10.2 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
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package proxy
import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/pydio/cells/common/registry"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyfile"
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/mholt/caddy/caddyhttp/proxy"
microregistry "github.com/micro/go-micro/registry"
)
func init() {
caddy.RegisterPlugin("pydioproxy", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
// setup configures a new Proxy middleware instance.
func setup(c *caddy.Controller) error {
upstreams, err := NewRegistryUpstreams(c.Dispenser, httpserver.GetConfig(c).Host())
if err != nil {
return err
}
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
return proxy.Proxy{Next: next, Upstreams: upstreams}
})
// Register shutdown handlers.
for _, upstream := range upstreams {
c.OnShutdown(upstream.Stop)
}
return nil
}
type registryUpstream struct {
name string
from string
stop chan struct{} // Signals running goroutines to stop.
upstreamHeaders http.Header
downstreamHeaders http.Header
KeepAlive int
Policy proxy.Policy
FailTimeout time.Duration
TryDuration time.Duration
TryInterval time.Duration
MaxConns int64
MaxFails int32
WithPathPrefix string
WithoutPathPrefix string
IgnoredSubPaths []string
Scheme string
insecureSkipVerify bool
lock *sync.RWMutex
hosts map[string]*proxy.UpstreamHost
}
// NewRegistryUpstreams parses the configuration input and sets up
// static upstreams for the proxy middleware. The host string parameter,
// if not empty, is used for setting the upstream Host header for the
// health checks if the upstream header config requires it.
func NewRegistryUpstreams(c caddyfile.Dispenser, host string) ([]proxy.Upstream, error) {
var upstreams []proxy.Upstream
for c.Next() {
upstream := ®istryUpstream{
from: "",
stop: make(chan struct{}),
upstreamHeaders: make(http.Header),
downstreamHeaders: make(http.Header),
MaxFails: 3,
TryInterval: 250 * time.Millisecond,
KeepAlive: http.DefaultMaxIdleConnsPerHost,
Scheme: "http",
lock: new(sync.RWMutex),
hosts: make(map[string]*proxy.UpstreamHost),
}
if !c.Args(&upstream.from) {
return upstreams, c.ArgErr()
}
var to []string
hasSrv := false
to = append(to, c.RemainingArgs()...)
upstream.name = to[0]
for c.NextBlock() {
if err := parseBlock(&c, upstream, hasSrv); err != nil {
return upstreams, err
}
}
if len(to) == 0 {
return upstreams, c.ArgErr()
}
services, _ := registry.GetRunningService(upstream.name)
for _, service := range services {
for _, node := range service.RunningNodes() {
host, err := upstream.newHost(service.Name(), service.Version(), node)
if err != nil {
continue
}
upstream.lock.Lock()
upstream.hosts[node.Id] = host
upstream.lock.Unlock()
}
}
go func() {
w, err := registry.Watch()
if err != nil {
return
}
for {
res, err := w.Next()
if err != nil {
return
}
if res.Service.Name != upstream.name {
continue
}
switch res.Action {
case "create", "update":
for _, node := range res.Service.Nodes {
host, err := upstream.newHost(res.Service.Name, res.Service.Version, node)
if err != nil {
continue
}
upstream.lock.Lock()
upstream.hosts[node.Id] = host
upstream.lock.Unlock()
}
case "delete":
for _, node := range res.Service.Nodes {
upstream.lock.Lock()
delete(upstream.hosts, node.Id)
upstream.lock.Unlock()
}
}
}
}()
upstreams = append(upstreams, upstream)
}
return upstreams, nil
}
func (r *registryUpstream) From() string {
return r.from
}
// Selects an upstream host to be routed to. It
// should return a suitable upstream host, or nil
// if no such hosts are available.
func (r *registryUpstream) Select(req *http.Request) *proxy.UpstreamHost {
var pool []*proxy.UpstreamHost
for _, host := range r.hosts {
pool = append(pool, host)
}
if r.Policy == nil {
return (&proxy.Random{}).Select(pool, req)
}
return r.Policy.Select(pool, req)
}
// Checks if subpath is not an ignored path
func (r *registryUpstream) AllowedPath(string) bool {
return true
}
// Gets how long to try selecting upstream hosts
// in the case of cascading failures.
func (r *registryUpstream) GetTryDuration() time.Duration {
return 0
}
// Gets how long to wait between selecting upstream
// hosts in the case of cascading failures.
func (r *registryUpstream) GetTryInterval() time.Duration {
return 10 * time.Second
}
// Gets the number of upstream hosts.
func (r *registryUpstream) GetHostCount() int {
return len(r.hosts)
}
// Stops the upstream from proxying requests to shutdown goroutines cleanly.
func (r *registryUpstream) Stop() error {
close(r.stop)
return nil
}
func (r *registryUpstream) newHost(name string, version string, node *microregistry.Node) (*proxy.UpstreamHost, error) {
host := fmt.Sprintf("%s://%s:%d%s", r.Scheme, node.Address, node.Port, r.WithPathPrefix)
uh := &proxy.UpstreamHost{
Name: host,
Conns: 0,
Fails: 0,
FailTimeout: r.FailTimeout,
Unhealthy: 0,
UpstreamHeaders: r.upstreamHeaders,
DownstreamHeaders: r.downstreamHeaders,
CheckDown: func(r *registryUpstream, nodeId string) proxy.UpstreamHostDownFunc {
return func(uh *proxy.UpstreamHost) bool {
// Don't try to kill it if we have only one left
if r.GetHostCount() == 1 {
return false
}
if atomic.LoadInt32(&uh.Unhealthy) != 0 {
fmt.Println("Considering service unhealthy ", name)
microregistry.Deregister(µregistry.Service{
Name: name,
Version: version,
Nodes: []*microregistry.Node{
node,
},
})
return true
}
if atomic.LoadInt32(&uh.Fails) >= r.MaxFails {
fmt.Println("Considering service max fails ", name)
microregistry.Deregister(µregistry.Service{
Name: name,
Version: version,
Nodes: []*microregistry.Node{
node,
},
})
return true
}
return false
}
}(r, node.Id),
WithoutPathPrefix: r.WithoutPathPrefix,
MaxConns: r.MaxConns,
HealthCheckResult: atomic.Value{},
}
baseURL, err := url.Parse(uh.Name)
if err != nil {
return nil, err
}
uh.ReverseProxy = proxy.NewSingleHostReverseProxy(baseURL, uh.WithoutPathPrefix, r.KeepAlive)
if r.insecureSkipVerify {
uh.ReverseProxy.UseInsecureTransport()
}
return uh, nil
}
func parseBlock(c *caddyfile.Dispenser, r *registryUpstream, hasSrv bool) error {
switch c.Val() {
//case "policy":
// if !c.NextArg() {
// return c.ArgErr()
// }
// policyCreateFunc, ok := supportedPolicies[c.Val()]
// if !ok {
// return c.ArgErr()
// }
// arg := ""
// if c.NextArg() {
// arg = c.Val()
// }
// u.Policy = policyCreateFunc(arg)
case "fail_timeout":
if !c.NextArg() {
return c.ArgErr()
}
dur, err := time.ParseDuration(c.Val())
if err != nil {
return err
}
r.FailTimeout = dur
case "max_fails":
if !c.NextArg() {
return c.ArgErr()
}
n, err := strconv.Atoi(c.Val())
if err != nil {
return err
}
if n < 1 {
return c.Err("max_fails must be at least 1")
}
r.MaxFails = int32(n)
case "try_duration":
if !c.NextArg() {
return c.ArgErr()
}
dur, err := time.ParseDuration(c.Val())
if err != nil {
return err
}
r.TryDuration = dur
case "try_interval":
if !c.NextArg() {
return c.ArgErr()
}
interval, err := time.ParseDuration(c.Val())
if err != nil {
return err
}
r.TryInterval = interval
case "max_conns":
if !c.NextArg() {
return c.ArgErr()
}
n, err := strconv.ParseInt(c.Val(), 10, 64)
if err != nil {
return err
}
r.MaxConns = n
case "header_upstream":
var header, value string
if !c.Args(&header, &value) {
// When removing a header, the value can be optional.
if !strings.HasPrefix(header, "-") {
return c.ArgErr()
}
}
r.upstreamHeaders.Add(header, value)
case "header_downstream":
var header, value string
if !c.Args(&header, &value) {
// When removing a header, the value can be optional.
if !strings.HasPrefix(header, "-") {
return c.ArgErr()
}
}
r.downstreamHeaders.Add(header, value)
case "transparent":
r.upstreamHeaders.Add("Host", "{host}")
r.upstreamHeaders.Add("X-Real-IP", "{remote}")
r.upstreamHeaders.Add("X-Forwarded-For", "{remote}")
r.upstreamHeaders.Add("X-Forwarded-Proto", "{scheme}")
case "websocket":
r.upstreamHeaders.Add("Connection", "{>Connection}")
r.upstreamHeaders.Add("Upgrade", "{>Upgrade}")
case "tls":
r.Scheme = "https"
case "with":
if !c.NextArg() {
return c.ArgErr()
}
r.WithPathPrefix = c.Val()
case "without":
if !c.NextArg() {
return c.ArgErr()
}
r.WithoutPathPrefix = c.Val()
case "except":
ignoredPaths := c.RemainingArgs()
if len(ignoredPaths) == 0 {
return c.ArgErr()
}
r.IgnoredSubPaths = ignoredPaths
case "insecure_skip_verify":
r.insecureSkipVerify = true
case "keepalive":
if !c.NextArg() {
return c.ArgErr()
}
n, err := strconv.Atoi(c.Val())
if err != nil {
return err
}
if n < 0 {
return c.ArgErr()
}
r.KeepAlive = n
default:
return c.Errf("unknown property '%s'", c.Val())
}
return nil
}