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>
126 lines
3.3 KiB
Go
126 lines
3.3 KiB
Go
package v2raywebsocket
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"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/common/buf"
|
|
"github.com/sagernet/sing/common/bufio"
|
|
"github.com/sagernet/sing/common/bufio/deadline"
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
N "github.com/sagernet/sing/common/network"
|
|
sHTTP "github.com/sagernet/sing/protocol/http"
|
|
"github.com/sagernet/ws"
|
|
)
|
|
|
|
var _ adapter.V2RayClientTransport = (*Client)(nil)
|
|
|
|
type Client struct {
|
|
dialer N.Dialer
|
|
serverAddr M.Socksaddr
|
|
requestURL url.URL
|
|
headers http.Header
|
|
maxEarlyData uint32
|
|
earlyDataHeaderName string
|
|
}
|
|
|
|
func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayWebsocketOptions, tlsConfig tls.Config) (adapter.V2RayClientTransport, error) {
|
|
if tlsConfig != nil {
|
|
if len(tlsConfig.NextProtos()) == 0 {
|
|
tlsConfig.SetNextProtos([]string{"http/1.1"})
|
|
}
|
|
dialer = tls.NewDialer(dialer, tlsConfig)
|
|
}
|
|
var requestURL url.URL
|
|
if tlsConfig == nil {
|
|
requestURL.Scheme = "ws"
|
|
} else {
|
|
requestURL.Scheme = "wss"
|
|
}
|
|
requestURL.Host = serverAddr.String()
|
|
requestURL.Path = options.Path
|
|
err := sHTTP.URLSetPath(&requestURL, options.Path)
|
|
if err != nil {
|
|
return nil, E.Cause(err, "parse path")
|
|
}
|
|
if !strings.HasPrefix(requestURL.Path, "/") {
|
|
requestURL.Path = "/" + requestURL.Path
|
|
}
|
|
headers := options.Headers.Build()
|
|
if host := headers.Get("Host"); host != "" {
|
|
headers.Del("Host")
|
|
requestURL.Host = host
|
|
}
|
|
if headers.Get("User-Agent") == "" {
|
|
headers.Set("User-Agent", "Go-http-client/1.1")
|
|
}
|
|
return &Client{
|
|
dialer,
|
|
serverAddr,
|
|
requestURL,
|
|
headers,
|
|
options.MaxEarlyData,
|
|
options.EarlyDataHeaderName,
|
|
}, nil
|
|
}
|
|
|
|
func (c *Client) dialContext(ctx context.Context, requestURL *url.URL, headers http.Header) (*WebsocketConn, error) {
|
|
conn, err := c.dialer.DialContext(ctx, N.NetworkTCP, c.serverAddr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var deadlineConn net.Conn
|
|
if deadline.NeedAdditionalReadDeadline(conn) {
|
|
deadlineConn = deadline.NewConn(conn)
|
|
} else {
|
|
deadlineConn = conn
|
|
}
|
|
deadlineConn.SetDeadline(time.Now().Add(C.TCPTimeout))
|
|
var protocols []string
|
|
if protocolHeader := headers.Get("Sec-WebSocket-Protocol"); protocolHeader != "" {
|
|
protocols = []string{protocolHeader}
|
|
headers.Del("Sec-WebSocket-Protocol")
|
|
}
|
|
reader, _, err := ws.Dialer{Header: ws.HandshakeHeaderHTTP(headers), Protocols: protocols}.Upgrade(deadlineConn, requestURL)
|
|
deadlineConn.SetDeadline(time.Time{})
|
|
if err != nil {
|
|
conn.Close()
|
|
return nil, err
|
|
}
|
|
if reader != nil {
|
|
buffer := buf.NewSize(reader.Buffered())
|
|
_, err = buffer.ReadFullFrom(reader, buffer.Len())
|
|
if err != nil {
|
|
conn.Close()
|
|
return nil, err
|
|
}
|
|
conn = bufio.NewCachedConn(conn, buffer)
|
|
}
|
|
return NewConn(conn, nil, ws.StateClientSide), nil
|
|
}
|
|
|
|
func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
|
|
if c.maxEarlyData <= 0 {
|
|
conn, err := c.dialContext(ctx, &c.requestURL, c.headers)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return conn, nil
|
|
} else {
|
|
return &EarlyWebsocketConn{Client: c, ctx: ctx, create: make(chan struct{})}, nil
|
|
}
|
|
}
|
|
|
|
func (c *Client) Close() error {
|
|
return nil
|
|
}
|