-
Notifications
You must be signed in to change notification settings - Fork 13
/
http_proxy.go
659 lines (565 loc) · 17 KB
/
http_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
// 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"
)
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.Logger
metrics *httpProxyMetrics
proxy *martian.Proxy
mitmCACert *x509.Certificate
proxyFunc ProxyFunc
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.Logger) (*HTTPProxy, error) {
hp, err := newHTTPProxy(cfg, pr, cm, rt, log)
if err != nil {
return nil, err
}
if hp.config.Protocol == HTTPSScheme {
if err := hp.configureHTTPS(); 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.Infof("PROXY server listen address=%s protocol=%s", l.Addr(), 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.Logger) (http.Handler, error) {
hp, err := newHTTPProxy(cfg, pr, cm, rt, log)
if err != nil {
return nil, err
}
return hp.handler(), nil
}
func newHTTPProxy(cfg *HTTPProxyConfig, pr PACResolver, cm *CredentialsMatcher, rt http.RoundTripper, log log.Logger) (*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.Infof("HTTP transport not configured, using standard library default")
rt = http.DefaultTransport.(*http.Transport).Clone()
} else if tr, ok := rt.(*http.Transport); !ok {
log.Debugf("using custom HTTP transport %T", rt)
} else if tr.TLSClientConfig != nil && tr.TLSClientConfig.RootCAs != nil {
log.Infof("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", "::"},
}
if err := hp.configureProxy(); err != nil {
return nil, err
}
return hp, nil
}
func (hp *HTTPProxy) configureHTTPS() error {
if hp.config.CertFile == "" && hp.config.KeyFile == "" {
hp.log.Infof("no TLS certificate provided, using self-signed certificate")
} else {
hp.log.Debugf("loading TLS certificate from %s and %s", hp.config.CertFile, 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.Infof("using MITM with self-signed CA certificate, sha256 fingerprint=%x", sha256.Sum256(mc.CACert().Raw))
} else {
hp.log.Infof("using MITM")
}
hp.mitmCACert = mc.CACert()
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.Infof("using external proxy function")
hp.proxyFunc = hp.config.UpstreamProxyFunc
case hp.config.UpstreamProxy != nil:
u := hp.upstreamProxyURL()
hp.log.Infof("using upstream proxy: %s", u.Redacted())
hp.proxyFunc = http.ProxyURL(u)
case hp.pac != nil:
hp.log.Infof("using PAC proxy")
hp.proxyFunc = hp.pacProxy
default:
hp.log.Infof("no upstream proxy specified")
}
if hp.config.DirectDomains != nil {
hp.proxyFunc = hp.directDomains(hp.proxyFunc)
}
hp.log.Infof("localhost proxying mode=%s", 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
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()
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.Infof("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)
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.NewLogger(hp.log.Infof, hp.config.LogHTTPMode).LogFunc()
fg.AddResponseModifier(lf)
}
if hp.config.PromRegistry != nil {
p := middleware.NewPrometheus(hp.config.PromRegistry, hp.config.PromNamespace, hp.config.PromHTTPOpts...)
stack.AddRequestModifier(p)
stack.AddResponseModifier(p)
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) 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.Infof("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()
var cancel context.CancelFunc
ctx, cancel = shutdownContext(hp.config.shutdownConfig)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
hp.log.Debugf("failed to gracefully shutdown server error=%s", err)
if err := srv.Close(); err != nil {
hp.log.Debugf("failed to close server error=%s", 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.Debugf("failed to close listeners error=%s", err)
}
var cancel context.CancelFunc
ctx, cancel = shutdownContext(hp.config.shutdownConfig)
defer cancel()
if err := hp.proxy.Shutdown(ctx); err != nil {
hp.log.Debugf("failed to gracefully shutdown server error=%s", err)
if err := hp.proxy.Close(); err != nil {
hp.log.Debugf("failed to close server error=%s", 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
}