feat(apply): guard managed target activation
Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
1 parent
71e37ffaef
commit
cdeb67f0dd
7 files changed
+914
-46
No files matched your search
+81
-36
@@ -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
|
||||
```
|
||||
+6
-4
@@ -4,14 +4,16 @@
|
||||
|
||||
## Готово
|
||||
|
||||
- Усилен guarded manual CLI `managed init|status|apply`: exact role `managed-test`, fixed ID `zapret-autotune-managed-v1`, provisioned runtime marker/base executables и private dedicated mountpoint `/var/lib/zapret-autotune/target` обязательны для всех команд. `ZAPRET_CONFIG` допускает только `/var/lib/zapret-autotune/target/config`; standalone full config разрешён только в новом target-owned volume, не поверх существующего service config (include fragments не реализованы). Proxy обязан совпадать с `socks5://127.0.0.1:$SOCKS_PORT`; проверяется ownership listening socket процессом `ss-local`. Старый контейнер/произвольный каталог/loopback proxy не проходят guards. Добавлены injected unit-тесты отказов и fresh-target success; transaction engine и shell config safety не изменены. Tests/build/network этого increment отложены producer; контейнер, deploy, remote и Xray не затронуты, image/config-loading compatibility ещё не доказана.
|
||||
|
||||
- Добавлена foundation schema v4: durable policy state по stable service ID, last probe/streak, recovery lock и cooldown. Операции с injected time атомарно записывают probe и захватывают recovery только при threshold/истёкшем cooldown/свободном lock; завершение требует точный ownership token и запускает cooldown от completion time. Stale/duplicate finish не меняет новую lease; crash сохраняет lock fail-closed (reconciliation пока не реализован). Unit-тесты streak/reset, threshold, competing acquisition, reload/cooldown и v3 migration прошли.
|
||||
- Добавлен одноразовый `health youtube`: bounded Player API-запрос только через loopback SOCKS (`ZAPRET_AUTOTUNE_SOCKS_PROXY`, default `socks5://127.0.0.1:11080`), без ambient proxy/direct fallback. Health требует `playabilityStatus.status=OK`, не streaming URL и не playback. Config валидирует video ID (default `dQw4w9WgXcQ`), interval `5m`, failure threshold `3`, cooldown `30m`. `go test ./...`, `go build` и `git diff --check` прошли для обоих последних increments.
|
||||
- Добавлена тестируемая transaction foundation для target-owned config: только квалифицированные stored candidates, separate frontend/exact-GGC profiles, versioned durable journal/backup, atomic replace, injected daemon reload + Player API post-check, rollback и crash reconciliation; external config drift fail-closed. Renderer не сохраняет wildcard GGC scope. CLI пока не вызывает apply, target config/runtime ещё не проверены. `go build` и повторный `go test ./...` прошли; первый test run поймал прежний flaky `TestRunBlockcheckCancellation/kill` process-state race.
|
||||
- Добавлена тестируемая transaction foundation для target-owned config: только квалифицированные stored candidates, separate frontend/exact-GGC profiles, versioned durable journal/backup, atomic replace, injected daemon reload + Player API post-check, rollback и crash reconciliation; external config drift fail-closed. Renderer не сохраняет wildcard GGC scope. На момент foundation CLI ещё не вызывал apply; теперь manual adapter есть, target config/runtime ещё не проверены. `go build` и повторный `go test ./...` прошли для foundation; первый test run поймал прежний flaky `TestRunBlockcheckCancellation/kill` process-state race.
|
||||
- Завершён удалённый ручной GGC scan для `rr16---sn-n8v7znsz.googlevideo.com`: TLS 1.2 — 1 525 рабочих стратегий, TLS 1.3 — 800. Shortlist check нашёл 20 проходящих вариантов; точная объединённая стратегия `--lua-init=fake_default_tls=tls_mod(fake_default_tls,'rnd') --payload=tls_client_hello --lua-desync=multidisorder:pos=2,midsld:seqovl=1:seqovl_pattern=fake_default_tls --in-range=-s1 --lua-desync=oob:urp=midsld` прошла по 3/3 для TLS 1.2 и TLS 1.3. Результат относится только к указанному hostname; не означает playback/SABR и не применялся.
|
||||
- Добавлена proposal-only команда `recover googlevideo --video-id ID --proxy socks5://HOST:PORT`: discovery одного свежего exact host → qualified schema-v3 сценарии newest-first → combined TLS1.2/1.3 3/3 точных сохранённых args. Первый успешный сценарий сохраняется новым immutable candidate/run и выдаётся как NOT ACTIVE без scan. Только отсутствие eligible candidates или валидные non-pass результаты разрешают обычный scan на уже найденном host; infrastructure/malformed-report ошибки fail closed. Apply/monitor и удалённый scan не затронуты.
|
||||
- Unit-тесты recovery порядка, fresh-host scope, short-circuit, fallback и fail-closed ошибок прошли; локальные `go test ./...`, `go build` и `git diff --check` успешны.
|
||||
|
||||
- CLI на Go: `tune youtube`, `tune googlevideo --video-id ID --proxy socks5://HOST:PORT`, `recover googlevideo --video-id ID --proxy socks5://HOST:PORT`, `discover youtube`, `health youtube`, `status`, `history`, `strategies youtube`, `render`. `check youtube` пока намеренно отказывает; `apply`, `rollback` и мониторинг не реализованы.
|
||||
- CLI на Go: `tune youtube`, `tune googlevideo --video-id ID --proxy socks5://HOST:PORT`, `recover googlevideo --video-id ID --proxy socks5://HOST:PORT`, `discover youtube`, `health youtube`, `status`, `history`, `strategies youtube`, `render`, `managed init|status|apply`. `check youtube` пока намеренно отказывает; manual managed apply только внутри designated target, rollback автоматический; мониторинг не реализован.
|
||||
- Proposal-only GGC pipeline: Android 21.02.35 discovery через явный proxy → scan TLS1.2/1.3 только одного найденного hostname → data-transfer shortlist (`--take 10 --passes 3`) → проверка точной комбинации 3/3 на обоих TLS → один инертный профиль с exact-host фильтром. Ранжирование по blockcheckw, не Selectel. SQLite v3 атомарно сохраняет googlevideo candidate и оба endpoint×TLS evidence; отдельный additive journal сохраняет failed runs. `history` и `strategies youtube` показывают точный endpoint/group; `status` пока относится только к frontend journal.
|
||||
- Добавлены injectable unit-тесты GGC success/order/scope/filter/persistence, обязательных флагов, discovery failure, неполной/неуспешной combined проверки и fail-closed DB. После исправления exact-host фильтра `go test ./...`, `go build` и `git diff --check` прошли локально. Полный новый `tune googlevideo` ещё не прогонялся удалённо.
|
||||
- `tune youtube` сканирует frontend `youtube.com`, проверяет shortlist через `blockcheckw` и выдаёт один TLS-профиль только после совместной проверки TLS 1.2 и 1.3 по 3/3. Это предложение конфигурации, оно ничего не применяет.
|
||||
@@ -36,14 +38,14 @@
|
||||
## Не готово
|
||||
|
||||
- Проверка реального YouTube video-transfer/SABR. Прямой GET `serverAbrStreamingUrl` возвращает `sabr.malformed_config`; hostname discovery и `blockcheckw` data-transfer checks не равны проверке воспроизведения видео.
|
||||
- Применение пока не подключено к CLI/контейнеру: disk transaction core есть, но нет proven zapret2 config/render/reload adapter, active profile integration, supervisor/recovery loop и реально запущенного managed target. Одноразовый Player API health и persisted policy пока не запускают automation. Цель: новый отдельный prod-like zapret2-ss контейнер только в тестовой среде; существующий контейнер не трогать. Стабильный настраиваемый loopback SOCKS для Xray; Xray config вне scope. Перед runtime пройти route/rollback gates.
|
||||
- Применение подключено только к guarded manual CLI внутри designated target: transaction core и adapter есть, но нет proven image/runtime compatibility, container provisioning, supervisor/recovery loop и реально запущенного managed target. Одноразовый Player API health и persisted policy пока не запускают automation. Цель: новый отдельный prod-like zapret2-ss контейнер только в тестовой среде; существующий контейнер не трогать. Стабильный настраиваемый loopback SOCKS для Xray; Xray config вне scope. Перед runtime пройти route/rollback gates.
|
||||
- Полный сервис из требований: YAML-конфигурация сервисов, endpoint-история/покрытие, daemon/периодический мониторинг, API/метрики и multi-service режим.
|
||||
|
||||
## Открытые ограничения и следующие шаги
|
||||
|
||||
1. Проверить новый `tune googlevideo` и `recover googlevideo` end-to-end на удалённом сервере; отдельно решить, как импортировать или переиспользовать уже полученные scan результаты без повторного перебора.
|
||||
2. Подготовить воспроизводимое описание отдельного prod-like target с закреплённым image digest, собственной bridge-сетью, независимой конфигурацией/секретами и только loopback-портами. До запуска проверить формат baseline-конфига и правила NFQUEUE внутри container network namespace; текущие mounts, сеть, порты и конфиг не переиспользовать.
|
||||
3. Подключить transaction core к target-specific CLI/agent; подтвердить shell config rendering, daemon-only reload и pre/post health на новом изолированном target.
|
||||
3. Проверить готовый manual managed CLI на новом изолированном target; подтвердить shell config rendering, config binding, daemon-only reload и pre/post health.
|
||||
4. Подключить persistent monitor policy к supervisor, recovery через сохранённые сценарии перед новым scan и crash reconciliation.
|
||||
5. До autonomous mode доказать safety gates из `Architecture.md`: изоляцию target, эквивалентность managed SOCKS пути, версионирование, atomic activation/rollback и crash recovery.
|
||||
6. Отдельно определить реальную video/SABR-проверку и traffic budget; API health не равен playback и не обнаруживает все GGC playback failures.
|
||||
@@ -2,6 +2,27 @@
|
||||
|
||||
Go CLI for scanning and checking YouTube TLS bypass strategies with `blockcheckw`.
|
||||
|
||||
## Manual managed-test activation (not deployed)
|
||||
|
||||
`managed init|status|apply` requires exact role `managed-test`, target ID
|
||||
`zapret-autotune-managed-v1`, a provisioned runtime marker and zapret executables,
|
||||
and a private dedicated mount at `/var/lib/zapret-autotune/target`.
|
||||
`ZAPRET_CONFIG` must equal `/var/lib/zapret-autotune/target/config`: the standalone
|
||||
full config is allowed only in that fresh target volume, never over an existing
|
||||
service config. Existing-service include fragments are not implemented.
|
||||
The managed proxy must equal `socks5://127.0.0.1:$SOCKS_PORT` and that listener
|
||||
must be owned by target-local `ss-local`; arbitrary loopback/host ports fail.
|
||||
Init creates a private disabled baseline and identity/journal without overwriting
|
||||
existing config. Status shows generation/hash/groups, not secrets or live health.
|
||||
Apply accepts `--frontend-candidate ID --googlevideo-candidate ID` and requires
|
||||
a qualified pair, fixed target init executable and explicit runtime-config
|
||||
binding. It uses daemon-only reload and target-local Player API post-check, with
|
||||
atomic rollback on failure. See [MANAGED_TARGET.md](MANAGED_TARGET.md) for guards
|
||||
and provisioning prerequisites. No container/deploy files, autonomous loop,
|
||||
remote activation, firewall setup or Xray changes are included. Image
|
||||
compatibility and route/rollback gates are not yet remotely proven; adapter
|
||||
tests/build are deferred to the orchestrator.
|
||||
|
||||
## One-shot Player API health
|
||||
|
||||
```sh
|
||||
@@ -35,7 +56,7 @@ threshold and cooldown are validated future recovery policy settings only:
|
||||
The future managed target is a **brand-new prod-like zapret2-ss container in the
|
||||
test environment only**, not the existing container. Its stable configurable
|
||||
SOCKS endpoint must be loopback-accessible to this probe and same-host Xray.
|
||||
The one-shot command does not persist health. This increment does not create or change any container, apply
|
||||
The one-shot command does not persist health. This command does not create or change any container, apply
|
||||
configuration, restart services or roll back anything. Xray configuration is
|
||||
out of scope and will be configured separately by the user. Run this command in
|
||||
the host network context where that loopback endpoint is reachable; Docker
|
||||
@@ -73,8 +94,9 @@ their existing target constraint. Database failures emit no options.
|
||||
Signed URLs are not printed. Scanner diagnostics are retained in restricted
|
||||
artifact files rather than relayed to the terminal. No configuration is applied,
|
||||
no profile is marked active, and no service is restarted. The staged pipeline is
|
||||
scan/propose now → eventual install → monitoring; install and monitoring are
|
||||
**not implemented**. Manual proposal-only recovery is available below.
|
||||
scan/propose → guarded manual managed-test activation → future monitoring;
|
||||
container provisioning and monitoring are **not implemented**. Manual
|
||||
proposal-only recovery is available below.
|
||||
No-argument `tune youtube` remains frontend-only and unchanged.
|
||||
|
||||
## Alternatives-first recovery (proposal only)
|
||||
@@ -129,7 +151,7 @@ and its TLS1.2/TLS1.3 3/3 pass results are committed together before options are
|
||||
printed; failed scans/checks are recorded as failed runs without a candidate.
|
||||
Database initialization or persistence errors return nonzero and emit no options.
|
||||
`status` shows the latest recorded run, or explicitly says there is no recorded
|
||||
run. Candidates are **PROPOSED, not active**; there is no apply command and
|
||||
run. Candidates are **PROPOSED, not active**; this journal does not track managed activation and
|
||||
production health is not checked. A later failed run is shown instead of an older
|
||||
proposal. An output-write failure can leave a verified proposal recorded, but
|
||||
still returns nonzero.
|
||||
@@ -158,7 +180,8 @@ the token does not introduce automatic expiry or crash reconciliation.
|
||||
State survives reopening the database; a crash during recovery leaves the lock
|
||||
held, pending future explicit crash reconciliation. Existing history/candidates
|
||||
are unchanged. These operations are not wired to `health youtube` or recovery:
|
||||
**no daemon, autonomous probe, apply or container/Xray changes are added**.
|
||||
**no daemon, autonomous probe or container/Xray changes are added**. Manual
|
||||
managed activation is separate from this policy foundation.
|
||||
|
||||
Schema v3 adds immutable strategy candidate definitions (group, declared exact
|
||||
endpoints, args and normalized-args fingerprint) and endpoint/protocol evidence.
|
||||
@@ -167,7 +190,8 @@ Each newly successful tune saves its exact verified combined frontend profile fo
|
||||
in the same transaction as the existing run. Qualification requires every declared
|
||||
endpoint to pass exactly 3/3 for both TLS1.2 and TLS1.3, with full success/coverage.
|
||||
Existing v2 history is unchanged and is not backfilled into independent candidates.
|
||||
Candidates have no active/installed state; apply and monitor remain unimplemented.
|
||||
Candidates have no DB active/installed state; managed activation uses its own
|
||||
durable config journal. Autonomous monitoring remains unimplemented.
|
||||
The explicit `tune googlevideo` path adds exact-cache proposals only.
|
||||
Proposed runs store a SHA-256 fingerprint of validated arguments with separator
|
||||
whitespace normalized and argument order preserved; repeated proposals retain
|
||||
|
||||
@@ -15,6 +15,8 @@ Commands:
|
||||
discover youtube --video-id ID --proxy socks5://HOST:PORT
|
||||
Discover cache hostname only (no media-transfer check)
|
||||
health youtube One-shot Player API health through configured loopback SOCKS
|
||||
managed init|status|apply
|
||||
Manual activation inside the designated managed-test target only
|
||||
check youtube Refuse checks until probe URLs/video IDs are configured and agreed
|
||||
tune youtube Test youtube.com frontend only; print options without applying
|
||||
tune googlevideo --video-id ID --proxy socks5://HOST:PORT
|
||||
@@ -33,6 +35,9 @@ func run(args []string, stdout, stderr io.Writer) int {
|
||||
}
|
||||
|
||||
func runWithPlayer(args []string, stdout, stderr io.Writer, player playerRunner) int {
|
||||
if len(args) > 0 && args[0] == "managed" {
|
||||
return managedCLI(args[1:], stdout, stderr, managedDependencies(player))
|
||||
}
|
||||
if len(args) == 2 && args[0] == "health" && args[1] == "youtube" {
|
||||
return healthYoutube(stdout, stderr, player)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,336 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
const managedMarkerName = ".autotune-target.json"
|
||||
const managedTargetID = "zapret-autotune-managed-v1"
|
||||
const managedTargetDirectory = "/var/lib/zapret-autotune/target"
|
||||
const managedTargetConfig = managedTargetDirectory + "/config"
|
||||
const managedBaseline = "# Autotuner-owned disabled baseline\nNFQWS2_ENABLE=0\nINIT_APPLY_FW=0\nNFQWS2_OPT=''\n"
|
||||
|
||||
type managedIdentity struct {
|
||||
Role, Directory, ID string
|
||||
}
|
||||
|
||||
type managedDeps struct {
|
||||
lookup func(string) (string, bool)
|
||||
openDB func() (*sql.DB, error)
|
||||
player playerRunner
|
||||
run func(string, ...string) error
|
||||
script, directory string
|
||||
verifyRuntime func(string) error
|
||||
verifySOCKS func(int) error
|
||||
}
|
||||
|
||||
func managedDependencies(player playerRunner) managedDeps {
|
||||
return managedDeps{
|
||||
lookup: os.LookupEnv, player: player,
|
||||
openDB: func() (*sql.DB, error) { return openHistoryStore(databasePath()) },
|
||||
script: "/opt/zapret2/init.d/sysv/zapret2", directory: managedTargetDirectory,
|
||||
verifyRuntime: verifyManagedRuntime, verifySOCKS: verifyManagedSOCKS,
|
||||
run: func(path string, args ...string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
if err := exec.CommandContext(ctx, path, args...).Run(); err != nil {
|
||||
return fmt.Errorf("daemon-only reload failed")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func managedArguments(args []string) (string, []int64, error) {
|
||||
if len(args) == 1 && (args[0] == "init" || args[0] == "status") {
|
||||
return args[0], nil, nil
|
||||
}
|
||||
if len(args) == 5 && args[0] == "apply" {
|
||||
ids := make([]int64, 2)
|
||||
seen := map[string]bool{}
|
||||
for i := 1; i < len(args); i += 2 {
|
||||
key := args[i]
|
||||
if seen[key] || (key != "--frontend-candidate" && key != "--googlevideo-candidate") || args[i+1] == "" || strings.Trim(args[i+1], "0123456789") != "" {
|
||||
break
|
||||
}
|
||||
seen[key] = true
|
||||
id, err := strconv.ParseInt(args[i+1], 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
break
|
||||
}
|
||||
index := 0
|
||||
if key == "--googlevideo-candidate" {
|
||||
index = 1
|
||||
}
|
||||
ids[index] = id
|
||||
}
|
||||
if ids[0] > 0 && ids[1] > 0 && ids[0] != ids[1] {
|
||||
return "apply", ids, nil
|
||||
}
|
||||
}
|
||||
return "", nil, fmt.Errorf("managed accepts init, status, or apply --frontend-candidate ID --googlevideo-candidate ID (positive distinct IDs); no path flags")
|
||||
}
|
||||
|
||||
// Provisioning supplies the directory. Never create it or follow aliases.
|
||||
func managedDirectory(dir string) error {
|
||||
if !filepath.IsAbs(dir) || filepath.Clean(dir) != dir || dir == "/" {
|
||||
return fmt.Errorf("ZAPRET_AUTOTUNE_TARGET_DIR requires an absolute dedicated clean directory")
|
||||
}
|
||||
for p := filepath.Dir(dir); ; p = filepath.Dir(p) {
|
||||
info, err := os.Lstat(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
|
||||
return fmt.Errorf("unsafe target ancestor")
|
||||
}
|
||||
if p == "/" {
|
||||
break
|
||||
}
|
||||
}
|
||||
info, err := os.Lstat(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stat, ok := info.Sys().(*syscall.Stat_t)
|
||||
if !info.IsDir() || info.Mode().Perm()&0077 != 0 || !ok || stat.Uid != uint32(os.Geteuid()) {
|
||||
return fmt.Errorf("target directory must be private and owned by this runtime user")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func managedPrivateFile(path string) error {
|
||||
info, err := os.Lstat(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stat, ok := info.Sys().(*syscall.Stat_t)
|
||||
if !info.Mode().IsRegular() || info.Mode().Perm()&0077 != 0 || !ok || stat.Uid != uint32(os.Geteuid()) || stat.Nlink != 1 {
|
||||
return fmt.Errorf("unsafe managed file")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func managedInitialize(m *managedTransaction) error {
|
||||
entries, err := os.ReadDir(m.Dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if entry.Name() != "config" {
|
||||
return fmt.Errorf("init requires a fresh dedicated directory (already initialized or nonempty)")
|
||||
}
|
||||
}
|
||||
path := filepath.Join(m.Dir, "config")
|
||||
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
|
||||
if err == nil {
|
||||
_, err = io.WriteString(f, managedBaseline)
|
||||
if err == nil {
|
||||
err = f.Sync()
|
||||
}
|
||||
closeErr := f.Close()
|
||||
if err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
} else if os.IsExist(err) {
|
||||
err = managedPrivateFile(path)
|
||||
if err == nil {
|
||||
var b []byte
|
||||
b, err = os.ReadFile(path)
|
||||
if err == nil && string(b) != managedBaseline {
|
||||
err = fmt.Errorf("existing config is not the disabled baseline; not overwritten or adopted")
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := m.Initialize(); err != nil {
|
||||
return err
|
||||
}
|
||||
id := make([]byte, 16)
|
||||
if _, err := rand.Read(id); err != nil {
|
||||
return err
|
||||
}
|
||||
marker, err := json.Marshal(managedIdentity{Role: "managed-test", Directory: m.Dir, ID: hex.EncodeToString(id)})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return m.replace(managedMarkerName, marker)
|
||||
}
|
||||
|
||||
func managedCheckIdentity(m *managedTransaction) error {
|
||||
for _, name := range []string{managedMarkerName, "config", managedJournalName, managedBackupName} {
|
||||
if err := managedPrivateFile(filepath.Join(m.Dir, name)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
b, err := m.read(managedMarkerName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var identity managedIdentity
|
||||
if err := json.Unmarshal(b, &identity); err != nil {
|
||||
return fmt.Errorf("invalid target identity")
|
||||
}
|
||||
id, err := hex.DecodeString(identity.ID)
|
||||
if err != nil || len(id) != 16 || identity.Role != "managed-test" || identity.Directory != m.Dir {
|
||||
return fmt.Errorf("invalid target identity")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func managedStatus(m *managedTransaction, out io.Writer) error {
|
||||
return m.locked(func() error {
|
||||
j, err := m.load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b, err := m.read("config")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if j.State != "committed" {
|
||||
return fmt.Errorf("pending transaction; managed apply must reconcile before activation")
|
||||
}
|
||||
if managedHash(b) != j.Hash {
|
||||
return errManagedDrift
|
||||
}
|
||||
groups := "none (disabled)"
|
||||
if strings.Contains(string(b), "\nNFQWS2_ENABLE=1\n") {
|
||||
groups = "frontend,googlevideo"
|
||||
}
|
||||
_, err = fmt.Fprintf(out, "managed-test generation=%d hash=%s groups=%s\nLive health: NOT CHECKED (Player API is not playback)\n", j.Generation, j.Hash, groups)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func managedCLI(args []string, stdout, stderr io.Writer, d managedDeps) int {
|
||||
command, ids, err := managedArguments(args)
|
||||
if err != nil {
|
||||
fmt.Fprintln(stderr, "error:", err)
|
||||
return 2
|
||||
}
|
||||
role, _ := d.lookup("ZAPRET_AUTOTUNE_TARGET_ROLE")
|
||||
if role != "managed-test" {
|
||||
fmt.Fprintln(stderr, "error: ZAPRET_AUTOTUNE_TARGET_ROLE must be managed-test")
|
||||
return 2
|
||||
}
|
||||
for key, expected := range map[string]string{
|
||||
"ZAPRET_AUTOTUNE_TARGET_ID": managedTargetID,
|
||||
"ZAPRET_AUTOTUNE_TARGET_DIR": managedTargetDirectory,
|
||||
"ZAPRET_CONFIG": managedTargetConfig,
|
||||
} {
|
||||
if value, _ := d.lookup(key); value != expected {
|
||||
fmt.Fprintln(stderr, "error:", key, "does not match the fresh managed target contract")
|
||||
return 2
|
||||
}
|
||||
}
|
||||
portText, _ := d.lookup("SOCKS_PORT")
|
||||
port, portErr := strconv.Atoi(portText)
|
||||
proxy, _ := d.lookup("ZAPRET_AUTOTUNE_SOCKS_PROXY")
|
||||
if portErr != nil || port < 1 || port > 65535 || strconv.Itoa(port) != portText || proxy != "socks5://127.0.0.1:"+portText {
|
||||
fmt.Fprintln(stderr, "error: managed SOCKS proxy must match the target SOCKS_PORT listener")
|
||||
return 2
|
||||
}
|
||||
if d.verifyRuntime == nil || d.verifySOCKS == nil {
|
||||
fmt.Fprintln(stderr, "error: managed runtime validators unavailable")
|
||||
return 1
|
||||
}
|
||||
if err := d.verifyRuntime(managedTargetDirectory); err != nil {
|
||||
fmt.Fprintln(stderr, "error:", err)
|
||||
return 1
|
||||
}
|
||||
if err := d.verifySOCKS(port); err != nil {
|
||||
fmt.Fprintln(stderr, "error:", err)
|
||||
return 1
|
||||
}
|
||||
dir := d.directory
|
||||
if err := managedDirectory(dir); err != nil {
|
||||
fmt.Fprintln(stderr, "error:", err)
|
||||
return 1
|
||||
}
|
||||
m := &managedTransaction{Dir: dir, Name: "config", Reload: func() error { return d.run(d.script, "restart-daemons") }, Health: func() (string, error) { return "", fmt.Errorf("health not configured") }}
|
||||
if command == "init" {
|
||||
err = managedInitialize(m)
|
||||
} else if err = managedCheckIdentity(m); err == nil {
|
||||
if command == "status" {
|
||||
err = managedStatus(m, stdout)
|
||||
} else {
|
||||
err = managedActivate(m, d, ids)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintln(stderr, "error:", err)
|
||||
return 1
|
||||
}
|
||||
if command != "status" {
|
||||
if _, err := fmt.Fprintln(stdout, "managed-test "+command+": OK"); err != nil {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func managedActivate(m *managedTransaction, d managedDeps, ids []int64) error {
|
||||
info, err := os.Stat(d.script)
|
||||
if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0111 == 0 {
|
||||
return fmt.Errorf("target zapret daemon init executable unavailable")
|
||||
}
|
||||
cfg, err := parseHealthConfig(d.lookup)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m.Health = func() (string, error) {
|
||||
portText, _ := d.lookup("SOCKS_PORT")
|
||||
port, err := strconv.Atoi(portText)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("target SOCKS_PORT unavailable")
|
||||
}
|
||||
if err := d.verifySOCKS(port); err != nil {
|
||||
return "", err
|
||||
}
|
||||
body, err := requestPlayer(cfg.VideoID, cfg.SOCKSProxy, d.player)
|
||||
if err == nil {
|
||||
err = playerHealth(body)
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "OK", nil
|
||||
}
|
||||
db, err := d.openDB()
|
||||
if err != nil {
|
||||
return fmt.Errorf("candidate database unavailable")
|
||||
}
|
||||
if db == nil {
|
||||
return fmt.Errorf("candidate database missing")
|
||||
}
|
||||
defer db.Close()
|
||||
for i, group := range []string{"frontend", "googlevideo"} {
|
||||
c, err := readStrategyCandidate(db, ids[i])
|
||||
if err != nil || c.Group != group {
|
||||
return fmt.Errorf("invalid %s candidate ID", group)
|
||||
}
|
||||
}
|
||||
if _, err := renderManagedConfig(db, ids); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := m.Reconcile(); err != nil {
|
||||
return err
|
||||
}
|
||||
return m.Apply(db, ids)
|
||||
}
|
||||
@@ -0,0 +1,374 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func managedCLIFixture(t *testing.T) (managedDeps, map[string]string, *int) {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
env := map[string]string{
|
||||
"ZAPRET_AUTOTUNE_TARGET_ROLE": "managed-test", "ZAPRET_AUTOTUNE_TARGET_ID": managedTargetID,
|
||||
"ZAPRET_AUTOTUNE_TARGET_DIR": managedTargetDirectory, "ZAPRET_CONFIG": managedTargetConfig,
|
||||
"SOCKS_PORT": "11080", "ZAPRET_AUTOTUNE_SOCKS_PROXY": "socks5://127.0.0.1:11080",
|
||||
}
|
||||
calls := new(int)
|
||||
d := managedDeps{
|
||||
lookup: func(key string) (string, bool) { value, ok := env[key]; return value, ok },
|
||||
script: filepath.Join(root, "zapret2"), directory: filepath.Join(root, "target"),
|
||||
verifyRuntime: func(dir string) error {
|
||||
if dir != managedTargetDirectory {
|
||||
return errors.New("wrong runtime directory")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
verifySOCKS: func(port int) error {
|
||||
if port != 11080 {
|
||||
return errors.New("wrong ss-local port")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
run: func(path string, args ...string) error {
|
||||
*calls++
|
||||
if path != filepath.Join(root, "zapret2") || !reflect.DeepEqual(args, []string{"restart-daemons"}) {
|
||||
t.Fatalf("unexpected executable/argv: %q %q", path, args)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
player: func(_ context.Context, args []string, stdout, _ io.Writer) error {
|
||||
if !strings.Contains(strings.Join(args, "|"), "--noproxy||--proxy|socks5://127.0.0.1:11080|") {
|
||||
t.Fatalf("unsafe health argv: %q", args)
|
||||
}
|
||||
_, err := io.WriteString(stdout, `{"playabilityStatus":{"status":"OK"}}`)
|
||||
return err
|
||||
},
|
||||
openDB: func() (*sql.DB, error) { t.Fatal("unexpected DB access"); return nil, nil },
|
||||
}
|
||||
if err := os.WriteFile(d.script, []byte("not executed"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Mkdir(d.directory, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return d, env, calls
|
||||
}
|
||||
|
||||
func callManaged(t *testing.T, d managedDeps, args ...string) (int, string, string) {
|
||||
t.Helper()
|
||||
var out, diagnostic bytes.Buffer
|
||||
code := managedCLI(args, &out, &diagnostic, d)
|
||||
return code, out.String(), diagnostic.String()
|
||||
}
|
||||
|
||||
func initManagedCLI(t *testing.T, d managedDeps) {
|
||||
t.Helper()
|
||||
if code, _, diagnostic := callManaged(t, d, "init"); code != 0 {
|
||||
t.Fatalf("init: %d %s", code, diagnostic)
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedCLIArguments(t *testing.T) {
|
||||
for _, args := range [][]string{nil, {"unknown"}, {"init", "--dir", "/etc"}, {"status", "extra"}, {"apply"}, {"apply", "--frontend-candidate", "1", "--frontend-candidate", "2"}, {"apply", "--frontend-candidate", "1", "--googlevideo-candidate", "1"}, {"apply", "--frontend-candidate", "-1", "--googlevideo-candidate", "2"}, {"apply", "--frontend-candidate", "1;id", "--googlevideo-candidate", "2"}, {"apply", "--frontend-candidate", "9223372036854775808", "--googlevideo-candidate", "2"}, {"apply", "--frontend-candidate", "0", "--googlevideo-candidate", "2"}} {
|
||||
d, _, calls := managedCLIFixture(t)
|
||||
if code, _, _ := callManaged(t, d, args...); code != 2 || *calls != 0 {
|
||||
t.Fatalf("accepted %q", args)
|
||||
}
|
||||
}
|
||||
command, ids, err := managedArguments([]string{"apply", "--googlevideo-candidate", "2", "--frontend-candidate", "1"})
|
||||
if err != nil || command != "apply" || !reflect.DeepEqual(ids, []int64{1, 2}) {
|
||||
t.Fatalf("%s %v %v", command, ids, err)
|
||||
}
|
||||
// Exercise the actual top-level dispatch without a runner or network call.
|
||||
t.Setenv("ZAPRET_AUTOTUNE_TARGET_ROLE", "old-container")
|
||||
var out, diagnostic bytes.Buffer
|
||||
if code := run([]string{"managed", "status"}, &out, &diagnostic); code != 2 || !strings.Contains(diagnostic.String(), "TARGET_ROLE") {
|
||||
t.Fatalf("dispatch: %d %s", code, diagnostic.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedCLIRoleAndDirectoryGuards(t *testing.T) {
|
||||
for _, role := range []string{"", "dev", "managed-test ", "old-container"} {
|
||||
d, env, _ := managedCLIFixture(t)
|
||||
env["ZAPRET_AUTOTUNE_TARGET_ROLE"] = role
|
||||
if code, _, _ := callManaged(t, d, "init"); code != 2 {
|
||||
t.Fatalf("accepted role %q", role)
|
||||
}
|
||||
if entries, err := os.ReadDir(d.directory); err != nil || len(entries) != 0 {
|
||||
t.Fatal("guard wrote target directory")
|
||||
}
|
||||
}
|
||||
for _, path := range []string{"", "relative", "/", "/tmp/../etc"} {
|
||||
d, env, _ := managedCLIFixture(t)
|
||||
env["ZAPRET_AUTOTUNE_TARGET_DIR"] = path
|
||||
if code, _, _ := callManaged(t, d, "init"); code == 0 {
|
||||
t.Fatalf("accepted path %q", path)
|
||||
}
|
||||
}
|
||||
for _, mode := range []string{"symlink", "public", "ancestor"} {
|
||||
d, env, _ := managedCLIFixture(t)
|
||||
dir := d.directory
|
||||
var err error
|
||||
if mode == "public" {
|
||||
err = os.Chmod(dir, 0755)
|
||||
} else {
|
||||
if err := os.Remove(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = os.Symlink(t.TempDir(), dir)
|
||||
if mode == "ancestor" {
|
||||
env["ZAPRET_AUTOTUNE_TARGET_DIR"] = filepath.Join(dir, "nested")
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if code, _, _ := callManaged(t, d, "init"); code == 0 {
|
||||
t.Fatalf("accepted %s", mode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedCLIInitNonOverwrite(t *testing.T) {
|
||||
d, _, calls := managedCLIFixture(t)
|
||||
dir := d.directory
|
||||
path := filepath.Join(dir, "config")
|
||||
secret := "EXISTING_SECRET='do not overwrite'\n"
|
||||
if err := os.WriteFile(path, []byte(secret), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if code, _, _ := callManaged(t, d, "init"); code == 0 {
|
||||
t.Fatal("adopted non-baseline config")
|
||||
}
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil || string(b) != secret || *calls != 0 {
|
||||
t.Fatalf("config=%q err=%v", b, err)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(managedBaseline), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
initManagedCLI(t, d)
|
||||
if code, _, _ := callManaged(t, d, "init"); code == 0 {
|
||||
t.Fatal("reinitialized identity")
|
||||
}
|
||||
if code, out, diagnostic := callManaged(t, d, "status"); code != 0 || !strings.Contains(out, "generation=0") || !strings.Contains(out, "none (disabled)") {
|
||||
t.Fatalf("status %d %s %s", code, out, diagnostic)
|
||||
}
|
||||
if *calls != 0 {
|
||||
t.Fatal("init/status reloaded")
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedCLIApplyAndRollback(t *testing.T) {
|
||||
for _, mode := range []string{"success", "health", "reload", "unqualified", "wrong-group", "missing-id", "missing-script", "wrong-binding", "bad-proxy", "missing-marker", "drift"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
d, env, calls := managedCLIFixture(t)
|
||||
initManagedCLI(t, d)
|
||||
db := testStore(t)
|
||||
front := managedCandidateFixture(t, db, "frontend", "youtube.com", "--lua-desync=fake", mode != "unqualified")
|
||||
ggc := managedCandidateFixture(t, db, "googlevideo", "rr1.googlevideo.com", "--lua-desync=multisplit", true)
|
||||
d.openDB = func() (*sql.DB, error) { return db, nil }
|
||||
config := filepath.Join(d.directory, "config")
|
||||
switch mode {
|
||||
case "health":
|
||||
d.player = func(_ context.Context, _ []string, stdout, _ io.Writer) error {
|
||||
_, err := io.WriteString(stdout, `{"playabilityStatus":{"status":"ERROR","secret":"do-not-print"}}`)
|
||||
return err
|
||||
}
|
||||
case "reload":
|
||||
fixed := d.run
|
||||
d.run = func(path string, args ...string) error {
|
||||
if err := fixed(path, args...); err != nil {
|
||||
return err
|
||||
}
|
||||
if *calls == 1 {
|
||||
return errors.New("reload failed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
case "wrong-group":
|
||||
front, ggc = ggc, front
|
||||
case "missing-id":
|
||||
front = 999999
|
||||
case "missing-script":
|
||||
if err := os.Remove(d.script); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case "wrong-binding":
|
||||
env["ZAPRET_CONFIG"] = "/opt/zapret2/config"
|
||||
case "bad-proxy":
|
||||
env["ZAPRET_AUTOTUNE_SOCKS_PROXY"] = "socks5://192.0.2.1:1080"
|
||||
case "missing-marker":
|
||||
if err := os.Remove(filepath.Join(d.directory, managedMarkerName)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case "drift":
|
||||
if err := os.WriteFile(config, []byte("EXTERNAL_SECRET=private\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
code, out, diagnostic := callManaged(t, d, "apply", "--frontend-candidate", fmt.Sprint(front), "--googlevideo-candidate", fmt.Sprint(ggc))
|
||||
if (mode == "success") != (code == 0) {
|
||||
t.Fatalf("apply: %d %s %s", code, out, diagnostic)
|
||||
}
|
||||
if strings.Contains(out+diagnostic, "do-not-print") || strings.Contains(out+diagnostic, "EXTERNAL_SECRET") {
|
||||
t.Fatal("leaked config/response")
|
||||
}
|
||||
b, err := os.ReadFile(config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
wantCalls := 0
|
||||
if mode == "success" {
|
||||
wantCalls = 1
|
||||
if !strings.Contains(string(b), "--hostlist-domains=^rr1.googlevideo.com") {
|
||||
t.Fatalf("wrong applied scope: %s", b)
|
||||
}
|
||||
code, out, diagnostic = callManaged(t, d, "status")
|
||||
if code != 0 || !strings.Contains(out, "generation=1") || !strings.Contains(out, "groups=frontend,googlevideo") || strings.Contains(out, "lua-desync") || strings.Contains(out, "rr1") {
|
||||
t.Fatalf("status: %d %s %s", code, out, diagnostic)
|
||||
}
|
||||
} else if mode == "drift" {
|
||||
if string(b) != "EXTERNAL_SECRET=private\n" {
|
||||
t.Fatal("drift overwritten")
|
||||
}
|
||||
} else {
|
||||
if string(b) != managedBaseline {
|
||||
t.Fatalf("baseline not restored: %s", b)
|
||||
}
|
||||
if mode == "health" || mode == "reload" {
|
||||
wantCalls = 2
|
||||
}
|
||||
}
|
||||
if *calls != wantCalls {
|
||||
t.Fatalf("reloads=%d want=%d", *calls, wantCalls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedCLIIdentityAndStatusRedaction(t *testing.T) {
|
||||
for _, mode := range []string{"identity", "symlink", "hardlink", "journal", "drift"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
d, _, calls := managedCLIFixture(t)
|
||||
initManagedCLI(t, d)
|
||||
dir := d.directory
|
||||
path := filepath.Join(dir, managedMarkerName)
|
||||
switch mode {
|
||||
case "identity":
|
||||
if err := os.WriteFile(path, []byte(`{"Role":"dev","Directory":"secret"}`), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case "symlink", "hardlink":
|
||||
outside := filepath.Join(t.TempDir(), "outside")
|
||||
if err := os.Rename(path, outside); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var err error
|
||||
if mode == "symlink" {
|
||||
err = os.Symlink(outside, path)
|
||||
} else {
|
||||
err = os.Link(outside, path)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if mode == "hardlink" {
|
||||
if err := os.Link(outside, outside+"-second"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
case "journal":
|
||||
if err := os.Remove(filepath.Join(dir, managedJournalName)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case "drift":
|
||||
if err := os.WriteFile(filepath.Join(dir, "config"), []byte("PASSWORD=secret\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
code, out, diagnostic := callManaged(t, d, "status")
|
||||
if code == 0 || strings.Contains(out+diagnostic, "secret") || *calls != 0 {
|
||||
t.Fatalf("status: %d %s %s", code, out, diagnostic)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedCLIFreshTargetContract(t *testing.T) {
|
||||
for _, mode := range []string{"wrong-id", "missing-id", "old-env", "other-directory", "old-config", "other-config", "non-mountpoint", "missing-base-runtime", "wrong-port", "other-loopback", "wrong-process", "missing-proxy", "missing-port"} {
|
||||
for _, command := range []string{"init", "status", "apply"} {
|
||||
t.Run(mode+"/"+command, func(t *testing.T) {
|
||||
d, env, calls := managedCLIFixture(t)
|
||||
if command != "init" {
|
||||
initManagedCLI(t, d)
|
||||
}
|
||||
switch mode {
|
||||
case "wrong-id":
|
||||
env["ZAPRET_AUTOTUNE_TARGET_ID"] = "another-target"
|
||||
case "missing-id":
|
||||
delete(env, "ZAPRET_AUTOTUNE_TARGET_ID")
|
||||
case "old-env":
|
||||
// A role and a self-created directory/identity alone cannot pass.
|
||||
delete(env, "ZAPRET_AUTOTUNE_TARGET_ID")
|
||||
delete(env, "ZAPRET_CONFIG")
|
||||
delete(env, "SOCKS_PORT")
|
||||
case "other-directory":
|
||||
env["ZAPRET_AUTOTUNE_TARGET_DIR"] = d.directory
|
||||
case "old-config":
|
||||
env["ZAPRET_CONFIG"] = "/opt/zapret2/config"
|
||||
case "other-config":
|
||||
env["ZAPRET_CONFIG"] = "/etc/config"
|
||||
case "non-mountpoint", "missing-base-runtime":
|
||||
d.verifyRuntime = func(string) error { return errors.New(mode) }
|
||||
case "wrong-port":
|
||||
env["SOCKS_PORT"] = "1080"
|
||||
case "other-loopback":
|
||||
env["ZAPRET_AUTOTUNE_SOCKS_PROXY"] = "socks5://127.0.0.1:1080"
|
||||
case "wrong-process":
|
||||
d.verifySOCKS = func(int) error { return errors.New("listener belongs to another process") }
|
||||
case "missing-proxy":
|
||||
delete(env, "ZAPRET_AUTOTUNE_SOCKS_PROXY")
|
||||
case "missing-port":
|
||||
delete(env, "SOCKS_PORT")
|
||||
}
|
||||
args := []string{command}
|
||||
if command == "apply" {
|
||||
args = append(args, "--frontend-candidate", "1", "--googlevideo-candidate", "2")
|
||||
}
|
||||
if code, _, _ := callManaged(t, d, args...); code == 0 || *calls != 0 {
|
||||
t.Fatal("unsafe target contract accepted or reloaded")
|
||||
}
|
||||
if command == "init" {
|
||||
entries, err := os.ReadDir(d.directory)
|
||||
if err != nil || len(entries) != 0 {
|
||||
t.Fatal("refused init modified target")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedMountpoint(t *testing.T) {
|
||||
for _, path := range []string{"/", filepath.Dir(managedTargetDirectory), managedTargetDirectory + "-other"} {
|
||||
if managedMountpoint("1 2 0:3 / "+path+" rw - tmpfs tmpfs rw\n", managedTargetDirectory) {
|
||||
t.Fatal("accepted another mount")
|
||||
}
|
||||
}
|
||||
if !managedMountpoint("1 2 0:3 / "+managedTargetDirectory+" rw - ext4 /dev/test rw\n", managedTargetDirectory) {
|
||||
t.Fatal("dedicated mount rejected")
|
||||
}
|
||||
if err := verifyManagedRuntime("/opt/zapret2"); err == nil {
|
||||
t.Fatal("production verifier accepted arbitrary directory")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Provisioning, not managed init, supplies this marker and the dedicated mount.
|
||||
func verifyManagedRuntime(dir string) error {
|
||||
if dir != managedTargetDirectory {
|
||||
return fmt.Errorf("unexpected managed target path")
|
||||
}
|
||||
marker, err := os.ReadFile("/etc/zapret-autotune/target-id")
|
||||
if err != nil || string(marker) != managedTargetID+"\n" {
|
||||
return fmt.Errorf("fresh target runtime marker unavailable")
|
||||
}
|
||||
mounts, err := os.ReadFile("/proc/self/mountinfo")
|
||||
if err != nil || !managedMountpoint(string(mounts), dir) {
|
||||
return fmt.Errorf("managed target directory must be a dedicated mountpoint")
|
||||
}
|
||||
for _, path := range []string{"/opt/zapret2/init.d/sysv/zapret2", "/opt/zapret2/nfq2/nfqws2", "/usr/bin/blockcheckw"} {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0111 == 0 {
|
||||
return fmt.Errorf("zapret base runtime executable unavailable")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func managedMountpoint(mountinfo, dir string) bool {
|
||||
for _, line := range strings.Split(mountinfo, "\n") {
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) >= 10 && fields[4] == dir {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Match the listening socket to an ss-local executable in this PID/network
|
||||
// namespace. Merely finding a process or an arbitrary loopback proxy is unsafe.
|
||||
func verifyManagedSOCKS(port int) error {
|
||||
tcp, err := os.ReadFile("/proc/net/tcp")
|
||||
if err != nil {
|
||||
return fmt.Errorf("target SOCKS listener unavailable")
|
||||
}
|
||||
inodes := map[string]bool{}
|
||||
for _, line := range strings.Split(string(tcp), "\n") {
|
||||
f := strings.Fields(line)
|
||||
if len(f) > 9 && f[3] == "0A" && (f[1] == fmt.Sprintf("0100007F:%04X", port) || f[1] == fmt.Sprintf("00000000:%04X", port)) {
|
||||
inodes["socket:["+f[9]+"]"] = true
|
||||
}
|
||||
}
|
||||
processes, err := os.ReadDir("/proc")
|
||||
if err != nil {
|
||||
return fmt.Errorf("target ss-local runtime unavailable")
|
||||
}
|
||||
for _, process := range processes {
|
||||
if _, err := strconv.Atoi(process.Name()); err != nil {
|
||||
continue
|
||||
}
|
||||
root := filepath.Join("/proc", process.Name())
|
||||
exe, err := os.Readlink(filepath.Join(root, "exe"))
|
||||
if err != nil || filepath.Base(exe) != "ss-local" {
|
||||
continue
|
||||
}
|
||||
fds, err := os.ReadDir(filepath.Join(root, "fd"))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, fd := range fds {
|
||||
link, err := os.Readlink(filepath.Join(root, "fd", fd.Name()))
|
||||
if err == nil && inodes[link] {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("target SOCKS_PORT listener is not owned by ss-local")
|
||||
}
|
||||
Reference in new issue
Block a user