2021-08-03 09:04:02 +00:00
|
|
|
package connection
|
|
|
|
|
|
|
|
import (
|
2021-08-03 07:09:56 +00:00
|
|
|
"bytes"
|
2021-08-03 09:04:02 +00:00
|
|
|
"context"
|
2024-05-14 04:22:06 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
2021-08-03 09:04:02 +00:00
|
|
|
"crypto/tls"
|
2024-05-14 04:22:06 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2021-08-03 07:09:56 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-05-14 04:22:06 +00:00
|
|
|
"math/big"
|
2021-08-03 09:04:02 +00:00
|
|
|
"net"
|
2021-08-03 07:09:56 +00:00
|
|
|
"net/http"
|
2024-10-18 21:38:05 +00:00
|
|
|
"net/netip"
|
2021-09-27 13:12:11 +00:00
|
|
|
"net/url"
|
2021-08-03 09:04:02 +00:00
|
|
|
"os"
|
2022-08-22 22:48:45 +00:00
|
|
|
"strings"
|
2021-08-03 09:04:02 +00:00
|
|
|
"testing"
|
2021-12-14 22:52:47 +00:00
|
|
|
"time"
|
2021-08-03 09:04:02 +00:00
|
|
|
|
2021-08-03 07:09:56 +00:00
|
|
|
"github.com/gobwas/ws/wsutil"
|
2021-12-14 22:52:47 +00:00
|
|
|
"github.com/google/uuid"
|
2021-08-03 07:09:56 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-05-06 00:42:41 +00:00
|
|
|
"github.com/quic-go/quic-go"
|
2021-08-03 09:04:02 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-10-18 21:38:05 +00:00
|
|
|
"golang.org/x/net/nettest"
|
2021-08-03 09:04:02 +00:00
|
|
|
|
2021-12-14 22:52:47 +00:00
|
|
|
"github.com/cloudflare/cloudflared/datagramsession"
|
2024-05-14 04:22:06 +00:00
|
|
|
cfdquic "github.com/cloudflare/cloudflared/quic"
|
2022-04-06 23:20:29 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tracing"
|
2022-09-09 04:42:11 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
2021-09-21 06:11:36 +00:00
|
|
|
tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
2024-05-14 04:22:06 +00:00
|
|
|
rpcquic "github.com/cloudflare/cloudflared/tunnelrpc/quic"
|
2021-08-03 09:04:02 +00:00
|
|
|
)
|
|
|
|
|
2021-12-14 22:52:47 +00:00
|
|
|
var (
|
2024-05-14 04:22:06 +00:00
|
|
|
testTLSServerConfig = GenerateTLSConfig()
|
2021-12-14 22:52:47 +00:00
|
|
|
testQUICConfig = &quic.Config{
|
2023-05-06 00:42:41 +00:00
|
|
|
KeepAlivePeriod: 5 * time.Second,
|
|
|
|
EnableDatagrams: true,
|
2021-08-03 09:04:02 +00:00
|
|
|
}
|
2024-02-12 18:58:55 +00:00
|
|
|
defaultQUICTimeout = 30 * time.Second
|
2021-12-14 22:52:47 +00:00
|
|
|
)
|
2021-08-03 09:04:02 +00:00
|
|
|
|
2022-07-26 21:00:53 +00:00
|
|
|
var _ ReadWriteAcker = (*streamReadWriteAcker)(nil)
|
|
|
|
|
2021-12-14 22:52:47 +00:00
|
|
|
// TestQUICServer tests if a quic server accepts and responds to a quic client with the acceptance protocol.
|
|
|
|
// It also serves as a demonstration for communication with the QUIC connection started by a cloudflared.
|
|
|
|
func TestQUICServer(t *testing.T) {
|
2021-08-03 07:09:56 +00:00
|
|
|
// This is simply a sample websocket frame message.
|
|
|
|
wsBuf := &bytes.Buffer{}
|
2021-10-19 19:01:17 +00:00
|
|
|
wsutil.WriteClientBinary(wsBuf, []byte("Hello"))
|
2021-08-03 07:09:56 +00:00
|
|
|
|
2021-08-03 09:04:02 +00:00
|
|
|
var tests = []struct {
|
2021-08-03 07:09:56 +00:00
|
|
|
desc string
|
|
|
|
dest string
|
2024-05-14 04:22:06 +00:00
|
|
|
connectionType pogs.ConnectionType
|
|
|
|
metadata []pogs.Metadata
|
2021-08-03 07:09:56 +00:00
|
|
|
message []byte
|
|
|
|
expectedResponse []byte
|
2021-08-03 09:04:02 +00:00
|
|
|
}{
|
|
|
|
{
|
2021-08-03 07:09:56 +00:00
|
|
|
desc: "test http proxy",
|
|
|
|
dest: "/ok",
|
2024-05-14 04:22:06 +00:00
|
|
|
connectionType: pogs.ConnectionTypeHTTP,
|
|
|
|
metadata: []pogs.Metadata{
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-08-03 07:09:56 +00:00
|
|
|
Key: "HttpHeader:Cf-Ray",
|
|
|
|
Val: "123123123",
|
|
|
|
},
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-08-03 07:09:56 +00:00
|
|
|
Key: "HttpHost",
|
|
|
|
Val: "cf.host",
|
|
|
|
},
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-08-03 07:09:56 +00:00
|
|
|
Key: "HttpMethod",
|
|
|
|
Val: "GET",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedResponse: []byte("OK"),
|
|
|
|
},
|
2022-08-11 20:31:36 +00:00
|
|
|
{
|
|
|
|
desc: "test http body request streaming",
|
|
|
|
dest: "/slow_echo_body",
|
2024-05-14 04:22:06 +00:00
|
|
|
connectionType: pogs.ConnectionTypeHTTP,
|
|
|
|
metadata: []pogs.Metadata{
|
2022-08-11 20:31:36 +00:00
|
|
|
{
|
|
|
|
Key: "HttpHeader:Cf-Ray",
|
|
|
|
Val: "123123123",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "HttpHost",
|
|
|
|
Val: "cf.host",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "HttpMethod",
|
|
|
|
Val: "POST",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "HttpHeader:Content-Length",
|
|
|
|
Val: "24",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
message: []byte("This is the message body"),
|
|
|
|
expectedResponse: []byte("This is the message body"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "test ws proxy",
|
|
|
|
dest: "/ws/echo",
|
2024-05-14 04:22:06 +00:00
|
|
|
connectionType: pogs.ConnectionTypeWebsocket,
|
|
|
|
metadata: []pogs.Metadata{
|
2022-08-11 20:31:36 +00:00
|
|
|
{
|
|
|
|
Key: "HttpHeader:Cf-Cloudflared-Proxy-Connection-Upgrade",
|
|
|
|
Val: "Websocket",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "HttpHeader:Another-Header",
|
|
|
|
Val: "Misc",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "HttpHost",
|
|
|
|
Val: "cf.host",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "HttpMethod",
|
|
|
|
Val: "get",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
message: wsBuf.Bytes(),
|
|
|
|
expectedResponse: []byte{0x82, 0x5, 0x48, 0x65, 0x6c, 0x6c, 0x6f},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "test tcp proxy",
|
2024-05-14 04:22:06 +00:00
|
|
|
connectionType: pogs.ConnectionTypeTCP,
|
|
|
|
metadata: []pogs.Metadata{},
|
2022-08-11 20:31:36 +00:00
|
|
|
message: []byte("Here is some tcp data"),
|
|
|
|
expectedResponse: []byte("Here is some tcp data"),
|
|
|
|
},
|
2021-08-03 09:04:02 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 08:31:16 +00:00
|
|
|
for i, test := range tests {
|
|
|
|
test := test // capture range variable
|
2021-08-03 09:04:02 +00:00
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2023-05-06 00:42:41 +00:00
|
|
|
// Start a UDP Listener for QUIC.
|
|
|
|
udpAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
udpListener, err := net.ListenUDP(udpAddr.Network(), udpAddr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer udpListener.Close()
|
|
|
|
quicTransport := &quic.Transport{Conn: udpListener, ConnectionIDLength: 16}
|
|
|
|
quicListener, err := quicTransport.Listen(testTLSServerConfig, testQUICConfig)
|
2022-10-18 08:31:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
serverDone := make(chan struct{})
|
2021-08-03 09:04:02 +00:00
|
|
|
go func() {
|
|
|
|
quicServer(
|
2022-10-18 08:31:16 +00:00
|
|
|
ctx, t, quicListener, test.dest, test.connectionType, test.metadata, test.message, test.expectedResponse,
|
2021-08-03 09:04:02 +00:00
|
|
|
)
|
2022-10-18 08:31:16 +00:00
|
|
|
close(serverDone)
|
2021-08-03 09:04:02 +00:00
|
|
|
}()
|
|
|
|
|
2024-10-18 21:38:05 +00:00
|
|
|
qc := testQUICConnection(netip.MustParseAddrPort(udpListener.LocalAddr().String()), t, uint8(i))
|
2022-10-18 08:31:16 +00:00
|
|
|
|
|
|
|
connDone := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
qc.Serve(ctx)
|
|
|
|
close(connDone)
|
|
|
|
}()
|
2021-08-03 09:04:02 +00:00
|
|
|
|
2022-10-18 08:31:16 +00:00
|
|
|
<-serverDone
|
2021-08-03 07:09:56 +00:00
|
|
|
cancel()
|
2022-10-18 08:31:16 +00:00
|
|
|
<-connDone
|
2021-08-03 09:04:02 +00:00
|
|
|
})
|
|
|
|
}
|
2021-09-21 06:11:36 +00:00
|
|
|
}
|
2021-08-03 09:04:02 +00:00
|
|
|
|
2021-09-21 06:11:36 +00:00
|
|
|
type fakeControlStream struct {
|
|
|
|
ControlStreamHandler
|
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func (fakeControlStream) ServeControlStream(ctx context.Context, rw io.ReadWriteCloser, connOptions *pogs.ConnectionOptions, tunnelConfigGetter TunnelConfigJSONGetter) error {
|
2022-01-05 16:01:56 +00:00
|
|
|
<-ctx.Done()
|
2021-09-21 06:11:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (fakeControlStream) IsStopped() bool {
|
|
|
|
return true
|
2021-08-03 09:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func quicServer(
|
2022-10-18 08:31:16 +00:00
|
|
|
ctx context.Context,
|
2021-08-03 09:04:02 +00:00
|
|
|
t *testing.T,
|
2023-05-06 00:42:41 +00:00
|
|
|
listener *quic.Listener,
|
2021-08-03 09:04:02 +00:00
|
|
|
dest string,
|
2024-05-14 04:22:06 +00:00
|
|
|
connectionType pogs.ConnectionType,
|
|
|
|
metadata []pogs.Metadata,
|
2021-08-03 09:04:02 +00:00
|
|
|
message []byte,
|
|
|
|
expectedResponse []byte,
|
|
|
|
) {
|
2022-10-18 08:31:16 +00:00
|
|
|
session, err := listener.Accept(ctx)
|
2021-08-03 09:04:02 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
TUN-5621: Correctly manage QUIC stream closing
Until this PR, we were naively closing the quic.Stream whenever
the callstack for handling the request (HTTP or TCP) finished.
However, our proxy handler may still be reading or writing from
the quic.Stream at that point, because we return the callstack if
either side finishes, but not necessarily both.
This is a problem for quic-go library because quic.Stream#Close
cannot be called concurrently with quic.Stream#Write
Furthermore, we also noticed that quic.Stream#Close does nothing
to do receiving stream (since, underneath, quic.Stream has 2 streams,
1 for each direction), thus leaking memory, as explained in:
https://github.com/lucas-clemente/quic-go/issues/3322
This PR addresses both problems by wrapping the quic.Stream that
is passed down to the proxying logic and handle all these concerns.
2022-01-27 22:37:45 +00:00
|
|
|
quicStream, err := session.OpenStreamSync(context.Background())
|
2021-08-03 09:04:02 +00:00
|
|
|
require.NoError(t, err)
|
2024-05-14 04:22:06 +00:00
|
|
|
stream := cfdquic.NewSafeStreamCloser(quicStream, defaultQUICTimeout, &log)
|
2021-08-03 09:04:02 +00:00
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
reqClientStream := rpcquic.RequestClientStream{ReadWriteCloser: stream}
|
2021-11-12 09:37:28 +00:00
|
|
|
err = reqClientStream.WriteConnectRequestData(dest, connectionType, metadata...)
|
2021-08-03 09:04:02 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-12 09:37:28 +00:00
|
|
|
_, err = reqClientStream.ReadConnectResponseData()
|
2021-08-03 09:04:02 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if message != nil {
|
|
|
|
// ALPN successful. Write data.
|
TUN-5621: Correctly manage QUIC stream closing
Until this PR, we were naively closing the quic.Stream whenever
the callstack for handling the request (HTTP or TCP) finished.
However, our proxy handler may still be reading or writing from
the quic.Stream at that point, because we return the callstack if
either side finishes, but not necessarily both.
This is a problem for quic-go library because quic.Stream#Close
cannot be called concurrently with quic.Stream#Write
Furthermore, we also noticed that quic.Stream#Close does nothing
to do receiving stream (since, underneath, quic.Stream has 2 streams,
1 for each direction), thus leaking memory, as explained in:
https://github.com/lucas-clemente/quic-go/issues/3322
This PR addresses both problems by wrapping the quic.Stream that
is passed down to the proxying logic and handle all these concerns.
2022-01-27 22:37:45 +00:00
|
|
|
_, err := stream.Write(message)
|
2021-08-03 09:04:02 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-08-03 07:09:56 +00:00
|
|
|
response := make([]byte, len(expectedResponse))
|
TUN-5621: Correctly manage QUIC stream closing
Until this PR, we were naively closing the quic.Stream whenever
the callstack for handling the request (HTTP or TCP) finished.
However, our proxy handler may still be reading or writing from
the quic.Stream at that point, because we return the callstack if
either side finishes, but not necessarily both.
This is a problem for quic-go library because quic.Stream#Close
cannot be called concurrently with quic.Stream#Write
Furthermore, we also noticed that quic.Stream#Close does nothing
to do receiving stream (since, underneath, quic.Stream has 2 streams,
1 for each direction), thus leaking memory, as explained in:
https://github.com/lucas-clemente/quic-go/issues/3322
This PR addresses both problems by wrapping the quic.Stream that
is passed down to the proxying logic and handle all these concerns.
2022-01-27 22:37:45 +00:00
|
|
|
_, err = stream.Read(response)
|
|
|
|
if err != io.EOF {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2021-08-03 09:04:02 +00:00
|
|
|
|
|
|
|
// For now it is an echo server. Verify if the same data is returned.
|
|
|
|
assert.Equal(t, expectedResponse, response)
|
|
|
|
}
|
|
|
|
|
2021-08-03 07:09:56 +00:00
|
|
|
type mockOriginProxyWithRequest struct{}
|
|
|
|
|
2022-07-26 21:00:53 +00:00
|
|
|
func (moc *mockOriginProxyWithRequest) ProxyHTTP(w ResponseWriter, tr *tracing.TracedHTTPRequest, isWebsocket bool) error {
|
2021-08-03 07:09:56 +00:00
|
|
|
// These are a series of crude tests to ensure the headers and http related data is transferred from
|
|
|
|
// metadata.
|
2022-04-06 23:20:29 +00:00
|
|
|
r := tr.Request
|
2021-08-03 07:09:56 +00:00
|
|
|
if r.Method == "" {
|
|
|
|
return errors.New("method not sent")
|
|
|
|
}
|
|
|
|
if r.Host == "" {
|
|
|
|
return errors.New("host not sent")
|
|
|
|
}
|
|
|
|
if len(r.Header) == 0 {
|
|
|
|
return errors.New("headers not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if isWebsocket {
|
2021-10-19 19:01:17 +00:00
|
|
|
return wsEchoEndpoint(w, r)
|
2021-08-03 07:09:56 +00:00
|
|
|
}
|
|
|
|
switch r.URL.Path {
|
|
|
|
case "/ok":
|
|
|
|
originRespEndpoint(w, http.StatusOK, []byte(http.StatusText(http.StatusOK)))
|
TUN-5621: Correctly manage QUIC stream closing
Until this PR, we were naively closing the quic.Stream whenever
the callstack for handling the request (HTTP or TCP) finished.
However, our proxy handler may still be reading or writing from
the quic.Stream at that point, because we return the callstack if
either side finishes, but not necessarily both.
This is a problem for quic-go library because quic.Stream#Close
cannot be called concurrently with quic.Stream#Write
Furthermore, we also noticed that quic.Stream#Close does nothing
to do receiving stream (since, underneath, quic.Stream has 2 streams,
1 for each direction), thus leaking memory, as explained in:
https://github.com/lucas-clemente/quic-go/issues/3322
This PR addresses both problems by wrapping the quic.Stream that
is passed down to the proxying logic and handle all these concerns.
2022-01-27 22:37:45 +00:00
|
|
|
case "/slow_echo_body":
|
|
|
|
time.Sleep(5)
|
|
|
|
fallthrough
|
2021-08-03 07:09:56 +00:00
|
|
|
case "/echo_body":
|
|
|
|
resp := &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
}
|
|
|
|
_ = w.WriteRespHeaders(resp.StatusCode, resp.Header)
|
|
|
|
io.Copy(w, r.Body)
|
|
|
|
case "/error":
|
|
|
|
return fmt.Errorf("Failed to proxy to origin")
|
|
|
|
default:
|
|
|
|
originRespEndpoint(w, http.StatusNotFound, []byte("page not found"))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-27 13:12:11 +00:00
|
|
|
func TestBuildHTTPRequest(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
name string
|
2024-05-14 04:22:06 +00:00
|
|
|
connectRequest *pogs.ConnectRequest
|
2021-10-07 14:47:27 +00:00
|
|
|
body io.ReadCloser
|
2021-09-27 13:12:11 +00:00
|
|
|
req *http.Request
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "check if http.Request is built correctly with content length",
|
2024-05-14 04:22:06 +00:00
|
|
|
connectRequest: &pogs.ConnectRequest{
|
2021-09-27 13:12:11 +00:00
|
|
|
Dest: "http://test.com",
|
2024-05-14 04:22:06 +00:00
|
|
|
Metadata: []pogs.Metadata{
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-09-27 13:12:11 +00:00
|
|
|
Key: "HttpHeader:Cf-Cloudflared-Proxy-Connection-Upgrade",
|
|
|
|
Val: "Websocket",
|
|
|
|
},
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-09-27 13:12:11 +00:00
|
|
|
Key: "HttpHeader:Content-Length",
|
|
|
|
Val: "514",
|
|
|
|
},
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-09-27 13:12:11 +00:00
|
|
|
Key: "HttpHeader:Another-Header",
|
|
|
|
Val: "Misc",
|
|
|
|
},
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-09-27 13:12:11 +00:00
|
|
|
Key: "HttpHost",
|
|
|
|
Val: "cf.host",
|
|
|
|
},
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-09-27 13:12:11 +00:00
|
|
|
Key: "HttpMethod",
|
|
|
|
Val: "get",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
req: &http.Request{
|
|
|
|
Method: "get",
|
|
|
|
URL: &url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Host: "test.com",
|
|
|
|
},
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
Header: http.Header{
|
|
|
|
"Another-Header": []string{"Misc"},
|
|
|
|
"Content-Length": []string{"514"},
|
|
|
|
},
|
|
|
|
ContentLength: 514,
|
|
|
|
Host: "cf.host",
|
2021-10-07 14:47:27 +00:00
|
|
|
Body: io.NopCloser(&bytes.Buffer{}),
|
2021-09-27 13:12:11 +00:00
|
|
|
},
|
2021-10-07 14:47:27 +00:00
|
|
|
body: io.NopCloser(&bytes.Buffer{}),
|
2021-09-27 13:12:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "if content length isn't part of request headers, then it's not set",
|
2024-05-14 04:22:06 +00:00
|
|
|
connectRequest: &pogs.ConnectRequest{
|
2021-09-27 13:12:11 +00:00
|
|
|
Dest: "http://test.com",
|
2024-05-14 04:22:06 +00:00
|
|
|
Metadata: []pogs.Metadata{
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-09-27 13:12:11 +00:00
|
|
|
Key: "HttpHeader:Cf-Cloudflared-Proxy-Connection-Upgrade",
|
|
|
|
Val: "Websocket",
|
|
|
|
},
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-09-27 13:12:11 +00:00
|
|
|
Key: "HttpHeader:Another-Header",
|
|
|
|
Val: "Misc",
|
|
|
|
},
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-09-27 13:12:11 +00:00
|
|
|
Key: "HttpHost",
|
|
|
|
Val: "cf.host",
|
|
|
|
},
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-09-27 13:12:11 +00:00
|
|
|
Key: "HttpMethod",
|
|
|
|
Val: "get",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
req: &http.Request{
|
|
|
|
Method: "get",
|
|
|
|
URL: &url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Host: "test.com",
|
|
|
|
},
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
Header: http.Header{
|
|
|
|
"Another-Header": []string{"Misc"},
|
|
|
|
},
|
|
|
|
ContentLength: 0,
|
|
|
|
Host: "cf.host",
|
2022-03-05 18:05:07 +00:00
|
|
|
Body: http.NoBody,
|
2021-10-07 14:47:27 +00:00
|
|
|
},
|
|
|
|
body: io.NopCloser(&bytes.Buffer{}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "if content length is 0, but transfer-encoding is chunked, body is not nil",
|
2024-05-14 04:22:06 +00:00
|
|
|
connectRequest: &pogs.ConnectRequest{
|
2021-10-07 14:47:27 +00:00
|
|
|
Dest: "http://test.com",
|
2024-05-14 04:22:06 +00:00
|
|
|
Metadata: []pogs.Metadata{
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-10-07 14:47:27 +00:00
|
|
|
Key: "HttpHeader:Another-Header",
|
|
|
|
Val: "Misc",
|
|
|
|
},
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-10-07 14:47:27 +00:00
|
|
|
Key: "HttpHeader:Transfer-Encoding",
|
|
|
|
Val: "chunked",
|
|
|
|
},
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-10-07 14:47:27 +00:00
|
|
|
Key: "HttpHost",
|
|
|
|
Val: "cf.host",
|
|
|
|
},
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-10-07 14:47:27 +00:00
|
|
|
Key: "HttpMethod",
|
|
|
|
Val: "get",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
req: &http.Request{
|
|
|
|
Method: "get",
|
|
|
|
URL: &url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Host: "test.com",
|
|
|
|
},
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
Header: http.Header{
|
|
|
|
"Another-Header": []string{"Misc"},
|
|
|
|
"Transfer-Encoding": []string{"chunked"},
|
|
|
|
},
|
|
|
|
ContentLength: 0,
|
|
|
|
Host: "cf.host",
|
|
|
|
Body: io.NopCloser(&bytes.Buffer{}),
|
|
|
|
},
|
|
|
|
body: io.NopCloser(&bytes.Buffer{}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "if content length is 0, but transfer-encoding is gzip,chunked, body is not nil",
|
2024-05-14 04:22:06 +00:00
|
|
|
connectRequest: &pogs.ConnectRequest{
|
2021-10-07 14:47:27 +00:00
|
|
|
Dest: "http://test.com",
|
2024-05-14 04:22:06 +00:00
|
|
|
Metadata: []pogs.Metadata{
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-10-07 14:47:27 +00:00
|
|
|
Key: "HttpHeader:Another-Header",
|
|
|
|
Val: "Misc",
|
|
|
|
},
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-10-07 14:47:27 +00:00
|
|
|
Key: "HttpHeader:Transfer-Encoding",
|
|
|
|
Val: "gzip,chunked",
|
|
|
|
},
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-10-07 14:47:27 +00:00
|
|
|
Key: "HttpHost",
|
|
|
|
Val: "cf.host",
|
|
|
|
},
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-10-07 14:47:27 +00:00
|
|
|
Key: "HttpMethod",
|
|
|
|
Val: "get",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
req: &http.Request{
|
|
|
|
Method: "get",
|
|
|
|
URL: &url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Host: "test.com",
|
|
|
|
},
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
Header: http.Header{
|
|
|
|
"Another-Header": []string{"Misc"},
|
|
|
|
"Transfer-Encoding": []string{"gzip,chunked"},
|
|
|
|
},
|
|
|
|
ContentLength: 0,
|
|
|
|
Host: "cf.host",
|
|
|
|
Body: io.NopCloser(&bytes.Buffer{}),
|
|
|
|
},
|
|
|
|
body: io.NopCloser(&bytes.Buffer{}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "if content length is 0, and connect request is a websocket, body is not nil",
|
2024-05-14 04:22:06 +00:00
|
|
|
connectRequest: &pogs.ConnectRequest{
|
|
|
|
Type: pogs.ConnectionTypeWebsocket,
|
2021-10-07 14:47:27 +00:00
|
|
|
Dest: "http://test.com",
|
2024-05-14 04:22:06 +00:00
|
|
|
Metadata: []pogs.Metadata{
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-10-07 14:47:27 +00:00
|
|
|
Key: "HttpHeader:Another-Header",
|
|
|
|
Val: "Misc",
|
|
|
|
},
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-10-07 14:47:27 +00:00
|
|
|
Key: "HttpHost",
|
|
|
|
Val: "cf.host",
|
|
|
|
},
|
2021-11-12 09:37:28 +00:00
|
|
|
{
|
2021-10-07 14:47:27 +00:00
|
|
|
Key: "HttpMethod",
|
|
|
|
Val: "get",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
req: &http.Request{
|
|
|
|
Method: "get",
|
|
|
|
URL: &url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Host: "test.com",
|
|
|
|
},
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
Header: http.Header{
|
|
|
|
"Another-Header": []string{"Misc"},
|
|
|
|
},
|
|
|
|
ContentLength: 0,
|
|
|
|
Host: "cf.host",
|
|
|
|
Body: io.NopCloser(&bytes.Buffer{}),
|
2021-09-27 13:12:11 +00:00
|
|
|
},
|
2024-08-09 19:43:35 +00:00
|
|
|
body: io.NopCloser(&bytes.Buffer{}),
|
|
|
|
},
|
2021-09-27 13:12:11 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 21:00:53 +00:00
|
|
|
log := zerolog.Nop()
|
2021-09-27 13:12:11 +00:00
|
|
|
for _, test := range tests {
|
2022-10-18 08:31:16 +00:00
|
|
|
test := test // capture range variable
|
2021-09-27 13:12:11 +00:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2023-02-22 14:52:44 +00:00
|
|
|
req, err := buildHTTPRequest(context.Background(), test.connectRequest, test.body, 0, &log)
|
2021-09-27 13:12:11 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
test.req = test.req.WithContext(req.Context())
|
2022-04-06 23:20:29 +00:00
|
|
|
assert.Equal(t, test.req, req.Request)
|
2021-09-27 13:12:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 07:09:56 +00:00
|
|
|
func (moc *mockOriginProxyWithRequest) ProxyTCP(ctx context.Context, rwa ReadWriteAcker, tcpRequest *TCPRequest) error {
|
2022-07-26 21:00:53 +00:00
|
|
|
rwa.AckConnection("")
|
2021-08-17 14:30:02 +00:00
|
|
|
io.Copy(rwa, rwa)
|
2021-08-03 07:09:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-12-14 22:52:47 +00:00
|
|
|
|
|
|
|
func TestServeUDPSession(t *testing.T) {
|
|
|
|
// Start a UDP Listener for QUIC.
|
|
|
|
udpAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
udpListener, err := net.ListenUDP(udpAddr.Network(), udpAddr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer udpListener.Close()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
// Establish QUIC connection with edge
|
2022-06-06 13:15:35 +00:00
|
|
|
edgeQUICSessionChan := make(chan quic.Connection)
|
2021-12-14 22:52:47 +00:00
|
|
|
go func() {
|
|
|
|
earlyListener, err := quic.Listen(udpListener, testTLSServerConfig, testQUICConfig)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
edgeQUICSession, err := earlyListener.Accept(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
edgeQUICSessionChan <- edgeQUICSession
|
|
|
|
}()
|
|
|
|
|
2022-10-18 08:31:16 +00:00
|
|
|
// Random index to avoid reusing port
|
2024-10-18 21:38:05 +00:00
|
|
|
qc := testQUICConnection(netip.MustParseAddrPort(udpListener.LocalAddr().String()), t, 28)
|
2021-12-14 22:52:47 +00:00
|
|
|
go qc.Serve(ctx)
|
|
|
|
|
|
|
|
edgeQUICSession := <-edgeQUICSessionChan
|
|
|
|
serveSession(ctx, qc, edgeQUICSession, closedByOrigin, io.EOF.Error(), t)
|
|
|
|
serveSession(ctx, qc, edgeQUICSession, closedByTimeout, datagramsession.SessionIdleErr(time.Millisecond*50).Error(), t)
|
|
|
|
serveSession(ctx, qc, edgeQUICSession, closedByRemote, "eyeball closed connection", t)
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
|
2022-08-22 22:48:45 +00:00
|
|
|
func TestNopCloserReadWriterCloseBeforeEOF(t *testing.T) {
|
|
|
|
readerWriter := nopCloserReadWriter{ReadWriteCloser: &mockReaderNoopWriter{Reader: strings.NewReader("123456789")}}
|
|
|
|
buffer := make([]byte, 5)
|
|
|
|
|
|
|
|
n, err := readerWriter.Read(buffer)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, n, 5)
|
|
|
|
|
|
|
|
// close
|
|
|
|
require.NoError(t, readerWriter.Close())
|
|
|
|
|
|
|
|
// read should get error
|
|
|
|
n, err = readerWriter.Read(buffer)
|
|
|
|
require.Equal(t, n, 0)
|
|
|
|
require.Equal(t, err, fmt.Errorf("closed by handler"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNopCloserReadWriterCloseAfterEOF(t *testing.T) {
|
|
|
|
readerWriter := nopCloserReadWriter{ReadWriteCloser: &mockReaderNoopWriter{Reader: strings.NewReader("123456789")}}
|
|
|
|
buffer := make([]byte, 20)
|
|
|
|
|
|
|
|
n, err := readerWriter.Read(buffer)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, n, 9)
|
|
|
|
|
|
|
|
// force another read to read eof
|
2022-09-09 04:42:11 +00:00
|
|
|
_, err = readerWriter.Read(buffer)
|
2022-08-22 22:48:45 +00:00
|
|
|
require.Equal(t, err, io.EOF)
|
|
|
|
|
|
|
|
// close
|
|
|
|
require.NoError(t, readerWriter.Close())
|
|
|
|
|
|
|
|
// read should get EOF still
|
|
|
|
n, err = readerWriter.Read(buffer)
|
|
|
|
require.Equal(t, n, 0)
|
|
|
|
require.Equal(t, err, io.EOF)
|
|
|
|
}
|
|
|
|
|
2022-10-12 16:01:25 +00:00
|
|
|
func TestCreateUDPConnReuseSourcePort(t *testing.T) {
|
2024-10-18 21:38:05 +00:00
|
|
|
edgeIPv4 := netip.MustParseAddrPort("0.0.0.0:0")
|
|
|
|
edgeIPv6 := netip.MustParseAddrPort("[::]:0")
|
|
|
|
|
|
|
|
// We assume the test environment has access to an IPv4 interface
|
|
|
|
testCreateUDPConnReuseSourcePortForEdgeIP(t, edgeIPv4)
|
|
|
|
|
|
|
|
if nettest.SupportsIPv6() {
|
|
|
|
testCreateUDPConnReuseSourcePortForEdgeIP(t, edgeIPv6)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCreateUDPConnReuseSourcePortForEdgeIP(t *testing.T, edgeIP netip.AddrPort) {
|
2022-10-12 16:01:25 +00:00
|
|
|
logger := zerolog.Nop()
|
2024-10-18 21:38:05 +00:00
|
|
|
conn, err := createUDPConnForConnIndex(0, nil, edgeIP, &logger)
|
2022-10-12 16:01:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
getPortFunc := func(conn *net.UDPConn) int {
|
|
|
|
addr := conn.LocalAddr().(*net.UDPAddr)
|
|
|
|
return addr.Port
|
|
|
|
}
|
|
|
|
|
|
|
|
initialPort := getPortFunc(conn)
|
|
|
|
|
|
|
|
// close conn
|
|
|
|
conn.Close()
|
|
|
|
|
|
|
|
// should get the same port as before.
|
2024-10-18 21:38:05 +00:00
|
|
|
conn, err = createUDPConnForConnIndex(0, nil, edgeIP, &logger)
|
2022-10-12 16:01:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, initialPort, getPortFunc(conn))
|
|
|
|
|
|
|
|
// new index, should get a different port
|
2024-10-18 21:38:05 +00:00
|
|
|
conn1, err := createUDPConnForConnIndex(1, nil, edgeIP, &logger)
|
2022-10-12 16:01:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, initialPort, getPortFunc(conn1))
|
|
|
|
|
|
|
|
// not closing the conn and trying to obtain a new conn for same index should give a different random port
|
2024-10-18 21:38:05 +00:00
|
|
|
conn, err = createUDPConnForConnIndex(0, nil, edgeIP, &logger)
|
2022-10-12 16:01:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, initialPort, getPortFunc(conn))
|
|
|
|
}
|
|
|
|
|
2022-06-06 13:15:35 +00:00
|
|
|
func serveSession(ctx context.Context, qc *QUICConnection, edgeQUICSession quic.Connection, closeType closeReason, expectedReason string, t *testing.T) {
|
2021-12-14 22:52:47 +00:00
|
|
|
var (
|
|
|
|
payload = []byte(t.Name())
|
|
|
|
)
|
|
|
|
sessionID := uuid.New()
|
|
|
|
cfdConn, originConn := net.Pipe()
|
|
|
|
// Registers and run a new session
|
|
|
|
session, err := qc.sessionManager.RegisterSession(ctx, sessionID, cfdConn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sessionDone := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
qc.serveUDPSession(session, time.Millisecond*50)
|
|
|
|
close(sessionDone)
|
|
|
|
}()
|
|
|
|
|
2022-09-20 10:39:51 +00:00
|
|
|
// Send a message to the quic session on edge side, it should be deumx to this datagram v2 session
|
2024-05-14 04:22:06 +00:00
|
|
|
muxedPayload, err := cfdquic.SuffixSessionID(sessionID, payload)
|
2022-09-20 10:39:51 +00:00
|
|
|
require.NoError(t, err)
|
2024-05-14 04:22:06 +00:00
|
|
|
muxedPayload, err = cfdquic.SuffixType(muxedPayload, cfdquic.DatagramTypeUDP)
|
2022-09-20 10:39:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-04 09:49:00 +00:00
|
|
|
err = edgeQUICSession.SendDatagram(muxedPayload)
|
2021-12-14 22:52:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
readBuffer := make([]byte, len(payload)+1)
|
|
|
|
n, err := originConn.Read(readBuffer)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(payload), n)
|
|
|
|
require.True(t, bytes.Equal(payload, readBuffer[:n]))
|
|
|
|
|
|
|
|
// Close connection to terminate session
|
|
|
|
switch closeType {
|
|
|
|
case closedByOrigin:
|
|
|
|
originConn.Close()
|
|
|
|
case closedByRemote:
|
|
|
|
err = qc.UnregisterUdpSession(ctx, sessionID, expectedReason)
|
|
|
|
require.NoError(t, err)
|
|
|
|
case closedByTimeout:
|
|
|
|
}
|
|
|
|
|
|
|
|
if closeType != closedByRemote {
|
|
|
|
// Session was not closed by remote, so closeUDPSession should be invoked to unregister from remote
|
|
|
|
unregisterFromEdgeChan := make(chan struct{})
|
2022-02-02 12:27:49 +00:00
|
|
|
sessionRPCServer := &mockSessionRPCServer{
|
2021-12-14 22:52:47 +00:00
|
|
|
sessionID: sessionID,
|
|
|
|
unregisterReason: expectedReason,
|
|
|
|
calledUnregisterChan: unregisterFromEdgeChan,
|
|
|
|
}
|
2022-02-02 12:27:49 +00:00
|
|
|
go runRPCServer(ctx, edgeQUICSession, sessionRPCServer, nil, t)
|
2021-12-14 22:52:47 +00:00
|
|
|
|
|
|
|
<-unregisterFromEdgeChan
|
|
|
|
}
|
|
|
|
|
|
|
|
<-sessionDone
|
|
|
|
}
|
|
|
|
|
|
|
|
type closeReason uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
closedByOrigin closeReason = iota
|
|
|
|
closedByRemote
|
|
|
|
closedByTimeout
|
|
|
|
)
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func runRPCServer(ctx context.Context, session quic.Connection, sessionRPCServer pogs.SessionManager, configRPCServer pogs.ConfigurationManager, t *testing.T) {
|
2021-12-14 22:52:47 +00:00
|
|
|
stream, err := session.AcceptStream(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if stream.StreamID() == 0 {
|
|
|
|
// Skip the first stream, it's the control stream of the QUIC connection
|
|
|
|
stream, err = session.AcceptStream(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2024-05-14 04:22:06 +00:00
|
|
|
ss := rpcquic.NewCloudflaredServer(
|
|
|
|
func(_ context.Context, _ *rpcquic.RequestServerStream) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
sessionRPCServer,
|
|
|
|
configRPCServer,
|
|
|
|
10*time.Second,
|
|
|
|
)
|
|
|
|
err = ss.Serve(ctx, stream)
|
2021-12-14 22:52:47 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockSessionRPCServer struct {
|
|
|
|
sessionID uuid.UUID
|
|
|
|
unregisterReason string
|
|
|
|
calledUnregisterChan chan struct{}
|
|
|
|
}
|
|
|
|
|
2022-09-09 04:42:11 +00:00
|
|
|
func (s mockSessionRPCServer) RegisterUdpSession(ctx context.Context, sessionID uuid.UUID, dstIP net.IP, dstPort uint16, closeIdleAfter time.Duration, traceContext string) (*pogs.RegisterUdpSessionResponse, error) {
|
|
|
|
return nil, fmt.Errorf("mockSessionRPCServer doesn't implement RegisterUdpSession")
|
2021-12-14 22:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s mockSessionRPCServer) UnregisterUdpSession(ctx context.Context, sessionID uuid.UUID, reason string) error {
|
|
|
|
if s.sessionID != sessionID {
|
|
|
|
return fmt.Errorf("expect session ID %s, got %s", s.sessionID, sessionID)
|
|
|
|
}
|
|
|
|
if s.unregisterReason != reason {
|
|
|
|
return fmt.Errorf("expect unregister reason %s, got %s", s.unregisterReason, reason)
|
|
|
|
}
|
|
|
|
close(s.calledUnregisterChan)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-18 21:38:05 +00:00
|
|
|
func testQUICConnection(udpListenerAddr netip.AddrPort, t *testing.T, index uint8) *QUICConnection {
|
2021-12-14 22:52:47 +00:00
|
|
|
tlsClientConfig := &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
NextProtos: []string{"argotunnel"},
|
|
|
|
}
|
|
|
|
// Start a mock httpProxy
|
|
|
|
log := zerolog.New(os.Stdout)
|
2023-05-06 00:42:41 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2021-12-14 22:52:47 +00:00
|
|
|
qc, err := NewQUICConnection(
|
2023-05-06 00:42:41 +00:00
|
|
|
ctx,
|
2021-12-14 22:52:47 +00:00
|
|
|
testQUICConfig,
|
|
|
|
udpListenerAddr,
|
2023-02-28 16:11:42 +00:00
|
|
|
nil,
|
2022-10-18 08:31:16 +00:00
|
|
|
index,
|
2021-12-14 22:52:47 +00:00
|
|
|
tlsClientConfig,
|
2022-02-11 10:49:06 +00:00
|
|
|
&mockOrchestrator{originProxy: &mockOriginProxyWithRequest{}},
|
2021-12-14 22:52:47 +00:00
|
|
|
&tunnelpogs.ConnectionOptions{},
|
|
|
|
fakeControlStream{},
|
2022-01-04 19:00:44 +00:00
|
|
|
&log,
|
2022-08-18 15:03:47 +00:00
|
|
|
nil,
|
2024-05-14 04:22:06 +00:00
|
|
|
15*time.Second,
|
2024-08-30 11:51:20 +00:00
|
|
|
0*time.Second,
|
2024-02-12 18:58:55 +00:00
|
|
|
0*time.Second,
|
2021-12-14 22:52:47 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return qc
|
|
|
|
}
|
2022-08-22 22:48:45 +00:00
|
|
|
|
|
|
|
type mockReaderNoopWriter struct {
|
|
|
|
io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockReaderNoopWriter) Write(p []byte) (n int, err error) {
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockReaderNoopWriter) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-14 04:22:06 +00:00
|
|
|
|
|
|
|
// GenerateTLSConfig sets up a bare-bones TLS config for a QUIC server
|
|
|
|
func GenerateTLSConfig() *tls.Config {
|
|
|
|
key, err := rsa.GenerateKey(rand.Reader, 1024)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
template := x509.Certificate{SerialNumber: big.NewInt(1)}
|
|
|
|
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
|
|
|
|
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
|
|
|
|
|
|
|
|
tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{tlsCert},
|
|
|
|
NextProtos: []string{"argotunnel"},
|
|
|
|
}
|
|
|
|
}
|