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"
|
2020-12-09 21:46:53 +00:00
|
|
|
"github.com/cloudflare/cloudflared/h2mux"
|
2020-10-20 15:26:55 +00:00
|
|
|
"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"
|
2020-12-09 21:46:53 +00:00
|
|
|
"github.com/cloudflare/cloudflared/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
|
|
|
|
|
|
|
func TestProxyBastionMode(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
flagSet := flag.NewFlagSet(t.Name(), flag.PanicOnError)
|
|
|
|
flagSet.Bool("bastion", true, "")
|
|
|
|
|
|
|
|
cliCtx := cli.NewContext(cli.NewApp(), flagSet, nil)
|
|
|
|
err := cliCtx.Set(config.BastionFlag, "true")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
allowURLFromArgs := false
|
|
|
|
ingressRule, err := ingress.NewSingleOrigin(cliCtx, allowURLFromArgs)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
errC := make(chan error)
|
|
|
|
|
|
|
|
log := logger.Create(nil)
|
|
|
|
|
|
|
|
ingressRule.StartOrigins(&wg, log, ctx.Done(), errC)
|
|
|
|
|
2021-01-17 20:22:53 +00:00
|
|
|
proxy := NewOriginProxy(ingressRule, unusedWarpRoutingService, testTags, log)
|
2020-12-09 21:46:53 +00:00
|
|
|
|
|
|
|
t.Run("testBastionWebsocket", testBastionWebsocket(proxy))
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
func testBastionWebsocket(proxy connection.OriginProxy) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
readPipe, _ := io.Pipe()
|
|
|
|
respWriter := newMockWSRespWriter(readPipe)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
msgFromConn := []byte("data from websocket proxy")
|
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
2021-01-11 19:59:45 +00:00
|
|
|
require.NoError(t, err)
|
2020-12-09 21:46:53 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
defer ln.Close()
|
2021-01-11 19:59:45 +00:00
|
|
|
conn, err := ln.Accept()
|
2020-12-09 21:46:53 +00:00
|
|
|
require.NoError(t, err)
|
2021-01-11 19:59:45 +00:00
|
|
|
wsConn := websocket.NewConn(conn, nil)
|
|
|
|
wsConn.Write(msgFromConn)
|
2020-12-09 21:46:53 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://dummy", nil)
|
|
|
|
req.Header.Set(h2mux.CFJumpDestinationHeader, ln.Addr().String())
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2021-01-11 19:59:45 +00:00
|
|
|
err = proxy.Proxy(respWriter, req, connection.TypeWebsocket)
|
2020-12-09 21:46:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusSwitchingProtocols, respWriter.Code)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// ReadServerText reads next data message from rw, considering that caller represents proxy side.
|
|
|
|
returnedMsg, err := wsutil.ReadServerText(respWriter.respBody())
|
|
|
|
if err != io.EOF {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, msgFromConn, returnedMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 19:59:45 +00:00
|
|
|
|
|
|
|
func TestTCPStream(t *testing.T) {
|
|
|
|
logger := logger.Create(nil)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
ingressConfig := &config.Configuration{
|
|
|
|
Ingress: []config.UnvalidatedIngressRule{
|
2021-01-17 20:22:53 +00:00
|
|
|
{
|
2021-01-11 19:59:45 +00:00
|
|
|
Hostname: "*",
|
2021-01-17 20:22:53 +00:00
|
|
|
Service: "bastion",
|
2021-01-11 19:59:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ingressRule, err := ingress.ParseIngress(ingressConfig)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
errC := make(chan error)
|
|
|
|
ingressRule.StartOrigins(&wg, logger, ctx.Done(), errC)
|
|
|
|
|
2021-01-17 20:22:53 +00:00
|
|
|
proxy := NewOriginProxy(ingressRule, ingress.NewWarpRoutingService(), testTags, logger)
|
2021-01-11 19:59:45 +00:00
|
|
|
|
|
|
|
t.Run("testTCPStream", testTCPStreamProxy(proxy))
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockTCPRespWriter struct {
|
|
|
|
w io.Writer
|
|
|
|
code int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTCPRespWriter) Read(p []byte) (n int, err error) {
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTCPRespWriter) Write(p []byte) (n int, err error) {
|
|
|
|
return m.w.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTCPRespWriter) WriteErrorResponse() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTCPRespWriter) WriteRespHeaders(status int, header http.Header) error {
|
|
|
|
m.code = status
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testTCPStreamProxy(proxy connection.OriginProxy) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
readPipe, writePipe := io.Pipe()
|
|
|
|
respWriter := &mockTCPRespWriter{
|
|
|
|
w: writePipe,
|
|
|
|
}
|
|
|
|
msgFromConn := []byte("data from tcp proxy")
|
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
go func() {
|
|
|
|
defer ln.Close()
|
|
|
|
conn, err := ln.Accept()
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer conn.Close()
|
|
|
|
_, err = conn.Write(msgFromConn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://dummy", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
req.Header.Set("Cf-Cloudflared-Proxy-Src", "non-blank-value")
|
|
|
|
req.Host = ln.Addr().String()
|
|
|
|
err = proxy.Proxy(respWriter, req, connection.TypeTCP)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusSwitchingProtocols, respWriter.code)
|
|
|
|
|
|
|
|
returnedMsg := make([]byte, len(msgFromConn))
|
|
|
|
|
|
|
|
_, err = readPipe.Read(returnedMsg)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, msgFromConn, returnedMsg)
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}
|