2022-09-02 16:29:50 +00:00
|
|
|
//go:build darwin || linux
|
|
|
|
|
|
|
|
package ingress
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-01-15 16:49:17 +00:00
|
|
|
"github.com/fortytw2/leaktest"
|
2022-09-02 16:29:50 +00:00
|
|
|
"github.com/google/gopacket/layers"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/net/icmp"
|
|
|
|
"golang.org/x/net/ipv4"
|
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/packet"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFunnelIdleTimeout(t *testing.T) {
|
2024-01-15 16:49:17 +00:00
|
|
|
defer leaktest.Check(t)()
|
|
|
|
|
2022-09-02 16:29:50 +00:00
|
|
|
const (
|
|
|
|
idleTimeout = time.Second
|
|
|
|
echoID = 42573
|
|
|
|
startSeq = 8129
|
|
|
|
)
|
|
|
|
logger := zerolog.New(os.Stderr)
|
2024-11-19 20:44:24 +00:00
|
|
|
proxy, err := newICMPProxy(localhostIP, &logger, idleTimeout)
|
2022-09-02 16:29:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
proxyDone := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
proxy.Serve(ctx)
|
|
|
|
close(proxyDone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Send a packet to register the flow
|
|
|
|
pk := packet.ICMP{
|
|
|
|
IP: &packet.IP{
|
|
|
|
Src: localhostIP,
|
|
|
|
Dst: localhostIP,
|
|
|
|
Protocol: layers.IPProtocolICMPv4,
|
|
|
|
},
|
|
|
|
Message: &icmp.Message{
|
|
|
|
Type: ipv4.ICMPTypeEcho,
|
|
|
|
Code: 0,
|
|
|
|
Body: &icmp.Echo{
|
|
|
|
ID: echoID,
|
|
|
|
Seq: startSeq,
|
|
|
|
Data: []byte(t.Name()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-10-13 10:01:25 +00:00
|
|
|
muxer := newMockMuxer(0)
|
2024-11-19 20:44:24 +00:00
|
|
|
responder := newPacketResponder(muxer, 0, packet.NewEncoder())
|
|
|
|
require.NoError(t, proxy.Request(ctx, &pk, responder))
|
2022-10-19 13:37:10 +00:00
|
|
|
validateEchoFlow(t, <-muxer.cfdToEdge, &pk)
|
2022-09-02 16:29:50 +00:00
|
|
|
|
|
|
|
// Send second request, should reuse the funnel
|
2024-11-19 20:44:24 +00:00
|
|
|
require.NoError(t, proxy.Request(ctx, &pk, responder))
|
2022-10-19 13:37:10 +00:00
|
|
|
validateEchoFlow(t, <-muxer.cfdToEdge, &pk)
|
2022-09-02 16:29:50 +00:00
|
|
|
|
2024-11-19 20:44:24 +00:00
|
|
|
// New muxer on a different connection should use a new flow
|
2022-09-02 16:29:50 +00:00
|
|
|
time.Sleep(idleTimeout * 2)
|
2022-10-13 10:01:25 +00:00
|
|
|
newMuxer := newMockMuxer(0)
|
2024-11-19 20:44:24 +00:00
|
|
|
newResponder := newPacketResponder(newMuxer, 1, packet.NewEncoder())
|
|
|
|
require.NoError(t, proxy.Request(ctx, &pk, newResponder))
|
2022-10-19 13:37:10 +00:00
|
|
|
validateEchoFlow(t, <-newMuxer.cfdToEdge, &pk)
|
2022-09-02 16:29:50 +00:00
|
|
|
|
2024-01-15 16:49:17 +00:00
|
|
|
time.Sleep(idleTimeout * 2)
|
2022-09-02 16:29:50 +00:00
|
|
|
cancel()
|
|
|
|
<-proxyDone
|
|
|
|
}
|
2022-11-10 17:09:47 +00:00
|
|
|
|
|
|
|
func TestReuseFunnel(t *testing.T) {
|
2024-01-15 16:49:17 +00:00
|
|
|
defer leaktest.Check(t)()
|
|
|
|
|
2022-11-10 17:09:47 +00:00
|
|
|
const (
|
2024-01-15 16:49:17 +00:00
|
|
|
idleTimeout = time.Millisecond * 100
|
2022-11-10 17:09:47 +00:00
|
|
|
echoID = 42573
|
|
|
|
startSeq = 8129
|
|
|
|
)
|
|
|
|
logger := zerolog.New(os.Stderr)
|
2024-11-19 20:44:24 +00:00
|
|
|
proxy, err := newICMPProxy(localhostIP, &logger, idleTimeout)
|
2022-11-10 17:09:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
proxyDone := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
proxy.Serve(ctx)
|
|
|
|
close(proxyDone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Send a packet to register the flow
|
|
|
|
pk := packet.ICMP{
|
|
|
|
IP: &packet.IP{
|
|
|
|
Src: localhostIP,
|
|
|
|
Dst: localhostIP,
|
|
|
|
Protocol: layers.IPProtocolICMPv4,
|
|
|
|
},
|
|
|
|
Message: &icmp.Message{
|
|
|
|
Type: ipv4.ICMPTypeEcho,
|
|
|
|
Code: 0,
|
|
|
|
Body: &icmp.Echo{
|
|
|
|
ID: echoID,
|
|
|
|
Seq: startSeq,
|
|
|
|
Data: []byte(t.Name()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tuple := flow3Tuple{
|
|
|
|
srcIP: pk.Src,
|
|
|
|
dstIP: pk.Dst,
|
|
|
|
originalEchoID: echoID,
|
|
|
|
}
|
|
|
|
muxer := newMockMuxer(0)
|
2024-11-19 20:44:24 +00:00
|
|
|
responder := newPacketResponder(muxer, 0, packet.NewEncoder())
|
|
|
|
require.NoError(t, proxy.Request(ctx, &pk, responder))
|
2022-11-10 17:09:47 +00:00
|
|
|
validateEchoFlow(t, <-muxer.cfdToEdge, &pk)
|
|
|
|
funnel1, found := getFunnel(t, proxy, tuple)
|
|
|
|
require.True(t, found)
|
|
|
|
|
|
|
|
// Send second request, should reuse the funnel
|
2024-11-19 20:44:24 +00:00
|
|
|
require.NoError(t, proxy.Request(ctx, &pk, responder))
|
2022-11-10 17:09:47 +00:00
|
|
|
validateEchoFlow(t, <-muxer.cfdToEdge, &pk)
|
|
|
|
funnel2, found := getFunnel(t, proxy, tuple)
|
|
|
|
require.True(t, found)
|
|
|
|
require.Equal(t, funnel1, funnel2)
|
|
|
|
|
2024-01-15 16:49:17 +00:00
|
|
|
time.Sleep(idleTimeout * 2)
|
|
|
|
|
2022-11-10 17:09:47 +00:00
|
|
|
cancel()
|
|
|
|
<-proxyDone
|
|
|
|
}
|