mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-01 06:54:47 +03:00
1ec55aed30
Every five seconds the master fetched each node's complete inbound list, settings JSON and enrichment included, parsed it and reconciled every client, whether anything had changed or not. A node that advertises traffic-snapshot-v1 now answers a revisioned endpoint: counters and lifecycle state on every poll, inbound configuration and attachments only when its revision changed. The master skips structural parsing and client reconciliation while the node's revision, its own rows for that node, the lifecycle state and the full remote-to-central inbound mapping are all unchanged; anything else falls back to the full merge. Old and new panels keep talking through the previous endpoints. Revisions come from a hidden per-row token on inbounds and clients, maintained by database triggers on SQLite and PostgreSQL so that direct and background writes count too. Counter and timestamp updates leave it alone. The triggers are dropped before migrations and recreated after them, and the previous build runs unchanged on a database that carries them. Measured here at 20,000 clients on one node, per idle poll: master SQL statements 551 -> 44, node statements 55 -> 4, transfer 23.0 MB -> 4.8 MB. When every client is shared between nodes the ownership checks keep the full master merge, so only the node and the transfer get cheaper. The node still reads every membership and counter on each poll. Closes #7007
96 lines
3.5 KiB
Go
96 lines
3.5 KiB
Go
// Package wirecodec holds the shared envelope codec for node-to-node config
|
|
// transport: zstd (de)compression, SHA-256 integrity hashing, and the header /
|
|
// capability constants both the panel (sender) and node (receiver) agree on.
|
|
package wirecodec
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"runtime"
|
|
|
|
"github.com/klauspost/compress/zstd"
|
|
)
|
|
|
|
const (
|
|
// HashHeader carries the lowercase-hex SHA-256 of the (uncompressed) body.
|
|
HashHeader = "X-Config-Sha256"
|
|
// CapsHeader is set by a node on its API responses to advertise support.
|
|
CapsHeader = "X-3x-Node-Caps"
|
|
// MasterPushHeader marks a request as a master's push, whatever its token scope.
|
|
MasterPushHeader = "X-3x-Master-Push"
|
|
// EncodingZstd is the Content-Encoding value for a zstd-compressed body.
|
|
EncodingZstd = "zstd"
|
|
// CapZstd is the capability token advertised in CapsHeader.
|
|
CapZstd = "zstd"
|
|
// CapAmneziaWGKernel marks a node that honours an AmneziaWG inbound's
|
|
// kernelMode; an older node drops the flag and serves it embedded.
|
|
CapAmneziaWGKernel = "awg-kernel"
|
|
// CapPreserveClientTraffic marks a node that supports inbound-only scheduled resets.
|
|
CapPreserveClientTraffic = "preserve-client-traffic"
|
|
// CapWireguardDevices marks a node that serves a WireGuard client's extra devices;
|
|
// an older node ignores them and keeps only the client's first peer.
|
|
CapWireguardDevices = "wg-devices"
|
|
// CapTrafficSnapshot marks a node that serves revisioned traffic snapshots.
|
|
CapTrafficSnapshot = "traffic-snapshot-v1"
|
|
// NodeCaps is the full CapsHeader value this build advertises.
|
|
NodeCaps = CapZstd + "," + CapAmneziaWGKernel + "," + CapPreserveClientTraffic + "," + CapWireguardDevices + "," + CapTrafficSnapshot
|
|
|
|
// maxDecodeBytes bounds in-memory decompression to defuse a zstd bomb from
|
|
// an (authenticated) node-API caller.
|
|
maxDecodeBytes = 16 << 20
|
|
)
|
|
|
|
// EncodeAll/DecodeAll on these shared instances are safe for concurrent use.
|
|
var (
|
|
decoderWorkers = runtime.GOMAXPROCS(0)
|
|
zstdEncoder, _ = zstd.NewWriter(nil)
|
|
zstdDecoder, _ = zstd.NewReader(nil, zstd.WithDecoderMaxMemory(maxDecodeBytes),
|
|
zstd.WithDecodeAllCapLimit(true), zstd.WithDecoderConcurrency(decoderWorkers))
|
|
// decodeSlots keeps callers waiting for a decoder worker from each holding an output buffer.
|
|
decodeSlots = make(chan struct{}, decoderWorkers)
|
|
errDecodedTooLarge = errors.New("wirecodec: decompressed body exceeds limit")
|
|
)
|
|
|
|
// Compress zstd-compresses b.
|
|
func Compress(b []byte) []byte {
|
|
return zstdEncoder.EncodeAll(b, nil)
|
|
}
|
|
|
|
// Decompress zstd-decompresses src, rejecting output larger than maxOut (and any
|
|
// input that would blow the in-memory bomb ceiling).
|
|
func Decompress(src []byte, maxOut int) ([]byte, error) {
|
|
if maxOut <= 0 || maxOut > maxDecodeBytes {
|
|
maxOut = maxDecodeBytes
|
|
}
|
|
var header zstd.Header
|
|
if err := header.Decode(src); err != nil {
|
|
return nil, err
|
|
}
|
|
// The output buffer is sized once, from the first frame (Compress writes a single
|
|
// frame): letting it grow per frame copied all output so far for every extra frame.
|
|
size := maxOut
|
|
if header.HasFCS {
|
|
if header.FrameContentSize > uint64(maxOut) {
|
|
return nil, errDecodedTooLarge
|
|
}
|
|
size = int(header.FrameContentSize)
|
|
}
|
|
decodeSlots <- struct{}{}
|
|
defer func() { <-decodeSlots }()
|
|
out, err := zstdDecoder.DecodeAll(src, make([]byte, 0, size))
|
|
if errors.Is(err, zstd.ErrDecoderSizeExceeded) {
|
|
return nil, errDecodedTooLarge
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Sha256Hex returns the lowercase-hex SHA-256 of b.
|
|
func Sha256Hex(b []byte) string {
|
|
sum := sha256.Sum256(b)
|
|
return hex.EncodeToString(sum[:])
|
|
}
|