2018-05-01 23:45:06 +00:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/base64"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2020-11-05 13:52:46 +00:00
|
|
|
"net/url"
|
2018-09-21 15:18:23 +00:00
|
|
|
"time"
|
2018-05-01 23:45:06 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
2021-03-23 14:30:43 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/h2mux"
|
2018-05-01 23:45:06 +00:00
|
|
|
)
|
|
|
|
|
2018-09-21 15:18:23 +00:00
|
|
|
var stripWebsocketHeaders = []string{
|
2018-05-01 23:45:06 +00:00
|
|
|
"Upgrade",
|
|
|
|
"Connection",
|
|
|
|
"Sec-Websocket-Key",
|
|
|
|
"Sec-Websocket-Version",
|
|
|
|
"Sec-Websocket-Extensions",
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsWebSocketUpgrade checks to see if the request is a WebSocket connection.
|
|
|
|
func IsWebSocketUpgrade(req *http.Request) bool {
|
|
|
|
return websocket.IsWebSocketUpgrade(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClientConnect creates a WebSocket client connection for provided request. Caller is responsible for closing
|
|
|
|
// the connection. The response body may not contain the entire response and does
|
|
|
|
// not need to be closed by the application.
|
2020-12-09 21:46:53 +00:00
|
|
|
func ClientConnect(req *http.Request, dialler *websocket.Dialer) (*websocket.Conn, *http.Response, error) {
|
2020-11-05 13:52:46 +00:00
|
|
|
req.URL.Scheme = ChangeRequestScheme(req.URL)
|
2018-05-01 23:45:06 +00:00
|
|
|
wsHeaders := websocketHeaders(req)
|
2020-10-30 21:37:40 +00:00
|
|
|
if dialler == nil {
|
2020-12-09 21:46:53 +00:00
|
|
|
dialler = &websocket.Dialer{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
}
|
2020-10-30 21:37:40 +00:00
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
conn, response, err := dialler.Dial(req.URL.String(), wsHeaders)
|
2018-05-01 23:45:06 +00:00
|
|
|
if err != nil {
|
2018-09-21 15:18:23 +00:00
|
|
|
return nil, response, err
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
response.Header.Set("Sec-WebSocket-Accept", generateAcceptKey(req))
|
2020-12-09 21:46:53 +00:00
|
|
|
return conn, response, nil
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 15:18:23 +00:00
|
|
|
// StartProxyServer will start a websocket server that will decode
|
|
|
|
// the websocket data and write the resulting data to the provided
|
2020-11-25 06:55:13 +00:00
|
|
|
func StartProxyServer(
|
|
|
|
log *zerolog.Logger,
|
|
|
|
listener net.Listener,
|
|
|
|
staticHost string,
|
|
|
|
shutdownC <-chan struct{},
|
2021-02-04 15:07:18 +00:00
|
|
|
streamHandler func(originConn io.ReadWriter, remoteConn net.Conn, log *zerolog.Logger),
|
2020-11-25 06:55:13 +00:00
|
|
|
) error {
|
2018-09-21 15:18:23 +00:00
|
|
|
upgrader := websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
}
|
2020-11-16 19:06:36 +00:00
|
|
|
h := handler{
|
|
|
|
upgrader: upgrader,
|
2020-11-25 06:55:13 +00:00
|
|
|
log: log,
|
2020-11-16 19:06:36 +00:00
|
|
|
staticHost: staticHost,
|
|
|
|
streamHandler: streamHandler,
|
|
|
|
}
|
2018-09-21 15:18:23 +00:00
|
|
|
|
2020-11-16 19:06:36 +00:00
|
|
|
httpServer := &http.Server{Addr: listener.Addr().String(), Handler: &h}
|
2018-09-21 15:18:23 +00:00
|
|
|
go func() {
|
|
|
|
<-shutdownC
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = httpServer.Close()
|
2018-09-21 15:18:23 +00:00
|
|
|
}()
|
|
|
|
|
2020-11-16 19:06:36 +00:00
|
|
|
return httpServer.Serve(listener)
|
|
|
|
}
|
2020-05-04 20:15:17 +00:00
|
|
|
|
2020-11-16 19:06:36 +00:00
|
|
|
// HTTP handler for the websocket proxy.
|
|
|
|
type handler struct {
|
2020-11-25 06:55:13 +00:00
|
|
|
log *zerolog.Logger
|
2020-11-16 19:06:36 +00:00
|
|
|
staticHost string
|
|
|
|
upgrader websocket.Upgrader
|
2021-02-04 15:07:18 +00:00
|
|
|
streamHandler func(originConn io.ReadWriter, remoteConn net.Conn, log *zerolog.Logger)
|
2020-11-16 19:06:36 +00:00
|
|
|
}
|
2018-09-21 15:18:23 +00:00
|
|
|
|
2020-11-16 19:06:36 +00:00
|
|
|
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// If remote is an empty string, get the destination from the client.
|
|
|
|
finalDestination := h.staticHost
|
|
|
|
if finalDestination == "" {
|
|
|
|
if jumpDestination := r.Header.Get(h2mux.CFJumpDestinationHeader); jumpDestination == "" {
|
2020-11-25 06:55:13 +00:00
|
|
|
h.log.Error().Msg("Did not receive final destination from client. The --destination flag is likely not set")
|
2018-10-19 20:44:35 +00:00
|
|
|
return
|
2020-11-16 19:06:36 +00:00
|
|
|
} else {
|
|
|
|
finalDestination = jumpDestination
|
2018-10-19 20:44:35 +00:00
|
|
|
}
|
2020-11-16 19:06:36 +00:00
|
|
|
}
|
2018-09-21 15:18:23 +00:00
|
|
|
|
2020-11-16 19:06:36 +00:00
|
|
|
stream, err := net.Dial("tcp", finalDestination)
|
|
|
|
if err != nil {
|
2020-12-28 18:10:01 +00:00
|
|
|
h.log.Err(err).Msg("Cannot connect to remote")
|
2020-11-16 19:06:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer stream.Close()
|
|
|
|
|
|
|
|
if !websocket.IsWebSocketUpgrade(r) {
|
2020-11-25 06:55:13 +00:00
|
|
|
_, _ = w.Write(nonWebSocketRequestPage())
|
2020-11-16 19:06:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
conn, err := h.upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
2020-12-28 18:10:01 +00:00
|
|
|
h.log.Err(err).Msg("failed to upgrade")
|
2020-11-16 19:06:36 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = conn.SetReadDeadline(time.Now().Add(pongWait))
|
|
|
|
conn.SetPongHandler(func(string) error { _ = conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
|
2021-03-09 15:57:04 +00:00
|
|
|
gorillaConn := &GorillaConn{Conn: conn, log: h.log}
|
2020-12-09 21:46:53 +00:00
|
|
|
go gorillaConn.pinger(r.Context())
|
|
|
|
defer conn.Close()
|
|
|
|
|
2021-02-04 15:07:18 +00:00
|
|
|
h.streamHandler(gorillaConn, stream, h.log)
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
2020-11-16 19:06:36 +00:00
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
// NewResponseHeader returns headers needed to return to origin for completing handshake
|
|
|
|
func NewResponseHeader(req *http.Request) http.Header {
|
|
|
|
header := http.Header{}
|
|
|
|
header.Add("Connection", "Upgrade")
|
|
|
|
header.Add("Sec-Websocket-Accept", generateAcceptKey(req))
|
|
|
|
header.Add("Upgrade", "websocket")
|
|
|
|
return header
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
// the gorilla websocket library sets its own Upgrade, Connection, Sec-WebSocket-Key,
|
|
|
|
// Sec-WebSocket-Version and Sec-Websocket-Extensions headers.
|
|
|
|
// https://github.com/gorilla/websocket/blob/master/client.go#L189-L194.
|
|
|
|
func websocketHeaders(req *http.Request) http.Header {
|
|
|
|
wsHeaders := make(http.Header)
|
|
|
|
for key, val := range req.Header {
|
2018-09-21 15:18:23 +00:00
|
|
|
wsHeaders[key] = val
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
// Assume the header keys are in canonical format.
|
2018-09-21 15:18:23 +00:00
|
|
|
for _, header := range stripWebsocketHeaders {
|
2018-05-01 23:45:06 +00:00
|
|
|
wsHeaders.Del(header)
|
|
|
|
}
|
2018-10-19 21:51:54 +00:00
|
|
|
wsHeaders.Set("Host", req.Host) // See TUN-1097
|
2018-05-01 23:45:06 +00:00
|
|
|
return wsHeaders
|
|
|
|
}
|
|
|
|
|
|
|
|
// sha1Base64 sha1 and then base64 encodes str.
|
|
|
|
func sha1Base64(str string) string {
|
|
|
|
hasher := sha1.New()
|
2020-11-25 06:55:13 +00:00
|
|
|
_, _ = io.WriteString(hasher, str)
|
2018-05-01 23:45:06 +00:00
|
|
|
hash := hasher.Sum(nil)
|
|
|
|
return base64.StdEncoding.EncodeToString(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateAcceptKey returns the string needed for the Sec-WebSocket-Accept header.
|
|
|
|
// https://tools.ietf.org/html/rfc6455#section-1.3 describes this process in more detail.
|
|
|
|
func generateAcceptKey(req *http.Request) string {
|
|
|
|
return sha1Base64(req.Header.Get("Sec-WebSocket-Key") + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
|
|
|
|
}
|
|
|
|
|
2020-11-05 13:52:46 +00:00
|
|
|
// ChangeRequestScheme is needed as the gorilla websocket library requires the ws scheme.
|
2018-05-01 23:45:06 +00:00
|
|
|
// (even though it changes it back to http/https, but ¯\_(ツ)_/¯.)
|
2020-11-05 13:52:46 +00:00
|
|
|
func ChangeRequestScheme(reqURL *url.URL) string {
|
|
|
|
switch reqURL.Scheme {
|
2018-05-01 23:45:06 +00:00
|
|
|
case "https":
|
|
|
|
return "wss"
|
|
|
|
case "http":
|
|
|
|
return "ws"
|
2020-11-05 13:52:46 +00:00
|
|
|
case "":
|
|
|
|
return "ws"
|
2018-05-01 23:45:06 +00:00
|
|
|
default:
|
2020-11-05 13:52:46 +00:00
|
|
|
return reqURL.Scheme
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
}
|