-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_proxy.go
1034 lines (876 loc) · 28.9 KB
/
reverse_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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Fork of Go's net/http/httputil/reverseproxy.go with multiple changes,
// including:
//
// * caching
// * load balancing
// * service discovery
package main
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/Sirupsen/logrus"
"github.com/pmylund/go-cache"
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/regexp"
"github.com/TykTechnologies/tyk/user"
)
const defaultUserAgent = "Tyk/" + VERSION
// Gateway's custom response headers
const (
XRateLimitLimit = "X-RateLimit-Limit"
XRateLimitRemaining = "X-RateLimit-Remaining"
XRateLimitReset = "X-RateLimit-Reset"
)
var ServiceCache *cache.Cache
func urlFromService(spec *APISpec) (*apidef.HostList, error) {
doCacheRefresh := func() (*apidef.HostList, error) {
log.Debug("--> Refreshing")
spec.ServiceRefreshInProgress = true
defer func() { spec.ServiceRefreshInProgress = false }()
sd := ServiceDiscovery{}
sd.Init(&spec.Proxy.ServiceDiscovery)
data, err := sd.Target(spec.Proxy.ServiceDiscovery.QueryEndpoint)
if err != nil {
return nil, err
}
spec.HasRun = true
// Set the cached value
if data.Len() == 0 {
log.Warning("[PROXY][SD] Service Discovery returned empty host list! Returning last good set.")
if spec.LastGoodHostList == nil {
log.Warning("[PROXY][SD] Last good host list is nil, returning empty set.")
spec.LastGoodHostList = apidef.NewHostList()
}
return spec.LastGoodHostList, nil
}
ServiceCache.Set(spec.APIID, data, cache.DefaultExpiration)
// Stash it too
spec.LastGoodHostList = data
return data, nil
}
// First time? Refresh the cache and return that
if !spec.HasRun {
log.Debug("First run! Setting cache")
return doCacheRefresh()
}
// Not first run - check the cache
cachedServiceData, found := ServiceCache.Get(spec.APIID)
if !found {
if spec.ServiceRefreshInProgress {
// Are we already refreshing the cache? skip and return last good conf
log.Debug("Cache expired! But service refresh in progress")
return spec.LastGoodHostList, nil
}
// Refresh the spec
log.Debug("Cache expired! Refreshing...")
return doCacheRefresh()
}
log.Debug("Returning from cache.")
return cachedServiceData.(*apidef.HostList), nil
}
// httpScheme matches http://* and https://*, case insensitive
var httpScheme = regexp.MustCompile(`^(?i)https?://`)
func EnsureTransport(host string) string {
if httpScheme.MatchString(host) {
return host
}
// no prototcol, assume http
return "http://" + host
}
func nextTarget(targetData *apidef.HostList, spec *APISpec) (string, error) {
if spec.Proxy.EnableLoadBalancing {
log.Debug("[PROXY] [LOAD BALANCING] Load balancer enabled, getting upstream target")
// Use a HostList
startPos := spec.RoundRobin.WithLen(targetData.Len())
pos := startPos
for {
gotHost, err := targetData.GetIndex(pos)
if err != nil {
return "", err
}
host := EnsureTransport(gotHost)
if !spec.Proxy.CheckHostAgainstUptimeTests {
return host, nil // we don't care if it's up
}
if !GlobalHostChecker.HostDown(host) {
return host, nil // we do care and it's up
}
// if the host is down, keep trying all the rest
// in order from where we started.
if pos = (pos + 1) % targetData.Len(); pos == startPos {
return "", fmt.Errorf("all hosts are down, uptime tests are failing")
}
}
}
// Use standard target - might still be service data
log.Debug("TARGET DATA:", targetData)
gotHost, err := targetData.GetIndex(0)
if err != nil {
return "", err
}
return EnsureTransport(gotHost), nil
}
var (
onceStartAllHostsDown sync.Once
allHostsDownURL string
)
// TykNewSingleHostReverseProxy returns a new ReverseProxy that rewrites
// URLs to the scheme, host, and base path provided in target. If the
// target's path is "/base" and the incoming request was for "/dir",
// the target request will be for /base/dir. This version modifies the
// stdlib version by also setting the host to the target, this allows
// us to work with heroku and other such providers
func TykNewSingleHostReverseProxy(target *url.URL, spec *APISpec) *ReverseProxy {
onceStartAllHostsDown.Do(func() {
handler := func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "all hosts are down", http.StatusServiceUnavailable)
}
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
panic(err)
}
server := &http.Server{
Handler: http.HandlerFunc(handler),
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
MaxHeaderBytes: 1 << 20,
}
allHostsDownURL = "http://" + listener.Addr().String()
go func() {
panic(server.Serve(listener))
}()
})
if spec.Proxy.ServiceDiscovery.UseDiscoveryService {
log.Debug("[PROXY] Service discovery enabled")
if ServiceCache == nil {
log.Debug("[PROXY] Service cache initialising")
expiry := 120
if spec.Proxy.ServiceDiscovery.CacheTimeout > 0 {
expiry = int(spec.Proxy.ServiceDiscovery.CacheTimeout)
} else if spec.GlobalConfig.ServiceDiscovery.DefaultCacheTimeout > 0 {
expiry = spec.GlobalConfig.ServiceDiscovery.DefaultCacheTimeout
}
ServiceCache = cache.New(time.Duration(expiry)*time.Second, 15*time.Second)
}
}
targetQuery := target.RawQuery
director := func(req *http.Request) {
hostList := spec.Proxy.StructuredTargetList
switch {
case spec.Proxy.ServiceDiscovery.UseDiscoveryService:
var err error
hostList, err = urlFromService(spec)
if err != nil {
log.Error("[PROXY] [SERVICE DISCOVERY] Failed target lookup: ", err)
break
}
fallthrough // implies load balancing, with replaced host list
case spec.Proxy.EnableLoadBalancing:
host, err := nextTarget(hostList, spec)
if err != nil {
log.Error("[PROXY] [LOAD BALANCING] ", err)
host = allHostsDownURL
}
lbRemote, err := url.Parse(host)
if err != nil {
log.Error("[PROXY] [LOAD BALANCING] Couldn't parse target URL:", err)
} else {
// Only replace target if everything is OK
target = lbRemote
targetQuery = target.RawQuery
}
}
targetToUse := target
if spec.URLRewriteEnabled && req.Context().Value(RetainHost) == true {
log.Debug("Detected host rewrite, overriding target")
tmpTarget, err := url.Parse(req.URL.String())
if err != nil {
log.Error("Failed to parse URL! Err: ", err)
} else {
// Specifically override with a URL rewrite
targetToUse = tmpTarget
}
}
// No override, and no load balancing? Use the existing target
// if this is false, there was an url rewrite, thus we
// don't want to do anything to the path - req.URL is
// already final.
if targetToUse == target {
req.URL.Scheme = targetToUse.Scheme
req.URL.Host = targetToUse.Host
req.URL.Path = singleJoiningSlash(targetToUse.Path, req.URL.Path, spec.Proxy.DisableStripSlash)
if req.URL.RawPath != "" {
req.URL.RawPath = singleJoiningSlash(targetToUse.Path, req.URL.RawPath, spec.Proxy.DisableStripSlash)
}
}
if !spec.Proxy.PreserveHostHeader {
req.Host = targetToUse.Host
}
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 {
// Set Tyk's own default user agent. Without
// this line, we would get the net/http default.
req.Header.Set("User-Agent", defaultUserAgent)
}
if spec.GlobalConfig.HttpServerOptions.SkipTargetPathEscaping {
// force RequestURI to skip escaping if API's proxy is set for this
// if we set opaque here it will force URL.RequestURI to skip escaping
if req.URL.RawPath != "" {
req.URL.Opaque = req.URL.RawPath
}
} else if req.URL.RawPath == req.URL.Path {
// this should force URL to do escaping
req.URL.RawPath = ""
}
}
proxy := &ReverseProxy{
Director: director,
TykAPISpec: spec,
FlushInterval: time.Duration(spec.GlobalConfig.HttpServerOptions.FlushInterval) * time.Millisecond,
}
proxy.ErrorHandler.BaseMiddleware = BaseMiddleware{Spec: spec, Proxy: proxy}
return proxy
}
// ReverseProxy is an HTTP Handler that takes an incoming request and
// sends it to another server, proxying the response back to the
// client.
type ReverseProxy struct {
// Director must be a function which modifies
// the request into a new request to be sent
// using Transport. Its response is then copied
// back to the original client unmodified.
Director func(*http.Request)
// The transport used to perform proxy requests.
// If nil, http.DefaultTransport is used.
Transport http.RoundTripper
// FlushInterval specifies the flush interval
// to flush to the client while copying the
// response body.
// If zero, no periodic flushing is done.
FlushInterval time.Duration
// TLSClientConfig specifies the TLS configuration to use for 'wss'.
// If nil, the default configuration is used.
TLSClientConfig *tls.Config
TykAPISpec *APISpec
ErrorHandler ErrorHandler
}
func defaultTransport() *http.Transport {
// allocate a new one every time for now, to avoid modifying a
// global variable for each request's needs (e.g. timeouts).
return &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: config.Global().MaxIdleConns,
MaxIdleConnsPerHost: config.Global().MaxIdleConnsPerHost, // default is 100
TLSHandshakeTimeout: 10 * time.Second,
}
}
func singleJoiningSlash(a, b string, disableStripSlash bool) string {
if disableStripSlash && len(b) == 0 {
return a
}
a = strings.TrimRight(a, "/")
b = strings.TrimLeft(b, "/")
if len(b) > 0 {
return a + "/" + b
}
return a
}
func copyHeader(dst, src http.Header) {
if val := dst.Get(http.CanonicalHeaderKey("Access-Control-Allow-Origin")); len(val) > 0 {
src.Del("Access-Control-Allow-Origin")
}
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
func cloneHeader(h http.Header) http.Header {
h2 := make(http.Header, len(h))
for k, vv := range h {
vv2 := make([]string, len(vv))
copy(vv2, vv)
h2[k] = vv2
}
return h2
}
// Hop-by-hop headers. These are removed when sent to the backend.
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
var hopHeaders = []string{
"Connection",
"Proxy-Connection", // non-standard but still sent by libcurl and rejected by e.g. google
"Keep-Alive",
"Proxy-Authenticate",
"Proxy-Authorization",
"Te", // canonicalized version of "TE"
"Trailer", // not Trailers per URL above; http://www.rfc-editor.org/errata_search.php?eid=4522
"Transfer-Encoding",
"Upgrade",
}
func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) *http.Response {
resp := p.WrappedServeHTTP(rw, req, recordDetail(req, config.Global()))
// make response body to be nopCloser and re-readable before serve it through chain of middlewares
nopCloseResponseBody(resp)
return resp
}
func (p *ReverseProxy) ServeHTTPForCache(rw http.ResponseWriter, req *http.Request) *http.Response {
resp := p.WrappedServeHTTP(rw, req, true)
nopCloseResponseBody(resp)
return resp
}
func (p *ReverseProxy) CheckHardTimeoutEnforced(spec *APISpec, req *http.Request) (bool, int) {
if !spec.EnforcedTimeoutEnabled {
return false, spec.GlobalConfig.ProxyDefaultTimeout
}
_, versionPaths, _, _ := spec.Version(req)
found, meta := spec.CheckSpecMatchesStatus(req, versionPaths, HardTimeout)
if found {
intMeta := meta.(*int)
log.Debug("HARD TIMEOUT ENFORCED: ", *intMeta)
return true, *intMeta
}
return false, spec.GlobalConfig.ProxyDefaultTimeout
}
func (p *ReverseProxy) CheckHeaderInRemoveList(hdr string, spec *APISpec, req *http.Request) bool {
vInfo, versionPaths, _, _ := spec.Version(req)
for _, gdKey := range vInfo.GlobalHeadersRemove {
if strings.ToLower(gdKey) == strings.ToLower(hdr) {
return true
}
}
// Check path config
if found, meta := spec.CheckSpecMatchesStatus(req, versionPaths, HeaderInjected); found {
hmeta := meta.(*apidef.HeaderInjectionMeta)
for _, gdKey := range hmeta.DeleteHeaders {
if strings.ToLower(gdKey) == strings.ToLower(hdr) {
return true
}
}
}
return false
}
func (p *ReverseProxy) CheckCircuitBreakerEnforced(spec *APISpec, req *http.Request) (bool, *ExtendedCircuitBreakerMeta) {
if !spec.CircuitBreakerEnabled {
return false, nil
}
_, versionPaths, _, _ := spec.Version(req)
found, meta := spec.CheckSpecMatchesStatus(req, versionPaths, CircuitBreaker)
if found {
exMeta := meta.(*ExtendedCircuitBreakerMeta)
log.Debug("CB Enforced for path: ", *exMeta)
return true, exMeta
}
return false, nil
}
func proxyFromAPI(api *APISpec) func(*http.Request) (*url.URL, error) {
return func(req *http.Request) (*url.URL, error) {
if api != nil && api.Proxy.Transport.ProxyURL != "" {
return url.Parse(api.Proxy.Transport.ProxyURL)
}
return http.ProxyFromEnvironment(req)
}
}
func httpTransport(timeOut int, rw http.ResponseWriter, req *http.Request, p *ReverseProxy) http.RoundTripper {
transport := defaultTransport() // modifies a newly created transport
transport.TLSClientConfig = &tls.Config{}
transport.Proxy = proxyFromAPI(p.TykAPISpec)
if config.Global().ProxySSLInsecureSkipVerify {
transport.TLSClientConfig.InsecureSkipVerify = true
}
if p.TykAPISpec.Proxy.Transport.SSLInsecureSkipVerify {
transport.TLSClientConfig.InsecureSkipVerify = true
}
// When request routed through the proxy `DialTLS` is not used, and only VerifyPeerCertificate is supported
// The reason behind two separate checks is that `DialTLS` supports specifying public keys per hostname, and `VerifyPeerCertificate` only global ones, e.g. `*`
if proxyURL, _ := transport.Proxy(req); proxyURL != nil {
transport.TLSClientConfig.VerifyPeerCertificate = verifyPeerCertificatePinnedCheck(p.TykAPISpec, transport.TLSClientConfig)
} else {
transport.DialTLS = customDialTLSCheck(p.TykAPISpec, transport.TLSClientConfig)
}
if p.TykAPISpec.GlobalConfig.ProxySSLMinVersion > 0 {
transport.TLSClientConfig.MinVersion = p.TykAPISpec.GlobalConfig.ProxySSLMinVersion
}
if p.TykAPISpec.Proxy.Transport.SSLMinVersion > 0 {
transport.TLSClientConfig.MinVersion = p.TykAPISpec.Proxy.Transport.SSLMinVersion
}
if len(p.TykAPISpec.GlobalConfig.ProxySSLCipherSuites) > 0 {
transport.TLSClientConfig.CipherSuites = getCipherAliases(p.TykAPISpec.GlobalConfig.ProxySSLCipherSuites)
}
if len(p.TykAPISpec.Proxy.Transport.SSLCipherSuites) > 0 {
transport.TLSClientConfig.CipherSuites = getCipherAliases(p.TykAPISpec.Proxy.Transport.SSLCipherSuites)
}
if !config.Global().ProxySSLDisableRenegotiation {
transport.TLSClientConfig.Renegotiation = tls.RenegotiateFreelyAsClient
}
// Use the default unless we've modified the timout
if timeOut > 0 {
log.Debug("Setting timeout for outbound request to: ", timeOut)
transport.DialContext = (&net.Dialer{
Timeout: time.Duration(timeOut) * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext
transport.ResponseHeaderTimeout = time.Duration(timeOut) * time.Second
}
transport.DisableKeepAlives = p.TykAPISpec.GlobalConfig.ProxyCloseConnections
if IsWebsocket(req) {
wsTransport := &WSDialer{transport, rw, p.TLSClientConfig}
return wsTransport
}
return transport
}
func setCommonNameVerifyPeerCertificate(spec *APISpec, tlsConfig *tls.Config, hostName string) {
tlsConfig.InsecureSkipVerify = true
// if verifyPeerCertificate was set previously, make sure it is also executed
prevFunc := tlsConfig.VerifyPeerCertificate
tlsConfig.VerifyPeerCertificate = func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
if prevFunc != nil {
err := prevFunc(rawCerts, verifiedChains)
if err != nil {
return err
}
}
// followed https://github.com/golang/go/issues/21971#issuecomment-332693931
certs := make([]*x509.Certificate, len(rawCerts))
for i, asn1Data := range rawCerts {
cert, err := x509.ParseCertificate(asn1Data)
if err != nil {
return errors.New("failed to parse certificate from server: " + err.Error())
}
certs[i] = cert
}
if !spec.Proxy.Transport.SSLInsecureSkipVerify && !config.Global().ProxySSLInsecureSkipVerify {
opts := x509.VerifyOptions{
Roots: tlsConfig.RootCAs,
CurrentTime: time.Now(),
DNSName: "", // <- skip hostname verification
Intermediates: x509.NewCertPool(),
}
for i, cert := range certs {
if i == 0 {
continue
}
opts.Intermediates.AddCert(cert)
}
_, err := certs[0].Verify(opts)
if err != nil {
return err
}
}
return validateCommonName(hostName, certs[0])
}
}
func (p *ReverseProxy) WrappedServeHTTP(rw http.ResponseWriter, req *http.Request, withCache bool) *http.Response {
outReqIsWebsocket := IsWebsocket(req)
var roundTripper http.RoundTripper
p.TykAPISpec.Lock()
if !outReqIsWebsocket { // check if it is a regular HTTP request
// create HTTP transport
createTransport := p.TykAPISpec.HTTPTransport == nil
// Check if timeouts are set for this endpoint
if !createTransport && config.Global().MaxConnTime != 0 {
createTransport = time.Since(p.TykAPISpec.HTTPTransportCreated) > time.Duration(config.Global().MaxConnTime)*time.Second
}
if createTransport {
_, timeout := p.CheckHardTimeoutEnforced(p.TykAPISpec, req)
p.TykAPISpec.HTTPTransport = httpTransport(timeout, rw, req, p)
p.TykAPISpec.HTTPTransportCreated = time.Now()
}
roundTripper = p.TykAPISpec.HTTPTransport
} else { // this is NEW WS-connection upgrade request
// create WS transport
createTransport := p.TykAPISpec.WSTransport == nil
// Check if timeouts are set for this endpoint
if !createTransport && config.Global().MaxConnTime != 0 {
createTransport = time.Since(p.TykAPISpec.WSTransportCreated) > time.Duration(config.Global().MaxConnTime)*time.Second
}
if createTransport {
_, timeout := p.CheckHardTimeoutEnforced(p.TykAPISpec, req)
p.TykAPISpec.WSTransport = httpTransport(timeout, rw, req, p)
p.TykAPISpec.WSTransportCreated = time.Now()
}
// overwrite transport's ResponseWriter from previous upgrade request
// as it was already hijacked and now is being used for other connection
p.TykAPISpec.WSTransport.(*WSDialer).RW = rw
roundTripper = p.TykAPISpec.WSTransport
}
p.TykAPISpec.Unlock()
ctx := req.Context()
if cn, ok := rw.(http.CloseNotifier); ok {
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(ctx)
defer cancel()
notifyChan := cn.CloseNotify()
go func() {
select {
case <-notifyChan:
cancel()
case <-ctx.Done():
}
}()
}
// Do this before we make a shallow copy
session := ctxGetSession(req)
outreq := new(http.Request)
logreq := new(http.Request)
*outreq = *req // includes shallow copies of maps, but okay
*logreq = *req
// remove context data from the copies
setContext(outreq, context.Background())
setContext(logreq, context.Background())
log.Debug("UPSTREAM REQUEST URL: ", req.URL)
// We need to double set the context for the outbound request to reprocess the target
if p.TykAPISpec.URLRewriteEnabled && req.Context().Value(RetainHost) == true {
log.Debug("Detected host rewrite, notifying director")
setCtxValue(outreq, RetainHost, true)
}
if req.ContentLength == 0 {
outreq.Body = nil // Issue 16036: nil Body for http.Transport retries
}
outreq = outreq.WithContext(ctx)
outreq.Header = cloneHeader(req.Header)
p.Director(outreq)
outreq.Close = false
log.Debug("Outbound Request: ", outreq.URL.String())
// Do not modify outbound request headers if they are WS
if !outReqIsWebsocket {
// Remove hop-by-hop headers listed in the "Connection" header.
// See RFC 2616, section 14.10.
if c := outreq.Header.Get("Connection"); c != "" {
for _, f := range strings.Split(c, ",") {
if f = strings.TrimSpace(f); f != "" {
outreq.Header.Del(f)
}
}
}
// Remove other hop-by-hop headers to the backend. Especially
// important is "Connection" because we want a persistent
// connection, regardless of what the client sent to us.
for _, h := range hopHeaders {
if outreq.Header.Get(h) != "" {
outreq.Header.Del(h)
logreq.Header.Del(h)
}
}
}
addrs := requestIPHops(req)
if !p.CheckHeaderInRemoveList("X-Forwarded-For", p.TykAPISpec, req) {
outreq.Header.Set("X-Forwarded-For", addrs)
}
// Circuit breaker
breakerEnforced, breakerConf := p.CheckCircuitBreakerEnforced(p.TykAPISpec, req)
// set up TLS certificates for upstream if needed
var tlsCertificates []tls.Certificate
if cert := getUpstreamCertificate(outreq.Host, p.TykAPISpec); cert != nil {
tlsCertificates = []tls.Certificate{*cert}
}
p.TykAPISpec.Lock()
if outReqIsWebsocket {
roundTripper.(*WSDialer).TLSClientConfig.Certificates = tlsCertificates
} else {
roundTripper.(*http.Transport).TLSClientConfig.Certificates = tlsCertificates
}
p.TykAPISpec.Unlock()
if p.TykAPISpec.Proxy.Transport.SSLForceCommonNameCheck || config.Global().SSLForceCommonNameCheck {
// if proxy is enabled, add CommonName verification in verifyPeerCertificate
// DialTLS is not executed if proxy is used
var httpTransport *http.Transport
if outReqIsWebsocket {
httpTransport = roundTripper.(*WSDialer).Transport
} else {
httpTransport = roundTripper.(*http.Transport)
}
if proxyURL, _ := httpTransport.Proxy(req); proxyURL != nil {
tlsConfig := httpTransport.TLSClientConfig
host, _, _ := net.SplitHostPort(outreq.Host)
setCommonNameVerifyPeerCertificate(p.TykAPISpec, tlsConfig, host)
}
}
// do request round trip
var res *http.Response
var err error
if breakerEnforced {
if !breakerConf.CB.Ready() {
log.Debug("ON REQUEST: Circuit Breaker is in OPEN state")
p.ErrorHandler.HandleError(rw, logreq, "Service temporarily unavailable.", 503)
return nil
}
log.Debug("ON REQUEST: Circuit Breaker is in CLOSED or HALF-OPEN state")
res, err = roundTripper.RoundTrip(outreq)
if err != nil || res.StatusCode == http.StatusInternalServerError {
breakerConf.CB.Fail()
} else {
breakerConf.CB.Success()
}
} else {
res, err = roundTripper.RoundTrip(outreq)
}
if err != nil {
token := ctxGetAuthToken(req)
var alias string
if session != nil {
alias = session.Alias
}
log.WithFields(logrus.Fields{
"prefix": "proxy",
"user_ip": addrs,
"server_name": outreq.Host,
"user_id": obfuscateKey(token),
"user_name": alias,
"org_id": p.TykAPISpec.OrgID,
"api_id": p.TykAPISpec.APIID,
}).Error("http: proxy error: ", err)
if strings.Contains(err.Error(), "timeout awaiting response headers") {
p.ErrorHandler.HandleError(rw, logreq, "Upstream service reached hard timeout.", http.StatusGatewayTimeout)
if p.TykAPISpec.Proxy.ServiceDiscovery.UseDiscoveryService {
if ServiceCache != nil {
log.Debug("[PROXY] [SERVICE DISCOVERY] Upstream host failed, refreshing host list")
ServiceCache.Delete(p.TykAPISpec.APIID)
}
}
return nil
}
if strings.Contains(err.Error(), "context canceled") {
p.ErrorHandler.HandleError(rw, logreq, "Client closed request", 499)
return nil
}
if strings.Contains(err.Error(), "no such host") {
p.ErrorHandler.HandleError(rw, logreq, "Upstream host lookup failed", http.StatusInternalServerError)
return nil
}
p.ErrorHandler.HandleError(rw, logreq, "There was a problem proxying the request", http.StatusInternalServerError)
return nil
}
if IsWebsocket(req) {
return nil
}
ses := new(user.SessionState)
if session != nil {
ses = session
}
// Middleware chain handling here - very simple, but should do
// the trick. Chain can be empty, in which case this is a no-op.
if err := handleResponseChain(p.TykAPISpec.ResponseChain, rw, res, req, ses); err != nil {
log.Error("Response chain failed! ", err)
}
inres := new(http.Response)
if withCache {
*inres = *res // includes shallow copies of maps, but okay
defer res.Body.Close()
// Buffer body data
var bodyBuffer bytes.Buffer
bodyBuffer2 := new(bytes.Buffer)
p.CopyResponse(&bodyBuffer, res.Body)
*bodyBuffer2 = bodyBuffer
// Create new ReadClosers so we can split output
res.Body = ioutil.NopCloser(&bodyBuffer)
inres.Body = ioutil.NopCloser(bodyBuffer2)
}
// We should at least copy the status code in
inres.StatusCode = res.StatusCode
inres.ContentLength = res.ContentLength
p.HandleResponse(rw, res, ses)
return inres
}
func (p *ReverseProxy) HandleResponse(rw http.ResponseWriter, res *http.Response, ses *user.SessionState) error {
// Remove hop-by-hop headers listed in the
// "Connection" header of the response.
if c := res.Header.Get("Connection"); c != "" {
for _, f := range strings.Split(c, ",") {
if f = strings.TrimSpace(f); f != "" {
res.Header.Del(f)
}
}
}
for _, h := range hopHeaders {
res.Header.Del(h)
}
defer res.Body.Close()
// Close connections
if config.Global().CloseConnections {
res.Header.Set("Connection", "close")
}
// Add resource headers
if ses != nil {
// We have found a session, lets report back
quotaMax, quotaRemaining, _, quotaRenews := ses.GetQuotaLimitByAPIID(p.TykAPISpec.APIID)
res.Header.Set(XRateLimitLimit, strconv.Itoa(int(quotaMax)))
res.Header.Set(XRateLimitRemaining, strconv.Itoa(int(quotaRemaining)))
res.Header.Set(XRateLimitReset, strconv.Itoa(int(quotaRenews)))
}
copyHeader(rw.Header(), res.Header)
rw.WriteHeader(res.StatusCode)
p.CopyResponse(rw, res.Body)
return nil
}
func (p *ReverseProxy) CopyResponse(dst io.Writer, src io.Reader) {
if p.FlushInterval != 0 {
if wf, ok := dst.(writeFlusher); ok {
mlw := &maxLatencyWriter{
dst: wf,
latency: p.FlushInterval,
done: make(chan bool),
}
go mlw.flushLoop()
defer mlw.stop()
dst = mlw
}
}
p.copyBuffer(dst, src, nil)
}
func (p *ReverseProxy) copyBuffer(dst io.Writer, src io.Reader, buf []byte) (int64, error) {
if len(buf) == 0 {
buf = make([]byte, 32*1024)
}
var written int64
for {
nr, rerr := src.Read(buf)
if rerr != nil && rerr != io.EOF && rerr != context.Canceled {
log.WithFields(logrus.Fields{
"prefix": "proxy",
"org_id": p.TykAPISpec.OrgID,
"api_id": p.TykAPISpec.APIID,
}).Error("http: proxy error during body copy: ", rerr)
}
if nr > 0 {
nw, werr := dst.Write(buf[:nr])
if nw > 0 {
written += int64(nw)
}
if werr != nil {
return written, werr
}
if nr != nw {
return written, io.ErrShortWrite
}
}
if rerr != nil {
return written, rerr
}
}
}
type writeFlusher interface {
io.Writer
http.Flusher
}
type maxLatencyWriter struct {
dst writeFlusher
latency time.Duration
mu sync.Mutex // protects Write + Flush
done chan bool
}
func (m *maxLatencyWriter) Write(p []byte) (int, error) {
m.mu.Lock()
defer m.mu.Unlock()
return m.dst.Write(p)
}
func (m *maxLatencyWriter) flushLoop() {
t := time.NewTicker(m.latency)
defer t.Stop()
for {
select {
case <-m.done:
return
case <-t.C:
m.mu.Lock()
m.dst.Flush()
m.mu.Unlock()
}
}
}
func (m *maxLatencyWriter) stop() { m.done <- true }
func requestIPHops(r *http.Request) string {
clientIP, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return ""
}
// If we aren't the first proxy retain prior
// X-Forwarded-For information as a comma+space
// separated list and fold multiple headers into one.
if prior, ok := r.Header["X-Forwarded-For"]; ok {
clientIP = strings.Join(prior, ", ") + ", " + clientIP
}
return clientIP
}
// nopCloser is just like ioutil's, but here to let us re-read the same
// buffer inside by moving position to the start every time we done with reading
type nopCloser struct {
io.ReadSeeker
}
// Read just a wrapper around real Read which also moves position to the start if we get EOF
// to have it ready for next read-cycle
func (n nopCloser) Read(p []byte) (int, error) {
num, err := n.ReadSeeker.Read(p)
if err == io.EOF { // move to start to have it ready for next read cycle
n.Seek(0, io.SeekStart)
}
return num, err
}
// Close is a no-op Close
func (n nopCloser) Close() error {
return nil
}
func copyBody(body io.ReadCloser) io.ReadCloser {
// check if body was already read and converted into our nopCloser
if nc, ok := body.(nopCloser); ok {
// seek to the beginning to have it ready for next read
nc.Seek(0, io.SeekStart)
return body
}
// body is http's io.ReadCloser - let's close it after we read data
defer body.Close()
// body is http's io.ReadCloser - read it up until EOF
var bodyRead bytes.Buffer
io.Copy(&bodyRead, body)
// use seek-able reader for further body usage