feat(apply): guard managed target activation

Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
ZeusinaandGPT-6 Bot committed 2026-10-10 08:29:56 +03:00
1 parent 71e37ffaef
commit cdeb67f0dd
7 files changed
+914 -46

No files matched your search

+81 -36
View File
@@ -1,41 +1,86 @@
# Local managed-target transaction core (not deployed)
# Fresh managed-test target contract (not deployed)
`managedTransaction` is intentionally not wired to the CLI. No container, remote,
production, existing `zapret2-ss-autotune`, or Xray configuration is changed.
The caller explicitly provisions a private persistent target-owned directory and
existing regular config, then calls `Initialize` once to adopt that config.
Missing/corrupt journals never trigger automatic adoption. The journal is the
source of truth; the DB provides immutable candidate definitions and qualification
evidence only. Call `Reconcile` before accepting work after every process startup.
`managed init|status|apply` is exclusively for a **new dedicated container**
which owns its entire config. The existing/dev/production container must refuse
these commands. A role env or CLI-created identity is not enough. Provisioning
must supply all of the following before any managed command can run:
Apply uses candidate IDs, distinct profiles and exact `^host` GGC filters. Shell
syntax is rejected, not interpreted. Journal/backup/config updates use same-dir
staging, file fsync, rename and directory fsync. An advisory lock serializes engine
instances; the directory must not be writable by untrusted processes. Hash checks
reject external drift (including during recovery). This is not protection against
a hostile writer racing a hash check or renaming directory ancestors. Crash-left
temporary files are inert, never adopted or promoted. Failed recovery blocks apply;
an operator must resolve drift/corruption explicitly, without deleting the journal
to silently adopt a different config.
| Setting | Exact required value |
| --- | --- |
| `ZAPRET_AUTOTUNE_TARGET_ROLE` | `managed-test` |
| `ZAPRET_AUTOTUNE_TARGET_ID` | `zapret-autotune-managed-v1` |
| `ZAPRET_AUTOTUNE_TARGET_DIR` | `/var/lib/zapret-autotune/target` |
| `ZAPRET_CONFIG` | `/var/lib/zapret-autotune/target/config` |
| `SOCKS_PORT` | Base image's actual ss-local service port (canonical decimal, 1–65535) |
| `ZAPRET_AUTOTUNE_SOCKS_PROXY` | `socks5://127.0.0.1:$SOCKS_PORT` (expanded value) |
Reload and health are injected no-argument functions. The future adapter must bind
reload ONLY to `/opt/zapret2/init.d/sysv/zapret2 restart-daemons`, never `start` or
firewall restart, and health ONLY to existing `health youtube` through the target's
configured host-loopback SOCKS, returning `OK` only for its successful status.
No command strings or shell scripts are persisted in transaction metadata.
On activation failure the prior config is atomically restored and reloaded; a
rollback reload failure retains a pending journal and is a distinct error. Recovery
always restores pending transactions, even after a successful pre-crash probe.
It marks recovery committed only after the injected reloader succeeds.
The fixed directory must already be a private runtime-user-owned **dedicated
persistent volume mountpoint**, not just a directory under another mount.
The production verifier checks the exact path in `/proc/self/mountinfo`.
Provisioning must also supply `/etc/zapret-autotune/target-id` containing exactly
`zapret-autotune-managed-v1` followed by a newline. Init never writes that marker.
Base runtime executables must exist: `/opt/zapret2/init.d/sysv/zapret2`,
`/opt/zapret2/nfq2/nfqws2`, and `/usr/bin/blockcheckw`.
There are no arbitrary directory flags or env overrides of these fixed values.
These are operator safety guards, not protection against a privileged user
deliberately forging a provisioned runtime.
Pending remote acceptance: create ONLY a new test prod-like `zapret2-ss` target
using the pinned image digest; verify config variable names, shell include loading,
payload/Lua dependencies, profile semantics and `restart-daemons` behavior against
the image's v0.9.0 scripts/blockcheckw. No actual compatibility is asserted here.
Later integration must preserve the stable loopback SOCKS interface for Xray,
own only this target's NFQWS2 config/process, and reuse DB v4 health state with
policy env defaults of 5 minutes / 3 failures / 30 minutes. Scheduling, environment
parsing, adapters, target creation and user-facing CLI are deliberately deferred.
The managed `config` is a standalone **full config only in this fresh volume**.
Never replace, adopt, or symlink over `/opt/zapret2/config` or any existing
service config. Future provisioning/entrypoint must ensure the base daemon
actually honors the exact `ZAPRET_CONFIG` path; compatibility remains unproven.
Existing services would require a separately designed managed **include
fragment**, not this full-config adapter. No such integration is implemented.
Local checks for the orchestrator (not executed in this increment):
`go test ./cmd/zapret-autotune -run TestManaged` and `go build ./cmd/zapret-autotune`.
```sh
# Only after fresh-target provisioning establishes the entire contract above:
zapret-autotune managed init
zapret-autotune managed status
zapret-autotune managed apply --frontend-candidate 1 --googlevideo-candidate 2
```
Init refuses nonempty directories except an exact private disabled baseline;
it never overwrites/adopts another config. It creates identity, journal and
backup; repeated initialization, symlinks, hardlinks, public/unowned files and
unsafe ancestors are refused. Status reports committed generation/hash/groups,
not config, secrets or live health; drift and pending transactions fail closed.
Init/status do not reload or probe, but both verify runtime and listener ownership.
All managed commands verify that the expected IPv4 listener is owned by a running
`ss-local` executable in the target PID/network namespace: `/proc/net/tcp` socket
inodes must match that process's file descriptors. A process alone, a wrong port,
or another loopback proxy is insufficient. Health repeats this validation before
its Player API request. The host's published SOCKS port may map to this listener,
but is never used inside the target. The standalone `health youtube` command
retains its separate generic loopback contract; managed activation is stricter.
Ambient proxies/direct fallback are disabled. Player API OK is **not playback**.
Apply requires distinct positive qualified frontend/exact-GGC SQLite candidate
IDs, complete endpoint × TLS evidence and inert shell-safe arguments. It opens
the candidate DB read-only via `ZAPRET_AUTOTUNE_DB`; candidates are not active
state. Bindings/inputs are checked before pending transaction reconciliation.
Production reload remains exclusively the fixed init executable with
`restart-daemons`, no shell, with a 30-second deadline; no firewall restart.
The transaction engine is unchanged: same-directory staging/fsync/rename,
durable journal/backup, advisory locking, external drift rejection, rollback
and fail-closed crash reconciliation. Failed rollback reload remains pending.
Directories must not be writable by untrusted processes; hostile concurrent
filesystem mutation is outside the transaction boundary. Shell config safety
and exact GGC filtering are unchanged.
No Docker socket, deployment/container/remote changes, old target paths, Xray
edits, monitor loop or firewall provisioning are included. New-target image
compatibility, actual config loading, NFQUEUE routing and rollback gates still
need independent acceptance. Persistent volume durability is a provisioning
requirement; mountinfo alone cannot establish the storage lifecycle.
Injected unit tests cover exact identity/path/config, mountpoint refusal,
old-container-like env, SOCKS port/process refusal, correct fresh-target success,
non-overwrite, transaction rollback and redaction. Tests/build/network are
deferred to the producer; not run by this increment:
```sh
go test ./cmd/zapret-autotune -run TestManaged
go build ./cmd/zapret-autotune
```