5
5
"context"
6
6
"crypto/rand"
7
7
"errors"
8
- "fmt"
9
8
"io"
10
9
mrand "math/rand"
11
10
"net"
@@ -564,6 +563,13 @@ func TestStatelessReset(t *testing.T) {
564
563
}
565
564
}
566
565
566
+ func newUDPConnLocalhost (t testing.TB , port int ) (* net.UDPConn , func ()) {
567
+ t .Helper ()
568
+ conn , err := net .ListenUDP ("udp" , & net.UDPAddr {IP : net .IPv4 (127 , 0 , 0 , 1 ), Port : port })
569
+ require .NoError (t , err )
570
+ return conn , func () { conn .Close () }
571
+ }
572
+
567
573
func testStatelessReset (t * testing.T , tc * connTestCase ) {
568
574
serverID , serverKey := createPeer (t )
569
575
_ , clientKey := createPeer (t )
@@ -575,12 +581,14 @@ func testStatelessReset(t *testing.T, tc *connTestCase) {
575
581
576
582
var drop uint32
577
583
dropCallback := func (quicproxy.Direction , []byte ) bool { return atomic .LoadUint32 (& drop ) > 0 }
578
- proxy , err := quicproxy .NewQuicProxy ("localhost:0" , & quicproxy.Opts {
579
- RemoteAddr : fmt .Sprintf ("localhost:%d" , ln .Addr ().(* net.UDPAddr ).Port ),
584
+ proxyConn , cleanup := newUDPConnLocalhost (t , 0 )
585
+ proxy := quicproxy.Proxy {
586
+ Conn : proxyConn ,
587
+ ServerAddr : ln .Addr ().(* net.UDPAddr ),
580
588
DropPacket : dropCallback ,
581
- })
589
+ }
590
+ err = proxy .Start ()
582
591
require .NoError (t , err )
583
- proxyLocalAddr := proxy .LocalAddr ()
584
592
585
593
// establish a connection
586
594
clientTransport , err := NewTransport (clientKey , newConnManager (t , tc .Options ... ), nil , nil , nil )
@@ -612,7 +620,9 @@ func testStatelessReset(t *testing.T, tc *connTestCase) {
612
620
atomic .StoreUint32 (& drop , 1 )
613
621
ln .Close ()
614
622
(<- connChan ).Close ()
623
+ proxyLocalPort := proxy .LocalAddr ().(* net.UDPAddr ).Port
615
624
proxy .Close ()
625
+ cleanup ()
616
626
617
627
// Start another listener (on a different port).
618
628
ln , err = serverTransport .Listen (ma .StringCast ("/ip4/127.0.0.1/udp/0/quic-v1" ))
@@ -621,13 +631,17 @@ func testStatelessReset(t *testing.T, tc *connTestCase) {
621
631
// Now that the new server is up, re-enable packet forwarding.
622
632
atomic .StoreUint32 (& drop , 0 )
623
633
634
+ proxyConn , cleanup = newUDPConnLocalhost (t , proxyLocalPort )
635
+ defer cleanup ()
624
636
// Recreate the proxy, such that its client-facing port stays constant.
625
- proxy , err = quicproxy .NewQuicProxy (proxyLocalAddr .String (), & quicproxy.Opts {
626
- RemoteAddr : fmt .Sprintf ("localhost:%d" , ln .Addr ().(* net.UDPAddr ).Port ),
637
+ proxyBis := quicproxy.Proxy {
638
+ Conn : proxyConn ,
639
+ ServerAddr : ln .Addr ().(* net.UDPAddr ),
627
640
DropPacket : dropCallback ,
628
- })
641
+ }
642
+ err = proxyBis .Start ()
629
643
require .NoError (t , err )
630
- defer proxy .Close ()
644
+ defer proxyBis .Close ()
631
645
632
646
// Trigger something (not too small) to be sent, so that we receive the stateless reset.
633
647
// The new server doesn't have any state for the previously established connection.
0 commit comments