2020-10-08 10:12:26 +00:00
|
|
|
package connection
|
|
|
|
|
|
|
|
import (
|
2023-03-29 16:21:19 +00:00
|
|
|
"bufio"
|
2020-10-08 10:12:26 +00:00
|
|
|
"context"
|
2022-03-04 11:35:57 +00:00
|
|
|
gojson "encoding/json"
|
2021-01-20 19:41:09 +00:00
|
|
|
"fmt"
|
2020-10-08 10:12:26 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2021-09-22 16:33:05 +00:00
|
|
|
"runtime/debug"
|
2020-10-08 10:12:26 +00:00
|
|
|
"strings"
|
2020-10-16 10:13:48 +00:00
|
|
|
"sync"
|
2020-10-08 10:12:26 +00:00
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
2020-10-08 10:12:26 +00:00
|
|
|
"golang.org/x/net/http2"
|
2021-03-23 14:30:43 +00:00
|
|
|
|
2022-04-06 23:20:29 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tracing"
|
2021-03-23 14:30:43 +00:00
|
|
|
tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
2020-10-08 10:12:26 +00:00
|
|
|
)
|
|
|
|
|
2021-03-26 04:04:56 +00:00
|
|
|
// note: these constants are exported so we can reuse them in the edge-side code
|
2020-10-08 10:12:26 +00:00
|
|
|
const (
|
2021-03-26 04:04:56 +00:00
|
|
|
InternalUpgradeHeader = "Cf-Cloudflared-Proxy-Connection-Upgrade"
|
|
|
|
InternalTCPProxySrcHeader = "Cf-Cloudflared-Proxy-Src"
|
|
|
|
WebsocketUpgrade = "websocket"
|
|
|
|
ControlStreamUpgrade = "control-stream"
|
2022-03-04 11:35:57 +00:00
|
|
|
ConfigurationUpdate = "update-configuration"
|
2020-10-08 10:12:26 +00:00
|
|
|
)
|
|
|
|
|
2021-01-20 19:41:09 +00:00
|
|
|
var errEdgeConnectionClosed = fmt.Errorf("connection with edge closed")
|
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
// HTTP2Connection represents a net.Conn that uses HTTP2 frames to proxy traffic from the edge to cloudflared on the
|
|
|
|
// origin.
|
|
|
|
type HTTP2Connection struct {
|
2022-02-11 10:49:06 +00:00
|
|
|
conn net.Conn
|
|
|
|
server *http2.Server
|
|
|
|
orchestrator Orchestrator
|
|
|
|
connOptions *tunnelpogs.ConnectionOptions
|
|
|
|
observer *Observer
|
|
|
|
connIndex uint8
|
2021-01-27 13:19:37 +00:00
|
|
|
|
2021-08-17 14:30:02 +00:00
|
|
|
log *zerolog.Logger
|
|
|
|
activeRequestsWG sync.WaitGroup
|
|
|
|
controlStreamHandler ControlStreamHandler
|
|
|
|
stoppedGracefully bool
|
|
|
|
controlStreamErr error // result of running control stream handler
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
// NewHTTP2Connection returns a new instance of HTTP2Connection.
|
2020-10-23 14:49:24 +00:00
|
|
|
func NewHTTP2Connection(
|
|
|
|
conn net.Conn,
|
2022-02-11 10:49:06 +00:00
|
|
|
orchestrator Orchestrator,
|
2020-10-23 14:49:24 +00:00
|
|
|
connOptions *tunnelpogs.ConnectionOptions,
|
|
|
|
observer *Observer,
|
|
|
|
connIndex uint8,
|
2021-08-17 14:30:02 +00:00
|
|
|
controlStreamHandler ControlStreamHandler,
|
2021-07-16 15:14:37 +00:00
|
|
|
log *zerolog.Logger,
|
|
|
|
) *HTTP2Connection {
|
|
|
|
return &HTTP2Connection{
|
2020-10-16 10:13:48 +00:00
|
|
|
conn: conn,
|
|
|
|
server: &http2.Server{
|
2021-10-08 12:48:20 +00:00
|
|
|
MaxConcurrentStreams: MaxConcurrentStreams,
|
2020-10-16 10:13:48 +00:00
|
|
|
},
|
2022-02-11 10:49:06 +00:00
|
|
|
orchestrator: orchestrator,
|
2021-08-17 14:30:02 +00:00
|
|
|
connOptions: connOptions,
|
|
|
|
observer: observer,
|
|
|
|
connIndex: connIndex,
|
|
|
|
controlStreamHandler: controlStreamHandler,
|
|
|
|
log: log,
|
2020-10-14 13:42:00 +00:00
|
|
|
}
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
// Serve serves an HTTP2 server that the edge can talk to.
|
|
|
|
func (c *HTTP2Connection) Serve(ctx context.Context) error {
|
2020-10-08 10:12:26 +00:00
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
c.close()
|
|
|
|
}()
|
|
|
|
c.server.ServeConn(c.conn, &http2.ServeConnOpts{
|
|
|
|
Context: ctx,
|
|
|
|
Handler: c,
|
|
|
|
})
|
2021-01-20 19:41:09 +00:00
|
|
|
|
2021-01-27 13:19:37 +00:00
|
|
|
switch {
|
2021-08-17 14:30:02 +00:00
|
|
|
case c.controlStreamHandler.IsStopped():
|
2021-01-27 13:19:37 +00:00
|
|
|
return nil
|
|
|
|
case c.controlStreamErr != nil:
|
|
|
|
return c.controlStreamErr
|
|
|
|
default:
|
|
|
|
c.observer.log.Info().Uint8(LogFieldConnIndex, c.connIndex).Msg("Lost connection with the edge")
|
2021-01-20 19:41:09 +00:00
|
|
|
return errEdgeConnectionClosed
|
|
|
|
}
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
func (c *HTTP2Connection) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2021-01-27 13:19:37 +00:00
|
|
|
c.activeRequestsWG.Add(1)
|
|
|
|
defer c.activeRequestsWG.Done()
|
2020-10-16 10:13:48 +00:00
|
|
|
|
2021-01-15 17:25:56 +00:00
|
|
|
connType := determineHTTP2Type(r)
|
2021-06-18 11:21:11 +00:00
|
|
|
handleMissingRequestParts(connType, r)
|
|
|
|
|
2022-03-04 09:31:40 +00:00
|
|
|
respWriter, err := NewHTTP2RespWriter(r, w, connType, c.log)
|
2021-01-15 17:25:56 +00:00
|
|
|
if err != nil {
|
|
|
|
c.observer.log.Error().Msg(err.Error())
|
2020-10-23 14:49:24 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-11 19:59:45 +00:00
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
originProxy, err := c.orchestrator.GetOriginProxy()
|
|
|
|
if err != nil {
|
|
|
|
c.observer.log.Error().Msg(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-16 12:42:59 +00:00
|
|
|
var requestErr error
|
2021-01-15 17:25:56 +00:00
|
|
|
switch connType {
|
|
|
|
case TypeControlStream:
|
2023-01-16 12:42:59 +00:00
|
|
|
requestErr = c.controlStreamHandler.ServeControlStream(r.Context(), respWriter, c.connOptions, c.orchestrator)
|
|
|
|
if requestErr != nil {
|
|
|
|
c.controlStreamErr = requestErr
|
2021-07-16 15:14:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 11:35:57 +00:00
|
|
|
case TypeConfiguration:
|
2023-01-16 12:42:59 +00:00
|
|
|
requestErr = c.handleConfigurationUpdate(respWriter, r)
|
2022-03-04 11:35:57 +00:00
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
case TypeWebsocket, TypeHTTP:
|
2020-10-08 10:12:26 +00:00
|
|
|
stripWebsocketUpgradeHeader(r)
|
2022-04-06 23:20:29 +00:00
|
|
|
// Check for tracing on request
|
2023-02-22 14:52:44 +00:00
|
|
|
tr := tracing.NewTracedHTTPRequest(r, c.connIndex, c.log)
|
2022-04-06 23:20:29 +00:00
|
|
|
if err := originProxy.ProxyHTTP(respWriter, tr, connType == TypeWebsocket); err != nil {
|
2023-01-16 12:42:59 +00:00
|
|
|
requestErr = fmt.Errorf("Failed to proxy HTTP: %w", err)
|
2021-07-16 15:14:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case TypeTCP:
|
|
|
|
host, err := getRequestHost(r)
|
|
|
|
if err != nil {
|
2023-01-16 12:42:59 +00:00
|
|
|
requestErr = fmt.Errorf(`cloudflared received a warp-routing request with an empty host value: %w`, err)
|
|
|
|
break
|
2021-07-16 15:14:37 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 13:42:44 +00:00
|
|
|
rws := NewHTTPResponseReadWriterAcker(respWriter, respWriter, r)
|
2023-01-16 12:42:59 +00:00
|
|
|
requestErr = originProxy.ProxyTCP(r.Context(), rws, &TCPRequest{
|
2022-08-11 21:54:12 +00:00
|
|
|
Dest: host,
|
|
|
|
CFRay: FindCfRayHeader(r),
|
|
|
|
LBProbe: IsLBProbeRequest(r),
|
|
|
|
CfTraceID: r.Header.Get(tracing.TracerContextName),
|
2023-02-22 14:52:44 +00:00
|
|
|
ConnIndex: c.connIndex,
|
2023-01-16 12:42:59 +00:00
|
|
|
})
|
2021-07-16 15:14:37 +00:00
|
|
|
|
2021-01-11 19:59:45 +00:00
|
|
|
default:
|
2023-01-16 12:42:59 +00:00
|
|
|
requestErr = fmt.Errorf("Received unknown connection type: %s", connType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if requestErr != nil {
|
|
|
|
c.log.Error().Err(requestErr).Msg("failed to serve incoming request")
|
|
|
|
|
|
|
|
// WriteErrorResponse will return false if status was already written. we need to abort handler.
|
|
|
|
if !respWriter.WriteErrorResponse() {
|
|
|
|
c.log.Debug().Msg("Handler aborted due to failure to write error response after status already sent")
|
|
|
|
panic(http.ErrAbortHandler)
|
|
|
|
}
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-04 11:35:57 +00:00
|
|
|
// ConfigurationUpdateBody is the representation followed by the edge to send updates to cloudflared.
|
|
|
|
type ConfigurationUpdateBody struct {
|
|
|
|
Version int32 `json:"version"`
|
|
|
|
Config gojson.RawMessage `json:"config"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HTTP2Connection) handleConfigurationUpdate(respWriter *http2RespWriter, r *http.Request) error {
|
|
|
|
var configBody ConfigurationUpdateBody
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&configBody); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp := c.orchestrator.UpdateConfig(configBody.Version, configBody.Config)
|
|
|
|
bdy, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = respWriter.Write(bdy)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
func (c *HTTP2Connection) close() {
|
2020-10-16 10:13:48 +00:00
|
|
|
// Wait for all serve HTTP handlers to return
|
2021-01-27 13:19:37 +00:00
|
|
|
c.activeRequestsWG.Wait()
|
2020-10-08 10:12:26 +00:00
|
|
|
c.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
type http2RespWriter struct {
|
2022-08-16 11:21:58 +00:00
|
|
|
r io.Reader
|
|
|
|
w http.ResponseWriter
|
|
|
|
flusher http.Flusher
|
|
|
|
shouldFlush bool
|
|
|
|
statusWritten bool
|
2023-03-07 18:41:15 +00:00
|
|
|
respHeaders http.Header
|
2023-03-29 16:21:19 +00:00
|
|
|
hijackedMutex sync.Mutex
|
|
|
|
hijackedv bool
|
2022-08-16 11:21:58 +00:00
|
|
|
log *zerolog.Logger
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 09:31:40 +00:00
|
|
|
func NewHTTP2RespWriter(r *http.Request, w http.ResponseWriter, connType Type, log *zerolog.Logger) (*http2RespWriter, error) {
|
2021-01-15 17:25:56 +00:00
|
|
|
flusher, isFlusher := w.(http.Flusher)
|
|
|
|
if !isFlusher {
|
|
|
|
respWriter := &http2RespWriter{
|
2022-03-04 09:31:40 +00:00
|
|
|
r: r.Body,
|
|
|
|
w: w,
|
|
|
|
log: log,
|
2021-01-15 17:25:56 +00:00
|
|
|
}
|
|
|
|
respWriter.WriteErrorResponse()
|
|
|
|
return nil, fmt.Errorf("%T doesn't implement http.Flusher", w)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &http2RespWriter{
|
|
|
|
r: r.Body,
|
|
|
|
w: w,
|
|
|
|
flusher: flusher,
|
|
|
|
shouldFlush: connType.shouldFlush(),
|
2023-03-21 18:42:25 +00:00
|
|
|
respHeaders: make(http.Header),
|
2022-03-04 09:31:40 +00:00
|
|
|
log: log,
|
2021-01-15 17:25:56 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-08-16 11:21:58 +00:00
|
|
|
func (rp *http2RespWriter) AddTrailer(trailerName, trailerValue string) {
|
|
|
|
if !rp.statusWritten {
|
|
|
|
rp.log.Warn().Msg("Tried to add Trailer to response before status written. Ignoring...")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rp.w.Header().Add(http2.TrailerPrefix+trailerName, trailerValue)
|
|
|
|
}
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
func (rp *http2RespWriter) WriteRespHeaders(status int, header http.Header) error {
|
2023-03-29 16:21:19 +00:00
|
|
|
if rp.hijacked() {
|
|
|
|
rp.log.Warn().Msg("WriteRespHeaders after hijack")
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-08 10:12:26 +00:00
|
|
|
dest := rp.w.Header()
|
2020-12-09 21:46:53 +00:00
|
|
|
userHeaders := make(http.Header, len(header))
|
2021-03-26 04:04:56 +00:00
|
|
|
for name, values := range header {
|
2022-08-16 11:21:58 +00:00
|
|
|
// lowercase headers for simplicity check
|
2021-03-26 04:04:56 +00:00
|
|
|
h2name := strings.ToLower(name)
|
2021-11-08 18:17:31 +00:00
|
|
|
|
2021-03-26 04:04:56 +00:00
|
|
|
if h2name == "content-length" {
|
|
|
|
// This header has meaning in HTTP/2 and will be used by the edge,
|
2021-11-08 18:17:31 +00:00
|
|
|
// so it should be sent *also* as an HTTP/2 response header.
|
2021-03-26 04:04:56 +00:00
|
|
|
dest[name] = values
|
2021-11-08 18:17:31 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 19:57:50 +00:00
|
|
|
if h2name == tracing.IntCloudflaredTracingHeader {
|
|
|
|
// Add cf-int-cloudflared-tracing header outside of serialized userHeaders
|
2022-08-16 11:21:58 +00:00
|
|
|
dest[tracing.CanonicalCloudflaredTracingHeader] = values
|
2022-04-11 19:57:50 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-08 18:17:31 +00:00
|
|
|
if !IsControlResponseHeader(h2name) || IsWebsocketClientHeader(h2name) {
|
2021-03-26 04:04:56 +00:00
|
|
|
// User headers, on the other hand, must all be serialized so that
|
|
|
|
// HTTP/2 header validation won't be applied to HTTP/1 header values
|
|
|
|
userHeaders[name] = values
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform user header serialization and set them in the single header
|
2021-03-26 04:04:56 +00:00
|
|
|
dest.Set(CanonicalResponseUserHeaders, SerializeHeaders(userHeaders))
|
2022-08-16 11:21:58 +00:00
|
|
|
|
2020-10-23 14:49:24 +00:00
|
|
|
rp.setResponseMetaHeader(responseMetaHeaderOrigin)
|
2020-10-08 10:12:26 +00:00
|
|
|
// HTTP2 removes support for 101 Switching Protocols https://tools.ietf.org/html/rfc7540#section-8.1.1
|
|
|
|
if status == http.StatusSwitchingProtocols {
|
|
|
|
status = http.StatusOK
|
|
|
|
}
|
|
|
|
rp.w.WriteHeader(status)
|
2022-08-16 11:21:58 +00:00
|
|
|
if shouldFlush(header) {
|
2020-10-23 14:49:24 +00:00
|
|
|
rp.shouldFlush = true
|
|
|
|
}
|
|
|
|
if rp.shouldFlush {
|
|
|
|
rp.flusher.Flush()
|
|
|
|
}
|
2022-08-16 11:21:58 +00:00
|
|
|
|
|
|
|
rp.statusWritten = true
|
2020-10-08 10:12:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-07 18:41:15 +00:00
|
|
|
func (rp *http2RespWriter) Header() http.Header {
|
|
|
|
return rp.respHeaders
|
|
|
|
}
|
|
|
|
|
2023-07-06 13:42:44 +00:00
|
|
|
func (rp *http2RespWriter) Flush() {
|
|
|
|
rp.flusher.Flush()
|
|
|
|
}
|
|
|
|
|
2023-03-07 18:41:15 +00:00
|
|
|
func (rp *http2RespWriter) WriteHeader(status int) {
|
2023-03-29 16:21:19 +00:00
|
|
|
if rp.hijacked() {
|
|
|
|
rp.log.Warn().Msg("WriteHeader after hijack")
|
|
|
|
return
|
|
|
|
}
|
2023-03-07 18:41:15 +00:00
|
|
|
rp.WriteRespHeaders(status, rp.respHeaders)
|
|
|
|
}
|
|
|
|
|
2023-03-29 16:21:19 +00:00
|
|
|
func (rp *http2RespWriter) hijacked() bool {
|
|
|
|
rp.hijackedMutex.Lock()
|
|
|
|
defer rp.hijackedMutex.Unlock()
|
|
|
|
return rp.hijackedv
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *http2RespWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
if !rp.statusWritten {
|
|
|
|
return nil, nil, fmt.Errorf("status not yet written before attempting to hijack connection")
|
|
|
|
}
|
|
|
|
// Make sure to flush anything left in the buffer before hijacking
|
|
|
|
if rp.shouldFlush {
|
|
|
|
rp.flusher.Flush()
|
|
|
|
}
|
|
|
|
rp.hijackedMutex.Lock()
|
|
|
|
defer rp.hijackedMutex.Unlock()
|
|
|
|
if rp.hijackedv {
|
|
|
|
return nil, nil, http.ErrHijacked
|
|
|
|
}
|
|
|
|
rp.hijackedv = true
|
|
|
|
conn := &localProxyConnection{rp}
|
|
|
|
// We return the http2RespWriter here because we want to make sure that we flush after every write
|
|
|
|
// otherwise the HTTP2 write buffer waits a few seconds before sending.
|
|
|
|
readWriter := bufio.NewReadWriter(
|
|
|
|
bufio.NewReader(rp),
|
|
|
|
bufio.NewWriter(rp),
|
|
|
|
)
|
|
|
|
return conn, readWriter, nil
|
|
|
|
}
|
|
|
|
|
2023-01-16 12:42:59 +00:00
|
|
|
func (rp *http2RespWriter) WriteErrorResponse() bool {
|
|
|
|
if rp.statusWritten {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-10-16 10:13:48 +00:00
|
|
|
rp.setResponseMetaHeader(responseMetaHeaderCfd)
|
2020-10-08 10:12:26 +00:00
|
|
|
rp.w.WriteHeader(http.StatusBadGateway)
|
2023-01-16 12:42:59 +00:00
|
|
|
rp.statusWritten = true
|
|
|
|
|
|
|
|
return true
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 10:13:48 +00:00
|
|
|
func (rp *http2RespWriter) setResponseMetaHeader(value string) {
|
2021-03-26 04:04:56 +00:00
|
|
|
rp.w.Header().Set(CanonicalResponseMetaHeader, value)
|
2020-10-16 10:13:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 10:12:26 +00:00
|
|
|
func (rp *http2RespWriter) Read(p []byte) (n int, err error) {
|
|
|
|
return rp.r.Read(p)
|
|
|
|
}
|
|
|
|
|
2020-10-23 14:49:24 +00:00
|
|
|
func (rp *http2RespWriter) Write(p []byte) (n int, err error) {
|
2020-10-30 11:41:14 +00:00
|
|
|
defer func() {
|
|
|
|
// Implementer of OriginClient should make sure it doesn't write to the connection after Proxy returns
|
|
|
|
// Register a recover routine just in case.
|
|
|
|
if r := recover(); r != nil {
|
2022-03-04 09:31:40 +00:00
|
|
|
rp.log.Debug().Msgf("Recover from http2 response writer panic, error %s", debug.Stack())
|
2020-10-30 11:41:14 +00:00
|
|
|
}
|
|
|
|
}()
|
2020-10-23 14:49:24 +00:00
|
|
|
n, err = rp.w.Write(p)
|
|
|
|
if err == nil && rp.shouldFlush {
|
|
|
|
rp.flusher.Flush()
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
2020-10-23 14:49:24 +00:00
|
|
|
return n, err
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 14:49:24 +00:00
|
|
|
func (rp *http2RespWriter) Close() error {
|
2020-10-08 10:12:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-15 17:25:56 +00:00
|
|
|
func determineHTTP2Type(r *http.Request) Type {
|
|
|
|
switch {
|
2022-03-04 11:35:57 +00:00
|
|
|
case isConfigurationUpdate(r):
|
|
|
|
return TypeConfiguration
|
2021-01-15 17:25:56 +00:00
|
|
|
case isWebsocketUpgrade(r):
|
|
|
|
return TypeWebsocket
|
|
|
|
case IsTCPStream(r):
|
|
|
|
return TypeTCP
|
|
|
|
case isControlStreamUpgrade(r):
|
|
|
|
return TypeControlStream
|
|
|
|
default:
|
|
|
|
return TypeHTTP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 11:21:11 +00:00
|
|
|
func handleMissingRequestParts(connType Type, r *http.Request) {
|
|
|
|
if connType == TypeHTTP {
|
|
|
|
// http library has no guarantees that we receive a filled URL. If not, then we fill it, as we reuse the request
|
2024-10-15 20:10:30 +00:00
|
|
|
// for proxying. For proxying they should not matter since we control the dialer on every egress proxied.
|
2021-06-18 11:21:11 +00:00
|
|
|
if len(r.URL.Scheme) == 0 {
|
|
|
|
r.URL.Scheme = "http"
|
|
|
|
}
|
|
|
|
if len(r.URL.Host) == 0 {
|
|
|
|
r.URL.Host = "localhost:8080"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 10:12:26 +00:00
|
|
|
func isControlStreamUpgrade(r *http.Request) bool {
|
2021-03-26 04:04:56 +00:00
|
|
|
return r.Header.Get(InternalUpgradeHeader) == ControlStreamUpgrade
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func isWebsocketUpgrade(r *http.Request) bool {
|
2021-03-26 04:04:56 +00:00
|
|
|
return r.Header.Get(InternalUpgradeHeader) == WebsocketUpgrade
|
2021-01-11 19:59:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 11:35:57 +00:00
|
|
|
func isConfigurationUpdate(r *http.Request) bool {
|
|
|
|
return r.Header.Get(InternalUpgradeHeader) == ConfigurationUpdate
|
|
|
|
}
|
|
|
|
|
2021-01-11 19:59:45 +00:00
|
|
|
// IsTCPStream discerns if the connection request needs a tcp stream proxy.
|
|
|
|
func IsTCPStream(r *http.Request) bool {
|
2021-03-26 04:04:56 +00:00
|
|
|
return r.Header.Get(InternalTCPProxySrcHeader) != ""
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func stripWebsocketUpgradeHeader(r *http.Request) {
|
2021-03-26 04:04:56 +00:00
|
|
|
r.Header.Del(InternalUpgradeHeader)
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
2021-07-16 15:14:37 +00:00
|
|
|
|
|
|
|
// getRequestHost returns the host of the http.Request.
|
|
|
|
func getRequestHost(r *http.Request) (string, error) {
|
|
|
|
if r.Host != "" {
|
|
|
|
return r.Host, nil
|
|
|
|
}
|
|
|
|
if r.URL != nil {
|
|
|
|
return r.URL.Host, nil
|
|
|
|
}
|
|
|
|
return "", errors.New("host not set in incoming request")
|
|
|
|
}
|