2020-10-27 22:27:15 +00:00
|
|
|
package connection
|
|
|
|
|
|
|
|
import (
|
2021-07-16 15:14:37 +00:00
|
|
|
"context"
|
2020-10-27 22:27:15 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-10-19 19:01:17 +00:00
|
|
|
"math/rand"
|
2020-10-27 22:27:15 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
2021-07-16 15:14:37 +00:00
|
|
|
|
2022-04-06 23:20:29 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tracing"
|
2022-02-07 09:42:07 +00:00
|
|
|
tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
2021-10-19 19:01:17 +00:00
|
|
|
"github.com/cloudflare/cloudflared/websocket"
|
2020-10-27 22:27:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-02-07 09:42:07 +00:00
|
|
|
largeFileSize = 2 * 1024 * 1024
|
|
|
|
testGracePeriod = time.Millisecond * 100
|
2020-10-27 22:27:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-02-11 10:49:06 +00:00
|
|
|
testOrchestrator = &mockOrchestrator{
|
2022-02-07 09:42:07 +00:00
|
|
|
originProxy: &mockOriginProxy{},
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
log = zerolog.Nop()
|
2020-10-27 22:27:15 +00:00
|
|
|
testLargeResp = make([]byte, largeFileSize)
|
|
|
|
)
|
|
|
|
|
2022-07-26 21:00:53 +00:00
|
|
|
var _ ReadWriteAcker = (*HTTPResponseReadWriteAcker)(nil)
|
|
|
|
|
2020-10-27 22:27:15 +00:00
|
|
|
type testRequest struct {
|
|
|
|
name string
|
|
|
|
endpoint string
|
|
|
|
expectedStatus int
|
|
|
|
expectedBody []byte
|
|
|
|
isProxyError bool
|
|
|
|
}
|
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
type mockOrchestrator struct {
|
2022-02-07 09:42:07 +00:00
|
|
|
originProxy OriginProxy
|
|
|
|
}
|
|
|
|
|
2022-04-27 10:51:06 +00:00
|
|
|
func (mcr *mockOrchestrator) GetConfigJSON() ([]byte, error) {
|
|
|
|
return nil, fmt.Errorf("not implemented")
|
|
|
|
}
|
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
func (*mockOrchestrator) UpdateConfig(version int32, config []byte) *tunnelpogs.UpdateConfigurationResponse {
|
2022-02-07 09:42:07 +00:00
|
|
|
return &tunnelpogs.UpdateConfigurationResponse{
|
|
|
|
LastAppliedVersion: version,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
func (mcr *mockOrchestrator) GetOriginProxy() (OriginProxy, error) {
|
|
|
|
return mcr.originProxy, nil
|
2022-02-07 09:42:07 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 09:43:39 +00:00
|
|
|
func (mcr *mockOrchestrator) WarpRoutingEnabled() (enabled bool) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
type mockOriginProxy struct{}
|
2020-10-27 22:27:15 +00:00
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
func (moc *mockOriginProxy) ProxyHTTP(
|
|
|
|
w ResponseWriter,
|
2022-07-26 21:00:53 +00:00
|
|
|
tr *tracing.TracedHTTPRequest,
|
2021-07-16 15:14:37 +00:00
|
|
|
isWebsocket bool,
|
|
|
|
) error {
|
2022-04-06 23:20:29 +00:00
|
|
|
req := tr.Request
|
2021-07-16 15:14:37 +00:00
|
|
|
if isWebsocket {
|
2021-10-19 19:01:17 +00:00
|
|
|
switch req.URL.Path {
|
|
|
|
case "/ws/echo":
|
|
|
|
return wsEchoEndpoint(w, req)
|
|
|
|
case "/ws/flaky":
|
|
|
|
return wsFlakyEndpoint(w, req)
|
|
|
|
default:
|
|
|
|
originRespEndpoint(w, http.StatusNotFound, []byte("ws endpoint not found"))
|
|
|
|
return fmt.Errorf("Unknwon websocket endpoint %s", req.URL.Path)
|
|
|
|
}
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
2021-07-16 15:14:37 +00:00
|
|
|
switch req.URL.Path {
|
2020-10-27 22:27:15 +00:00
|
|
|
case "/ok":
|
|
|
|
originRespEndpoint(w, http.StatusOK, []byte(http.StatusText(http.StatusOK)))
|
|
|
|
case "/large_file":
|
|
|
|
originRespEndpoint(w, http.StatusOK, testLargeResp)
|
|
|
|
case "/400":
|
|
|
|
originRespEndpoint(w, http.StatusBadRequest, []byte(http.StatusText(http.StatusBadRequest)))
|
|
|
|
case "/500":
|
|
|
|
originRespEndpoint(w, http.StatusInternalServerError, []byte(http.StatusText(http.StatusInternalServerError)))
|
|
|
|
case "/error":
|
|
|
|
return fmt.Errorf("Failed to proxy to origin")
|
|
|
|
default:
|
|
|
|
originRespEndpoint(w, http.StatusNotFound, []byte("page not found"))
|
|
|
|
}
|
|
|
|
return nil
|
2021-07-16 15:14:37 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (moc *mockOriginProxy) ProxyTCP(
|
|
|
|
ctx context.Context,
|
|
|
|
rwa ReadWriteAcker,
|
|
|
|
r *TCPRequest,
|
|
|
|
) error {
|
|
|
|
return nil
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
type echoPipe struct {
|
|
|
|
reader *io.PipeReader
|
|
|
|
writer *io.PipeWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *echoPipe) Read(p []byte) (int, error) {
|
|
|
|
return ep.reader.Read(p)
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
func (ep *echoPipe) Write(p []byte) (int, error) {
|
|
|
|
return ep.writer.Write(p)
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
// A mock origin that echos data by streaming like a tcpOverWSConnection
|
|
|
|
// https://github.com/cloudflare/cloudflared/blob/master/ingress/origin_connection.go
|
|
|
|
func wsEchoEndpoint(w ResponseWriter, r *http.Request) error {
|
2020-10-27 22:27:15 +00:00
|
|
|
resp := &http.Response{
|
|
|
|
StatusCode: http.StatusSwitchingProtocols,
|
|
|
|
}
|
2021-10-19 19:01:17 +00:00
|
|
|
if err := w.WriteRespHeaders(resp.StatusCode, resp.Header); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
wsCtx, cancel := context.WithCancel(r.Context())
|
|
|
|
readPipe, writePipe := io.Pipe()
|
|
|
|
wsConn := websocket.NewConn(wsCtx, NewHTTPResponseReadWriterAcker(w, r), &log)
|
2020-10-27 22:27:15 +00:00
|
|
|
go func() {
|
2021-10-19 19:01:17 +00:00
|
|
|
select {
|
|
|
|
case <-wsCtx.Done():
|
|
|
|
case <-r.Context().Done():
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
2021-10-19 19:01:17 +00:00
|
|
|
readPipe.Close()
|
|
|
|
writePipe.Close()
|
2020-10-27 22:27:15 +00:00
|
|
|
}()
|
2021-10-19 19:01:17 +00:00
|
|
|
|
|
|
|
originConn := &echoPipe{reader: readPipe, writer: writePipe}
|
|
|
|
websocket.Stream(wsConn, originConn, &log)
|
|
|
|
cancel()
|
|
|
|
wsConn.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type flakyConn struct {
|
|
|
|
closeAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *flakyConn) Read(p []byte) (int, error) {
|
|
|
|
if time.Now().After(fc.closeAt) {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
n := copy(p, "Read from flaky connection")
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *flakyConn) Write(p []byte) (int, error) {
|
|
|
|
if time.Now().After(fc.closeAt) {
|
|
|
|
return 0, fmt.Errorf("flaky connection closed")
|
|
|
|
}
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func wsFlakyEndpoint(w ResponseWriter, r *http.Request) error {
|
|
|
|
resp := &http.Response{
|
|
|
|
StatusCode: http.StatusSwitchingProtocols,
|
|
|
|
}
|
|
|
|
if err := w.WriteRespHeaders(resp.StatusCode, resp.Header); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
wsCtx, cancel := context.WithCancel(r.Context())
|
|
|
|
|
|
|
|
wsConn := websocket.NewConn(wsCtx, NewHTTPResponseReadWriterAcker(w, r), &log)
|
|
|
|
|
|
|
|
closedAfter := time.Millisecond * time.Duration(rand.Intn(50))
|
|
|
|
originConn := &flakyConn{closeAt: time.Now().Add(closedAfter)}
|
|
|
|
websocket.Stream(wsConn, originConn, &log)
|
|
|
|
cancel()
|
|
|
|
wsConn.Close()
|
2020-10-27 22:27:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func originRespEndpoint(w ResponseWriter, status int, data []byte) {
|
|
|
|
resp := &http.Response{
|
|
|
|
StatusCode: status,
|
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
_ = w.WriteRespHeaders(resp.StatusCode, resp.Header)
|
2020-11-25 06:55:13 +00:00
|
|
|
_, _ = w.Write(data)
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type mockConnectedFuse struct{}
|
|
|
|
|
|
|
|
func (mcf mockConnectedFuse) Connected() {}
|
|
|
|
|
|
|
|
func (mcf mockConnectedFuse) IsConnected() bool {
|
|
|
|
return true
|
|
|
|
}
|