This commit is contained in:
2026-07-22 17:30:42 -03:00
parent b903775fb7
commit 3d64d6394b
11 changed files with 173 additions and 223 deletions
+16 -50
View File
@@ -18,10 +18,11 @@ type XrayNativeTuning struct {
const (
defaultNativeRuntimeGOMAXPROCS = 0
defaultNativeMuxGlobalSessions = 32768
defaultNativeMaxConnections = 32768
// XHTTP packet handlers are governed by HTTP/2 flow control and bounded byte
// queues, not a website-style request ceiling. A negative configured value is
// normalized to the internal unlimited representation.
// Transport sockets, XHTTP requests, and XHTTP sessions are VPN traffic, not
// website requests. Keep the legacy JSON fields for config compatibility, but
// always normalize them to unlimited. Actual resource protection is provided by
// socket/HTTP flow control and the bounded byte queues in xray_xhttp.go.
defaultNativeMaxConnections = -1
defaultNativeMaxXHTTPRequests = -1
fixedNativeMuxMaxSessions = 64
@@ -29,8 +30,7 @@ const (
fixedNativeMuxUDPReadBuffer = 256 * 1024
fixedNativeMuxUDPWriteBuffer = 256 * 1024
defaultNativeXHTTPMaxSessions = 32768
defaultNativeHTTP2MaxStreams = 1024
defaultNativeXHTTPMaxSessions = -1
// Packet-up posts are also protected by byte budgets in xray_xhttp.go. Keep
// the default reorder queue modest so thousands of unauthenticated sessions
// cannot consume large amounts of memory merely by allocating empty channel
@@ -48,9 +48,6 @@ const (
var (
nativeTuneRuntimeGOMAXPROCS atomic.Int64
nativeTuneMuxGlobalSessions atomic.Int64
nativeTuneMaxConnections atomic.Int64
nativeTuneMaxXHTTPRequests atomic.Int64
nativeTuneXHTTPMaxSessions atomic.Int64
nativeTuneTracePackets atomic.Bool
)
@@ -63,33 +60,18 @@ func normalizeNativeXrayTuning(t *XrayNativeTuning) XrayNativeTuning {
t = &XrayNativeTuning{}
}
out := *t
// Migrate the two profiles written by older panel builds. Those defaults were
// sized like a web service (4K/8K sessions and a global request cap) and cause
// valid high-volume XHTTP VPN traffic to be rejected after an upgrade unless
// the persisted values are translated here.
legacySafe := out.MaxConcurrentConnections == 4096 && out.MaxConcurrentXHTTPRequests == 8192 && out.XHTTPMaxSessions == 4096
legacy2K := out.MaxConcurrentConnections == 8192 && out.MaxConcurrentXHTTPRequests == 16384 && out.XHTTPMaxSessions == 8192
if legacySafe || legacy2K {
out.MuxGlobalSessions = defaultNativeMuxGlobalSessions
out.MaxConcurrentConnections = defaultNativeMaxConnections
out.MaxConcurrentXHTTPRequests = defaultNativeMaxXHTTPRequests
out.XHTTPMaxSessions = defaultNativeXHTTPMaxSessions
}
if out.RuntimeGOMAXPROCS < 0 {
out.RuntimeGOMAXPROCS = defaultNativeRuntimeGOMAXPROCS
}
if out.MuxGlobalSessions <= 0 {
out.MuxGlobalSessions = defaultNativeMuxGlobalSessions
}
if out.MaxConcurrentConnections == 0 {
out.MaxConcurrentConnections = defaultNativeMaxConnections
}
if out.MaxConcurrentXHTTPRequests == 0 {
out.MaxConcurrentXHTTPRequests = defaultNativeMaxXHTTPRequests
}
if out.XHTTPMaxSessions == 0 {
out.XHTTPMaxSessions = defaultNativeXHTTPMaxSessions
}
// Ignore every old positive/zero admission ceiling. This migration is
// deliberately unconditional so upgrading an existing server immediately
// removes the old 4K/8K/32K web-style caps without requiring a panel save.
out.MaxConcurrentConnections = defaultNativeMaxConnections
out.MaxConcurrentXHTTPRequests = defaultNativeMaxXHTTPRequests
out.XHTTPMaxSessions = defaultNativeXHTTPMaxSessions
return out
}
@@ -105,27 +87,12 @@ func applyNativeXrayTuning(t *XrayNativeTuning) XrayNativeTuning {
runtime.GOMAXPROCS(gomax)
nativeTuneRuntimeGOMAXPROCS.Store(int64(gomax))
nativeTuneMuxGlobalSessions.Store(int64(out.MuxGlobalSessions))
nativeTuneMaxConnections.Store(nativeLimitValue(out.MaxConcurrentConnections))
nativeTuneMaxXHTTPRequests.Store(nativeLimitValue(out.MaxConcurrentXHTTPRequests))
nativeTuneXHTTPMaxSessions.Store(nativeLimitValue(out.XHTTPMaxSessions))
nativeTuneTracePackets.Store(out.TracePackets)
return out
}
// Native tuning limits use zero internally for unlimited. In configuration,
// zero means "use the safe default" and any negative value disables the cap.
func nativeLimitValue(v int) int64 {
if v < 0 {
return 0
}
return int64(v)
}
func nativeRuntimeGOMAXPROCS() int { return int(nativeTuneRuntimeGOMAXPROCS.Load()) }
func nativeMuxGlobalSessionLimit() int { return int(nativeTuneMuxGlobalSessions.Load()) }
func nativeMaxConnectionLimit() int { return int(nativeTuneMaxConnections.Load()) }
func nativeMaxXHTTPRequestLimit() int { return int(nativeTuneMaxXHTTPRequests.Load()) }
func nativeXHTTPMaxSessionLimit() int { return int(nativeTuneXHTTPMaxSessions.Load()) }
func nativeTracePacketsEnabled() bool { return nativeTuneTracePackets.Load() }
func nativeMuxMaxSessionLimit() int { return fixedNativeMuxMaxSessions }
@@ -133,11 +100,10 @@ func nativeMuxUDPReadBufferSize() int { return fixedNativeMuxUDPReadBuffer }
func nativeMuxUDPWriteBufferSize() int { return fixedNativeMuxUDPWriteBuffer }
func nativeXHTTPBufferedPostLimit() int { return defaultNativeXHTTPBufferedPosts }
func nativeHTTP2MaxConcurrentStreams() uint32 {
limit := nativeMaxXHTTPRequestLimit()
if limit <= 0 || limit > defaultNativeHTTP2MaxStreams {
return defaultNativeHTTP2MaxStreams
}
return uint32(limit)
// x/net/http2 otherwise installs its own finite default when this is zero.
// Advertise the protocol's full uint32 range so ordinary packet-up bursts can
// never be refused by a website-style concurrent-stream setting.
return ^uint32(0)
}
func nativeMuxUDPIdleTimeout() time.Duration {
return fixedNativeMuxUDPIdleMS * time.Millisecond