2018-05-01 23:45:06 +00:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
2021-03-09 15:57:04 +00:00
|
|
|
"context"
|
2018-09-21 15:18:23 +00:00
|
|
|
"crypto/tls"
|
2018-11-15 15:43:50 +00:00
|
|
|
"crypto/x509"
|
2021-03-09 15:57:04 +00:00
|
|
|
"fmt"
|
2018-09-21 15:18:23 +00:00
|
|
|
"io"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
2021-03-09 15:57:04 +00:00
|
|
|
"time"
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
gws "github.com/gorilla/websocket"
|
2021-03-23 14:30:43 +00:00
|
|
|
"github.com/rs/zerolog"
|
2018-09-21 15:18:23 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-09 15:57:04 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-09-21 15:18:23 +00:00
|
|
|
"golang.org/x/net/websocket"
|
2021-03-23 14:30:43 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/hello"
|
|
|
|
"github.com/cloudflare/cloudflared/tlsconfig"
|
2018-05-01 23:45:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-09-21 15:18:23 +00:00
|
|
|
// example in Sec-Websocket-Key in rfc6455
|
|
|
|
testSecWebsocketKey = "dGhlIHNhbXBsZSBub25jZQ=="
|
|
|
|
// example Sec-Websocket-Accept in rfc6455
|
|
|
|
testSecWebsocketAccept = "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="
|
2018-05-01 23:45:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func testRequest(t *testing.T, url string, stream io.ReadWriter) *http.Request {
|
2018-09-21 15:18:23 +00:00
|
|
|
req, err := http.NewRequest("GET", url, stream)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("testRequestHeader error")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Connection", "Upgrade")
|
|
|
|
req.Header.Add("Upgrade", "WebSocket")
|
|
|
|
req.Header.Add("Sec-Websocket-Key", testSecWebsocketKey)
|
|
|
|
req.Header.Add("Sec-Websocket-Protocol", "tunnel-protocol")
|
|
|
|
req.Header.Add("Sec-Websocket-Version", "13")
|
|
|
|
req.Header.Add("User-Agent", "curl/7.59.0")
|
|
|
|
|
|
|
|
return req
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func websocketClientTLSConfig(t *testing.T) *tls.Config {
|
2018-11-15 15:43:50 +00:00
|
|
|
certPool := x509.NewCertPool()
|
|
|
|
helloCert, err := tlsconfig.GetHelloCertificateX509()
|
2018-09-21 15:18:23 +00:00
|
|
|
assert.NoError(t, err)
|
2018-11-15 15:43:50 +00:00
|
|
|
certPool.AddCert(helloCert)
|
2018-09-21 15:18:23 +00:00
|
|
|
assert.NotNil(t, certPool)
|
|
|
|
return &tls.Config{RootCAs: certPool}
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebsocketHeaders(t *testing.T) {
|
2018-09-21 15:18:23 +00:00
|
|
|
req := testRequest(t, "http://example.com", nil)
|
|
|
|
wsHeaders := websocketHeaders(req)
|
|
|
|
for _, header := range stripWebsocketHeaders {
|
|
|
|
assert.Empty(t, wsHeaders[header])
|
|
|
|
}
|
|
|
|
assert.Equal(t, "curl/7.59.0", wsHeaders.Get("User-Agent"))
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateAcceptKey(t *testing.T) {
|
2018-09-21 15:18:23 +00:00
|
|
|
req := testRequest(t, "http://example.com", nil)
|
|
|
|
assert.Equal(t, testSecWebsocketAccept, generateAcceptKey(req))
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServe(t *testing.T) {
|
2020-11-25 06:55:13 +00:00
|
|
|
log := zerolog.Nop()
|
2018-09-21 15:18:23 +00:00
|
|
|
shutdownC := make(chan struct{})
|
|
|
|
errC := make(chan error)
|
|
|
|
listener, err := hello.CreateTLSListener("localhost:1111")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer listener.Close()
|
|
|
|
|
|
|
|
go func() {
|
2020-11-25 06:55:13 +00:00
|
|
|
errC <- hello.StartHelloWorldServer(&log, listener, shutdownC)
|
2018-09-21 15:18:23 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
req := testRequest(t, "https://localhost:1111/ws", nil)
|
|
|
|
|
|
|
|
tlsConfig := websocketClientTLSConfig(t)
|
|
|
|
assert.NotNil(t, tlsConfig)
|
2020-12-09 21:46:53 +00:00
|
|
|
d := gws.Dialer{TLSClientConfig: tlsConfig}
|
2020-10-30 21:37:40 +00:00
|
|
|
conn, resp, err := ClientConnect(req, &d)
|
2018-09-21 15:18:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, testSecWebsocketAccept, resp.Header.Get("Sec-WebSocket-Accept"))
|
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
messageSize := rand.Int()%2048 + 1
|
|
|
|
clientMessage := make([]byte, messageSize)
|
|
|
|
// rand.Read always returns len(clientMessage) and a nil error
|
|
|
|
rand.Read(clientMessage)
|
|
|
|
err = conn.WriteMessage(websocket.BinaryFrame, clientMessage)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
messageType, message, err := conn.ReadMessage()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, websocket.BinaryFrame, messageType)
|
|
|
|
assert.Equal(t, clientMessage, message)
|
|
|
|
}
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = conn.Close()
|
2018-09-21 15:18:23 +00:00
|
|
|
close(shutdownC)
|
|
|
|
<-errC
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
2018-09-21 15:18:23 +00:00
|
|
|
|
2021-03-09 15:57:04 +00:00
|
|
|
func TestWebsocketWrapper(t *testing.T) {
|
|
|
|
|
|
|
|
listener, err := hello.CreateTLSListener("localhost:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
serverErrorChan := make(chan error)
|
|
|
|
helloSvrCtx, cancelHelloSvr := context.WithCancel(context.Background())
|
|
|
|
defer func() { <-serverErrorChan }()
|
|
|
|
defer cancelHelloSvr()
|
|
|
|
go func() {
|
|
|
|
log := zerolog.Nop()
|
|
|
|
serverErrorChan <- hello.StartHelloWorldServer(&log, listener, helloSvrCtx.Done())
|
|
|
|
}()
|
|
|
|
|
|
|
|
tlsConfig := websocketClientTLSConfig(t)
|
|
|
|
d := gws.Dialer{TLSClientConfig: tlsConfig, HandshakeTimeout: time.Minute}
|
|
|
|
testAddr := fmt.Sprintf("https://%s/ws", listener.Addr().String())
|
|
|
|
req := testRequest(t, testAddr, nil)
|
|
|
|
conn, resp, err := ClientConnect(req, &d)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, testSecWebsocketAccept, resp.Header.Get("Sec-WebSocket-Accept"))
|
|
|
|
|
|
|
|
// Websocket now connected to test server so lets check our wrapper
|
|
|
|
wrapper := GorillaConn{Conn: conn}
|
|
|
|
buf := make([]byte, 100)
|
|
|
|
wrapper.Write([]byte("abc"))
|
|
|
|
n, err := wrapper.Read(buf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, n, 3)
|
|
|
|
require.Equal(t, "abc", string(buf[:n]))
|
|
|
|
|
|
|
|
// Test partial read, read 1 of 3 bytes in one read and the other 2 in another read
|
|
|
|
wrapper.Write([]byte("abc"))
|
|
|
|
buf = buf[:1]
|
|
|
|
n, err = wrapper.Read(buf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, n, 1)
|
|
|
|
require.Equal(t, "a", string(buf[:n]))
|
|
|
|
buf = buf[:cap(buf)]
|
|
|
|
n, err = wrapper.Read(buf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, n, 2)
|
|
|
|
require.Equal(t, "bc", string(buf[:n]))
|
|
|
|
}
|