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"
|
2022-01-28 14:32:09 +00:00
|
|
|
"strings"
|
2020-10-20 15:26:55 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
2020-10-23 14:49:24 +00:00
|
|
|
"time"
|
2020-10-20 15:26:55 +00:00
|
|
|
|
2021-03-23 14:30:43 +00:00
|
|
|
"github.com/gobwas/ws/wsutil"
|
|
|
|
gorillaWS "github.com/gorilla/websocket"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/urfave/cli/v2"
|
2021-04-02 06:10:43 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2021-01-17 20:22:53 +00:00
|
|
|
|
2021-03-08 16:46:23 +00:00
|
|
|
"github.com/cloudflare/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"
|
2021-03-23 14:30:43 +00:00
|
|
|
"github.com/cloudflare/cloudflared/logger"
|
2020-11-02 11:21:34 +00:00
|
|
|
tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
2020-10-20 15:26:55 +00:00
|
|
|
)
|
|
|
|
|
2020-11-02 11:21:34 +00:00
|
|
|
var (
|
2021-08-23 15:04:09 +00:00
|
|
|
testTags = []tunnelpogs.Tag{tunnelpogs.Tag{Name: "Name", Value: "value"}}
|
2021-01-17 20:22:53 +00:00
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
func (w *mockHTTPRespWriter) WriteResponse() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-04-02 06:10:43 +00:00
|
|
|
func (w *mockWSRespWriter) Close() error {
|
|
|
|
close(w.writeNotification)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-20 15:26:55 +00:00
|
|
|
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)
|
2021-04-02 06:10:43 +00:00
|
|
|
t.Run("testProxyHTTP", testProxyHTTP(proxy))
|
|
|
|
t.Run("testProxyWebsocket", testProxyWebsocket(proxy))
|
|
|
|
t.Run("testProxySSE", testProxySSE(proxy))
|
2022-01-28 14:32:09 +00:00
|
|
|
t.Run("testProxySSEAllData", testProxySSEAllData(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
|
|
|
}
|
|
|
|
|
2021-04-02 06:10:43 +00:00
|
|
|
func testProxyHTTP(proxy connection.OriginProxy) func(t *testing.T) {
|
2020-10-20 15:26:55 +00:00
|
|
|
return func(t *testing.T) {
|
2021-02-11 14:36:42 +00:00
|
|
|
responseWriter := 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-07-16 15:14:37 +00:00
|
|
|
err = proxy.ProxyHTTP(responseWriter, req, false)
|
2020-10-20 15:26:55 +00:00
|
|
|
require.NoError(t, err)
|
2021-08-23 15:04:09 +00:00
|
|
|
for _, tag := range testTags {
|
|
|
|
assert.Equal(t, tag.Value, req.Header.Get(TagHeaderNamePrefix+tag.Name))
|
|
|
|
}
|
2020-10-20 15:26:55 +00:00
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
assert.Equal(t, http.StatusOK, responseWriter.Code)
|
2020-10-20 15:26:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 06:10:43 +00:00
|
|
|
func testProxyWebsocket(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
|
2021-04-02 06:10:43 +00:00
|
|
|
const testTimeout = 5 * time.Second * 1000
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
|
|
|
|
defer cancel()
|
2020-10-20 15:26:55 +00:00
|
|
|
readPipe, writePipe := io.Pipe()
|
2021-02-11 14:36:42 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://localhost:8080%s", hello.WSRoute), readPipe)
|
2021-04-02 06:10:43 +00:00
|
|
|
req.Header.Set("Sec-Websocket-Key", "dGhlIHNhbXBsZSBub25jZQ==")
|
|
|
|
req.Header.Set("Connection", "Upgrade")
|
|
|
|
req.Header.Set("Upgrade", "websocket")
|
|
|
|
responseWriter := newMockWSRespWriter(nil)
|
2020-10-20 15:26:55 +00:00
|
|
|
|
2021-04-02 06:10:43 +00:00
|
|
|
finished := make(chan struct{})
|
|
|
|
|
|
|
|
errGroup, ctx := errgroup.WithContext(ctx)
|
|
|
|
errGroup.Go(func() error {
|
2021-07-16 15:14:37 +00:00
|
|
|
err = proxy.ProxyHTTP(responseWriter, req, true)
|
2020-10-20 15:26:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
require.Equal(t, http.StatusSwitchingProtocols, responseWriter.Code)
|
2021-04-02 06:10:43 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
errGroup.Go(func() error {
|
|
|
|
select {
|
|
|
|
case <-finished:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
|
|
t.Errorf("Test timed out")
|
|
|
|
readPipe.Close()
|
|
|
|
writePipe.Close()
|
|
|
|
responseWriter.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2020-10-20 15:26:55 +00:00
|
|
|
|
|
|
|
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.
|
2021-02-11 14:36:42 +00:00
|
|
|
returnedMsg, err := wsutil.ReadServerText(responseWriter.respBody())
|
2020-10-20 15:26:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, msg, returnedMsg)
|
|
|
|
|
|
|
|
err = wsutil.WriteClientBinary(writePipe, msg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
returnedMsg, err = wsutil.ReadServerBinary(responseWriter.respBody())
|
2020-10-20 15:26:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, msg, returnedMsg)
|
|
|
|
|
2021-04-02 06:10:43 +00:00
|
|
|
_ = readPipe.Close()
|
|
|
|
_ = writePipe.Close()
|
|
|
|
_ = responseWriter.Close()
|
|
|
|
|
|
|
|
close(finished)
|
|
|
|
errGroup.Wait()
|
2020-10-20 15:26:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-23 14:49:24 +00:00
|
|
|
|
2021-04-02 06:10:43 +00:00
|
|
|
func testProxySSE(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
|
|
|
)
|
2021-02-11 14:36:42 +00:00
|
|
|
responseWriter := newMockSSERespWriter()
|
2020-10-23 14:49:24 +00:00
|
|
|
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-07-16 15:14:37 +00:00
|
|
|
err = proxy.ProxyHTTP(responseWriter, req, false)
|
2020-10-23 14:49:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
require.Equal(t, http.StatusOK, responseWriter.Code)
|
2020-10-23 14:49:24 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
for i := 0; i < pushCount; i++ {
|
2021-02-11 14:36:42 +00:00
|
|
|
line := responseWriter.ReadBytes()
|
2020-10-23 14:49:24 +00:00
|
|
|
expect := fmt.Sprintf("%d\n", i)
|
|
|
|
require.Equal(t, []byte(expect), line, fmt.Sprintf("Expect to read %v, got %v", expect, line))
|
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
line = responseWriter.ReadBytes()
|
2020-10-23 14:49:24 +00:00
|
|
|
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
|
|
|
|
2022-01-28 14:32:09 +00:00
|
|
|
// Regression test to guarantee that we always write the contents downstream even if EOF is reached without
|
|
|
|
// hitting the delimiter
|
|
|
|
func testProxySSEAllData(proxy *Proxy) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
eyeballReader := io.NopCloser(strings.NewReader("data\r\r"))
|
|
|
|
responseWriter := newMockSSERespWriter()
|
|
|
|
|
|
|
|
// responseWriter uses an unbuffered channel, so we call in a different go-routine
|
|
|
|
go proxy.writeEventStream(responseWriter, eyeballReader)
|
|
|
|
|
|
|
|
result := string(<-responseWriter.writeNotification)
|
|
|
|
require.Equal(t, "data\r\r", result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-06-18 11:21:11 +00:00
|
|
|
tests := []MultipleIngressTest{
|
2020-11-02 11:21:34 +00:00
|
|
|
{
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-06-18 11:21:11 +00:00
|
|
|
runIngressTestScenarios(t, unvalidatedIngress, tests)
|
|
|
|
}
|
|
|
|
|
|
|
|
type MultipleIngressTest struct {
|
|
|
|
url string
|
|
|
|
expectedStatus int
|
|
|
|
expectedBody []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func runIngressTestScenarios(t *testing.T, unvalidatedIngress []config.UnvalidatedIngressRule, tests []MultipleIngressTest) {
|
|
|
|
ingress, err := ingress.ParseIngress(&config.Configuration{
|
|
|
|
TunnelID: t.Name(),
|
|
|
|
Ingress: unvalidatedIngress,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
log := zerolog.Nop()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
errC := make(chan error)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
require.NoError(t, ingress.StartOrigins(&wg, &log, ctx.Done(), errC))
|
|
|
|
|
|
|
|
proxy := NewOriginProxy(ingress, unusedWarpRoutingService, testTags, &log)
|
|
|
|
|
2020-11-02 11:21:34 +00:00
|
|
|
for _, test := range tests {
|
2021-02-11 14:36:42 +00:00
|
|
|
responseWriter := newMockHTTPRespWriter()
|
2020-11-02 11:21:34 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, test.url, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
err = proxy.ProxyHTTP(responseWriter, req, false)
|
2020-11-02 11:21:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
assert.Equal(t, test.expectedStatus, responseWriter.Code)
|
2020-11-02 11:21:34 +00:00
|
|
|
if test.expectedBody != nil {
|
2021-02-11 14:36:42 +00:00
|
|
|
assert.Equal(t, test.expectedBody, responseWriter.Body.Bytes())
|
2020-11-02 11:21:34 +00:00
|
|
|
} else {
|
2021-02-11 14:36:42 +00:00
|
|
|
assert.Equal(t, 0, responseWriter.Body.Len())
|
2020-11-02 11:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
2021-07-16 15:14:37 +00:00
|
|
|
ing := ingress.Ingress{
|
2020-10-30 11:41:14 +00:00
|
|
|
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-07-16 15:14:37 +00:00
|
|
|
proxy := NewOriginProxy(ing, unusedWarpRoutingService, testTags, &log)
|
2020-10-30 11:41:14 +00:00
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
responseWriter := newMockHTTPRespWriter()
|
2020-10-30 11:41:14 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1", nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
assert.Error(t, proxy.ProxyHTTP(responseWriter, req, false))
|
2020-10-30 11:41:14 +00:00
|
|
|
}
|
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{}}
|
2021-02-11 14:36:42 +00:00
|
|
|
type args struct {
|
|
|
|
ingressServiceScheme string
|
|
|
|
originService func(*testing.T, net.Listener)
|
|
|
|
eyeballResponseWriter connection.ResponseWriter
|
|
|
|
eyeballRequestBody io.ReadCloser
|
2020-12-09 21:46:53 +00:00
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
// Can be set to nil to show warp routing is not enabled.
|
|
|
|
warpRoutingService *ingress.WarpRoutingService
|
2020-12-09 21:46:53 +00:00
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
// eyeball connection type.
|
2021-02-02 18:27:50 +00:00
|
|
|
connectionType connection.Type
|
2021-02-11 14:36:42 +00:00
|
|
|
|
2022-01-28 14:32:09 +00:00
|
|
|
// requestheaders to be sent in the call to proxy.Proxy
|
2021-02-04 18:03:34 +00:00
|
|
|
requestHeaders http.Header
|
2021-02-11 14:36:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type want struct {
|
|
|
|
message []byte
|
|
|
|
headers http.Header
|
|
|
|
err bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want want
|
2021-02-02 18:27:50 +00:00
|
|
|
}{
|
|
|
|
{
|
2021-02-11 14:36:42 +00:00
|
|
|
name: "ws-ws proxy",
|
|
|
|
args: args{
|
|
|
|
ingressServiceScheme: "ws://",
|
|
|
|
originService: runEchoWSService,
|
|
|
|
eyeballResponseWriter: newWSRespWriter(replayer),
|
|
|
|
eyeballRequestBody: newWSRequestBody([]byte("test1")),
|
|
|
|
connectionType: connection.TypeWebsocket,
|
|
|
|
requestHeaders: map[string][]string{
|
|
|
|
// Example key from https://tools.ietf.org/html/rfc6455#section-1.2
|
|
|
|
"Sec-Websocket-Key": {"dGhlIHNhbXBsZSBub25jZQ=="},
|
|
|
|
"Test-Cloudflared-Echo": {"Echo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: want{
|
|
|
|
message: []byte("echo-test1"),
|
|
|
|
headers: map[string][]string{
|
|
|
|
"Connection": {"Upgrade"},
|
|
|
|
"Sec-Websocket-Accept": {"s3pPLMBiTxaQ9kYGzzhZRbK+xOo="},
|
|
|
|
"Upgrade": {"websocket"},
|
|
|
|
"Test-Cloudflared-Echo": {"Echo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "tcp-tcp proxy",
|
|
|
|
args: args{
|
|
|
|
ingressServiceScheme: "tcp://",
|
|
|
|
originService: runEchoTCPService,
|
|
|
|
eyeballResponseWriter: newTCPRespWriter(replayer),
|
|
|
|
eyeballRequestBody: newTCPRequestBody([]byte("test2")),
|
|
|
|
warpRoutingService: ingress.NewWarpRoutingService(),
|
|
|
|
connectionType: connection.TypeTCP,
|
|
|
|
requestHeaders: map[string][]string{
|
|
|
|
"Cf-Cloudflared-Proxy-Src": {"non-blank-value"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: want{
|
|
|
|
message: []byte("echo-test2"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "tcp-ws proxy",
|
|
|
|
args: args{
|
|
|
|
ingressServiceScheme: "ws://",
|
|
|
|
originService: runEchoWSService,
|
2022-01-28 14:32:09 +00:00
|
|
|
// eyeballResponseWriter gets set after roundtrip dial.
|
2021-02-11 14:36:42 +00:00
|
|
|
eyeballRequestBody: newPipedWSRequestBody([]byte("test3")),
|
|
|
|
warpRoutingService: ingress.NewWarpRoutingService(),
|
|
|
|
requestHeaders: map[string][]string{
|
|
|
|
"Cf-Cloudflared-Proxy-Src": {"non-blank-value"},
|
|
|
|
},
|
|
|
|
connectionType: connection.TypeTCP,
|
|
|
|
},
|
|
|
|
want: want{
|
|
|
|
message: []byte("echo-test3"),
|
|
|
|
// We expect no headers here because they are sent back via
|
|
|
|
// the stream.
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ws-tcp proxy",
|
|
|
|
args: args{
|
|
|
|
ingressServiceScheme: "tcp://",
|
|
|
|
originService: runEchoTCPService,
|
|
|
|
eyeballResponseWriter: newWSRespWriter(replayer),
|
|
|
|
eyeballRequestBody: newWSRequestBody([]byte("test4")),
|
|
|
|
connectionType: connection.TypeWebsocket,
|
|
|
|
requestHeaders: map[string][]string{
|
|
|
|
// Example key from https://tools.ietf.org/html/rfc6455#section-1.2
|
|
|
|
"Sec-Websocket-Key": {"dGhlIHNhbXBsZSBub25jZQ=="},
|
|
|
|
},
|
2021-02-04 18:03:34 +00:00
|
|
|
},
|
2021-02-11 14:36:42 +00:00
|
|
|
want: want{
|
|
|
|
message: []byte("echo-test4"),
|
|
|
|
headers: map[string][]string{
|
|
|
|
"Connection": {"Upgrade"},
|
|
|
|
"Sec-Websocket-Accept": {"s3pPLMBiTxaQ9kYGzzhZRbK+xOo="},
|
|
|
|
"Upgrade": {"websocket"},
|
|
|
|
},
|
2021-02-04 18:03:34 +00:00
|
|
|
},
|
2021-02-02 18:27:50 +00:00
|
|
|
},
|
|
|
|
{
|
2021-02-11 14:36:42 +00:00
|
|
|
name: "tcp-tcp proxy without warpRoutingService enabled",
|
|
|
|
args: args{
|
|
|
|
ingressServiceScheme: "tcp://",
|
|
|
|
originService: runEchoTCPService,
|
|
|
|
eyeballResponseWriter: newTCPRespWriter(replayer),
|
|
|
|
eyeballRequestBody: newTCPRequestBody([]byte("test2")),
|
|
|
|
connectionType: connection.TypeTCP,
|
|
|
|
requestHeaders: map[string][]string{
|
|
|
|
"Cf-Cloudflared-Proxy-Src": {"non-blank-value"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: want{
|
|
|
|
message: []byte{},
|
|
|
|
err: true,
|
2021-02-04 18:03:34 +00:00
|
|
|
},
|
2021-02-02 18:27:50 +00:00
|
|
|
},
|
|
|
|
{
|
2021-02-11 14:36:42 +00:00
|
|
|
name: "ws-ws proxy when origin is different",
|
|
|
|
args: args{
|
|
|
|
ingressServiceScheme: "ws://",
|
|
|
|
originService: runEchoWSService,
|
|
|
|
eyeballResponseWriter: newWSRespWriter(replayer),
|
|
|
|
eyeballRequestBody: newWSRequestBody([]byte("test1")),
|
|
|
|
connectionType: connection.TypeWebsocket,
|
|
|
|
requestHeaders: map[string][]string{
|
|
|
|
// Example key from https://tools.ietf.org/html/rfc6455#section-1.2
|
|
|
|
"Sec-Websocket-Key": {"dGhlIHNhbXBsZSBub25jZQ=="},
|
|
|
|
"Origin": {"Different origin"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: want{
|
2021-07-01 09:29:53 +00:00
|
|
|
message: []byte("Forbidden\n"),
|
|
|
|
err: false,
|
|
|
|
headers: map[string][]string{
|
|
|
|
"Content-Length": {"10"},
|
|
|
|
"Content-Type": {"text/plain; charset=utf-8"},
|
|
|
|
"Sec-Websocket-Version": {"13"},
|
|
|
|
"X-Content-Type-Options": {"nosniff"},
|
|
|
|
},
|
2021-02-04 18:03:34 +00:00
|
|
|
},
|
2021-02-02 18:27:50 +00:00
|
|
|
},
|
|
|
|
{
|
2021-02-11 14:36:42 +00:00
|
|
|
name: "tcp-* proxy when origin service has already closed the connection/ is no longer running",
|
|
|
|
args: args{
|
|
|
|
ingressServiceScheme: "tcp://",
|
|
|
|
originService: func(t *testing.T, ln net.Listener) {
|
|
|
|
// closing the listener created by the test.
|
|
|
|
ln.Close()
|
|
|
|
},
|
|
|
|
eyeballResponseWriter: newTCPRespWriter(replayer),
|
|
|
|
eyeballRequestBody: newTCPRequestBody([]byte("test2")),
|
|
|
|
connectionType: connection.TypeTCP,
|
|
|
|
requestHeaders: map[string][]string{
|
|
|
|
"Cf-Cloudflared-Proxy-Src": {"non-blank-value"},
|
|
|
|
},
|
2021-02-05 13:01:53 +00:00
|
|
|
},
|
2021-02-11 14:36:42 +00:00
|
|
|
want: want{
|
|
|
|
message: []byte{},
|
|
|
|
err: true,
|
2021-02-05 13:01: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
|
|
|
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)
|
2021-02-05 13:01:53 +00:00
|
|
|
// Starts origin service
|
2021-02-11 14:36:42 +00:00
|
|
|
test.args.originService(t, ln)
|
2021-02-05 13:01:53 +00:00
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
ingressRule := createSingleIngressConfig(t, test.args.ingressServiceScheme+ln.Addr().String())
|
2021-02-02 18:27:50 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
errC := make(chan error)
|
|
|
|
ingressRule.StartOrigins(&wg, logger, ctx.Done(), errC)
|
2021-02-11 14:36:42 +00:00
|
|
|
proxy := NewOriginProxy(ingressRule, test.args.warpRoutingService, testTags, logger)
|
2021-02-05 13:01:53 +00:00
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
dest := ln.Addr().String()
|
2021-02-11 14:36:42 +00:00
|
|
|
req, err := http.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
test.args.ingressServiceScheme+ln.Addr().String(),
|
|
|
|
test.args.eyeballRequestBody,
|
|
|
|
)
|
2021-02-02 18:27:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
req.Header = test.args.requestHeaders
|
|
|
|
respWriter := test.args.eyeballResponseWriter
|
|
|
|
|
|
|
|
if pipedReqBody, ok := test.args.eyeballRequestBody.(*pipedRequestBody); ok {
|
|
|
|
respWriter = newTCPRespWriter(pipedReqBody.pipedConn)
|
2021-02-02 18:27:50 +00:00
|
|
|
go func() {
|
2021-02-11 14:36:42 +00:00
|
|
|
resp := pipedReqBody.roundtrip(test.args.ingressServiceScheme + ln.Addr().String())
|
2021-02-02 18:27:50 +00:00
|
|
|
replayer.Write(resp)
|
|
|
|
}()
|
|
|
|
}
|
2021-07-16 15:14:37 +00:00
|
|
|
if test.args.connectionType == connection.TypeTCP {
|
|
|
|
rws := connection.NewHTTPResponseReadWriterAcker(respWriter, req)
|
|
|
|
err = proxy.ProxyTCP(ctx, rws, &connection.TCPRequest{Dest: dest})
|
|
|
|
} else {
|
|
|
|
err = proxy.ProxyHTTP(respWriter, req, test.args.connectionType == connection.TypeWebsocket)
|
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
cancel()
|
2021-02-11 14:36:42 +00:00
|
|
|
assert.Equal(t, test.want.err, err != nil)
|
|
|
|
assert.Equal(t, test.want.message, replayer.Bytes())
|
|
|
|
respPrinter := respWriter.(responsePrinter)
|
|
|
|
assert.Equal(t, test.want.headers, respPrinter.headers())
|
2021-02-02 18:27:50 +00:00
|
|
|
replayer.rw.Reset()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
type requestBody struct {
|
|
|
|
pw *io.PipeWriter
|
|
|
|
pr *io.PipeReader
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWSRequestBody(data []byte) *requestBody {
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go wsutil.WriteClientBinary(pw, data)
|
|
|
|
return &requestBody{
|
|
|
|
pr: pr,
|
|
|
|
pw: pw,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func newTCPRequestBody(data []byte) *requestBody {
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go pw.Write(data)
|
|
|
|
return &requestBody{
|
|
|
|
pr: pr,
|
|
|
|
pw: pw,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *requestBody) Read(p []byte) (n int, err error) {
|
|
|
|
return r.pr.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *requestBody) Close() error {
|
|
|
|
r.pw.Close()
|
|
|
|
r.pr.Close()
|
|
|
|
return nil
|
2021-02-04 18:03:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
type pipedRequestBody struct {
|
2021-02-02 18:27:50 +00:00
|
|
|
dialer gorillaWS.Dialer
|
|
|
|
pipedConn net.Conn
|
2021-02-11 14:36:42 +00:00
|
|
|
wsConn net.Conn
|
2021-02-02 18:27:50 +00:00
|
|
|
messageToWrite []byte
|
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
func newPipedWSRequestBody(data []byte) *pipedRequestBody {
|
2021-02-02 18:27:50 +00:00
|
|
|
conn1, conn2 := net.Pipe()
|
|
|
|
dialer := gorillaWS.Dialer{
|
|
|
|
NetDial: func(network, addr string) (net.Conn, error) {
|
|
|
|
return conn2, nil
|
|
|
|
},
|
|
|
|
}
|
2021-02-11 14:36:42 +00:00
|
|
|
return &pipedRequestBody{
|
2021-02-02 18:27:50 +00:00
|
|
|
dialer: dialer,
|
|
|
|
pipedConn: conn1,
|
|
|
|
wsConn: conn2,
|
2021-02-11 14:36:42 +00:00
|
|
|
messageToWrite: data,
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-11 19:59:45 +00:00
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
func (p *pipedRequestBody) roundtrip(addr string) []byte {
|
2021-02-02 18:27:50 +00:00
|
|
|
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-11 14:36:42 +00:00
|
|
|
func (p *pipedRequestBody) Read(data []byte) (n int, err error) {
|
2021-02-02 18:27:50 +00:00
|
|
|
return p.pipedConn.Read(data)
|
2021-01-11 19:59:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
func (p *pipedRequestBody) Close() error {
|
2021-02-02 18:27:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
type responsePrinter interface {
|
|
|
|
headers() http.Header
|
2021-02-04 18:03:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
type wsRespWriter struct {
|
2021-02-11 14:36:42 +00:00
|
|
|
w io.Writer
|
|
|
|
responseHeaders 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.
|
2021-02-11 14:36:42 +00:00
|
|
|
func newWSRespWriter(w io.Writer) *wsRespWriter {
|
2021-02-02 18:27:50 +00:00
|
|
|
return &wsRespWriter{
|
2021-02-11 14:36:42 +00:00
|
|
|
w: w,
|
2021-02-02 18:27:50 +00:00
|
|
|
}
|
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) {
|
|
|
|
returnedMsg, err := wsutil.ReadServerBinary(bytes.NewBuffer(p))
|
|
|
|
if err != nil {
|
2021-11-12 15:38:06 +00:00
|
|
|
// The data was not returned by a websocket connection.
|
2021-02-02 18:27:50 +00:00
|
|
|
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-11 14:36:42 +00:00
|
|
|
w.responseHeaders = header
|
2021-02-02 18:27:50 +00:00
|
|
|
w.code = status
|
2021-01-11 19:59:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
// respHeaders is a test function to read respHeaders
|
|
|
|
func (w *wsRespWriter) headers() http.Header {
|
2021-07-01 09:29:53 +00:00
|
|
|
// Removing indeterminstic header because it cannot be asserted.
|
|
|
|
w.responseHeaders.Del("Date")
|
2021-02-11 14:36:42 +00:00
|
|
|
return w.responseHeaders
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockTCPRespWriter struct {
|
|
|
|
w io.Writer
|
|
|
|
responseHeaders http.Header
|
|
|
|
code int
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTCPRespWriter(w io.Writer) *mockTCPRespWriter {
|
|
|
|
return &mockTCPRespWriter{
|
|
|
|
w: w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
func (m *mockTCPRespWriter) Read(p []byte) (n int, err error) {
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
func (m *mockTCPRespWriter) Write(p []byte) (n int, err error) {
|
|
|
|
return m.w.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTCPRespWriter) WriteRespHeaders(status int, header http.Header) error {
|
|
|
|
m.responseHeaders = header
|
|
|
|
m.code = status
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// respHeaders is a test function to read respHeaders
|
|
|
|
func (m *mockTCPRespWriter) headers() http.Header {
|
|
|
|
return m.responseHeaders
|
2021-02-02 18:27:50 +00:00
|
|
|
}
|
2021-01-11 19:59:45 +00:00
|
|
|
|
2021-02-11 14:36:42 +00:00
|
|
|
func createSingleIngressConfig(t *testing.T, service string) ingress.Ingress {
|
|
|
|
ingressConfig := &config.Configuration{
|
|
|
|
Ingress: []config.UnvalidatedIngressRule{
|
|
|
|
{
|
|
|
|
Hostname: "*",
|
|
|
|
Service: service,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ingressRule, err := ingress.ParseIngress(ingressConfig)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return ingressRule
|
2021-02-04 18:03:34 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2021-02-11 14:36:42 +00:00
|
|
|
return
|
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 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-02-11 14:36:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
return
|
|
|
|
}
|
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
|
|
|
}()
|
|
|
|
}
|