2020-10-08 10:12:26 +00:00
|
|
|
package connection
|
|
|
|
|
|
|
|
import (
|
2021-07-16 15:14:37 +00:00
|
|
|
"context"
|
2021-01-15 17:25:56 +00:00
|
|
|
"fmt"
|
2020-10-08 10:12:26 +00:00
|
|
|
"io"
|
2021-10-08 12:48:20 +00:00
|
|
|
"math"
|
2020-10-08 10:12:26 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2020-10-23 14:49:24 +00:00
|
|
|
"strings"
|
2020-10-08 10:12:26 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2021-03-23 14:30:43 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
2021-07-16 15:14:37 +00:00
|
|
|
"github.com/cloudflare/cloudflared/websocket"
|
2020-10-08 10:12:26 +00:00
|
|
|
)
|
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
const (
|
|
|
|
lbProbeUserAgentPrefix = "Mozilla/5.0 (compatible; Cloudflare-Traffic-Manager/1.0; +https://www.cloudflare.com/traffic-manager/;"
|
|
|
|
LogFieldConnIndex = "connIndex"
|
2021-09-21 10:02:59 +00:00
|
|
|
MaxGracePeriod = time.Minute * 3
|
2021-10-08 12:48:20 +00:00
|
|
|
MaxConcurrentStreams = math.MaxUint32
|
2021-07-16 15:14:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var switchingProtocolText = fmt.Sprintf("%d %s", http.StatusSwitchingProtocols, http.StatusText(http.StatusSwitchingProtocols))
|
2020-10-08 10:12:26 +00:00
|
|
|
|
2022-02-07 09:42:07 +00:00
|
|
|
type ConfigManager interface {
|
|
|
|
Update(version int32, config []byte) *pogs.UpdateConfigurationResponse
|
|
|
|
GetOriginProxy() OriginProxy
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 09:42:07 +00:00
|
|
|
type NamedTunnelProperties struct {
|
2021-07-09 17:52:41 +00:00
|
|
|
Credentials Credentials
|
|
|
|
Client pogs.ClientInfo
|
|
|
|
QuickTunnelUrl string
|
2020-11-23 21:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Credentials are stored in the credentials file and contain all info needed to run a tunnel.
|
|
|
|
type Credentials struct {
|
|
|
|
AccountTag string
|
|
|
|
TunnelSecret []byte
|
|
|
|
TunnelID uuid.UUID
|
|
|
|
TunnelName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Credentials) Auth() pogs.TunnelAuth {
|
|
|
|
return pogs.TunnelAuth{
|
|
|
|
AccountTag: c.AccountTag,
|
|
|
|
TunnelSecret: c.TunnelSecret,
|
|
|
|
}
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 09:42:07 +00:00
|
|
|
type ClassicTunnelProperties struct {
|
2020-10-08 10:12:26 +00:00
|
|
|
Hostname string
|
|
|
|
OriginCert []byte
|
|
|
|
// feature-flag to use new edge reconnect tokens
|
|
|
|
UseReconnectToken bool
|
|
|
|
}
|
|
|
|
|
2021-01-11 19:59:45 +00:00
|
|
|
// Type indicates the connection type of the connection.
|
|
|
|
type Type int
|
|
|
|
|
|
|
|
const (
|
|
|
|
TypeWebsocket Type = iota
|
|
|
|
TypeTCP
|
2021-01-15 17:25:56 +00:00
|
|
|
TypeControlStream
|
2021-01-11 19:59:45 +00:00
|
|
|
TypeHTTP
|
|
|
|
)
|
|
|
|
|
2021-01-15 17:25:56 +00:00
|
|
|
// ShouldFlush returns whether this kind of connection should actively flush data
|
|
|
|
func (t Type) shouldFlush() bool {
|
|
|
|
switch t {
|
|
|
|
case TypeWebsocket, TypeTCP, TypeControlStream:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Type) String() string {
|
|
|
|
switch t {
|
|
|
|
case TypeWebsocket:
|
|
|
|
return "websocket"
|
|
|
|
case TypeTCP:
|
|
|
|
return "tcp"
|
|
|
|
case TypeControlStream:
|
|
|
|
return "control stream"
|
|
|
|
case TypeHTTP:
|
|
|
|
return "http"
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("Unknown Type %d", t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
// OriginProxy is how data flows from cloudflared to the origin services running behind it.
|
2020-12-09 21:46:53 +00:00
|
|
|
type OriginProxy interface {
|
2021-07-16 15:14:37 +00:00
|
|
|
ProxyHTTP(w ResponseWriter, req *http.Request, isWebsocket bool) error
|
|
|
|
ProxyTCP(ctx context.Context, rwa ReadWriteAcker, req *TCPRequest) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// TCPRequest defines the input format needed to perform a TCP proxy.
|
|
|
|
type TCPRequest struct {
|
|
|
|
Dest string
|
|
|
|
CFRay string
|
|
|
|
LBProbe bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadWriteAcker is a readwriter with the ability to Acknowledge to the downstream (edge) that the origin has
|
|
|
|
// accepted the connection.
|
|
|
|
type ReadWriteAcker interface {
|
|
|
|
io.ReadWriter
|
|
|
|
AckConnection() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPResponseReadWriteAcker is an HTTP implementation of ReadWriteAcker.
|
|
|
|
type HTTPResponseReadWriteAcker struct {
|
|
|
|
r io.Reader
|
|
|
|
w ResponseWriter
|
|
|
|
req *http.Request
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHTTPResponseReadWriterAcker returns a new instance of HTTPResponseReadWriteAcker.
|
|
|
|
func NewHTTPResponseReadWriterAcker(w ResponseWriter, req *http.Request) *HTTPResponseReadWriteAcker {
|
|
|
|
return &HTTPResponseReadWriteAcker{
|
|
|
|
r: req.Body,
|
|
|
|
w: w,
|
|
|
|
req: req,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HTTPResponseReadWriteAcker) Read(p []byte) (int, error) {
|
|
|
|
return h.r.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HTTPResponseReadWriteAcker) Write(p []byte) (int, error) {
|
|
|
|
return h.w.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AckConnection acks an HTTP connection by sending a switch protocols status code that enables the caller to
|
|
|
|
// upgrade to streams.
|
|
|
|
func (h *HTTPResponseReadWriteAcker) AckConnection() error {
|
|
|
|
resp := &http.Response{
|
|
|
|
Status: switchingProtocolText,
|
|
|
|
StatusCode: http.StatusSwitchingProtocols,
|
|
|
|
ContentLength: -1,
|
|
|
|
}
|
|
|
|
|
|
|
|
if secWebsocketKey := h.req.Header.Get("Sec-WebSocket-Key"); secWebsocketKey != "" {
|
|
|
|
resp.Header = websocket.NewResponseHeader(h.req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.w.WriteRespHeaders(resp.StatusCode, resp.Header)
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ResponseWriter interface {
|
2020-12-09 21:46:53 +00:00
|
|
|
WriteRespHeaders(status int, header http.Header) error
|
2021-02-11 14:36:42 +00:00
|
|
|
io.Writer
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ConnectedFuse interface {
|
|
|
|
Connected()
|
|
|
|
IsConnected() bool
|
|
|
|
}
|
|
|
|
|
2020-11-18 11:53:59 +00:00
|
|
|
func IsServerSentEvent(headers http.Header) bool {
|
|
|
|
if contentType := headers.Get("content-type"); contentType != "" {
|
|
|
|
return strings.HasPrefix(strings.ToLower(contentType), "text/event-stream")
|
|
|
|
}
|
|
|
|
return false
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
2020-10-23 14:49:24 +00:00
|
|
|
|
2020-11-18 11:53:59 +00:00
|
|
|
func uint8ToString(input uint8) string {
|
|
|
|
return strconv.FormatUint(uint64(input), 10)
|
2020-10-23 14:49:24 +00:00
|
|
|
}
|
2021-07-16 15:14:37 +00:00
|
|
|
|
|
|
|
func FindCfRayHeader(req *http.Request) string {
|
|
|
|
return req.Header.Get("Cf-Ray")
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsLBProbeRequest(req *http.Request) bool {
|
|
|
|
return strings.HasPrefix(req.UserAgent(), lbProbeUserAgentPrefix)
|
|
|
|
}
|