2022-11-04 23:39:42 +00:00
|
|
|
// Package carrier provides a WebSocket proxy to carry or proxy a connection
|
|
|
|
// from the local client to the edge. See it as a wrapper around any protocol
|
|
|
|
// that it packages up in a WebSocket connection to the edge.
|
2018-09-21 15:18:23 +00:00
|
|
|
package carrier
|
|
|
|
|
|
|
|
import (
|
2021-02-03 19:00:55 +00:00
|
|
|
"crypto/tls"
|
2021-03-26 04:04:56 +00:00
|
|
|
"fmt"
|
2018-09-21 15:18:23 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2021-03-26 04:04:56 +00:00
|
|
|
"net/url"
|
2018-09-21 15:18:23 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2020-03-31 14:56:22 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
2021-03-23 14:30:43 +00:00
|
|
|
|
2024-05-09 19:07:59 +00:00
|
|
|
"github.com/cloudflare/cloudflared/config"
|
2021-03-23 14:30:43 +00:00
|
|
|
"github.com/cloudflare/cloudflared/token"
|
2018-09-21 15:18:23 +00:00
|
|
|
)
|
|
|
|
|
2021-03-26 04:04:56 +00:00
|
|
|
const (
|
|
|
|
LogFieldOriginURL = "originURL"
|
|
|
|
CFAccessTokenHeader = "Cf-Access-Token"
|
2024-05-09 19:07:59 +00:00
|
|
|
CFJumpDestinationHeader = "Cf-Access-Jump-Destination"
|
2021-03-26 04:04:56 +00:00
|
|
|
)
|
2020-12-28 18:10:01 +00:00
|
|
|
|
2019-01-23 21:42:10 +00:00
|
|
|
type StartOptions struct {
|
2021-03-10 21:52:35 +00:00
|
|
|
AppInfo *token.AppInfo
|
2021-02-03 19:00:55 +00:00
|
|
|
OriginURL string
|
|
|
|
Headers http.Header
|
|
|
|
Host string
|
|
|
|
TLSClientConfig *tls.Config
|
2019-01-23 21:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 14:56:22 +00:00
|
|
|
// Connection wraps up all the needed functions to forward over the tunnel
|
|
|
|
type Connection interface {
|
|
|
|
// ServeStream is used to forward data from the client to the edge
|
|
|
|
ServeStream(*StartOptions, io.ReadWriter) error
|
|
|
|
}
|
|
|
|
|
2018-09-21 15:18:23 +00:00
|
|
|
// StdinoutStream is empty struct for wrapping stdin/stdout
|
|
|
|
// into a single ReadWriter
|
2021-03-26 04:04:56 +00:00
|
|
|
type StdinoutStream struct{}
|
2018-09-21 15:18:23 +00:00
|
|
|
|
|
|
|
// Read will read from Stdin
|
|
|
|
func (c *StdinoutStream) Read(p []byte) (int, error) {
|
|
|
|
return os.Stdin.Read(p)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write will write to Stdout
|
|
|
|
func (c *StdinoutStream) Write(p []byte) (int, error) {
|
|
|
|
return os.Stdout.Write(p)
|
|
|
|
}
|
|
|
|
|
2021-11-12 15:38:06 +00:00
|
|
|
// Helper to allow deferring the response close with a check that the resp is not nil
|
2019-06-26 15:48:45 +00:00
|
|
|
func closeRespBody(resp *http.Response) {
|
|
|
|
if resp != nil {
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = resp.Body.Close()
|
2019-06-26 15:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 14:56:22 +00:00
|
|
|
// StartForwarder will setup a listener on a specified address/port and then
|
2019-04-05 06:57:00 +00:00
|
|
|
// forward connections to the origin by calling `Serve()`.
|
2020-03-31 14:56:22 +00:00
|
|
|
func StartForwarder(conn Connection, address string, shutdownC <-chan struct{}, options *StartOptions) error {
|
2018-09-21 15:18:23 +00:00
|
|
|
listener, err := net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
2020-03-31 14:56:22 +00:00
|
|
|
return errors.Wrap(err, "failed to start forwarding server")
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|
2020-03-31 14:56:22 +00:00
|
|
|
return Serve(conn, listener, shutdownC, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartClient will copy the data from stdin/stdout over a WebSocket connection
|
|
|
|
// to the edge (originURL)
|
|
|
|
func StartClient(conn Connection, stream io.ReadWriter, options *StartOptions) error {
|
2020-06-05 14:26:06 +00:00
|
|
|
return conn.ServeStream(options, stream)
|
2019-04-05 06:57:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serve accepts incoming connections on the specified net.Listener.
|
|
|
|
// Each connection is handled in a new goroutine: its data is copied over a
|
|
|
|
// WebSocket connection to the edge (originURL).
|
|
|
|
// `Serve` always closes `listener`.
|
2020-03-31 14:56:22 +00:00
|
|
|
func Serve(remoteConn Connection, listener net.Listener, shutdownC <-chan struct{}, options *StartOptions) error {
|
2018-09-21 15:18:23 +00:00
|
|
|
defer listener.Close()
|
2020-04-13 17:22:00 +00:00
|
|
|
errChan := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2018-09-21 15:18:23 +00:00
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
2020-05-05 22:56:39 +00:00
|
|
|
// don't block if parent goroutine quit early
|
|
|
|
select {
|
|
|
|
case errChan <- err:
|
|
|
|
default:
|
|
|
|
}
|
2020-04-13 17:22:00 +00:00
|
|
|
return
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|
2020-03-31 14:56:22 +00:00
|
|
|
go serveConnection(remoteConn, conn, options)
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|
2020-04-13 17:22:00 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-shutdownC:
|
|
|
|
return nil
|
|
|
|
case err := <-errChan:
|
|
|
|
return err
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 06:57:00 +00:00
|
|
|
// serveConnection handles connections for the Serve() call
|
2020-03-31 14:56:22 +00:00
|
|
|
func serveConnection(remoteConn Connection, c net.Conn, options *StartOptions) {
|
2018-09-21 15:18:23 +00:00
|
|
|
defer c.Close()
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = remoteConn.ServeStream(options, c)
|
2019-06-26 15:48:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 18:47:08 +00:00
|
|
|
// IsAccessResponse checks the http Response to see if the url location
|
2019-06-26 15:48:45 +00:00
|
|
|
// contains the Access structure.
|
2019-09-19 18:47:08 +00:00
|
|
|
func IsAccessResponse(resp *http.Response) bool {
|
|
|
|
if resp == nil || resp.StatusCode != http.StatusFound {
|
2019-06-26 15:48:45 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
location, err := resp.Location()
|
|
|
|
if err != nil || location == nil {
|
|
|
|
return false
|
|
|
|
}
|
2021-03-10 21:52:35 +00:00
|
|
|
if strings.HasPrefix(location.Path, token.AccessLoginWorkerPath) {
|
2019-06-26 15:48:45 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-03-31 14:56:22 +00:00
|
|
|
// BuildAccessRequest builds an HTTP request with the Access token set
|
2020-11-25 06:55:13 +00:00
|
|
|
func BuildAccessRequest(options *StartOptions, log *zerolog.Logger) (*http.Request, error) {
|
2019-06-26 15:48:45 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, options.OriginURL, nil)
|
2018-09-21 15:18:23 +00:00
|
|
|
if err != nil {
|
2019-05-22 20:41:21 +00:00
|
|
|
return nil, err
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 21:52:35 +00:00
|
|
|
token, err := token.FetchTokenWithRedirect(req.URL, options.AppInfo, log)
|
2018-09-21 15:18:23 +00:00
|
|
|
if err != nil {
|
2019-05-22 20:41:21 +00:00
|
|
|
return nil, err
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:08:56 +00:00
|
|
|
// We need to create a new request as FetchToken will modify req (boo mutable)
|
|
|
|
// as it has to follow redirect on the API and such, so here we init a new one
|
2019-06-26 15:48:45 +00:00
|
|
|
originRequest, err := http.NewRequest(http.MethodGet, options.OriginURL, nil)
|
2018-11-15 19:08:56 +00:00
|
|
|
if err != nil {
|
2019-05-22 20:41:21 +00:00
|
|
|
return nil, err
|
2018-11-15 19:08:56 +00:00
|
|
|
}
|
2021-03-26 04:04:56 +00:00
|
|
|
originRequest.Header.Set(CFAccessTokenHeader, token)
|
2018-11-15 19:08:56 +00:00
|
|
|
|
2019-06-26 15:48:45 +00:00
|
|
|
for k, v := range options.Headers {
|
|
|
|
if len(v) >= 1 {
|
|
|
|
originRequest.Header.Set(k, v[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 20:41:21 +00:00
|
|
|
return originRequest, nil
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|
2021-03-26 04:04:56 +00:00
|
|
|
|
|
|
|
func SetBastionDest(header http.Header, destination string) {
|
|
|
|
if destination != "" {
|
2024-05-09 19:07:59 +00:00
|
|
|
header.Set(CFJumpDestinationHeader, destination)
|
2021-03-26 04:04:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-09 19:07:59 +00:00
|
|
|
func ResolveBastionDest(req *http.Request, bastionMode bool, service string) (string, error) {
|
|
|
|
jumpDestination := req.Header.Get(CFJumpDestinationHeader)
|
|
|
|
if bastionMode && service != config.BastionFlag {
|
|
|
|
jumpDestination = service
|
|
|
|
}
|
|
|
|
|
2021-03-26 04:04:56 +00:00
|
|
|
if jumpDestination == "" {
|
|
|
|
return "", fmt.Errorf("Did not receive final destination from client. The --destination flag is likely not set on the client side")
|
|
|
|
}
|
|
|
|
// Strip scheme and path set by client. Without a scheme
|
|
|
|
// Parsing a hostname and path without scheme might not return an error due to parsing ambiguities
|
|
|
|
if jumpURL, err := url.Parse(jumpDestination); err == nil && jumpURL.Host != "" {
|
|
|
|
return removePath(jumpURL.Host), nil
|
|
|
|
}
|
|
|
|
return removePath(jumpDestination), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removePath(dest string) string {
|
|
|
|
return strings.SplitN(dest, "/", 2)[0]
|
|
|
|
}
|