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"
|
2023-07-15 01:42:48 +00:00
|
|
|
"io"
|
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"
|
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"
|
2024-05-10 20:16:18 +00:00
|
|
|
"github.com/cloudflare/cloudflared/config"
|
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
|
2024-02-12 18:58:55 +00:00
|
|
|
_, err = rawTCPService.EstablishConnection(context.Background(), req.URL.String(), TestLogger)
|
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 {
|
2024-05-10 20:16:18 +00:00
|
|
|
testCase string
|
|
|
|
service *tcpOverWSService
|
|
|
|
req *http.Request
|
|
|
|
bastionMode bool
|
|
|
|
expectErr bool
|
2021-02-05 13:01:53 +00:00
|
|
|
}{
|
|
|
|
{
|
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
|
|
|
},
|
|
|
|
{
|
2024-05-10 20:16:18 +00:00
|
|
|
testCase: "bastion service",
|
|
|
|
service: newBastionService(),
|
|
|
|
bastionMode: true,
|
|
|
|
req: bastionReq,
|
2021-02-05 13:01:53 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-10 20:16:18 +00:00
|
|
|
testCase: "invalid bastion request",
|
|
|
|
service: newBastionService(),
|
|
|
|
bastionMode: true,
|
|
|
|
req: baseReq,
|
|
|
|
expectErr: true,
|
2021-02-05 13:01:53 +00:00
|
|
|
},
|
2024-05-09 19:07:59 +00:00
|
|
|
{
|
2024-05-10 20:16:18 +00:00
|
|
|
testCase: "bastion service",
|
|
|
|
service: newBastionServiceWithDest(MustParseURL(t, "https://place-holder1")),
|
|
|
|
req: bastionReq,
|
|
|
|
bastionMode: true,
|
2024-05-09 19:07:59 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-10 20:16:18 +00:00
|
|
|
testCase: "bastion service",
|
|
|
|
service: newBastionServiceWithDest(MustParseURL(t, "https://place-holder1")),
|
|
|
|
req: baseReq,
|
|
|
|
bastionMode: true,
|
|
|
|
expectErr: true,
|
2024-05-09 19:07:59 +00:00
|
|
|
},
|
2021-02-05 13:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2021-03-26 04:04:56 +00:00
|
|
|
t.Run(test.testCase, func(t *testing.T) {
|
|
|
|
if test.expectErr {
|
2024-05-10 20:16:18 +00:00
|
|
|
bastionHost, _ := carrier.ResolveBastionDest(test.req, test.bastionMode, config.BastionFlag)
|
2024-02-12 18:58:55 +00:00
|
|
|
_, err := test.service.EstablishConnection(context.Background(), bastionHost, TestLogger)
|
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
|
2024-05-09 19:07:59 +00:00
|
|
|
bastionHost, _ := carrier.ResolveBastionDest(bastionReq, false, "bastion")
|
2024-02-12 18:58:55 +00:00
|
|
|
_, err := service.EstablishConnection(context.Background(), bastionHost, TestLogger)
|
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,
|
|
|
|
}
|
|
|
|
shutdownC := make(chan struct{})
|
2024-02-12 18:58:55 +00:00
|
|
|
require.NoError(t, httpService.start(TestLogger, shutdownC, 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)
|
|
|
|
|
2023-07-15 01:42:48 +00:00
|
|
|
respBody, err := io.ReadAll(resp.Body)
|
2021-10-22 08:27:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, respBody, []byte(originURL.Host))
|
2022-02-04 16:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestHTTPServiceUsesIngressRuleScheme makes sure httpService uses scheme defined in ingress rule and not by eyeball request
|
|
|
|
func TestHTTPServiceUsesIngressRuleScheme(t *testing.T) {
|
|
|
|
handler := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
require.NotNil(t, r.TLS)
|
|
|
|
// Echo the X-Forwarded-Proto header for assertions
|
|
|
|
w.Write([]byte(r.Header.Get("X-Forwarded-Proto")))
|
|
|
|
}
|
|
|
|
origin := httptest.NewTLSServer(http.HandlerFunc(handler))
|
|
|
|
defer origin.Close()
|
2021-10-22 08:27:29 +00:00
|
|
|
|
2022-02-04 16:51:37 +00:00
|
|
|
originURL, err := url.Parse(origin.URL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "https", originURL.Scheme)
|
|
|
|
|
|
|
|
cfg := OriginRequestConfig{
|
|
|
|
NoTLSVerify: true,
|
|
|
|
}
|
|
|
|
httpService := &httpService{
|
|
|
|
url: originURL,
|
|
|
|
}
|
|
|
|
shutdownC := make(chan struct{})
|
2024-02-12 18:58:55 +00:00
|
|
|
require.NoError(t, httpService.start(TestLogger, shutdownC, cfg))
|
2022-02-04 16:51:37 +00:00
|
|
|
|
|
|
|
// Tunnel uses scheme defined in the service field of the ingress rule, independent of the X-Forwarded-Proto header
|
|
|
|
protos := []string{"https", "http", "dne"}
|
|
|
|
for _, p := range protos {
|
|
|
|
req, err := http.NewRequest(http.MethodGet, originURL.String(), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Add("X-Forwarded-Proto", p)
|
|
|
|
|
|
|
|
resp, err := httpService.RoundTrip(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
2023-07-15 01:42:48 +00:00
|
|
|
respBody, err := io.ReadAll(resp.Body)
|
2022-02-04 16:51:37 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, respBody, []byte(p))
|
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|