From f5145eb627f090124f077a98ab74bc7cdee8ed4a Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sat, 25 Jan 2025 04:18:22 +0100 Subject: [PATCH] proxy: remove QuicProxy.LocalPort method (#4920) --- integrationtests/self/http_test.go | 3 ++- integrationtests/self/mitm_test.go | 14 ++--------- integrationtests/tools/proxy/proxy.go | 6 +---- integrationtests/tools/proxy/proxy_test.go | 28 +--------------------- 4 files changed, 6 insertions(+), 45 deletions(-) diff --git a/integrationtests/self/http_test.go b/integrationtests/self/http_test.go index 0bf7ad575..55c202c59 100644 --- a/integrationtests/self/http_test.go +++ b/integrationtests/self/http_test.go @@ -870,7 +870,8 @@ func TestHTTP0RTT(t *testing.T) { } defer tr.Close() - req, err := http.NewRequest(http3.MethodGet0RTT, fmt.Sprintf("https://localhost:%d/0rtt", proxy.LocalPort()), nil) + proxyPort := proxy.LocalAddr().(*net.UDPAddr).Port + req, err := http.NewRequest(http3.MethodGet0RTT, fmt.Sprintf("https://localhost:%d/0rtt", proxyPort), nil) require.NoError(t, err) rsp, err := tr.RoundTrip(req) require.NoError(t, err) diff --git a/integrationtests/self/mitm_test.go b/integrationtests/self/mitm_test.go index 0469602ef..b5096d5f3 100644 --- a/integrationtests/self/mitm_test.go +++ b/integrationtests/self/mitm_test.go @@ -215,12 +215,7 @@ func runMITMTest(t *testing.T, serverTr, clientTr *quic.Transport, rtt time.Dura ctx, cancel := context.WithTimeout(context.Background(), scaleDuration(time.Second)) defer cancel() - conn, err := clientTr.Dial( - ctx, - &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: proxy.LocalPort()}, - getTLSClientConfig(), - getQuicConfig(nil), - ) + conn, err := clientTr.Dial(ctx, proxy.LocalAddr(), getTLSClientConfig(), getQuicConfig(nil)) require.NoError(t, err) defer conn.CloseWithError(0, "") @@ -428,12 +423,7 @@ func runMITMTestSuccessful(t *testing.T, serverTransport, clientTransport *quic. ctx, cancel := context.WithTimeout(context.Background(), scaleDuration(50*time.Millisecond)) defer cancel() - _, err = clientTransport.Dial( - ctx, - &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: proxy.LocalPort()}, - getTLSClientConfig(), - getQuicConfig(nil), - ) + _, err = clientTransport.Dial(ctx, proxy.LocalAddr(), getTLSClientConfig(), getQuicConfig(nil)) require.Error(t, err) return err } diff --git a/integrationtests/tools/proxy/proxy.go b/integrationtests/tools/proxy/proxy.go index 4ed2d89de..d2873bf98 100644 --- a/integrationtests/tools/proxy/proxy.go +++ b/integrationtests/tools/proxy/proxy.go @@ -209,6 +209,7 @@ func NewQuicProxy(local string, opts *Opts) (*QuicProxy, error) { func (p *QuicProxy) Close() error { p.mutex.Lock() defer p.mutex.Unlock() + close(p.closeChan) for _, c := range p.clientDict { if err := c.ServerConn.Close(); err != nil { @@ -225,11 +226,6 @@ func (p *QuicProxy) LocalAddr() net.Addr { return p.conn.LocalAddr() } -// LocalPort is the UDP port number the proxy is listening on. -func (p *QuicProxy) LocalPort() int { - return p.conn.LocalAddr().(*net.UDPAddr).Port -} - func (p *QuicProxy) newConnection(cliAddr *net.UDPAddr) (*connection, error) { conn, err := net.DialUDP("udp", nil, p.serverAddr) if err != nil { diff --git a/integrationtests/tools/proxy/proxy_test.go b/integrationtests/tools/proxy/proxy_test.go index 3f119dd5f..adae89536 100644 --- a/integrationtests/tools/proxy/proxy_test.go +++ b/integrationtests/tools/proxy/proxy_test.go @@ -2,9 +2,7 @@ package quicproxy import ( "bytes" - "fmt" "net" - "runtime" "runtime/pprof" "strconv" "strings" @@ -49,27 +47,6 @@ func readPacketNumber(t *testing.T, b []byte) protocol.PacketNumber { return extHdr.PacketNumber } -func TestProxySetupt(t *testing.T) { - proxy, err := NewQuicProxy("localhost:0", nil) - require.NoError(t, err) - require.Len(t, proxy.clientDict, 0) - - // Check that the proxy port is in use - addr, err := net.ResolveUDPAddr("udp", "localhost:"+strconv.Itoa(proxy.LocalPort())) - require.NoError(t, err) - _, err = net.ListenUDP("udp", addr) - if runtime.GOOS == "windows" { - require.EqualError(t, err, fmt.Sprintf("listen udp 127.0.0.1:%d: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.", proxy.LocalPort())) - } else { - require.EqualError(t, err, fmt.Sprintf("listen udp 127.0.0.1:%d: bind: address already in use", proxy.LocalPort())) - } - - require.Equal(t, "127.0.0.1:"+strconv.Itoa(proxy.LocalPort()), proxy.LocalAddr().String()) - require.NotZero(t, proxy.LocalPort()) - - require.NoError(t, proxy.Close()) -} - func TestProxyShutdown(t *testing.T) { isProxyRunning := func() bool { var b bytes.Buffer @@ -79,7 +56,6 @@ func TestProxyShutdown(t *testing.T) { proxy, err := NewQuicProxy("localhost:0", nil) require.NoError(t, err) - port := proxy.LocalPort() require.Eventually(t, func() bool { return isProxyRunning() }, time.Second, 10*time.Millisecond) conn, err := net.DialUDP("udp", nil, proxy.LocalAddr().(*net.UDPAddr)) @@ -90,11 +66,9 @@ func TestProxyShutdown(t *testing.T) { require.NoError(t, proxy.Close()) // check that the proxy port is not in use anymore - addr, err := net.ResolveUDPAddr("udp", "localhost:"+strconv.Itoa(port)) - require.NoError(t, err) // sometimes it takes a while for the OS to free the port require.Eventually(t, func() bool { - ln, err := net.ListenUDP("udp", addr) + ln, err := net.ListenUDP("udp", proxy.LocalAddr().(*net.UDPAddr)) if err != nil { return false }