2020-12-09 21:46:53 +00:00
|
|
|
package ingress
|
|
|
|
|
|
|
|
import (
|
2021-02-08 19:25:08 +00:00
|
|
|
"context"
|
2021-02-05 13:01:53 +00:00
|
|
|
"fmt"
|
2021-10-22 08:27:29 +00:00
|
|
|
"io/ioutil"
|
2021-02-05 13:01:53 +00:00
|
|
|
"net"
|
2020-12-09 21:46:53 +00:00
|
|
|
"net/http"
|
2021-02-08 19:25:08 +00:00
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"sync"
|
2020-12-09 21:46:53 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-08 19:25:08 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-03-23 14:30:43 +00:00
|
|
|
|
2021-03-26 04:04:56 +00:00
|
|
|
"github.com/cloudflare/cloudflared/carrier"
|
2021-03-23 14:30:43 +00:00
|
|
|
"github.com/cloudflare/cloudflared/websocket"
|
2020-12-09 21:46:53 +00:00
|
|
|
)
|
|
|
|
|
2021-02-05 13:01:53 +00:00
|
|
|
func TestRawTCPServiceEstablishConnection(t *testing.T) {
|
|
|
|
originListener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
listenerClosed := make(chan struct{})
|
|
|
|
tcpListenRoutine(originListener, listenerClosed)
|
|
|
|
|
|
|
|
rawTCPService := &rawTCPService{name: ServiceWarpRouting}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s", originListener.Addr()), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
originListener.Close()
|
|
|
|
<-listenerClosed
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s", originListener.Addr()), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Origin not listening for new connection, should return an error
|
2021-07-01 18:30:26 +00:00
|
|
|
_, err = rawTCPService.EstablishConnection(req.URL.String())
|
2021-02-05 13:01:53 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTCPOverWSServiceEstablishConnection(t *testing.T) {
|
|
|
|
originListener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
listenerClosed := make(chan struct{})
|
|
|
|
tcpListenRoutine(originListener, listenerClosed)
|
|
|
|
|
|
|
|
originURL := &url.URL{
|
|
|
|
Scheme: "tcp",
|
|
|
|
Host: originListener.Addr().String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
baseReq, err := http.NewRequest(http.MethodGet, "https://place-holder", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
baseReq.Header.Set("Sec-Websocket-Key", "dGhlIHNhbXBsZSBub25jZQ==")
|
|
|
|
|
|
|
|
bastionReq := baseReq.Clone(context.Background())
|
2021-03-26 04:04:56 +00:00
|
|
|
carrier.SetBastionDest(bastionReq.Header, originListener.Addr().String())
|
2021-02-05 13:01:53 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
2021-03-26 04:04:56 +00:00
|
|
|
testCase string
|
2021-02-05 13:01:53 +00:00
|
|
|
service *tcpOverWSService
|
|
|
|
req *http.Request
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
2021-03-26 04:04:56 +00:00
|
|
|
testCase: "specific TCP service",
|
|
|
|
service: newTCPOverWSService(originURL),
|
|
|
|
req: baseReq,
|
2021-02-05 13:01:53 +00:00
|
|
|
},
|
|
|
|
{
|
2021-03-26 04:04:56 +00:00
|
|
|
testCase: "bastion service",
|
|
|
|
service: newBastionService(),
|
|
|
|
req: bastionReq,
|
2021-02-05 13:01:53 +00:00
|
|
|
},
|
|
|
|
{
|
2021-03-26 04:04:56 +00:00
|
|
|
testCase: "invalid bastion request",
|
2021-02-05 13:01:53 +00:00
|
|
|
service: newBastionService(),
|
|
|
|
req: baseReq,
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2021-03-26 04:04:56 +00:00
|
|
|
t.Run(test.testCase, func(t *testing.T) {
|
|
|
|
if test.expectErr {
|
2021-07-01 18:30:26 +00:00
|
|
|
bastionHost, _ := carrier.ResolveBastionDest(test.req)
|
|
|
|
_, err := test.service.EstablishConnection(bastionHost)
|
2021-03-26 04:04:56 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
})
|
2021-02-05 13:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
originListener.Close()
|
|
|
|
<-listenerClosed
|
|
|
|
|
|
|
|
for _, service := range []*tcpOverWSService{newTCPOverWSService(originURL), newBastionService()} {
|
|
|
|
// Origin not listening for new connection, should return an error
|
2021-07-01 18:30:26 +00:00
|
|
|
bastionHost, _ := carrier.ResolveBastionDest(bastionReq)
|
|
|
|
_, err := service.EstablishConnection(bastionHost)
|
2021-02-05 13:01:53 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 19:25:08 +00:00
|
|
|
func TestHTTPServiceHostHeaderOverride(t *testing.T) {
|
|
|
|
cfg := OriginRequestConfig{
|
|
|
|
HTTPHostHeader: t.Name(),
|
|
|
|
}
|
|
|
|
handler := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
require.Equal(t, r.Host, t.Name())
|
|
|
|
if websocket.IsWebSocketUpgrade(r) {
|
|
|
|
respHeaders := websocket.NewResponseHeader(r)
|
|
|
|
for k, v := range respHeaders {
|
|
|
|
w.Header().Set(k, v[0])
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusSwitchingProtocols)
|
|
|
|
return
|
|
|
|
}
|
2021-10-22 08:27:29 +00:00
|
|
|
// return the X-Forwarded-Host header for assertions
|
|
|
|
// as the httptest Server URL isn't available here yet
|
|
|
|
w.Write([]byte(r.Header.Get("X-Forwarded-Host")))
|
2021-02-08 19:25:08 +00:00
|
|
|
}
|
|
|
|
origin := httptest.NewServer(http.HandlerFunc(handler))
|
|
|
|
defer origin.Close()
|
|
|
|
|
|
|
|
originURL, err := url.Parse(origin.URL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
httpService := &httpService{
|
|
|
|
url: originURL,
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
shutdownC := make(chan struct{})
|
|
|
|
errC := make(chan error)
|
2021-02-05 13:01:53 +00:00
|
|
|
require.NoError(t, httpService.start(&wg, testLogger, shutdownC, errC, cfg))
|
2021-02-08 19:25:08 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, originURL.String(), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp, err := httpService.RoundTrip(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
2021-10-22 08:27:29 +00:00
|
|
|
respBody, err := ioutil.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, respBody, []byte(originURL.Host))
|
|
|
|
|
2021-02-08 19:25:08 +00:00
|
|
|
}
|
2021-02-05 13:01:53 +00:00
|
|
|
|
|
|
|
func tcpListenRoutine(listener net.Listener, closeChan chan struct{}) {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
close(closeChan)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Close immediately, this test is not about testing read/write on connection
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|