DialEarly with a packet conn the caller made sets a flag that means quic-go does not own it: closing the transport only stops reading from the socket. Neither DNS transport closed it. On the QUIC one it was closed on a failed handshake and never on success, so every redial — idle timeout, retry error, engine reload — left a UDP socket for the life of the process. On the HTTP/3 one the library drives its own reconnects, so the leak compounds without anything in our code looking wrong. That is the same shape as v2rayquic's, where offerNew overwrote the raw conn on every reconnect without closing the previous one. Both are now owned by a watcher tied to the connection's own context, so the socket lives exactly as long as the connection does. This matters more than it did last week: the shipped resolvers are DoH, and DNS is intercepted by default now, so the whole network's query stream rides this path on a router with 512 MB. The same upstream commit fixes both halves. We had taken the v2ray half and not the DNS one — the third time this session a paired fix arrived half-applied, and the first of those cost a day of debugging. These two files are now byte-identical to upstream so a rebase cannot reopen it. Also from that family: websocket and httpupgrade leaked their conn on failed handshakes, and a QUIC stream's Close did not release a blocked write. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
117 lines
2.7 KiB
Go
117 lines
2.7 KiB
Go
//go:build with_quic
|
|
|
|
package v2rayquic
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"sync"
|
|
|
|
"github.com/sagernet/quic-go"
|
|
"github.com/sagernet/quic-go/http3"
|
|
"github.com/sagernet/sing-box/adapter"
|
|
"github.com/sagernet/sing-box/common/tls"
|
|
C "github.com/sagernet/sing-box/constant"
|
|
"github.com/sagernet/sing-box/option"
|
|
"github.com/sagernet/sing-quic"
|
|
"github.com/sagernet/sing/common"
|
|
"github.com/sagernet/sing/common/bufio"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
N "github.com/sagernet/sing/common/network"
|
|
)
|
|
|
|
var _ adapter.V2RayClientTransport = (*Client)(nil)
|
|
|
|
type Client struct {
|
|
ctx context.Context
|
|
dialer N.Dialer
|
|
serverAddr M.Socksaddr
|
|
tlsConfig tls.Config
|
|
quicConfig *quic.Config
|
|
connAccess sync.Mutex
|
|
conn common.TypedValue[*quic.Conn]
|
|
rawConn net.Conn
|
|
}
|
|
|
|
func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayQUICOptions, tlsConfig tls.Config) (adapter.V2RayClientTransport, error) {
|
|
quicConfig := &quic.Config{
|
|
DisablePathMTUDiscovery: !C.IsLinux && !C.IsWindows,
|
|
}
|
|
if len(tlsConfig.NextProtos()) == 0 {
|
|
tlsConfig.SetNextProtos([]string{http3.NextProtoH3})
|
|
}
|
|
return &Client{
|
|
ctx: ctx,
|
|
dialer: dialer,
|
|
serverAddr: serverAddr,
|
|
tlsConfig: tlsConfig,
|
|
quicConfig: quicConfig,
|
|
}, nil
|
|
}
|
|
|
|
func (c *Client) offer() (*quic.Conn, error) {
|
|
conn := c.conn.Load()
|
|
if conn != nil && !common.Done(conn.Context()) {
|
|
return conn, nil
|
|
}
|
|
c.connAccess.Lock()
|
|
defer c.connAccess.Unlock()
|
|
conn = c.conn.Load()
|
|
if conn != nil && !common.Done(conn.Context()) {
|
|
return conn, nil
|
|
}
|
|
conn, err := c.offerNew()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return conn, nil
|
|
}
|
|
|
|
func (c *Client) offerNew() (*quic.Conn, error) {
|
|
udpConn, err := c.dialer.DialContext(c.ctx, "udp", c.serverAddr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
packetConn := bufio.NewUnbindPacketConn(udpConn)
|
|
quicConn, err := qtls.Dial(c.ctx, packetConn, udpConn.RemoteAddr(), c.tlsConfig, c.quicConfig)
|
|
if err != nil {
|
|
packetConn.Close()
|
|
return nil, err
|
|
}
|
|
// quic-go does not take ownership of the packet conn passed to Dial:
|
|
// when the connection ends it only stops reading.
|
|
go func() {
|
|
<-quicConn.Context().Done()
|
|
packetConn.Close()
|
|
}()
|
|
c.conn.Store(quicConn)
|
|
c.rawConn = udpConn
|
|
return quicConn, nil
|
|
}
|
|
|
|
func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
|
|
conn, err := c.offer()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
stream, err := conn.OpenStream()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &StreamWrapper{Conn: conn, Stream: stream}, nil
|
|
}
|
|
|
|
func (c *Client) Close() error {
|
|
c.connAccess.Lock()
|
|
defer c.connAccess.Unlock()
|
|
conn := c.conn.Swap(nil)
|
|
if conn != nil {
|
|
conn.CloseWithError(0, "")
|
|
}
|
|
if c.rawConn != nil {
|
|
c.rawConn.Close()
|
|
}
|
|
c.rawConn = nil
|
|
return nil
|
|
}
|