Files
3x-ui/internal/tuic/udp_pending.go
T
Egor 207c705300 fix(tuic): preserve client identity for Xray user routing (#6875)
TUIC traffic reached Xray through a loopback SOCKS relay that took no login,
so Xray never learned which client a connection belonged to and user routing
rules never matched TUIC (#6874). The relay now carries one account per client
email, so per-user rules match on TCP and both UDP modes; admission stays with
the native TUIC server.

Adding, removing or renaming a TUIC client swaps only that inbound's relay
instead of restarting Xray: new sessions wait behind a bounded gate while live
streams and UDP associations continue, accounts are verified after the swap and
rolled back on failure, one failing relay no longer strands the others or blocks
unrelated live changes, and emails over SOCKS5's 255-byte limit are excluded
with a warning.
2026-10-24 06:20:39 +02:00

114 lines
3.0 KiB
Go

package tuic
import (
"context"
"errors"
"sync/atomic"
)
const (
maxPendingUdpBytesPerInbound = 1 << 20
maxPendingUdpBytesPerConn = 128 << 10
maxPendingUdpPackets = 16
udpPacketOverhead = 256
)
var errUdpQueueFull = errors.New("tuic: pending UDP packet limit reached")
type pendingUdpBudget struct{ bytes atomic.Int64 }
func (b *pendingUdpBudget) reserve(size int) bool {
if b == nil {
return true
}
for {
used := b.bytes.Load()
if used+int64(size) > maxPendingUdpBytesPerInbound {
return false
}
if b.bytes.CompareAndSwap(used, used+int64(size)) {
return true
}
}
}
type pendingUdpPacket struct {
target *Address
payload []byte
}
func (r *udpAssociationRegistry) queuePacket(ctx context.Context, id uint16, expected *udpAssociation, target *Address, payload []byte) (*udpRelaySession, context.Context, error) {
r.mu.Lock()
defer r.mu.Unlock()
if r.associations[id] != expected {
return nil, nil, errUdpAssociationClosed
}
if expected.relay != nil && !expected.opening {
return expected.relay, nil, nil
}
charge := len(payload) + udpPacketOverhead
if len(expected.pending) >= maxPendingUdpPackets || r.pendingBytes+charge > maxPendingUdpBytesPerConn || !r.pendingBudget.reserve(charge) {
return nil, nil, errUdpQueueFull
}
r.pendingBytes += charge
addr := *target
addr.IP = append([]byte(nil), target.IP...)
expected.pending = append(expected.pending, pendingUdpPacket{target: &addr, payload: append([]byte(nil), payload...)})
if expected.opening {
return nil, nil, nil
}
expected.opening = true
opening, cancel := context.WithCancel(ctx)
expected.openCancel = cancel
return nil, opening, nil
}
func (r *udpAssociationRegistry) nextPending(id uint16, expected *udpAssociation) (pendingUdpPacket, bool) {
r.mu.Lock()
defer r.mu.Unlock()
if r.associations[id] != expected || len(expected.pending) == 0 {
expected.opening = false
if expected.openCancel != nil {
expected.openCancel()
expected.openCancel = nil
}
return pendingUdpPacket{}, false
}
packet := expected.pending[0]
expected.pending[0] = pendingUdpPacket{}
expected.pending = expected.pending[1:]
return packet, true
}
func (r *udpAssociationRegistry) releasePendingPacketLocked(packet pendingUdpPacket) {
charge := len(packet.payload) + udpPacketOverhead
r.pendingBytes -= charge
if r.pendingBudget != nil {
r.pendingBudget.bytes.Add(-int64(charge))
}
}
func (r *udpAssociationRegistry) releasePendingPacket(packet pendingUdpPacket) {
r.mu.Lock()
r.releasePendingPacketLocked(packet)
r.mu.Unlock()
}
func (r *udpAssociationRegistry) clearPendingLocked(association *udpAssociation) {
for _, packet := range association.pending {
r.releasePendingPacketLocked(packet)
}
association.pending = nil
association.opening = false
if association.openCancel != nil {
association.openCancel()
association.openCancel = nil
}
}
func (r *udpAssociationRegistry) failPending(expected *udpAssociation) {
r.mu.Lock()
r.clearPendingLocked(expected)
r.mu.Unlock()
}