2018-09-21 15:18:23 +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.
|
|
|
|
package carrier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2018-10-19 20:44:35 +00:00
|
|
|
"github.com/cloudflare/cloudflared/cmd/cloudflared/token"
|
2018-09-21 15:18:23 +00:00
|
|
|
"github.com/cloudflare/cloudflared/websocket"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StdinoutStream is empty struct for wrapping stdin/stdout
|
|
|
|
// into a single ReadWriter
|
|
|
|
type StdinoutStream struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartClient will copy the data from stdin/stdout over a WebSocket connection
|
|
|
|
// to the edge (originURL)
|
2019-02-07 16:56:33 +00:00
|
|
|
func StartClient(logger *logrus.Logger, originURL string, stream io.ReadWriter, headers http.Header) error {
|
|
|
|
return serveStream(logger, originURL, stream, headers)
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartServer will setup a server on a specified port and copy data over a WebSocket connection
|
|
|
|
// to the edge (originURL)
|
2019-02-07 16:56:33 +00:00
|
|
|
func StartServer(logger *logrus.Logger, address, originURL string, shutdownC <-chan struct{}, headers http.Header) error {
|
2018-09-21 15:18:23 +00:00
|
|
|
listener, err := net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Error("failed to start forwarding server")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer listener.Close()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-shutdownC:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-07 16:56:33 +00:00
|
|
|
go serveConnection(logger, conn, originURL, headers)
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// serveConnection handles connections for the StartServer call
|
2019-02-07 16:56:33 +00:00
|
|
|
func serveConnection(logger *logrus.Logger, c net.Conn, originURL string, headers http.Header) {
|
2018-09-21 15:18:23 +00:00
|
|
|
defer c.Close()
|
2019-02-07 16:56:33 +00:00
|
|
|
serveStream(logger, originURL, c, headers)
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// serveStream will serve the data over the WebSocket stream
|
2019-02-07 16:56:33 +00:00
|
|
|
func serveStream(logger *logrus.Logger, originURL string, conn io.ReadWriter, headers http.Header) error {
|
|
|
|
wsConn, err := createWebsocketStream(originURL, headers)
|
2018-09-21 15:18:23 +00:00
|
|
|
if err != nil {
|
2019-01-24 21:24:50 +00:00
|
|
|
logger.WithError(err).Errorf("failed to connect to %s\n", originURL)
|
2018-09-21 15:18:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer wsConn.Close()
|
|
|
|
|
|
|
|
websocket.Stream(wsConn, conn)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createWebsocketStream will create a WebSocket connection to stream data over
|
|
|
|
// It also handles redirects from Access and will present that flow if
|
|
|
|
// the token is not present on the request
|
2019-02-07 16:56:33 +00:00
|
|
|
func createWebsocketStream(originURL string, headers http.Header) (*websocket.Conn, error) {
|
2018-09-21 15:18:23 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, originURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-07 16:56:33 +00:00
|
|
|
req.Header = headers
|
2018-11-15 19:08:56 +00:00
|
|
|
|
2018-09-21 15:18:23 +00:00
|
|
|
wsConn, resp, err := websocket.ClientConnect(req, nil)
|
|
|
|
if err != nil && resp != nil && resp.StatusCode > 300 {
|
|
|
|
location, err := resp.Location()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !strings.Contains(location.String(), "cdn-cgi/access/login") {
|
|
|
|
return nil, errors.New("not an Access redirect")
|
|
|
|
}
|
|
|
|
req, err := buildAccessRequest(originURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
wsConn, _, err = websocket.ClientConnect(req, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &websocket.Conn{Conn: wsConn}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildAccessRequest builds an HTTP request with the Access token set
|
|
|
|
func buildAccessRequest(originURL string) (*http.Request, error) {
|
|
|
|
req, err := http.NewRequest(http.MethodGet, originURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:44:35 +00:00
|
|
|
token, err := token.FetchToken(req.URL)
|
2018-09-21 15:18:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
originRequest, err := http.NewRequest(http.MethodGet, originURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
originRequest.Header.Set("cf-access-token", token)
|
|
|
|
|
|
|
|
return originRequest, nil
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|