forked from easegress-io/easegress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
433 lines (373 loc) · 11.9 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
/*
* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* 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:https://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 proxy
import (
"crypto/tls"
"crypto/x509"
"encoding/base64"
"fmt"
"net"
"net/http"
"sync"
"sync/atomic"
"time"
"github.com/megaease/easegress/pkg/context"
"github.com/megaease/easegress/pkg/logger"
"github.com/megaease/easegress/pkg/object/httppipeline"
"github.com/megaease/easegress/pkg/tracing"
"github.com/megaease/easegress/pkg/util/fallback"
zipkinhttp "github.com/openzipkin/zipkin-go/middleware/http"
)
const (
// Kind is the kind of Proxy.
Kind = "Proxy"
resultFallback = "fallback"
resultInternalError = "internalError"
resultClientError = "clientError"
resultServerError = "serverError"
)
var results = []string{
resultFallback,
resultInternalError,
resultClientError,
resultServerError,
}
func init() {
httppipeline.Register(&Proxy{})
}
var fnSendRequest = func(r *http.Request, client *Client) (*http.Response, error) {
return client.Do(r)
}
type (
// Proxy is the filter Proxy.
Proxy struct {
filterSpec *httppipeline.FilterSpec
spec *Spec
fallback *fallback.Fallback
mainPool *pool
candidatePools []*pool
mirrorPool *pool
client atomic.Value //*Client
compression *compression
}
// Client is a wrapper around http.Client.
Client struct {
client *http.Client
zipkinClient *zipkinhttp.Client
tracing *tracing.Tracing
}
// Spec describes the Proxy.
Spec struct {
Fallback *FallbackSpec `yaml:"fallback,omitempty" jsonschema:"omitempty"`
MainPool *PoolSpec `yaml:"mainPool" jsonschema:"required"`
CandidatePools []*PoolSpec `yaml:"candidatePools,omitempty" jsonschema:"omitempty"`
MirrorPool *PoolSpec `yaml:"mirrorPool,omitempty" jsonschema:"omitempty"`
FailureCodes []int `yaml:"failureCodes" jsonschema:"omitempty,uniqueItems=true,format=httpcode-array"`
Compression *CompressionSpec `yaml:"compression,omitempty" jsonschema:"omitempty"`
MTLS *MTLS `yaml:"mtls,omitempty" jsonschema:"omitempty"`
MaxIdleConns int `yaml:"maxIdleConns" jsonschema:"omitempty"`
MaxIdleConnsPerHost int `yaml:"maxIdleConnsPerHost" jsonschema:"omitempty"`
}
// FallbackSpec describes the fallback policy.
FallbackSpec struct {
ForCodes bool `yaml:"forCodes"`
fallback.Spec `yaml:",inline"`
}
// Status is the status of Proxy.
Status struct {
MainPool *PoolStatus `yaml:"mainPool"`
CandidatePools []*PoolStatus `yaml:"candidatePools,omitempty"`
MirrorPool *PoolStatus `yaml:"mirrorPool,omitempty"`
}
// MTLS is the configuration for client side mTLS.
MTLS struct {
CertBase64 string `yaml:"certBase64" jsonschema:"required,format=base64"`
KeyBase64 string `yaml:"keyBase64" jsonschema:"required,format=base64"`
RootCertBase64 string `yaml:"rootCertBase64" jsonschema:"required,format=base64"`
}
)
// NewClient creates a wrapper around http.Client
func NewClient(cl *http.Client, tr *tracing.Tracing) *Client {
var zClient *zipkinhttp.Client
if tr != nil && tr != tracing.NoopTracing {
tracer := tr.Tracer
zClient, _ = zipkinhttp.NewClient(tracer, zipkinhttp.WithClient(cl))
}
return &Client{client: cl, zipkinClient: zClient, tracing: tr}
}
// Do calls the correct http client
func (c *Client) Do(r *http.Request) (*http.Response, error) {
if c.zipkinClient != nil {
return c.zipkinClient.DoWithAppSpan(r, r.URL.Path)
}
return c.client.Do(r)
}
// Validate validates Spec.
func (s Spec) Validate() error {
// NOTE: The tag of v parent may be behind mainPool.
if s.MainPool == nil {
return fmt.Errorf("mainPool is required")
}
if s.MainPool.Filter != nil {
return fmt.Errorf("filter must be empty in mainPool")
}
if len(s.CandidatePools) > 0 {
for _, v := range s.CandidatePools {
if v.Filter == nil {
return fmt.Errorf("filter of candidatePool is required")
}
}
}
if s.MirrorPool != nil {
if s.MirrorPool.Filter == nil {
return fmt.Errorf("filter of mirrorPool is required")
}
if s.MirrorPool.MemoryCache != nil {
return fmt.Errorf("memoryCache must be empty in mirrorPool")
}
}
if len(s.FailureCodes) == 0 {
if s.Fallback != nil {
return fmt.Errorf("fallback needs failureCodes")
}
}
return nil
}
// Kind returns the kind of Proxy.
func (b *Proxy) Kind() string {
return Kind
}
// DefaultSpec returns the default spec of Proxy.
func (b *Proxy) DefaultSpec() interface{} {
return &Spec{
MaxIdleConns: 10240,
MaxIdleConnsPerHost: 1024,
}
}
// Description returns the description of Proxy.
func (b *Proxy) Description() string {
return "Proxy sets the proxy of proxy servers"
}
// Results returns the results of Proxy.
func (b *Proxy) Results() []string {
return results
}
// Init initializes Proxy.
func (b *Proxy) Init(filterSpec *httppipeline.FilterSpec) {
b.filterSpec, b.spec = filterSpec, filterSpec.FilterSpec().(*Spec)
b.reload()
}
// Inherit inherits previous generation of Proxy.
func (b *Proxy) Inherit(filterSpec *httppipeline.FilterSpec, previousGeneration httppipeline.Filter) {
previousGeneration.Close()
b.Init(filterSpec)
}
func (b *Proxy) needmTLS() bool {
return b.spec.MTLS != nil
}
func (b *Proxy) tlsConfig() *tls.Config {
if !b.needmTLS() {
return &tls.Config{
InsecureSkipVerify: true,
}
}
rootCertPem, _ := base64.StdEncoding.DecodeString(b.spec.MTLS.RootCertBase64)
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(rootCertPem)
var certificates []tls.Certificate
certPem, _ := base64.StdEncoding.DecodeString(b.spec.MTLS.CertBase64)
keyPem, _ := base64.StdEncoding.DecodeString(b.spec.MTLS.KeyBase64)
cert, err := tls.X509KeyPair(certPem, keyPem)
if err != nil {
logger.Errorf("proxy generates x509 key pair failed: %v", err)
return &tls.Config{
InsecureSkipVerify: true,
}
}
certificates = append(certificates, cert)
return &tls.Config{
Certificates: certificates,
RootCAs: caCertPool,
}
}
func (b *Proxy) reload() {
super := b.filterSpec.Super()
b.mainPool = newPool(super, b.spec.MainPool, "proxy#main",
true /*writeResponse*/, b.spec.FailureCodes)
if b.spec.Fallback != nil {
b.fallback = fallback.New(&b.spec.Fallback.Spec)
}
if len(b.spec.CandidatePools) > 0 {
var candidatePools []*pool
for k := range b.spec.CandidatePools {
candidatePools = append(candidatePools,
newPool(super, b.spec.CandidatePools[k], fmt.Sprintf("proxy#candidate#%d", k),
true, b.spec.FailureCodes))
}
b.candidatePools = candidatePools
}
if b.spec.MirrorPool != nil {
b.mirrorPool = newPool(super, b.spec.MirrorPool, "proxy#mirror",
false /*writeResponse*/, b.spec.FailureCodes)
}
if b.spec.Compression != nil {
b.compression = newCompression(b.spec.Compression)
}
b.client.Store(NewClient(b.createHTTPClient() /*tracing=*/, nil))
}
func (b *Proxy) createHTTPClient() *http.Client {
return &http.Client{
// NOTE: Timeout could be no limit, real client or server could cancel it.
Timeout: 0,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 60 * time.Second,
DualStack: true,
}).DialContext,
TLSClientConfig: b.tlsConfig(),
DisableCompression: false,
// NOTE: The large number of Idle Connections can
// reduce overhead of building connections.
MaxIdleConns: b.spec.MaxIdleConns,
MaxIdleConnsPerHost: b.spec.MaxIdleConnsPerHost,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
}
// Status returns Proxy status.
func (b *Proxy) Status() interface{} {
s := &Status{
MainPool: b.mainPool.status(),
}
if b.candidatePools != nil {
for k := range b.candidatePools {
s.CandidatePools = append(s.CandidatePools, b.candidatePools[k].status())
}
}
if b.mirrorPool != nil {
s.MirrorPool = b.mirrorPool.status()
}
return s
}
// Close closes Proxy.
func (b *Proxy) Close() {
b.mainPool.close()
if b.candidatePools != nil {
for _, v := range b.candidatePools {
v.close()
}
}
if b.mirrorPool != nil {
b.mirrorPool.close()
}
}
func (b *Proxy) fallbackForCodes(ctx context.HTTPContext) bool {
if b.fallback != nil && b.spec.Fallback.ForCodes {
for _, code := range b.spec.FailureCodes {
if ctx.Response().StatusCode() == code {
b.fallback.Fallback(ctx)
return true
}
}
}
return false
}
// Handle handles HTTPContext.
// When we create new request for backend, we call http.NewRequestWithContext method and use context.Request().Body() as body.
// Based on golang std lib comments:
// https://github.com/golang/go/blob/95b68e1e02fa713719f02f6c59fb1532bd05e824/src/net/http/request.go#L856-L860
// If body is of type *bytes.Buffer, *bytes.Reader, or
// *strings.Reader, the returned request's ContentLength is set to its
// exact value (instead of -1), GetBody is populated (so 307 and 308
// redirects can replay the body), and Body is set to NoBody if the
// ContentLength is 0.
//
// So in this way, http.Request.ContentLength will be 0, and when http.Client send this request, it will delete
// "Content-Length" key in header. We solve this problem by set http.Request.ContentLength equal to
// http.Request.Header["Content-Length"] (if it is presented).
// Reading all context.Request().Body() and create new request with bytes.NewReader is another way, but it may cause performance loss.
//
// It is important that "Content-Length" in the Header is equal to the length of the Body. In easegress, when a filter change Request.Body,
// it will delete the header of "Content-Length". So, you should not worry about this when using our filters.
// But for customer filters, developer should make sure to delete or set "Context-Length" value in header when change Request.Body.
func (b *Proxy) Handle(ctx context.HTTPContext) (result string) {
result = b.handle(ctx)
return ctx.CallNextHandler(result)
}
func (b *Proxy) updateAndGetClient(tracingInstance *tracing.Tracing) *Client {
client := b.client.Load().(*Client)
if client.tracing == tracingInstance {
return client
}
// tracingInstance is updated so recreate http.Client
newClient := NewClient(b.createHTTPClient(), tracingInstance)
b.client.Store(newClient)
return newClient
}
func (b *Proxy) handle(ctx context.HTTPContext) (result string) {
if b.mirrorPool != nil && b.mirrorPool.filter.Filter(ctx) {
primaryBody, secondaryBody := newPrimarySecondaryReader(ctx.Request().Body())
ctx.Request().SetBody(primaryBody, false)
wg := &sync.WaitGroup{}
wg.Add(1)
defer wg.Wait()
go func() {
defer wg.Done()
client := b.updateAndGetClient(ctx.Tracing())
b.mirrorPool.handle(ctx, secondaryBody, client)
}()
}
var p *pool
if len(b.candidatePools) > 0 {
for k, v := range b.candidatePools {
if v.filter.Filter(ctx) {
p = b.candidatePools[k]
break
}
}
}
if p == nil {
p = b.mainPool
}
if p.memoryCache != nil && p.memoryCache.Load(ctx) {
return ""
}
client := b.updateAndGetClient(ctx.Tracing())
result = p.handle(ctx, ctx.Request().Body(), client)
if result != "" {
if b.fallbackForCodes(ctx) {
return resultFallback
}
return result
}
// compression and memoryCache only work for
// normal traffic from real proxy servers.
if b.compression != nil {
b.compression.compress(ctx)
}
if p.memoryCache != nil {
p.memoryCache.Store(ctx)
}
return ""
}