mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-05 13:42:07 +03:00
2ffc694a6d
Invariant: every inbound the master holds for a node, with its clients and
enable flag, converges onto that node at the next push or reconcile.
823db059 made an inbound save keep the stored clients and enable unless the
request is a master push, recognised only by a node-sync token scope. Nodes
are usually enrolled with an admin token (the -getApiToken and install
default), so their nodes treated every master push as a stale form save:
clients attached on the master (Attach existing clients, bulk attach above
the per-client threshold, any dirty-node reconcile) never reached the node,
yet the push was recorded as successful so reconcile stopped retrying.
Remote now marks every request with X-3x-Master-Push, and the node honours
it like the node-sync scope. The browser form never sends it, so the
stale-form protection for panel saves and plain API scripts is unchanged.
Separately, an inbound deleted on the node kept its tag->id in the
master's cache, so reconcile sent update/<deleted id> forever instead of
re-creating it. When the node reports no form of the tag, ReconcileInbound
now drops the cached id so the resolve re-reads the node and falls back to
add. Two runtime tests pinned the old update-to-cached-id path for an
absent inbound; they now count the add, or report the inbound present.
60 lines
1.8 KiB
Go
60 lines
1.8 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"
|
|
|
|
"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"
|
|
|
|
// 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 (
|
|
zstdEncoder, _ = zstd.NewWriter(nil)
|
|
zstdDecoder, _ = zstd.NewReader(nil, zstd.WithDecoderMaxMemory(maxDecodeBytes))
|
|
)
|
|
|
|
// 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) {
|
|
out, err := zstdDecoder.DecodeAll(src, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if maxOut > 0 && len(out) > maxOut {
|
|
return nil, errors.New("wirecodec: decompressed body exceeds limit")
|
|
}
|
|
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[:])
|
|
}
|