forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forward_proxy.go
414 lines (350 loc) · 12.1 KB
/
forward_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
/*
Copyright 2022 Gravitational, Inc.
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://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 alpnproxy
import (
"context"
"crypto/tls"
"io"
"net"
"net/http"
"net/url"
"strings"
"sync"
"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
"golang.org/x/net/http/httpproxy"
"github.com/gravitational/teleport/api/types"
awsapiutils "github.com/gravitational/teleport/api/utils/aws"
"github.com/gravitational/teleport/api/utils/azure"
"github.com/gravitational/teleport/lib/utils"
)
// IsConnectRequest returns true if the request is a HTTP CONNECT tunnel
// request.
//
// https://datatracker.ietf.org/doc/html/rfc7231#section-4.3.6
func IsConnectRequest(req *http.Request) bool {
return req.Method == http.MethodConnect
}
// ConnectRequestHandler defines handler for handling CONNECT requests.
type ConnectRequestHandler interface {
// Match returns true if this handler wants to handle the provided request.
Match(req *http.Request) bool
// Handle handles the request with provided client connection.
Handle(ctx context.Context, clientConn net.Conn, req *http.Request)
}
// ForwardProxyConfig is the config for forward proxy server.
type ForwardProxyConfig struct {
// Listener is the network listener.
Listener net.Listener
// CloseContext is the close context.
CloseContext context.Context
// Handlers is a list of CONNECT request handlers.
Handlers []ConnectRequestHandler
}
// CheckAndSetDefaults checks and sets default config values.
func (c *ForwardProxyConfig) CheckAndSetDefaults() error {
if c.Listener == nil {
return trace.BadParameter("missing listener")
}
if c.CloseContext == nil {
return trace.BadParameter("missing close context")
}
return nil
}
// ForwardProxy is a forward proxy that serves CONNECT tunnel requests.
type ForwardProxy struct {
cfg ForwardProxyConfig
}
// NewForwardProxy creates a new forward proxy server.
func NewForwardProxy(cfg ForwardProxyConfig) (*ForwardProxy, error) {
if err := cfg.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return &ForwardProxy{
cfg: cfg,
}, nil
}
// Start starts serving on the listener.
func (p *ForwardProxy) Start() error {
err := http.Serve(p.cfg.Listener, p)
if err != nil && !utils.IsUseOfClosedNetworkError(err) {
return trace.Wrap(err)
}
return nil
}
// Close closes the forward proxy.
func (p *ForwardProxy) Close() error {
if err := p.cfg.Listener.Close(); err != nil {
return trace.Wrap(err)
}
return nil
}
// GetAddr returns the listener address.
func (p *ForwardProxy) GetAddr() string {
return p.cfg.Listener.Addr().String()
}
// ServeHTTP serves HTTP requests. Implements http.Handler.
func (p *ForwardProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// Only allow CONNECT tunnel requests. Reject if clients send original HTTP
// requests without CONNECT tunnel.
if !IsConnectRequest(req) {
rw.WriteHeader(http.StatusBadRequest)
return
}
clientConn := hijackClientConnection(rw)
if clientConn == nil {
log.Error("Failed to hijack client connection.")
rw.WriteHeader(http.StatusInternalServerError)
return
}
defer clientConn.Close()
for _, handler := range p.cfg.Handlers {
if handler.Match(req) {
handler.Handle(p.cfg.CloseContext, clientConn, req)
return
}
}
writeHeaderToHijackedConnection(clientConn, req, http.StatusBadRequest)
}
// ForwardToHostHandlerConfig is the config for ForwardToHostHandler.
type ForwardToHostHandlerConfig struct {
// Match returns true if this handler wants to handle the provided request.
MatchFunc func(req *http.Request) bool
// Host is the destination to forward the request to. If empty, the request
// is forwarded to its original host.
Host string
}
// SetDefaults sets default config values.
func (c *ForwardToHostHandlerConfig) SetDefaults() {
if c.MatchFunc == nil {
c.MatchFunc = MatchAllRequests
}
}
// MatchAllRequests is a MatchFunc that returns true for all requests.
func MatchAllRequests(req *http.Request) bool {
return true
}
// MatchAWSRequests is a MatchFunc that returns true if request is an AWS API
// request.
func MatchAWSRequests(req *http.Request) bool {
return awsapiutils.IsAWSEndpoint(req.Host)
}
// MatchAzureRequests is a MatchFunc that returns true if request is an Azure API
// request.
func MatchAzureRequests(req *http.Request) bool {
h := req.URL.Hostname()
return azure.IsAzureEndpoint(h) || types.TeleportAzureMSIEndpoint == h
}
// ForwardToHostHandler is a CONNECT request handler that forwards requests to
// designated host.
type ForwardToHostHandler struct {
cfg ForwardToHostHandlerConfig
}
// NewForwardToHostHandler creates a new ForwardToHostHandler.
func NewForwardToHostHandler(cfg ForwardToHostHandlerConfig) *ForwardToHostHandler {
cfg.SetDefaults()
return &ForwardToHostHandler{
cfg: cfg,
}
}
// NewForwardToOriginalHostHandler creates a new CONNECT request handler that
// forwards all requests to their original hosts.
func NewForwardToOriginalHostHandler() *ForwardToHostHandler {
return NewForwardToHostHandler(ForwardToHostHandlerConfig{})
}
// Match returns true if this handler wants to handle the provided request.
func (h *ForwardToHostHandler) Match(req *http.Request) bool {
return h.cfg.MatchFunc(req)
}
// Handle handles the request with provided client connection.
func (h *ForwardToHostHandler) Handle(ctx context.Context, clientConn net.Conn, req *http.Request) {
host := h.cfg.Host
if host == "" {
host = req.Host
}
serverConn, err := net.Dial("tcp", host)
if err != nil {
log.WithError(err).Errorf("Failed to connect to host %q.", host)
writeHeaderToHijackedConnection(clientConn, req, http.StatusServiceUnavailable)
return
}
defer serverConn.Close()
// Send OK to client to let it know the tunnel is ready.
if ok := writeHeaderToHijackedConnection(clientConn, req, http.StatusOK); !ok {
return
}
startForwardProxy(ctx, clientConn, serverConn, req.Host)
}
// ForwardToSystemProxyHandlerConfig is the config for
// ForwardToSystemProxyHandler.
type ForwardToSystemProxyHandlerConfig struct {
// TunnelProtocol is the protocol of the requests being tunneled.
TunnelProtocol string
// InsecureSystemProxy allows insecure system proxy when forwarding
// unwanted requests.
InsecureSystemProxy bool
// SystemProxyFunc is the function that determines the system proxy URL to
// use for provided request URL.
SystemProxyFunc func(reqURL *url.URL) (*url.URL, error)
}
// SetDefaults sets default config values.
func (c *ForwardToSystemProxyHandlerConfig) SetDefaults() {
if c.TunnelProtocol == "" {
c.TunnelProtocol = "https"
}
// By default, use the HTTPS_PROXY etc. settings from environment where our
// server is run.
if c.SystemProxyFunc == nil {
c.SystemProxyFunc = httpproxy.FromEnvironment().ProxyFunc()
}
}
// ForwardToSystemProxyHandler is a CONNECT request handler that forwards
// requests to existing system or corporate forward proxies where our server is
// run.
//
// Here "system" is used to differentiate the forward proxy users have outside
// Teleport from our own forward proxy server. The purpose of this handler is
// to honor "system" proxy settings so the requests are forwarded to "system"
// proxies as intended instead of going to their original hosts.
type ForwardToSystemProxyHandler struct {
cfg ForwardToSystemProxyHandlerConfig
}
// NewForwardToSystemProxyHandler creates a new ForwardToSystemProxyHandler.
func NewForwardToSystemProxyHandler(cfg ForwardToSystemProxyHandlerConfig) *ForwardToSystemProxyHandler {
cfg.SetDefaults()
return &ForwardToSystemProxyHandler{
cfg: cfg,
}
}
// Match returns true if this handler wants to handle the provided request.
func (h *ForwardToSystemProxyHandler) Match(req *http.Request) bool {
return h.getSystemProxyURL(req) != nil
}
// Handle handles the request with provided client connection.
func (h *ForwardToSystemProxyHandler) Handle(ctx context.Context, clientConn net.Conn, req *http.Request) {
systemProxyURL := h.getSystemProxyURL(req)
if systemProxyURL == nil {
writeHeaderToHijackedConnection(clientConn, req, http.StatusBadRequest)
return
}
serverConn, err := h.connectToSystemProxy(systemProxyURL)
if err != nil {
log.WithError(err).Errorf("Failed to connect to system proxy %q.", systemProxyURL.Host)
writeHeaderToHijackedConnection(clientConn, req, http.StatusBadGateway)
return
}
defer serverConn.Close()
log.Debugf("Connected to system proxy %v.", systemProxyURL)
// Send original CONNECT request to system proxy.
if err = req.WriteProxy(serverConn); err != nil {
log.WithError(err).Errorf("Failed to send CONNTECT request to system proxy %q.", systemProxyURL.Host)
writeHeaderToHijackedConnection(clientConn, req, http.StatusBadGateway)
return
}
startForwardProxy(ctx, clientConn, serverConn, req.Host)
}
// getSystemProxyURL returns the system proxy URL.
func (h *ForwardToSystemProxyHandler) getSystemProxyURL(req *http.Request) *url.URL {
systemProxyURL, err := h.cfg.SystemProxyFunc(&url.URL{
Host: req.Host,
Scheme: h.cfg.TunnelProtocol,
})
if err == nil && systemProxyURL != nil {
return systemProxyURL
}
// If error exists, make a log for debugging purpose.
if err != nil {
log.WithError(err).Debugf("Failed to get system proxy.")
}
return nil
}
// connectToSystemProxy connects to the system proxy and returns the server
// connection.
func (h *ForwardToSystemProxyHandler) connectToSystemProxy(systemProxyURL *url.URL) (net.Conn, error) {
var err error
var serverConn net.Conn
switch strings.ToLower(systemProxyURL.Scheme) {
case "http":
serverConn, err = net.Dial("tcp", systemProxyURL.Host)
if err != nil {
return nil, trace.Wrap(err)
}
case "https":
serverConn, err = tls.Dial("tcp", systemProxyURL.Host, &tls.Config{
InsecureSkipVerify: h.cfg.InsecureSystemProxy,
})
if err != nil {
return nil, trace.Wrap(err)
}
default:
return nil, trace.BadParameter("unsupported system proxy %v", systemProxyURL)
}
return serverConn, nil
}
// startForwardProxy starts streaming between client and server.
func startForwardProxy(ctx context.Context, clientConn, serverConn net.Conn, host string) {
log.Debugf("Started forwarding request for %q.", host)
defer log.Debugf("Stopped forwarding request for %q.", host)
closeContext, closeCancel := context.WithCancel(ctx)
defer closeCancel()
// Forcefully close connections when input context is done, to make sure
// the stream goroutines exit.
go func() {
<-closeContext.Done()
clientConn.Close()
serverConn.Close()
}()
wg := sync.WaitGroup{}
wg.Add(2)
stream := func(reader, writer net.Conn) {
_, err := io.Copy(reader, writer)
if err != nil && !utils.IsOKNetworkError(err) {
log.WithError(err).Errorf("Failed to stream from %q to %q.", reader.LocalAddr(), writer.LocalAddr())
}
// Close one side at a time.
if readerConn, ok := reader.(*net.TCPConn); ok {
readerConn.CloseRead()
}
if writerConn, ok := writer.(*net.TCPConn); ok {
writerConn.CloseWrite()
}
wg.Done()
}
go stream(clientConn, serverConn)
go stream(serverConn, clientConn)
wg.Wait()
}
// hijackClientConnection hijacks client connection.
func hijackClientConnection(rw http.ResponseWriter) net.Conn {
hijacker, ok := rw.(http.Hijacker)
if !ok {
return nil
}
clientConn, _, _ := hijacker.Hijack()
return clientConn
}
// writeHeaderToHijackedConnection writes HTTP status to hijacked connection.
func writeHeaderToHijackedConnection(conn net.Conn, req *http.Request, statusCode int) bool {
resp := http.Response{
StatusCode: statusCode,
Proto: req.Proto,
ProtoMajor: req.ProtoMajor,
ProtoMinor: req.ProtoMinor,
}
err := resp.Write(conn)
if err != nil && !utils.IsOKNetworkError(err) {
log.WithError(err).Errorf("Failed to write status code %d to client connection.", statusCode)
return false
}
return true
}