cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2018.12.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

carrier/carrier.go

139lines · modecode

1//Package carrier provides a WebSocket proxy to carry or proxy a connection
2//from the local client to the edge. See it as a wrapper around any protocol
3//that it packages up in a WebSocket connection to the edge.
4package carrier
5
6import (
7 "errors"
8 "io"
9 "net"
10 "net/http"
11 "os"
12 "strings"
13
14 "github.com/cloudflare/cloudflared/cmd/cloudflared/token"
15 "github.com/cloudflare/cloudflared/websocket"
16 "github.com/sirupsen/logrus"
17)
18
19// StdinoutStream is empty struct for wrapping stdin/stdout
20// into a single ReadWriter
21type StdinoutStream struct {
22}
23
24// Read will read from Stdin
25func (c *StdinoutStream) Read(p []byte) (int, error) {
26 return os.Stdin.Read(p)
27
28}
29
30// Write will write to Stdout
31func (c *StdinoutStream) Write(p []byte) (int, error) {
32 return os.Stdout.Write(p)
33}
34
35// StartClient will copy the data from stdin/stdout over a WebSocket connection
36// to the edge (originURL)
37func StartClient(logger *logrus.Logger, originURL string, stream io.ReadWriter) error {
38 return serveStream(logger, originURL, stream)
39}
40
41// StartServer will setup a server on a specified port and copy data over a WebSocket connection
42// to the edge (originURL)
43func StartServer(logger *logrus.Logger, address, originURL string, shutdownC <-chan struct{}) error {
44 listener, err := net.Listen("tcp", address)
45 if err != nil {
46 logger.WithError(err).Error("failed to start forwarding server")
47 return err
48 }
49 defer listener.Close()
50 for {
51 select {
52 case <-shutdownC:
53 return nil
54 default:
55 conn, err := listener.Accept()
56 if err != nil {
57 return err
58 }
59 go serveConnection(logger, conn, originURL)
60 }
61 }
62}
63
64// serveConnection handles connections for the StartServer call
65func serveConnection(logger *logrus.Logger, c net.Conn, originURL string) {
66 defer c.Close()
67 serveStream(logger, originURL, c)
68}
69
70// serveStream will serve the data over the WebSocket stream
71func serveStream(logger *logrus.Logger, originURL string, conn io.ReadWriter) error {
72 wsConn, err := createWebsocketStream(originURL)
73 if err != nil {
74 logger.WithError(err).Error("failed to create websocket stream")
75 return err
76 }
77 defer wsConn.Close()
78
79 websocket.Stream(wsConn, conn)
80
81 return nil
82}
83
84// createWebsocketStream will create a WebSocket connection to stream data over
85// It also handles redirects from Access and will present that flow if
86// the token is not present on the request
87func createWebsocketStream(originURL string) (*websocket.Conn, error) {
88 req, err := http.NewRequest(http.MethodGet, originURL, nil)
89 if err != nil {
90 return nil, err
91 }
92
93 wsConn, resp, err := websocket.ClientConnect(req, nil)
94 if err != nil && resp != nil && resp.StatusCode > 300 {
95 location, err := resp.Location()
96 if err != nil {
97 return nil, err
98 }
99 if !strings.Contains(location.String(), "cdn-cgi/access/login") {
100 return nil, errors.New("not an Access redirect")
101 }
102 req, err := buildAccessRequest(originURL)
103 if err != nil {
104 return nil, err
105 }
106
107 wsConn, _, err = websocket.ClientConnect(req, nil)
108 if err != nil {
109 return nil, err
110 }
111 } else if err != nil {
112 return nil, err
113 }
114
115 return &websocket.Conn{Conn: wsConn}, nil
116}
117
118// buildAccessRequest builds an HTTP request with the Access token set
119func buildAccessRequest(originURL string) (*http.Request, error) {
120 req, err := http.NewRequest(http.MethodGet, originURL, nil)
121 if err != nil {
122 return nil, err
123 }
124
125 token, err := token.FetchToken(req.URL)
126 if err != nil {
127 return nil, err
128 }
129
130 // We need to create a new request as FetchToken will modify req (boo mutable)
131 // as it has to follow redirect on the API and such, so here we init a new one
132 originRequest, err := http.NewRequest(http.MethodGet, originURL, nil)
133 if err != nil {
134 return nil, err
135 }
136 originRequest.Header.Set("cf-access-token", token)
137
138 return originRequest, nil
139}