From 8ddbf881b37ab869d5fe39ec90dc862e9bc6c3fa Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Tue, 3 Oct 2023 16:50:11 +0000 Subject: [PATCH] Sync quic transport code with DOH transport Otherwise, the old code will leave un-used connections open-ed, causing ports leaking and prevent others from creating UDP conn. --- config_quic.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/config_quic.go b/config_quic.go index f4c129b..cd3eaee 100644 --- a/config_quic.go +++ b/config_quic.go @@ -8,6 +8,7 @@ import ( "errors" "net" "net/http" + "runtime" "sync" "time" @@ -70,6 +71,9 @@ func (uc *UpstreamConfig) newDOH3Transport(addrs []string) http.RoundTripper { ProxyLogger.Load().Debug().Msgf("sending doh3 request to: %s", conn.RemoteAddr()) return conn, err } + runtime.SetFinalizer(rt, func(rt *http3.RoundTripper) { + rt.CloseIdleConnections() + }) return rt } @@ -113,6 +117,8 @@ func (d *quicParallelDialer) Dial(ctx context.Context, addrs []string, tlsCfg *t ctx, cancel := context.WithCancel(ctx) defer cancel() + done := make(chan struct{}) + defer close(done) ch := make(chan *parallelDialerResult, len(addrs)) var wg sync.WaitGroup wg.Add(len(addrs)) @@ -135,7 +141,13 @@ func (d *quicParallelDialer) Dial(ctx context.Context, addrs []string, tlsCfg *t return } conn, err := quic.DialEarly(ctx, udpConn, remoteAddr, tlsCfg, cfg) - ch <- ¶llelDialerResult{conn: conn, err: err} + select { + case ch <- ¶llelDialerResult{conn: conn, err: err}: + case <-done: + if conn != nil { + conn.CloseWithError(quic.ApplicationErrorCode(http3.ErrCodeNoError), "") + } + } }(addr) }