diff -ruN a/app/dispatcher/default.go b/app/dispatcher/default.go --- a/app/dispatcher/default.go +++ b/app/dispatcher/default.go @@ -376,7 +376,10 @@ } func sniffer(ctx context.Context, cReader *cachedReader, metadataOnly bool, network net.Network) (SniffResult, error) { - payload := buf.NewWithSize(32767) + // RayRay patch: 8 KB instead of 32 KB, it is held by every new connection while sniffing, and a burst of them is + // when an iOS network extension is closest to its memory limit. A TLS ClientHello or the QUIC Initial packets that + // carry one, post-quantum key share included, are about 2 KB; the rest of the data waits in the cache unsniffed. + payload := buf.NewWithSize(8192) defer payload.Release() sniffer := NewSniffer(ctx) diff -ruN a/app/proxyman/outbound/handler.go b/app/proxyman/outbound/handler.go --- a/app/proxyman/outbound/handler.go +++ b/app/proxyman/outbound/handler.go @@ -122,7 +122,9 @@ if config.Concurrency < 0 { h.mux = &mux.ClientManager{Enabled: false} } - if config.Concurrency >= 0 && config.Concurrency <= 128 { + // RayRay patch: only an unset value takes the default. Upstream's 0...128 here would turn every position + // of the app's stepper (1...128) into 128, so what the user chose would not be what runs. + if config.Concurrency == 0 { config.Concurrency = 128 } if config.Concurrency > 0 { diff -ruN a/infra/conf/transport_security.go b/infra/conf/transport_security.go --- a/infra/conf/transport_security.go +++ b/infra/conf/transport_security.go @@ -218,7 +218,11 @@ return nil, errors.New(`invalid "spiderX": `, c.SpiderX) } config.SpiderY = make([]int64, 10) - u, _ := url.Parse(c.SpiderX) + // RayRay patch: url.Parse gives nil for a bad escape such as "/%zz", and u.Query would end the app. + u, err := url.Parse(c.SpiderX) + if err != nil { + return nil, errors.New(`invalid "spiderX": `, c.SpiderX) + } q := u.Query() parse := func(param string, index int) { if q.Get(param) != "" { diff -ruN a/infra/conf/vless.go b/infra/conf/vless.go --- a/infra/conf/vless.go +++ b/infra/conf/vless.go @@ -364,6 +364,11 @@ return false } } + // RayRay patch: a value of padding segments only, with no key after them, would slice past its end below + // and a provider's link would end the app. Checked before anything is cut, so a tail of "none" is refused too. + if padding >= len(account.Encryption)-27-len(s[2]) { + return false + } account.Encryption = account.Encryption[27+len(s[2]):] if padding > 0 { account.Padding = account.Encryption[:padding-1] diff -ruN a/transport/internet/finalmask/realm/client.go b/transport/internet/finalmask/realm/client.go --- a/transport/internet/finalmask/realm/client.go +++ b/transport/internet/finalmask/realm/client.go @@ -32,7 +32,7 @@ punchInterval time.Duration } -func NewConnClient(config *Config, raw net.PacketConn) (net.PacketConn, error) { +func NewConnClient(config *Config, raw net.PacketConn, dial func(ctx context.Context, network, addr string) (net.Conn, error)) (net.PacketConn, error) { ctx, cancel := context.WithCancel(context.Background()) family := Family_Dual @@ -62,7 +62,7 @@ cancel: cancel, PacketConn: raw, - realmClient: NewClient(config.Scheme, config.Host, config.Port, config.Token, config.TlsConfig), + realmClient: NewClient(config.Scheme, config.Host, config.Port, config.Token, config.TlsConfig, dial), realmID: config.ID, stunServers: config.StunServers, family: family, diff -ruN a/transport/internet/finalmask/realm/config.go b/transport/internet/finalmask/realm/config.go --- a/transport/internet/finalmask/realm/config.go +++ b/transport/internet/finalmask/realm/config.go @@ -1,12 +1,49 @@ package realm import ( + "context" + go_errors "errors" + gonet "net" + "github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/transport/internet/finalmask" ) func (c *Config) WrapPacketConnClient(conn net.PacketConn, dest *net.Destination, dialer *finalmask.Dialer) (net.PacketConn, error) { - return NewConnClient(c, conn) + // RayRay: the realm server is reached the way this outbound dials, with its socket options. Its name is looked up + // by Go's resolver, as before, not through the outbound's domainStrategy, which would ask Xray's DNS, whose queries + // can go out through this very outbound, still being dialed. (In an Android VPN the system resolver asks through + // the tunnel as well: RayRay's config sends these names to a resolver of its own that goes direct.) + var dial func(ctx context.Context, network, addr string) (net.Conn, error) + if dialer != nil && dialer.DialTCP != nil { + dial = func(ctx context.Context, _, addr string) (net.Conn, error) { + host, port, err := gonet.SplitHostPort(addr) + if err != nil { + return nil, err + } + ips := []string{host} + if gonet.ParseIP(host) == nil { + found, err := gonet.DefaultResolver.LookupHost(ctx, host) + if err != nil { + return nil, err + } + ips = found + } + errs := make([]error, 0, len(ips)) + for _, ip := range ips { + to, err := net.ParseDestination("tcp:" + gonet.JoinHostPort(ip, port)) + if err == nil { + var conn net.Conn + if conn, err = dialer.DialTCP(to); err == nil { + return conn, nil + } + } + errs = append(errs, err) + } + return nil, go_errors.Join(errs...) + } + } + return NewConnClient(c, conn, dial) } func (c *Config) WrapPacketConnServer(conn net.PacketConn, addr net.Addr, lc *finalmask.ListenConfig) (net.PacketConn, error) { diff -ruN a/transport/internet/finalmask/realm/http.go b/transport/internet/finalmask/realm/http.go --- a/transport/internet/finalmask/realm/http.go +++ b/transport/internet/finalmask/realm/http.go @@ -81,11 +81,20 @@ return fmt.Sprintf("realm server returned %d", e.StatusCode) } -func NewClient(scheme, host, port, token string, tlsConfig *tls.Config) *Client { +// RayRay: dial is how the client reaches the realm server, nil for Go's own dialer. A client outbound passes its own +// dial, so these requests leave the way its packets do, with its socket options. +func NewClient(scheme, host, port, token string, tlsConfig *tls.Config, dial func(ctx context.Context, network, addr string) (net.Conn, error)) *Client { client := http.DefaultClient - if tlsConfig != nil { + if tlsConfig != nil || dial != nil { tr := http.DefaultTransport.(*http.Transport).Clone() - tr.TLSClientConfig = tlsConfig.GetTLSConfig() + if tlsConfig != nil { + tr.TLSClientConfig = tlsConfig.GetTLSConfig() + } + if dial != nil { + tr.DialContext = dial + // Nothing closes this per-connection transport, so its sockets must not stay idle after a request. + tr.DisableKeepAlives = true + } client = &http.Client{Transport: tr} } return &Client{ diff -ruN a/transport/internet/finalmask/realm/server.go b/transport/internet/finalmask/realm/server.go --- a/transport/internet/finalmask/realm/server.go +++ b/transport/internet/finalmask/realm/server.go @@ -86,7 +86,7 @@ cancel: cancel, PacketConn: raw, - realmClient: NewClient(config.Scheme, config.Host, config.Port, config.Token, config.TlsConfig), + realmClient: NewClient(config.Scheme, config.Host, config.Port, config.Token, config.TlsConfig, nil), realmID: config.ID, stunServers: config.StunServers, family: family, diff -ruN a/transport/internet/grpc/dial.go b/transport/internet/grpc/dial.go --- a/transport/internet/grpc/dial.go +++ b/transport/internet/grpc/dial.go @@ -48,6 +48,27 @@ globalDialerAccess sync.Mutex ) +// ResetDialers closes every cached gRPC client and forgets it. +// RayRay patch: the cache is global and keyed by each config's stream settings, so an Xray that is stopped and started +// again in the same process (a network change, a server switch) would leave its clients and their goroutines behind for +// good. The map is swapped at once, so that a client made after this call by the next Xray is not touched; the old ones +// are closed apart, Close can wait on a dead network. A dial still under way when the map is reset finds it nil, which +// getGrpcClient starts over from, or gets a closed client and returns its error. +func ResetDialers() { + globalDialerAccess.Lock() + clients := globalDialerMap + globalDialerMap = nil + globalDialerAccess.Unlock() + go func() { + for _, client := range clients { + // grpc.NewClient's error leaves a nil client in the map. + if client != nil { + _ = client.Close() + } + } + }() +} + func dialgRPC(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (net.Conn, error) { grpcSettings := streamSettings.ProtocolSettings.(*Config) diff -ruN a/transport/internet/splithttp/client.go b/transport/internet/splithttp/client.go --- a/transport/internet/splithttp/client.go +++ b/transport/internet/splithttp/client.go @@ -183,6 +183,9 @@ transport := c.client.Transport if h3Transport, ok := transport.(*http3.Transport); ok { h3Transport.Close() + } else { + // RayRay patch: this client is retired, and an idle connection of it would stay for ConnIdleTimeout (5 minutes). + c.client.CloseIdleConnections() } return nil } diff -ruN a/transport/internet/splithttp/dialer.go b/transport/internet/splithttp/dialer.go --- a/transport/internet/splithttp/dialer.go +++ b/transport/internet/splithttp/dialer.go @@ -43,6 +43,24 @@ globalDialerMap map[dialerConf]*XmuxManager globalDialerAccess sync.Mutex ) + +// ResetDialers retires every cached XMUX client and forgets the managers. +// RayRay patch: see grpc.ResetDialers. A client with requests still running is closed by DoneRunning, when the last one +// ends. The managers' slices are read under globalDialerAccess only, and no one can reach them once the map is swapped. +func ResetDialers() { + globalDialerAccess.Lock() + managers := globalDialerMap + globalDialerMap = nil + globalDialerAccess.Unlock() + go func() { + for _, manager := range managers { + for _, xmuxClient := range manager.xmuxClients { + xmuxClient.NotUsed.Store(true) + xmuxClient.maybeClose() + } + } + }() +} func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (DialerClient, *XmuxClient) { realityConfig := reality.ConfigFromStreamSettings(streamSettings)