Files
3x-ui/internal/tuic/protocol.go
T
Egor 0054e671f8 feat(tuic): implement native in-process Go TUIC v5 server (#6577)
* feat(tuic): implement native in-process Go TUIC v5 server

- Implement native TUIC v5 protocol server on pure Go using quic-go
- Bridge decrypted TCP/UDP traffic into Xray-core via loopback SOCKS5 inbound
- Support full Xray routing rules (geosite/geoip) and cascading outbounds
- Implement atomic per-client traffic accounting with TotalGB and ExpiryTime
- Add automatic legacy cleanup for older Rust tuic-server binaries, configs, and orphaned processes
- Eliminate external Rust tuic-server downloads from install/CI scripts

* fix(tuic): address traffic accounting, client reload, and socket lifecycle issues

* fix(service): update checkTuicSocksReverseConflict to use bindAddr for listenOverlaps

* fix(tuic): resolve traffic double-accounting, UDP fragmentation, and socket lifecycle issues

* feat(tuic): complete native Go integration and address audit findings

- Integrate an isolated QUIC fork pinned to a specific commit
- Preserve original QUIC dependencies for Xray, Hysteria and Gin
- Apply BBR, CUBIC and Reno to server connections and exported client profiles
- Bridge Xray BBR with correct monotonic time and congestion type conversions
- Handle congestion sender recreation after PMTU changes
- Update congestion control for new connections without restarting the listener
- Preserve existing connections and their selected congestion controller
- Apply per-inbound log levels through the shared panel logger
- Add lifecycle, authentication and TCP/UDP relay events without exposing secrets
- Rate-limit repeated authentication and relay warnings
- Support native and QUIC UDP relay modes on the same listener
- Recover UDP associations after relay worker failures
- Fix TCP relay cancellation, idle shutdown and half-close handling
- Close active sessions when client credentials are revoked or disabled
- Track traffic by immutable client statistics IDs across email and UUID changes
- Prevent ambiguous accounting and duplicate UUIDs within TUIC inbounds
- Persist pending traffic in a durable shutdown journal
- Replay journal batches transactionally without duplicate accounting
- Report server shutdown failures through the shared logger
- Preserve legacy flat and nested TUIC settings compatibility
- Normalize congestion controller values consistently across backend and frontend
- Preserve controller, UDP mode and SNI in client links and subscriptions
- Separate client profile options from server settings in the TUIC form
- Keep certificate path autofill explicit when changing client SNI
- Align UDP packet size validation with protocol limits
- Simplify and localize TUIC field hints and certificate autofill messages
- Add controller, TCP/UDP, logging and live settings update tests
- Add accounting identity, journal replay and shutdown regression tests
- Add relay recovery, session revocation and legacy frontend form tests

* fix(service): alias the TUIC duplicate-UUID subquery for PostgreSQL < 16

syncInboundClients runs a COUNT(*) FROM (subquery) for every client sync,
whatever the protocol. PostgreSQL before 16 rejects a FROM subquery with
no alias, so on the distro PostgreSQL install.sh provisions (14 on Ubuntu
22.04, 15 on Debian 12) every client add or edit failed with SQLSTATE
42601. Reproduced against postgres:15 with the new env-gated test.

* fix(database): create tuic_traffic_receipts through the model migration

AddTuicTrafficBatch issued CREATE TABLE IF NOT EXISTS at runtime, a schema
change outside db.go. The table was invisible to allModels and
migrationModels, so x-ui migrate-db dropped the receipts and a retained
journal could be counted twice after a SQLite to PostgreSQL move. It is
now a GORM model in both lists, and the insert uses OnConflict DoNothing.

* chore(tuic): skip the ICMP-dependent relay test on Windows, drop dead collectors

Go disables SIO_UDP_CONNRESET on Windows, so a dead UDP bridge never fails
a read there and TestAudit3UDPAssociationMustRecoverAfterBridgeReadFailure
was red on every Windows run. Server.CollectTotalTraffic and
Manager.CollectTraffic had no caller.

* refactor(tuic): serve TUIC on apernet/quic-go instead of a personal fork

The native server depended on github.com/poise52/quic-go, a personal fork
of apernet/quic-go patched only to pick the congestion controller before
the handshake. That put a second QUIC/TLS stack in the binary that no
upstream security fix reaches. apernet/quic-go is already in the graph
through xray-core and exposes SetCongestionControl, so BBR is now installed
on each accepted connection with Xray's own congestion.UseBBR; the
cross-module BBR adapter is gone.

apernet ships New Reno as its only built-in sender, so a cubic setting is
served as new_reno server-side (clients still get cubic in their profile).
The test inspectors now read the sender under congestionMutex, which the
post-handshake install writes under.

Linux loopback, single stream through Xray: 2428 -> 3383 Mbit/s (bbr).

---------

Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
2026-10-03 02:56:02 +02:00

226 lines
5.1 KiB
Go

package tuic
import (
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"strconv"
)
const (
// ProtocolVersion is the TUIC protocol version (0x05).
ProtocolVersion byte = 0x05
// Command types
CmdAuthenticate byte = 0x00
CmdConnect byte = 0x01
CmdPacket byte = 0x02
CmdDissociate byte = 0x03
CmdHeartbeat byte = 0x04
// Address types
AddrTypeDomain byte = 0x00
AddrTypeIPv4 byte = 0x01
AddrTypeIPv6 byte = 0x02
AddrTypeNone byte = 0xff
)
var (
ErrInvalidVersion = errors.New("tuic: invalid protocol version")
ErrInvalidCmd = errors.New("tuic: invalid command type")
ErrInvalidAddr = errors.New("tuic: invalid address format")
)
// Address represents a network endpoint (host + port) in TUIC v5.
type Address struct {
Type byte
Host string
IP net.IP
Port uint16
}
// String returns "host:port" suitable for net.Dial.
func (a *Address) String() string {
if a == nil || a.Type == AddrTypeNone {
return ""
}
if len(a.IP) > 0 {
return net.JoinHostPort(a.IP.String(), strconv.Itoa(int(a.Port)))
}
return net.JoinHostPort(a.Host, strconv.Itoa(int(a.Port)))
}
// ReadAddress decodes a TUIC v5 address from the reader.
func ReadAddress(r io.Reader) (*Address, error) {
var typeBuf [1]byte
if _, err := io.ReadFull(r, typeBuf[:]); err != nil {
return nil, err
}
addrType := typeBuf[0]
if addrType == AddrTypeNone {
return &Address{Type: AddrTypeNone}, nil
}
addr := &Address{Type: addrType}
switch addrType {
case AddrTypeIPv4:
var ip [4]byte
if _, err := io.ReadFull(r, ip[:]); err != nil {
return nil, err
}
addr.IP = net.IP(ip[:])
addr.Host = addr.IP.String()
case AddrTypeIPv6:
var ip [16]byte
if _, err := io.ReadFull(r, ip[:]); err != nil {
return nil, err
}
addr.IP = net.IP(ip[:])
addr.Host = addr.IP.String()
case AddrTypeDomain:
var lenBuf [1]byte
if _, err := io.ReadFull(r, lenBuf[:]); err != nil {
return nil, err
}
dLen := int(lenBuf[0])
if dLen == 0 {
return nil, ErrInvalidAddr
}
domainBuf := make([]byte, dLen)
if _, err := io.ReadFull(r, domainBuf); err != nil {
return nil, err
}
addr.Host = string(domainBuf)
default:
return nil, fmt.Errorf("%w: unknown type 0x%02x", ErrInvalidAddr, addrType)
}
var portBuf [2]byte
if _, err := io.ReadFull(r, portBuf[:]); err != nil {
return nil, err
}
addr.Port = binary.BigEndian.Uint16(portBuf[:])
return addr, nil
}
// WriteAddress encodes a TUIC v5 address to the writer.
func WriteAddress(w io.Writer, addr *Address) error {
if addr == nil || addr.Type == AddrTypeNone {
_, err := w.Write([]byte{AddrTypeNone})
return err
}
var buf []byte
switch addr.Type {
case AddrTypeIPv4:
ip4 := addr.IP.To4()
if len(ip4) != 4 {
return ErrInvalidAddr
}
buf = make([]byte, 1+4+2)
buf[0] = AddrTypeIPv4
copy(buf[1:5], ip4)
binary.BigEndian.PutUint16(buf[5:7], addr.Port)
case AddrTypeIPv6:
ip16 := addr.IP.To16()
if len(ip16) != 16 {
return ErrInvalidAddr
}
buf = make([]byte, 1+16+2)
buf[0] = AddrTypeIPv6
copy(buf[1:17], ip16)
binary.BigEndian.PutUint16(buf[17:19], addr.Port)
case AddrTypeDomain:
dLen := len(addr.Host)
if dLen == 0 || dLen > 255 {
return ErrInvalidAddr
}
buf = make([]byte, 1+1+dLen+2)
buf[0] = AddrTypeDomain
buf[1] = byte(dLen)
copy(buf[2:2+dLen], []byte(addr.Host))
binary.BigEndian.PutUint16(buf[2+dLen:4+dLen], addr.Port)
default:
return ErrInvalidAddr
}
_, err := w.Write(buf)
return err
}
// ReadCommand reads the 2-byte TUIC command header: [VER (1)][TYPE (1)].
func ReadCommand(r io.Reader) (byte, byte, error) {
var hdr [2]byte
if _, err := io.ReadFull(r, hdr[:]); err != nil {
return 0, 0, err
}
if hdr[0] != ProtocolVersion {
return hdr[0], hdr[1], fmt.Errorf("%w: got 0x%02x, want 0x%02x", ErrInvalidVersion, hdr[0], ProtocolVersion)
}
return hdr[0], hdr[1], nil
}
// PacketHeader represents the header of a UDP Packet command (0x02).
type PacketHeader struct {
AssocID uint16
PktID uint16
FragTotal uint8
FragID uint8
Size uint16
Addr *Address
}
// ReadPacketHeader reads the packet command fields following [VER][0x02].
func ReadPacketHeader(r io.Reader) (*PacketHeader, error) {
var fixed [8]byte
if _, err := io.ReadFull(r, fixed[:]); err != nil {
return nil, err
}
ph := &PacketHeader{
AssocID: binary.BigEndian.Uint16(fixed[0:2]),
PktID: binary.BigEndian.Uint16(fixed[2:4]),
FragTotal: fixed[4],
FragID: fixed[5],
Size: binary.BigEndian.Uint16(fixed[6:8]),
}
addr, err := ReadAddress(r)
if err != nil {
return nil, err
}
ph.Addr = addr
return ph, nil
}
// WritePacket writes a complete Packet command frame to w.
func WritePacket(w io.Writer, assocID, pktID uint16, fragTotal, fragID uint8, addr *Address, payload []byte) error {
hdr := make([]byte, 10)
hdr[0] = ProtocolVersion
hdr[1] = CmdPacket
binary.BigEndian.PutUint16(hdr[2:4], assocID)
binary.BigEndian.PutUint16(hdr[4:6], pktID)
hdr[6] = fragTotal
hdr[7] = fragID
binary.BigEndian.PutUint16(hdr[8:10], uint16(len(payload)))
if _, err := w.Write(hdr); err != nil {
return err
}
if err := WriteAddress(w, addr); err != nil {
return err
}
_, err := w.Write(payload)
return err
}