forked from saucelabs/forwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_proxy.go
More file actions
776 lines (658 loc) · 21 KB
/
http_proxy.go
File metadata and controls
776 lines (658 loc) · 21 KB
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
// Copyright 2022-2024 Sauce Labs Inc., all rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package forwarder
import (
"context"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"slices"
"strings"
"time"
"github.com/saucelabs/forwarder/hostsfile"
"github.com/saucelabs/forwarder/httplog"
"github.com/saucelabs/forwarder/internal/martian"
"github.com/saucelabs/forwarder/internal/martian/fifo"
"github.com/saucelabs/forwarder/internal/martian/httpspec"
"github.com/saucelabs/forwarder/log"
"github.com/saucelabs/forwarder/middleware"
"github.com/saucelabs/forwarder/pac"
"go.uber.org/multierr"
"golang.org/x/sync/errgroup"
)
//nolint:recvcheck // That is by design.
type ProxyLocalhostMode string
const (
DenyProxyLocalhost ProxyLocalhostMode = "deny"
AllowProxyLocalhost ProxyLocalhostMode = "allow"
DirectProxyLocalhost ProxyLocalhostMode = "direct"
)
func (m *ProxyLocalhostMode) UnmarshalText(text []byte) error {
switch ProxyLocalhostMode(text) {
case DenyProxyLocalhost, AllowProxyLocalhost, DirectProxyLocalhost:
*m = ProxyLocalhostMode(text)
return nil
default:
return fmt.Errorf("invalid mode: %s", text)
}
}
func (m ProxyLocalhostMode) String() string {
return string(m)
}
func (m ProxyLocalhostMode) isValid() bool {
switch m {
case DenyProxyLocalhost, AllowProxyLocalhost, DirectProxyLocalhost:
return true
default:
return false
}
}
type ProxyFunc func(*http.Request) (*url.URL, error)
// Alias all martian types to avoid exposing them.
type (
RequestModifier = martian.RequestModifier
ResponseModifier = martian.ResponseModifier
RequestResponseModifier = martian.RequestResponseModifier
RequestModifierFunc = martian.RequestModifierFunc
ResponseModifierFunc = martian.ResponseModifierFunc
ConnectFunc = martian.ConnectFunc
)
// ErrConnectFallback is returned by a ConnectFunc to indicate
// that the CONNECT request should be handled by martian.
var ErrConnectFallback = martian.ErrConnectFallback
type HTTPProxyConfig struct {
HTTPServerConfig
ExtraListeners []NamedListenerConfig
Name string
MITM *MITMConfig
MITMDomains Matcher
ProxyLocalhost ProxyLocalhostMode
UpstreamProxy *url.URL
UpstreamProxyFunc ProxyFunc
DenyDomains Matcher
DirectDomains Matcher
RequestIDHeader string
RequestModifiers []RequestModifier
ResponseModifiers []ResponseModifier
ConnectFunc ConnectFunc
ConnectTimeout time.Duration
PromHTTPOpts []middleware.PrometheusOpt
// TestingHTTPHandler uses Martian's [http.Handler] implementation
// over [http.Server] instead of the default TCP server.
TestingHTTPHandler bool
}
func DefaultHTTPProxyConfig() *HTTPProxyConfig {
return &HTTPProxyConfig{
HTTPServerConfig: HTTPServerConfig{
ListenerConfig: *DefaultListenerConfig(":3128"),
Protocol: HTTPScheme,
IdleTimeout: 1 * time.Hour,
ReadHeaderTimeout: 1 * time.Minute,
shutdownConfig: defaultShutdownConfig(),
TLSServerConfig: TLSServerConfig{
HandshakeTimeout: 10 * time.Second,
},
},
Name: "forwarder",
ProxyLocalhost: DenyProxyLocalhost,
RequestIDHeader: "X-Request-Id",
ConnectTimeout: 60 * time.Second, // http.Transport sets a constant 1m timeout for CONNECT requests.
}
}
func (c *HTTPProxyConfig) Validate() error {
if err := c.HTTPServerConfig.Validate(); err != nil {
return err
}
for _, lc := range c.ExtraListeners {
if lc.Name == "" {
return errors.New("extra listener name is required")
}
}
if c.Protocol != HTTPScheme && c.Protocol != HTTPSScheme {
return fmt.Errorf("unsupported protocol: %s", c.Protocol)
}
if !c.ProxyLocalhost.isValid() {
return fmt.Errorf("unsupported proxy_localhost: %s", c.ProxyLocalhost)
}
if err := validateProxyURL(c.UpstreamProxy); err != nil {
return fmt.Errorf("upstream_proxy_uri: %w", err)
}
return nil
}
type HTTPProxy struct {
config HTTPProxyConfig
pac PACResolver
creds *CredentialsMatcher
transport http.RoundTripper
log log.StructuredLogger
metrics *httpProxyMetrics
proxy *martian.Proxy
mitmCACert *x509.Certificate
proxyFunc ProxyFunc
kerberosAdapter KerberosAdapter
localhost []string
tlsConfig *tls.Config
listeners []net.Listener
}
// NewHTTPProxy creates a new HTTP proxy.
// It is the caller's responsibility to call Close on the returned server.
func NewHTTPProxy(cfg *HTTPProxyConfig, pr PACResolver, cm *CredentialsMatcher, rt http.RoundTripper, log log.StructuredLogger,
kerberosAdapter KerberosAdapter,
) (*HTTPProxy, error) {
hp, err := newHTTPProxy(cfg, pr, cm, rt, log, kerberosAdapter)
if err != nil {
return nil, err
}
if hp.config.Protocol == HTTPSScheme {
if err := hp.configureHTTPS(); err != nil {
return nil, err
}
if err := reportTLSCertsExpiration(hp.config.PromConfig, hp.tlsConfig, "proxy"); err != nil {
return nil, err
}
}
lh, err := hostsfile.LocalhostAliases()
if err != nil {
return nil, fmt.Errorf("read localhost aliases: %w", err)
}
for i := range lh {
lh[i] = strings.ToLower(lh[i])
}
hp.localhost = append(hp.localhost, lh...)
ll, err := hp.listen()
if err != nil {
return nil, err
}
hp.listeners = ll
for _, l := range hp.listeners {
hp.log.Info("PROXY server listen", "address", l.Addr().String(), "protocol", hp.config.Protocol)
}
return hp, nil
}
// NewHTTPProxyHandler is like NewHTTPProxy but returns http.Handler instead of *HTTPProxy.
func NewHTTPProxyHandler(cfg *HTTPProxyConfig, pr PACResolver, cm *CredentialsMatcher,
rt http.RoundTripper, log log.StructuredLogger, kerberosAdapter KerberosAdapter,
) (http.Handler, error) {
hp, err := newHTTPProxy(cfg, pr, cm, rt, log, kerberosAdapter)
if err != nil {
return nil, err
}
return hp.handler(), nil
}
func newHTTPProxy(cfg *HTTPProxyConfig, pr PACResolver, cm *CredentialsMatcher, rt http.RoundTripper, log log.StructuredLogger, kerberosAdapter KerberosAdapter) (*HTTPProxy, error) {
if err := cfg.Validate(); err != nil {
return nil, err
}
if cfg.UpstreamProxy != nil && pr != nil {
return nil, errors.New("cannot use both upstream proxy and PAC")
}
// If not set, use http.DefaultTransport.
if rt == nil {
log.Info("HTTP transport not configured, using standard library default")
rt = http.DefaultTransport.(*http.Transport).Clone()
} else if tr, ok := rt.(*http.Transport); !ok {
log.Debug("using custom HTTP transport", "type", fmt.Sprintf("%T", rt))
} else if tr.TLSClientConfig != nil && tr.TLSClientConfig.RootCAs != nil {
log.Info("using custom root CA certificates")
}
hp := &HTTPProxy{
config: *cfg,
pac: pr,
creds: cm,
transport: rt,
log: log,
metrics: newHTTPProxyMetrics(cfg.PromRegistry, cfg.PromNamespace),
localhost: []string{"localhost", "0.0.0.0", "::"},
kerberosAdapter: kerberosAdapter,
}
if err := hp.configureProxy(); err != nil {
return nil, err
}
// connect to Kerberos KDC server and authenticate
if hp.kerberosAdapter != nil {
err := hp.kerberosAdapter.ConnectToKDC()
if err != nil {
return nil, err
}
}
return hp, nil
}
func (hp *HTTPProxy) configureHTTPS() error {
if hp.config.CertFile == "" && hp.config.KeyFile == "" {
hp.log.Info("no TLS certificate provided, using self-signed certificate")
} else {
hp.log.Debug("loading TLS certificate", "cert", hp.config.CertFile, "key", hp.config.KeyFile)
}
hp.tlsConfig = httpsTLSConfigTemplate()
return hp.config.ConfigureTLSConfig(hp.tlsConfig)
}
func (hp *HTTPProxy) configureProxy() error {
hp.proxy = new(martian.Proxy)
hp.proxy.AllowHTTP = true
hp.proxy.RequestIDHeader = hp.config.RequestIDHeader
hp.proxy.ConnectFunc = hp.config.ConnectFunc
hp.proxy.ConnectTimeout = hp.config.ConnectTimeout
hp.proxy.WithoutWarning = true
hp.proxy.ErrorResponse = hp.errorResponse
hp.proxy.IdleTimeout = hp.config.IdleTimeout
hp.proxy.TLSHandshakeTimeout = hp.config.TLSServerConfig.HandshakeTimeout
hp.proxy.ReadTimeout = hp.config.ReadTimeout
hp.proxy.ReadHeaderTimeout = hp.config.ReadHeaderTimeout
hp.proxy.WriteTimeout = hp.config.WriteTimeout
if hp.config.MITM != nil {
mc, err := newMartianMITMConfig(hp.config.MITM)
if err != nil {
return fmt.Errorf("mitm: %w", err)
}
if hp.config.MITM.CACertFile == "" {
hp.log.Info("using MITM with self-signed CA certificate", "sha256 fingerprint", sha256.Sum256(mc.CACert().Raw))
} else {
hp.log.Info("using MITM")
}
registerMITMCacheMetrics(hp.config.PromRegistry, hp.config.PromNamespace+"_mitm_", mc.CacheMetrics)
hp.mitmCACert = mc.CACert()
if err := registerCertExpirationMetric(hp.config.PromConfig, hp.mitmCACert, "mitm"); err != nil {
return fmt.Errorf("report mitm cert expiration: %w", err)
}
hp.proxy.MITMConfig = mc
if hp.config.MITMDomains != nil {
hp.proxy.MITMFilter = func(req *http.Request) bool {
return hp.config.MITMDomains.Match(req.URL.Hostname())
}
}
hp.proxy.MITMTLSHandshakeTimeout = hp.config.TLSServerConfig.HandshakeTimeout
}
hp.proxy.RoundTripper = hp.transport
switch {
case hp.config.UpstreamProxyFunc != nil:
hp.log.Info("using external proxy function")
hp.proxyFunc = hp.config.UpstreamProxyFunc
case hp.config.UpstreamProxy != nil:
u := hp.upstreamProxyURL()
hp.log.Info("using upstream proxy", "url", u.Redacted())
hp.proxyFunc = http.ProxyURL(u)
case hp.pac != nil:
hp.log.Info("using PAC proxy")
hp.proxyFunc = hp.pacProxy
default:
hp.log.Info("no upstream proxy specified")
}
if hp.config.DirectDomains != nil {
hp.proxyFunc = hp.directDomains(hp.proxyFunc)
}
hp.log.Info("proxy localhost", "mode", hp.config.ProxyLocalhost)
if hp.config.ProxyLocalhost == DirectProxyLocalhost {
hp.proxyFunc = hp.directLocalhost(hp.proxyFunc)
}
hp.proxy.ProxyURL = hp.proxyFunc
mw, trace := hp.middlewareStack()
hp.proxy.RequestModifier = mw
hp.proxy.ResponseModifier = mw
hp.proxy.Trace = trace
return nil
}
func (hp *HTTPProxy) upstreamProxyURL() *url.URL {
proxyURL := new(url.URL)
*proxyURL = *hp.config.UpstreamProxy
// do not attach proxy credentials if we are using Kerberos
// to auth upstream proxy and clear existing auth data
// so http.RoundTripper would not try to add custom Authorization header
if hp.kerberosAdapter != nil && hp.kerberosAdapter.GetConfig().AuthUpstreamProxy {
hp.log.Info("Kerberos upstream proxy authentication is enabled. " +
"Existing proxy credentials for basic authentication will be ignored.")
proxyURL.User = nil
return proxyURL
}
if proxyURL.User == nil {
if u := hp.creds.MatchURL(proxyURL); u != nil {
proxyURL.User = u
}
}
return proxyURL
}
func (hp *HTTPProxy) pacProxy(r *http.Request) (*url.URL, error) {
s, err := hp.pac.FindProxyForURL(r.URL, "")
if err != nil {
return nil, err
}
p, err := pac.Proxies(s).First()
if err != nil {
return nil, err
}
proxyURL := p.URL()
// do not attach proxy credentials if we are using Kerberos
// to auth upstream proxy and clear existing auth data
// so http.RoundTripper would not try to add custom Authorization header
if hp.kerberosAdapter != nil && hp.kerberosAdapter.GetConfig().AuthUpstreamProxy {
proxyURL.User = nil
return proxyURL, nil
}
if u := hp.creds.MatchURL(proxyURL); u != nil {
proxyURL.User = u
}
return proxyURL, nil
}
func (hp *HTTPProxy) middlewareStack() (martian.RequestResponseModifier, *martian.ProxyTrace) {
var trace *martian.ProxyTrace
// Wrap stack in a group so that we can run security checks before the httpspec modifiers.
topg := fifo.NewGroup()
if hp.config.BasicAuth != nil {
hp.log.Info("basic auth enabled")
topg.AddRequestModifier(hp.basicAuth(hp.config.BasicAuth))
}
if hp.config.ProxyLocalhost == DenyProxyLocalhost {
topg.AddRequestModifier(hp.denyLocalhost())
}
if hp.config.DenyDomains != nil {
topg.AddRequestModifier(hp.denyDomains(hp.config.DenyDomains))
}
// stack contains the request/response modifiers in the order they are applied.
// fg is the inner stack that is executed after the core request modifiers and before the core response modifiers.
stack, fg := httpspec.NewStack(hp.config.Name)
// inject Kerberos SPNEGO authentication header to selected domains
// when Kerberos is enabled
// it needs to be in new stack to avoid Martian HopByHopModifier
// messing with Proxy-Authorization header
if hp.kerberosAdapter != nil {
stack.AddRequestModifier(hp.injectKerberosSPNEGOAuthentication())
}
if hp.kerberosAdapter != nil && hp.kerberosAdapter.GetConfig().AuthUpstreamProxy && hp.proxyFunc != nil {
stack.AddRequestModifier(hp.injectKerberosUpstreamProxyAuthorizationHeader())
}
topg.AddRequestModifier(stack)
topg.AddResponseModifier(stack)
for _, m := range hp.config.RequestModifiers {
fg.AddRequestModifier(m)
}
for _, m := range hp.config.ResponseModifiers {
fg.AddResponseModifier(m)
}
if hp.config.LogHTTPMode != httplog.None {
lf := httplog.NewStructuredLogger(hp.log.Info, hp.config.LogHTTPMode).LogFunc()
fg.AddResponseModifier(lf)
}
if hp.config.PromRegistry != nil {
p := middleware.NewPrometheus(hp.config.PromRegistry, hp.config.PromNamespace, hp.config.PromHTTPOpts...)
trace = new(martian.ProxyTrace)
trace.ReadRequest = func(info martian.ReadRequestInfo) {
if info.Req != nil {
p.ReadRequest(info.Req)
}
}
trace.WroteResponse = func(info martian.WroteResponseInfo) {
if info.Res != nil {
p.WroteResponse(info.Res)
}
}
}
fg.AddRequestModifier(martian.RequestModifierFunc(hp.setBasicAuth))
fg.AddRequestModifier(martian.RequestModifierFunc(setEmptyUserAgent))
return topg.ToImmutable(), trace
}
func (hp *HTTPProxy) basicAuth(u *url.Userinfo) martian.RequestModifier {
user := u.Username()
pass, _ := u.Password()
ba := middleware.NewProxyBasicAuth()
return martian.RequestModifierFunc(func(req *http.Request) error {
if !ba.AuthenticatedRequest(req, user, pass) {
return ErrProxyAuthentication
}
return nil
})
}
func (hp *HTTPProxy) injectKerberosSPNEGOAuthentication() martian.RequestModifier {
// Preemprive SPNEGO authentication - do not wait for 401 code and negotiation
// Generate and inject auth header in advance using configured host list
return martian.RequestModifierFunc(func(req *http.Request) error {
// TODO: use a map or something faster than array lookup
if slices.Contains(hp.kerberosAdapter.GetConfig().KerberosEnabledHosts, strings.ToLower(req.URL.Hostname())) {
spn, err := hp.kerberosAdapter.GetSPNForHost(req.URL.Hostname())
if err != nil {
return err
}
authHeaderValue, err := hp.kerberosAdapter.GetSPNEGOHeaderValue(spn)
if err != nil {
return fmt.Errorf("error getting upstream proxy Kerberos authentication header for host %s: %w", req.URL.Hostname(), err)
}
req.Header.Set("Authorization", authHeaderValue)
}
return nil
})
}
func (hp *HTTPProxy) injectKerberosUpstreamProxyAuthorizationHeader() martian.RequestModifier {
// Kerberos upstream proxy authentication case for non-TLS requests
// When using TLS, we use proxy CONNECT method and different code flow to handle
// CONNECT headers and Kerberos authentication
// when using plain HTTP over proxy, there is no CONNECT method and we need to
// detect such case and inject Proxy-Authentication
return martian.RequestModifierFunc(func(req *http.Request) error {
if req.URL.Scheme != "http" || req.Method == http.MethodConnect {
return nil
}
if hp.proxyFunc == nil {
return nil
}
proxyURL, err := hp.proxyFunc(req)
if err != nil {
return err
}
if proxyURL != nil {
spn, err := hp.kerberosAdapter.GetSPNForHost(proxyURL.Hostname())
if err != nil {
return err
}
authHeaderValue, err := hp.kerberosAdapter.GetSPNEGOHeaderValue(spn)
if err != nil {
return fmt.Errorf("error getting upstream proxy Kerberos authentication header for proxy %s: %w", proxyURL.Hostname(), err)
}
req.Header.Set("Proxy-Authorization", authHeaderValue)
}
return nil
})
}
func (hp *HTTPProxy) denyLocalhost() martian.RequestModifier {
return martian.RequestModifierFunc(func(req *http.Request) error {
if hp.isLocalhost(req.URL.Hostname()) {
return ErrProxyLocalhost
}
return nil
})
}
func (hp *HTTPProxy) denyDomains(r Matcher) martian.RequestModifier {
return martian.RequestModifierFunc(func(req *http.Request) error {
if r.Match(req.URL.Hostname()) {
return ErrProxyDenied
}
return nil
})
}
func (hp *HTTPProxy) directDomains(fn ProxyFunc) ProxyFunc {
if fn == nil {
return nil
}
return func(req *http.Request) (*url.URL, error) {
if hp.config.DirectDomains.Match(req.URL.Hostname()) {
return nil, nil
}
return fn(req)
}
}
func (hp *HTTPProxy) directLocalhost(fn ProxyFunc) ProxyFunc {
if fn == nil {
return nil
}
return func(req *http.Request) (*url.URL, error) {
if hp.isLocalhost(req.URL.Hostname()) {
return nil, nil
}
return fn(req)
}
}
func (hp *HTTPProxy) isLocalhost(host string) bool {
host = strings.ToLower(host)
if slices.Contains(hp.localhost, host) {
return true
}
if ip := net.ParseIP(host); ip != nil && ip.IsLoopback() {
return true
}
return false
}
func (hp *HTTPProxy) setBasicAuth(req *http.Request) error {
if req.Header.Get("Authorization") == "" {
if u := hp.creds.MatchURL(req.URL); u != nil {
p, _ := u.Password()
req.SetBasicAuth(u.Username(), p)
}
}
return nil
}
func setEmptyUserAgent(req *http.Request) error {
if _, ok := req.Header["User-Agent"]; !ok {
// If the outbound request doesn't have a User-Agent header set,
// don't send the default Go HTTP client User-Agent.
req.Header.Set("User-Agent", "")
}
return nil
}
func (hp *HTTPProxy) MITMCACert() *x509.Certificate {
return hp.mitmCACert
}
func (hp *HTTPProxy) ProxyFunc() ProxyFunc {
return hp.proxyFunc
}
func (hp *HTTPProxy) handler() http.Handler {
return hp.proxy.Handler()
}
func (hp *HTTPProxy) Run(ctx context.Context) error {
if hp.config.TestingHTTPHandler {
hp.log.Info("using http handler")
return hp.runHTTPHandler(ctx)
}
return hp.run(ctx)
}
func (hp *HTTPProxy) runHTTPHandler(ctx context.Context) error {
srv := http.Server{
Handler: hp.handler(),
IdleTimeout: hp.config.IdleTimeout,
ReadTimeout: hp.config.ReadTimeout,
ReadHeaderTimeout: hp.config.ReadHeaderTimeout,
WriteTimeout: hp.config.WriteTimeout,
}
var g errgroup.Group
g.Go(func() error {
<-ctx.Done()
ctxErr := ctx.Err()
ctx, cancel := shutdownContext(hp.config.shutdownConfig)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
hp.log.Debug("failed to gracefully shutdown server", "error", err)
if err := srv.Close(); err != nil {
hp.log.Debug("failed to close server", "error", err)
}
}
return ctxErr
})
for i := range hp.listeners {
l := hp.listeners[i]
g.Go(func() error {
err := srv.Serve(l)
if errors.Is(err, http.ErrServerClosed) {
err = nil
}
return err
})
}
return g.Wait()
}
func (hp *HTTPProxy) run(ctx context.Context) error {
var g errgroup.Group
g.Go(func() error {
<-ctx.Done()
ctxErr := ctx.Err()
// Close listeners first to prevent new connections.
if err := hp.Close(); err != nil {
hp.log.Debug("failed to close listeners", "error", err)
}
ctx, cancel := shutdownContext(hp.config.shutdownConfig)
defer cancel()
if err := hp.proxy.Shutdown(ctx); err != nil {
hp.log.Debug("failed to gracefully shutdown server", "error", err)
if err := hp.proxy.Close(); err != nil {
hp.log.Debug("failed to close server", "error", err)
}
}
if tr, ok := hp.transport.(*http.Transport); ok {
tr.CloseIdleConnections()
}
return ctxErr
})
for i := range hp.listeners {
l := hp.listeners[i]
g.Go(func() error {
err := hp.proxy.Serve(l)
if errors.Is(err, net.ErrClosed) {
err = nil
}
return err
})
}
return g.Wait()
}
func (hp *HTTPProxy) listen() ([]net.Listener, error) {
switch hp.config.Protocol {
case HTTPScheme, HTTPSScheme, HTTP2Scheme:
default:
return nil, fmt.Errorf("invalid protocol %q", hp.config.Protocol)
}
if len(hp.config.ExtraListeners) == 0 {
l := &Listener{
ListenerConfig: hp.config.ListenerConfig,
TLSConfig: hp.tlsConfig,
PromConfig: PromConfig{
PromNamespace: hp.config.PromNamespace,
PromRegistry: hp.config.PromRegistry,
},
}
if err := l.Listen(); err != nil {
return nil, err
}
return []net.Listener{l}, nil
}
return MultiListener{
ListenerConfigs: append([]NamedListenerConfig{{ListenerConfig: hp.config.ListenerConfig}}, hp.config.ExtraListeners...),
TLSConfig: func(lc NamedListenerConfig) *tls.Config {
return hp.tlsConfig
},
PromConfig: hp.config.PromConfig,
}.Listen()
}
// Addr returns the address the server is listening on.
func (hp *HTTPProxy) Addr() (addrs []string, ok bool) {
addrs = make([]string, len(hp.listeners))
ok = true
for i, l := range hp.listeners {
addrs[i] = l.Addr().String()
if addrs[i] == "" {
ok = false
}
}
return
}
func (hp *HTTPProxy) Close() error {
var err error
for _, l := range hp.listeners {
if e := l.Close(); e != nil {
err = multierr.Append(err, e)
}
}
return err
}