2020-10-20 15:26:55 +00:00
|
|
|
package origin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-11-02 11:21:34 +00:00
|
|
|
"flag"
|
2020-10-20 15:26:55 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-12-09 21:46:53 +00:00
|
|
|
"net"
|
2020-10-20 15:26:55 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
2020-10-23 14:49:24 +00:00
|
|
|
"time"
|
2020-10-20 15:26:55 +00:00
|
|
|
|
2021-01-17 20:22:53 +00:00
|
|
|
"github.com/cloudflare/cloudflared/logger"
|
|
|
|
|
2020-11-02 11:21:34 +00:00
|
|
|
"github.com/cloudflare/cloudflared/cmd/cloudflared/config"
|
2020-10-20 15:26:55 +00:00
|
|
|
"github.com/cloudflare/cloudflared/connection"
|
|
|
|
"github.com/cloudflare/cloudflared/hello"
|
2020-11-02 11:21:34 +00:00
|
|
|
"github.com/cloudflare/cloudflared/ingress"
|
|
|
|
tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
2021-02-02 18:27:50 +00:00
|
|
|
gorillaWS "github.com/gorilla/websocket"
|
2020-11-02 11:21:34 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-10-20 15:26:55 +00:00
|
|
|
|
|
|
|
"github.com/gobwas/ws/wsutil"
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
2020-10-20 15:26:55 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-11-02 11:21:34 +00:00
|
|
|
var (
|
2021-01-17 20:22:53 +00:00
|
|
|
testTags = []tunnelpogs.Tag(nil)
|
|
|
|
unusedWarpRoutingService = (*ingress.WarpRoutingService)(nil)
|
2020-11-02 11:21:34 +00:00
|
|
|
)
|
|
|
|
|
2020-10-20 15:26:55 +00:00
|
|
|
type mockHTTPRespWriter struct {
|
|
|
|
*httptest.ResponseRecorder
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockHTTPRespWriter() *mockHTTPRespWriter {
|
|
|
|
return &mockHTTPRespWriter{
|
|
|
|
httptest.NewRecorder(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
func (w *mockHTTPRespWriter) WriteRespHeaders(status int, header http.Header) error {
|
|
|
|
w.WriteHeader(status)
|
|
|
|
for header, val := range header {
|
2020-10-20 15:26:55 +00:00
|
|
|
w.Header()[header] = val
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-27 22:27:15 +00:00
|
|
|
func (w *mockHTTPRespWriter) WriteErrorResponse() {
|
2020-10-20 15:26:55 +00:00
|
|
|
w.WriteHeader(http.StatusBadGateway)
|
2020-11-25 06:55:13 +00:00
|
|
|
_, _ = w.Write([]byte("http response error"))
|
2020-10-20 15:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *mockHTTPRespWriter) Read(data []byte) (int, error) {
|
|
|
|
return 0, fmt.Errorf("mockHTTPRespWriter doesn't implement io.Reader")
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockWSRespWriter struct {
|
|
|
|
*mockHTTPRespWriter
|
|
|
|
writeNotification chan []byte
|
|
|
|
reader io.Reader
|
|
|
|
}
|
|
|
|
|
2020-10-23 14:49:24 +00:00
|
|
|
func newMockWSRespWriter(reader io.Reader) *mockWSRespWriter {
|
2020-10-20 15:26:55 +00:00
|
|
|
return &mockWSRespWriter{
|
2020-10-23 14:49:24 +00:00
|
|
|
newMockHTTPRespWriter(),
|
2020-10-20 15:26:55 +00:00
|
|
|
make(chan []byte),
|
|
|
|
reader,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *mockWSRespWriter) Write(data []byte) (int, error) {
|
|
|
|
w.writeNotification <- data
|
|
|
|
return len(data), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *mockWSRespWriter) respBody() io.ReadWriter {
|
|
|
|
data := <-w.writeNotification
|
|
|
|
return bytes.NewBuffer(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *mockWSRespWriter) Read(data []byte) (int, error) {
|
|
|
|
return w.reader.Read(data)
|
|
|
|
}
|
|
|
|
|
2020-10-23 14:49:24 +00:00
|
|
|
type mockSSERespWriter struct {
|
|
|
|
*mockHTTPRespWriter
|
|
|
|
writeNotification chan []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockSSERespWriter() *mockSSERespWriter {
|
|
|
|
return &mockSSERespWriter{
|
|
|
|
newMockHTTPRespWriter(),
|
|
|
|
make(chan []byte),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *mockSSERespWriter) Write(data []byte) (int, error) {
|
|
|
|
w.writeNotification <- data
|
|
|
|
return len(data), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *mockSSERespWriter) ReadBytes() []byte {
|
|
|
|
return <-w.writeNotification
|
|
|
|
}
|
|
|
|
|
2020-11-02 11:21:34 +00:00
|
|
|
func TestProxySingleOrigin(t *testing.T) {
|
2020-11-25 06:55:13 +00:00
|
|
|
log := zerolog.Nop()
|
2020-10-20 15:26:55 +00:00
|
|
|
|
2020-11-02 11:21:34 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
flagSet := flag.NewFlagSet(t.Name(), flag.PanicOnError)
|
|
|
|
flagSet.Bool("hello-world", true, "")
|
|
|
|
|
|
|
|
cliCtx := cli.NewContext(cli.NewApp(), flagSet, nil)
|
2020-11-25 06:55:13 +00:00
|
|
|
err := cliCtx.Set("hello-world", "true")
|
2020-10-20 15:26:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-11-02 11:21:34 +00:00
|
|
|
allowURLFromArgs := false
|
2020-11-25 06:55:13 +00:00
|
|
|
ingressRule, err := ingress.NewSingleOrigin(cliCtx, allowURLFromArgs)
|
2020-11-02 11:21:34 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-20 15:26:55 +00:00
|
|
|
|
2020-11-02 11:21:34 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
errC := make(chan error)
|
2020-12-30 19:48:19 +00:00
|
|
|
require.NoError(t, ingressRule.StartOrigins(&wg, &log, ctx.Done(), errC))
|
2020-10-20 15:26:55 +00:00
|
|
|
|
2021-01-17 20:22:53 +00:00
|
|
|
proxy := NewOriginProxy(ingressRule, unusedWarpRoutingService, testTags, &log)
|
2020-12-09 21:46:53 +00:00
|
|
|
t.Run("testProxyHTTP", testProxyHTTP(t, proxy))
|
|
|
|
t.Run("testProxyWebsocket", testProxyWebsocket(t, proxy))
|
|
|
|
t.Run("testProxySSE", testProxySSE(t, proxy))
|
2020-10-20 15:26:55 +00:00
|
|
|
cancel()
|
2020-11-02 11:21:34 +00:00
|
|
|
wg.Wait()
|
2020-10-20 15:26:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
func testProxyHTTP(t *testing.T, proxy connection.OriginProxy) func(t *testing.T) {
|
2020-10-20 15:26:55 +00:00
|
|
|
return func(t *testing.T) {
|
|
|
|
respWriter := newMockHTTPRespWriter()
|
2020-11-02 11:21:34 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://localhost:8080", nil)
|
2020-10-20 15:26:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-11 19:59:45 +00:00
|
|
|
err = proxy.Proxy(respWriter, req, connection.TypeHTTP)
|
2020-10-20 15:26:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, respWriter.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
func testProxyWebsocket(t *testing.T, proxy connection.OriginProxy) func(t *testing.T) {
|
2020-10-20 15:26:55 +00:00
|
|
|
return func(t *testing.T) {
|
|
|
|
// WSRoute is a websocket echo handler
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2020-11-02 11:21:34 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://localhost:8080%s", hello.WSRoute), nil)
|
2020-10-20 15:26:55 +00:00
|
|
|
|
|
|
|
readPipe, writePipe := io.Pipe()
|
2020-10-23 14:49:24 +00:00
|
|
|
respWriter := newMockWSRespWriter(readPipe)
|
2020-10-20 15:26:55 +00:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2021-01-11 19:59:45 +00:00
|
|
|
err = proxy.Proxy(respWriter, req, connection.TypeWebsocket)
|
2020-10-20 15:26:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusSwitchingProtocols, respWriter.Code)
|
|
|
|
}()
|
|
|
|
|
|
|
|
msg := []byte("test websocket")
|
|
|
|
err = wsutil.WriteClientText(writePipe, msg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
// ReadServerText reads next data message from rw, considering that caller represents proxy side.
|
2020-10-20 15:26:55 +00:00
|
|
|
returnedMsg, err := wsutil.ReadServerText(respWriter.respBody())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, msg, returnedMsg)
|
|
|
|
|
|
|
|
err = wsutil.WriteClientBinary(writePipe, msg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
returnedMsg, err = wsutil.ReadServerBinary(respWriter.respBody())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, msg, returnedMsg)
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
}
|
2020-10-23 14:49:24 +00:00
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
func testProxySSE(t *testing.T, proxy connection.OriginProxy) func(t *testing.T) {
|
2020-10-23 14:49:24 +00:00
|
|
|
return func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
pushCount = 50
|
2020-11-25 06:55:13 +00:00
|
|
|
pushFreq = time.Millisecond * 10
|
2020-10-23 14:49:24 +00:00
|
|
|
)
|
|
|
|
respWriter := newMockSSERespWriter()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2020-11-02 11:21:34 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://localhost:8080%s?freq=%s", hello.SSERoute, pushFreq), nil)
|
2020-10-23 14:49:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2021-01-11 19:59:45 +00:00
|
|
|
err = proxy.Proxy(respWriter, req, connection.TypeHTTP)
|
2020-10-23 14:49:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusOK, respWriter.Code)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for i := 0; i < pushCount; i++ {
|
|
|
|
line := respWriter.ReadBytes()
|
|
|
|
expect := fmt.Sprintf("%d\n", i)
|
|
|
|
require.Equal(t, []byte(expect), line, fmt.Sprintf("Expect to read %v, got %v", expect, line))
|
|
|
|
|
|
|
|
line = respWriter.ReadBytes()
|
|
|
|
require.Equal(t, []byte("\n"), line, fmt.Sprintf("Expect to read '\n', got %v", line))
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
}
|
2020-11-02 11:21:34 +00:00
|
|
|
|
|
|
|
func TestProxyMultipleOrigins(t *testing.T) {
|
|
|
|
api := httptest.NewServer(mockAPI{})
|
|
|
|
defer api.Close()
|
|
|
|
|
|
|
|
unvalidatedIngress := []config.UnvalidatedIngressRule{
|
|
|
|
{
|
|
|
|
Hostname: "api.example.com",
|
|
|
|
Service: api.URL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Hostname: "hello.example.com",
|
|
|
|
Service: "hello-world",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Hostname: "health.example.com",
|
|
|
|
Path: "/health",
|
|
|
|
Service: "http_status:200",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Hostname: "*",
|
|
|
|
Service: "http_status:404",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ingress, err := ingress.ParseIngress(&config.Configuration{
|
|
|
|
TunnelID: t.Name(),
|
|
|
|
Ingress: unvalidatedIngress,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
log := zerolog.Nop()
|
2020-11-02 11:21:34 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
errC := make(chan error)
|
|
|
|
var wg sync.WaitGroup
|
2020-12-30 19:48:19 +00:00
|
|
|
require.NoError(t, ingress.StartOrigins(&wg, &log, ctx.Done(), errC))
|
2020-11-02 11:21:34 +00:00
|
|
|
|
2021-01-17 20:22:53 +00:00
|
|
|
proxy := NewOriginProxy(ingress, unusedWarpRoutingService, testTags, &log)
|
2020-11-02 11:21:34 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
url string
|
|
|
|
expectedStatus int
|
|
|
|
expectedBody []byte
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
url: "http://api.example.com",
|
|
|
|
expectedStatus: http.StatusCreated,
|
|
|
|
expectedBody: []byte("Created"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
url: fmt.Sprintf("http://hello.example.com%s", hello.HealthRoute),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: []byte("ok"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
url: "http://health.example.com/health",
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
url: "http://health.example.com/",
|
|
|
|
expectedStatus: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
url: "http://not-found.example.com",
|
|
|
|
expectedStatus: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
respWriter := newMockHTTPRespWriter()
|
|
|
|
req, err := http.NewRequest(http.MethodGet, test.url, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-11 19:59:45 +00:00
|
|
|
err = proxy.Proxy(respWriter, req, connection.TypeHTTP)
|
2020-11-02 11:21:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expectedStatus, respWriter.Code)
|
|
|
|
if test.expectedBody != nil {
|
|
|
|
assert.Equal(t, test.expectedBody, respWriter.Body.Bytes())
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, 0, respWriter.Body.Len())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockAPI struct{}
|
|
|
|
|
|
|
|
func (ma mockAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
2020-11-25 06:55:13 +00:00
|
|
|
_, _ = w.Write([]byte("Created"))
|
2020-11-02 11:21:34 +00:00
|
|
|
}
|
2020-10-30 11:41:14 +00:00
|
|
|
|
|
|
|
type errorOriginTransport struct{}
|
|
|
|
|
|
|
|
func (errorOriginTransport) RoundTrip(*http.Request) (*http.Response, error) {
|
|
|
|
return nil, fmt.Errorf("Proxy error")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxyError(t *testing.T) {
|
|
|
|
ingress := ingress.Ingress{
|
|
|
|
Rules: []ingress.Rule{
|
|
|
|
{
|
|
|
|
Hostname: "*",
|
|
|
|
Path: nil,
|
2020-12-09 21:46:53 +00:00
|
|
|
Service: ingress.MockOriginHTTPService{
|
2020-10-30 11:41:14 +00:00
|
|
|
Transport: errorOriginTransport{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
log := zerolog.Nop()
|
2020-10-30 11:41:14 +00:00
|
|
|
|
2021-01-17 20:22:53 +00:00
|
|
|
proxy := NewOriginProxy(ingress, unusedWarpRoutingService, testTags, &log)
|
2020-10-30 11:41:14 +00:00
|
|
|
|
|
|
|
respWriter := newMockHTTPRespWriter()
|
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1", nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-01-11 19:59:45 +00:00
|
|
|
err = proxy.Proxy(respWriter, req, connection.TypeHTTP)
|
2020-10-30 11:41:14 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, http.StatusBadGateway, respWriter.Code)
|
|
|
|
assert.Equal(t, "http response error", respWriter.Body.String())
|
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
type replayer struct {
|
|
|
|
sync.RWMutex
|
|
|
|
writeDone chan struct{}
|
|
|
|
rw *bytes.Buffer
|
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
func newReplayer(buffer *bytes.Buffer) {
|
2020-12-09 21:46:53 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
func (r *replayer) Read(p []byte) (int, error) {
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
|
|
|
return r.rw.Read(p)
|
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
func (r *replayer) Write(p []byte) (int, error) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
n, err := r.rw.Write(p)
|
|
|
|
return n, err
|
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
func (r *replayer) String() string {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
return r.rw.String()
|
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
func (r *replayer) Bytes() []byte {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
return r.rw.Bytes()
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
// TestConnections tests every possible permutation of connection protocols
|
|
|
|
// proxied by cloudflared.
|
|
|
|
//
|
|
|
|
// WS - WS : When a websocket based ingress is configured on the origin and
|
|
|
|
// the eyeball is also a websocket client streaming data.
|
|
|
|
// TCP - TCP : When teamnet is enabled and an http or tcp service is running
|
|
|
|
// on the origin.
|
|
|
|
// TCP - WS: When teamnet is enabled and a websocket based service is running
|
|
|
|
// on the origin.
|
|
|
|
// WS - TCP: When a tcp based ingress is configured on the origin and the
|
|
|
|
// eyeball sends tcp packets wrapped in websockets. (E.g: cloudflared access).
|
|
|
|
func TestConnections(t *testing.T) {
|
|
|
|
logger := logger.Create(nil)
|
|
|
|
replayer := &replayer{rw: &bytes.Buffer{}}
|
2020-12-09 21:46:53 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
skip bool
|
|
|
|
ingressServicePrefix string
|
2020-12-09 21:46:53 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
originService func(*testing.T, net.Listener)
|
|
|
|
eyeballService connection.ResponseWriter
|
|
|
|
connectionType connection.Type
|
2021-02-04 18:03:34 +00:00
|
|
|
requestHeaders http.Header
|
2021-02-02 18:27:50 +00:00
|
|
|
wantMessage []byte
|
2021-02-04 18:03:34 +00:00
|
|
|
wantHeaders http.Header
|
2021-02-02 18:27:50 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ws-ws proxy",
|
|
|
|
ingressServicePrefix: "ws://",
|
|
|
|
originService: runEchoWSService,
|
|
|
|
eyeballService: newWSRespWriter([]byte("test1"), replayer),
|
|
|
|
connectionType: connection.TypeWebsocket,
|
2021-02-04 18:03:34 +00:00
|
|
|
requestHeaders: map[string][]string{
|
|
|
|
"Test-Cloudflared-Echo": []string{"Echo"},
|
|
|
|
},
|
|
|
|
wantMessage: []byte("echo-test1"),
|
|
|
|
wantHeaders: map[string][]string{
|
|
|
|
"Connection": []string{"Upgrade"},
|
|
|
|
"Sec-Websocket-Accept": []string{"Kfh9QIsMVZcl6xEPYxPHzW8SZ8w="},
|
|
|
|
"Upgrade": []string{"websocket"},
|
|
|
|
"Test-Cloudflared-Echo": []string{"Echo"},
|
|
|
|
},
|
2021-02-02 18:27:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "tcp-tcp proxy",
|
|
|
|
ingressServicePrefix: "tcp://",
|
|
|
|
originService: runEchoTCPService,
|
|
|
|
eyeballService: newTCPRespWriter(
|
|
|
|
[]byte(`test2`),
|
|
|
|
replayer,
|
|
|
|
),
|
|
|
|
connectionType: connection.TypeTCP,
|
2021-02-04 18:03:34 +00:00
|
|
|
requestHeaders: map[string][]string{
|
|
|
|
"Cf-Cloudflared-Proxy-Src": []string{"non-blank-value"},
|
|
|
|
},
|
|
|
|
wantMessage: []byte("echo-test2"),
|
|
|
|
wantHeaders: http.Header{},
|
2021-02-02 18:27:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "tcp-ws proxy",
|
|
|
|
ingressServicePrefix: "ws://",
|
|
|
|
originService: runEchoWSService,
|
|
|
|
eyeballService: newPipedWSWriter(&mockTCPRespWriter{}, []byte("test3")),
|
2021-02-04 18:03:34 +00:00
|
|
|
requestHeaders: map[string][]string{
|
|
|
|
"Cf-Cloudflared-Proxy-Src": []string{"non-blank-value"},
|
|
|
|
},
|
|
|
|
connectionType: connection.TypeTCP,
|
|
|
|
wantMessage: []byte("echo-test3"),
|
|
|
|
// We expect no headers here because they are sent back via
|
|
|
|
// the stream.
|
|
|
|
wantHeaders: http.Header{},
|
2021-02-02 18:27:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ws-tcp proxy",
|
|
|
|
ingressServicePrefix: "tcp://",
|
|
|
|
originService: runEchoTCPService,
|
|
|
|
eyeballService: newWSRespWriter([]byte("test4"), replayer),
|
|
|
|
connectionType: connection.TypeWebsocket,
|
|
|
|
wantMessage: []byte("echo-test4"),
|
2021-02-04 18:03:34 +00:00
|
|
|
wantHeaders: http.Header{},
|
2021-02-02 18:27:50 +00:00
|
|
|
},
|
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
test.originService(t, ln)
|
|
|
|
ingressRule := createSingleIngressConfig(t, test.ingressServicePrefix+ln.Addr().String())
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
errC := make(chan error)
|
|
|
|
ingressRule.StartOrigins(&wg, logger, ctx.Done(), errC)
|
|
|
|
proxy := NewOriginProxy(ingressRule, ingress.NewWarpRoutingService(), testTags, logger)
|
|
|
|
req, err := http.NewRequest(http.MethodGet, test.ingressServicePrefix+ln.Addr().String(), nil)
|
|
|
|
require.NoError(t, err)
|
2021-02-04 18:03:34 +00:00
|
|
|
reqHeaders := make(http.Header)
|
|
|
|
for k, vs := range test.requestHeaders {
|
|
|
|
reqHeaders[k] = vs
|
|
|
|
}
|
|
|
|
req.Header = reqHeaders
|
2021-02-02 18:27:50 +00:00
|
|
|
|
|
|
|
if pipedWS, ok := test.eyeballService.(*pipedWSWriter); ok {
|
|
|
|
go func() {
|
|
|
|
resp := pipedWS.roundtrip(test.ingressServicePrefix + ln.Addr().String())
|
|
|
|
replayer.Write(resp)
|
|
|
|
}()
|
|
|
|
}
|
2021-02-04 18:03:34 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
err = proxy.Proxy(test.eyeballService, req, test.connectionType)
|
2020-12-09 21:46:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
cancel()
|
|
|
|
assert.Equal(t, test.wantMessage, replayer.Bytes())
|
2021-02-04 18:03:34 +00:00
|
|
|
respPrinter := test.eyeballService.(responsePrinter)
|
|
|
|
assert.Equal(t, test.wantHeaders, respPrinter.printRespHeaders())
|
2021-02-02 18:27:50 +00:00
|
|
|
replayer.rw.Reset()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
|
2021-02-04 18:03:34 +00:00
|
|
|
type responsePrinter interface {
|
|
|
|
printRespHeaders() http.Header
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
type pipedWSWriter struct {
|
|
|
|
dialer gorillaWS.Dialer
|
|
|
|
wsConn net.Conn
|
|
|
|
pipedConn net.Conn
|
|
|
|
respWriter connection.ResponseWriter
|
2021-02-04 18:03:34 +00:00
|
|
|
respHeaders http.Header
|
2021-02-02 18:27:50 +00:00
|
|
|
messageToWrite []byte
|
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
func newPipedWSWriter(rw *mockTCPRespWriter, messageToWrite []byte) *pipedWSWriter {
|
|
|
|
conn1, conn2 := net.Pipe()
|
|
|
|
dialer := gorillaWS.Dialer{
|
|
|
|
NetDial: func(network, addr string) (net.Conn, error) {
|
|
|
|
return conn2, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
rw.pr = conn1
|
|
|
|
rw.w = conn1
|
|
|
|
return &pipedWSWriter{
|
|
|
|
dialer: dialer,
|
|
|
|
pipedConn: conn1,
|
|
|
|
wsConn: conn2,
|
|
|
|
messageToWrite: messageToWrite,
|
|
|
|
respWriter: rw,
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-11 19:59:45 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
func (p *pipedWSWriter) roundtrip(addr string) []byte {
|
|
|
|
header := http.Header{}
|
|
|
|
conn, resp, err := p.dialer.Dial(addr, header)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
2021-01-11 19:59:45 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
if resp.StatusCode != http.StatusSwitchingProtocols {
|
|
|
|
panic(fmt.Errorf("resp returned status code: %d", resp.StatusCode))
|
|
|
|
}
|
2021-01-11 19:59:45 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
err = conn.WriteMessage(gorillaWS.TextMessage, p.messageToWrite)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2021-01-11 19:59:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
_, data, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-01-11 19:59:45 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
return data
|
|
|
|
}
|
2021-01-11 19:59:45 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
func (p *pipedWSWriter) Read(data []byte) (int, error) {
|
|
|
|
return p.pipedConn.Read(data)
|
2021-01-11 19:59:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
func (p *pipedWSWriter) Write(data []byte) (int, error) {
|
|
|
|
return p.pipedConn.Write(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pipedWSWriter) WriteErrorResponse() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pipedWSWriter) WriteRespHeaders(status int, header http.Header) error {
|
2021-02-04 18:03:34 +00:00
|
|
|
p.respHeaders = header
|
2021-02-02 18:27:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-04 18:03:34 +00:00
|
|
|
// printRespHeaders is a test function to read respHeaders
|
|
|
|
func (p *pipedWSWriter) printRespHeaders() http.Header {
|
|
|
|
return p.respHeaders
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
type wsRespWriter struct {
|
2021-02-04 18:03:34 +00:00
|
|
|
w io.Writer
|
|
|
|
pr *io.PipeReader
|
|
|
|
pw *io.PipeWriter
|
|
|
|
respHeaders http.Header
|
|
|
|
code int
|
2021-01-11 19:59:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
// newWSRespWriter uses wsutil.WriteClientText to generate websocket frames.
|
|
|
|
// and wsutil.ReadClientText to translate frames from server to byte data.
|
|
|
|
// In essence, this acts as a wsClient.
|
|
|
|
func newWSRespWriter(data []byte, w io.Writer) *wsRespWriter {
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go wsutil.WriteClientBinary(pw, data)
|
|
|
|
return &wsRespWriter{
|
|
|
|
w: w,
|
|
|
|
pr: pr,
|
|
|
|
pw: pw,
|
|
|
|
}
|
2021-01-11 19:59:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
// Read is read by ingress.Stream and serves as the input from the client.
|
|
|
|
func (w *wsRespWriter) Read(p []byte) (int, error) {
|
|
|
|
return w.pr.Read(p)
|
2021-01-11 19:59:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
// Write is written to by ingress.Stream and serves as the output to the client.
|
|
|
|
func (w *wsRespWriter) Write(p []byte) (int, error) {
|
|
|
|
defer w.pw.Close()
|
|
|
|
returnedMsg, err := wsutil.ReadServerBinary(bytes.NewBuffer(p))
|
|
|
|
if err != nil {
|
|
|
|
// The data was not returned by a websocket connecton.
|
|
|
|
if err != io.ErrUnexpectedEOF {
|
|
|
|
return w.w.Write(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return w.w.Write(returnedMsg)
|
2021-01-11 19:59:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
func (w *wsRespWriter) WriteRespHeaders(status int, header http.Header) error {
|
2021-02-04 18:03:34 +00:00
|
|
|
w.respHeaders = header
|
2021-02-02 18:27:50 +00:00
|
|
|
w.code = status
|
2021-01-11 19:59:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
func (w *wsRespWriter) WriteErrorResponse() {
|
|
|
|
}
|
2021-01-11 19:59:45 +00:00
|
|
|
|
2021-02-04 18:03:34 +00:00
|
|
|
// printRespHeaders is a test function to read respHeaders
|
|
|
|
func (w *wsRespWriter) printRespHeaders() http.Header {
|
|
|
|
return w.respHeaders
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
func runEchoTCPService(t *testing.T, l net.Listener) {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := l.Accept()
|
2021-01-11 19:59:45 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer conn.Close()
|
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
for {
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
size, err := conn.Read(buf)
|
|
|
|
if err == io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data := []byte("echo-")
|
|
|
|
data = append(data, buf[:size]...)
|
|
|
|
_, err = conn.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2021-01-11 19:59:45 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
func runEchoWSService(t *testing.T, l net.Listener) {
|
|
|
|
var upgrader = gorillaWS.Upgrader{
|
|
|
|
ReadBufferSize: 10,
|
|
|
|
WriteBufferSize: 10,
|
|
|
|
}
|
|
|
|
|
|
|
|
var ws = func(w http.ResponseWriter, r *http.Request) {
|
2021-02-04 18:03:34 +00:00
|
|
|
header := make(http.Header)
|
|
|
|
for k, vs := range r.Header {
|
|
|
|
if k == "Test-Cloudflared-Echo" {
|
|
|
|
header[k] = vs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
conn, err := upgrader.Upgrade(w, r, header)
|
2021-01-11 19:59:45 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-02 18:27:50 +00:00
|
|
|
defer conn.Close()
|
2021-01-11 19:59:45 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
for {
|
|
|
|
messageType, p, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-02-04 18:03:34 +00:00
|
|
|
data := []byte("echo-")
|
|
|
|
data = append(data, p...)
|
|
|
|
if err := conn.WriteMessage(messageType, data); err != nil {
|
2021-02-02 18:27:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 19:59:45 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
server := http.Server{
|
|
|
|
Handler: http.HandlerFunc(ws),
|
|
|
|
}
|
2021-01-11 19:59:45 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
go func() {
|
|
|
|
err := server.Serve(l)
|
2021-01-11 19:59:45 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-02 18:27:50 +00:00
|
|
|
}()
|
|
|
|
}
|
2021-01-11 19:59:45 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
func createSingleIngressConfig(t *testing.T, service string) ingress.Ingress {
|
|
|
|
ingressConfig := &config.Configuration{
|
|
|
|
Ingress: []config.UnvalidatedIngressRule{
|
|
|
|
{
|
|
|
|
Hostname: "*",
|
|
|
|
Service: service,
|
|
|
|
},
|
|
|
|
},
|
2021-01-11 19:59:45 +00:00
|
|
|
}
|
2021-02-02 18:27:50 +00:00
|
|
|
ingressRule, err := ingress.ParseIngress(ingressConfig)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return ingressRule
|
|
|
|
}
|
|
|
|
|
|
|
|
type tcpWrappedWs struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockTCPRespWriter struct {
|
2021-02-04 18:03:34 +00:00
|
|
|
w io.Writer
|
|
|
|
pr io.Reader
|
|
|
|
pw *io.PipeWriter
|
|
|
|
respHeaders http.Header
|
|
|
|
code int
|
2021-02-02 18:27:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTCPRespWriter(data []byte, w io.Writer) *mockTCPRespWriter {
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go pw.Write(data)
|
|
|
|
return &mockTCPRespWriter{
|
|
|
|
w: w,
|
|
|
|
pr: pr,
|
|
|
|
pw: pw,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTCPRespWriter) Read(p []byte) (n int, err error) {
|
|
|
|
return m.pr.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTCPRespWriter) Write(p []byte) (n int, err error) {
|
|
|
|
defer m.pw.Close()
|
|
|
|
return m.w.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTCPRespWriter) WriteErrorResponse() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTCPRespWriter) WriteRespHeaders(status int, header http.Header) error {
|
2021-02-04 18:03:34 +00:00
|
|
|
m.respHeaders = header
|
2021-02-02 18:27:50 +00:00
|
|
|
m.code = status
|
|
|
|
return nil
|
2021-01-11 19:59:45 +00:00
|
|
|
}
|
2021-02-04 18:03:34 +00:00
|
|
|
|
|
|
|
// printRespHeaders is a test function to read respHeaders
|
|
|
|
func (m *mockTCPRespWriter) printRespHeaders() http.Header {
|
|
|
|
return m.respHeaders
|
|
|
|
}
|