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>
121 lines
3.0 KiB
Go
121 lines
3.0 KiB
Go
package v2rayhttpupgrade
|
|
|
|
import (
|
|
std_bufio "bufio"
|
|
"context"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
"github.com/sagernet/sing-box/common/tls"
|
|
"github.com/sagernet/sing-box/option"
|
|
"github.com/sagernet/sing/common/buf"
|
|
"github.com/sagernet/sing/common/bufio"
|
|
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"
|
|
)
|
|
|
|
var _ adapter.V2RayClientTransport = (*Client)(nil)
|
|
|
|
type Client struct {
|
|
dialer N.Dialer
|
|
serverAddr M.Socksaddr
|
|
requestURL url.URL
|
|
headers http.Header
|
|
host string
|
|
}
|
|
|
|
func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayHTTPUpgradeOptions, tlsConfig tls.Config) (*Client, error) {
|
|
if tlsConfig != nil {
|
|
if len(tlsConfig.NextProtos()) == 0 {
|
|
tlsConfig.SetNextProtos([]string{"http/1.1"})
|
|
}
|
|
dialer = tls.NewDialer(dialer, tlsConfig)
|
|
}
|
|
var host string
|
|
if options.Host != "" {
|
|
host = options.Host
|
|
} else if tlsConfig != nil && tlsConfig.ServerName() != "" {
|
|
host = tlsConfig.ServerName()
|
|
} else {
|
|
host = serverAddr.String()
|
|
}
|
|
var requestURL url.URL
|
|
if tlsConfig == nil {
|
|
requestURL.Scheme = "http"
|
|
} else {
|
|
requestURL.Scheme = "https"
|
|
}
|
|
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 := make(http.Header)
|
|
for key, value := range options.Headers {
|
|
headers[key] = value
|
|
}
|
|
return &Client{
|
|
dialer: dialer,
|
|
serverAddr: serverAddr,
|
|
requestURL: requestURL,
|
|
headers: headers,
|
|
host: host,
|
|
}, nil
|
|
}
|
|
|
|
func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
|
|
conn, err := c.dialer.DialContext(ctx, N.NetworkTCP, c.serverAddr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
request := &http.Request{
|
|
Method: http.MethodGet,
|
|
URL: &c.requestURL,
|
|
Header: c.headers.Clone(),
|
|
Host: c.host,
|
|
}
|
|
request.Header.Set("Connection", "Upgrade")
|
|
request.Header.Set("Upgrade", "websocket")
|
|
err = request.Write(conn)
|
|
if err != nil {
|
|
conn.Close()
|
|
return nil, err
|
|
}
|
|
bufReader := std_bufio.NewReader(conn)
|
|
response, err := http.ReadResponse(bufReader, request)
|
|
if err != nil {
|
|
conn.Close()
|
|
return nil, err
|
|
}
|
|
if response.StatusCode != 101 ||
|
|
!strings.EqualFold(response.Header.Get("Connection"), "upgrade") ||
|
|
!strings.EqualFold(response.Header.Get("Upgrade"), "websocket") {
|
|
conn.Close()
|
|
response.Body.Close()
|
|
return nil, E.New("v2ray-http-upgrade: unexpected status: ", response.Status)
|
|
}
|
|
if bufReader.Buffered() > 0 {
|
|
buffer := buf.NewSize(bufReader.Buffered())
|
|
_, err = buffer.ReadFullFrom(bufReader, buffer.Len())
|
|
if err != nil {
|
|
conn.Close()
|
|
return nil, err
|
|
}
|
|
conn = bufio.NewCachedConn(conn, buffer)
|
|
}
|
|
return conn, nil
|
|
}
|
|
|
|
func (c *Client) Close() error {
|
|
return nil
|
|
}
|