diff --git a/MANAGED_TARGET.md b/MANAGED_TARGET.md index e78304b..c7ca9e3 100644 --- a/MANAGED_TARGET.md +++ b/MANAGED_TARGET.md @@ -59,8 +59,23 @@ 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. +Production reload is exclusively the fixed argv +`/opt/zapret2/init.d/sysv/zapret2 restart`, no shell, with a 30-second deadline. +Apply and rollback both use this full firewall/daemon restart; daemon-only +restart is not a supported fallback. This changes only the **new target +container's dedicated network namespace**, never the host or old container's +firewall. Provisioning must not use host networking or share an existing +container's network namespace. + +The baseline explicitly sets `INIT_APPLY_FW=1`, `NFQWS2_ENABLE=0`, empty +`NFQWS2_OPT`, TCP ports `80,443`, UDP ports `443`, and TCP/UDP in/out packet +limits of `15`. QNUM is deliberately inherited from the base runtime default. +Active configs keep those fixed firewall values and set `NFQWS2_ENABLE=1` +with the qualified managed strategy arguments. Restoring the baseline and +running full restart removes its target-local queues and stops nfqws2; +restoring an earlier active config reinstates its firewall/daemon state. +The config replacement is atomic; the init script's stop/start sequence is +not a zero-interruption or kernel-atomic firewall transaction. The transaction engine is unchanged: same-directory staging/fsync/rename, durable journal/backup, advisory locking, external drift rejection, rollback @@ -70,9 +85,14 @@ 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 +edits, monitor loop or host firewall provisioning are included. Before deployment, +provisioning must verify the pinned image honors `ZAPRET_CONFIG`, supports all +listed variables, and starts ss-local while the baseline leaves nfqws2 disabled. +Start the fresh target with that baseline, then verify active apply installs +target-local nft NFQUEUE rules and starts nfqws2, and rollback restores both. +These image/startup/routing gates remain unverified: if any is unsupported or +uncertain, deployment must fail closed, not substitute `restart-daemons` or +alter an old config. Persistent volume durability is a provisioning requirement; mountinfo alone cannot establish the storage lifecycle. Injected unit tests cover exact identity/path/config, mountpoint refusal, diff --git a/cmd/zapret-autotune/managed_cli.go b/cmd/zapret-autotune/managed_cli.go index 549ad7f..d8f0575 100644 --- a/cmd/zapret-autotune/managed_cli.go +++ b/cmd/zapret-autotune/managed_cli.go @@ -21,7 +21,7 @@ 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" +const managedBaseline = "# Autotuner-owned disabled baseline\nNFQWS2_ENABLE=0\n" + managedFirewallConfig + "NFQWS2_OPT=''\n" type managedIdentity struct { Role, Directory, ID string @@ -47,7 +47,7 @@ func managedDependencies(player playerRunner) managedDeps { 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 fmt.Errorf("target-local firewall/daemon restart failed: %w", err) } return nil }, @@ -263,7 +263,7 @@ func managedCLI(args []string, stdout, stderr io.Writer, d managedDeps) int { 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") }} + m := &managedTransaction{Dir: dir, Name: "config", Reload: func() error { return d.run(d.script, "restart") }, Health: func() (string, error) { return "", fmt.Errorf("health not configured") }} if command == "init" { err = managedInitialize(m) } else if err = managedCheckIdentity(m); err == nil { diff --git a/cmd/zapret-autotune/managed_cli_test.go b/cmd/zapret-autotune/managed_cli_test.go index be02011..a838c0a 100644 --- a/cmd/zapret-autotune/managed_cli_test.go +++ b/cmd/zapret-autotune/managed_cli_test.go @@ -40,7 +40,7 @@ func managedCLIFixture(t *testing.T) (managedDeps, map[string]string, *int) { }, run: func(path string, args ...string) error { *calls++ - if path != filepath.Join(root, "zapret2") || !reflect.DeepEqual(args, []string{"restart-daemons"}) { + if path != filepath.Join(root, "zapret2") || !reflect.DeepEqual(args, []string{"restart"}) { t.Fatalf("unexpected executable/argv: %q %q", path, args) } return nil @@ -75,6 +75,14 @@ func initManagedCLI(t *testing.T, d managedDeps) { if code, _, diagnostic := callManaged(t, d, "init"); code != 0 { t.Fatalf("init: %d %s", code, diagnostic) } + b, err := os.ReadFile(filepath.Join(d.directory, "config")) + if err != nil { + t.Fatal(err) + } + assertManagedFirewallConfig(t, string(b), "0") + if !strings.Contains(string(b), "\nNFQWS2_OPT=''\n") { + t.Fatal("baseline strategy is not disabled") + } } func TestManagedCLIArguments(t *testing.T) { @@ -96,6 +104,13 @@ func TestManagedCLIArguments(t *testing.T) { } } +func TestManagedProductionRestartExecutable(t *testing.T) { + d := managedDependencies(nil) + if d.script != "/opt/zapret2/init.d/sysv/zapret2" { + t.Fatalf("unexpected production restart executable: %q", d.script) + } +} + func TestManagedCLIRoleAndDirectoryGuards(t *testing.T) { for _, role := range []string{"", "dev", "managed-test ", "old-container"} { d, env, _ := managedCLIFixture(t) @@ -178,6 +193,22 @@ func TestManagedCLIApplyAndRollback(t *testing.T) { 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") + fixedRestart := d.run + d.run = func(path string, args ...string) error { + if err := fixedRestart(path, args...); err != nil { + return err + } + b, err := os.ReadFile(config) + if err != nil { + t.Fatal(err) + } + if *calls == 1 { + assertManagedFirewallConfig(t, string(b), "1") + } else if string(b) != managedBaseline { + t.Fatalf("rollback restart did not load disabled baseline: %s", b) + } + return nil + } switch mode { case "health": d.player = func(_ context.Context, _ []string, stdout, _ io.Writer) error { @@ -230,6 +261,7 @@ func TestManagedCLIApplyAndRollback(t *testing.T) { wantCalls := 0 if mode == "success" { wantCalls = 1 + assertManagedFirewallConfig(t, string(b), "1") if !strings.Contains(string(b), "--hostlist-domains=^rr1.googlevideo.com") { t.Fatalf("wrong applied scope: %s", b) } diff --git a/cmd/zapret-autotune/managed_config.go b/cmd/zapret-autotune/managed_config.go index 2e9c156..33623b1 100644 --- a/cmd/zapret-autotune/managed_config.go +++ b/cmd/zapret-autotune/managed_config.go @@ -6,6 +6,12 @@ import ( "strings" ) +// Shared by the disabled baseline and active configs so rollback restores the +// same target-local firewall contract. QNUM stays at the base runtime default. +const managedFirewallConfig = "INIT_APPLY_FW=1\n" + + "NFQWS2_PORTS_TCP=80,443\nNFQWS2_PORTS_UDP=443\n" + + "NFQWS2_TCP_PKT_OUT=15\nNFQWS2_TCP_PKT_IN=15\nNFQWS2_UDP_PKT_OUT=15\nNFQWS2_UDP_PKT_IN=15\n" + // renderManagedConfig accepts identifiers, not caller-supplied strategies or scope. // Qualification is the existing complete endpoint × TLS evidence rule. func renderManagedConfig(db *sql.DB, ids []int64) ([]byte, error) { @@ -50,7 +56,6 @@ func renderManagedConfig(db *sql.DB, ids []int64) ([]byte, error) { // All values are shell-inert after validateArgs; no shell evaluation/escaping // contract is extended here. QNUM is intentionally left at the target default. return []byte("# Autotuner-owned standalone config; runtime acceptance pending.\n" + - "NFQWS2_ENABLE=1\nNFQWS2_PORTS_TCP=80,443\nNFQWS2_PORTS_UDP=443\n" + - "NFQWS2_TCP_PKT_OUT=15\nNFQWS2_TCP_PKT_IN=15\nNFQWS2_UDP_PKT_OUT=15\nNFQWS2_UDP_PKT_IN=15\n" + - "INIT_APPLY_FW=1\nNFQWS2_OPT='" + strings.Join(profiles, " --new ") + "'\n"), nil + "NFQWS2_ENABLE=1\n" + managedFirewallConfig + + "NFQWS2_OPT='" + strings.Join(profiles, " --new ") + "'\n"), nil } diff --git a/cmd/zapret-autotune/managed_transaction.go b/cmd/zapret-autotune/managed_transaction.go index b7ffca0..d9dfba0 100644 --- a/cmd/zapret-autotune/managed_transaction.go +++ b/cmd/zapret-autotune/managed_transaction.go @@ -33,10 +33,11 @@ type managedJournal struct { Next string `json:"pending_hash,omitempty"` } -// Hooks take no commands or arguments. The future target adapter must bind Reload -// exclusively to /opt/zapret2/init.d/sysv/zapret2 restart-daemons and Health to +// Hooks take no commands or arguments. The target adapter binds Reload +// exclusively to /opt/zapret2/init.d/sysv/zapret2 restart and Health to // health youtube over its configured loopback SOCKS, returning exactly "OK". -// This engine does not manage firewall, SOCKS, health scheduling or DB active state. +// Reload switches target-local firewall and daemons for apply and rollback. +// This engine does not manage SOCKS, health scheduling or DB active state. type managedTransaction struct { Dir, Name string Reload func() error diff --git a/cmd/zapret-autotune/managed_transaction_test.go b/cmd/zapret-autotune/managed_transaction_test.go index 02638c7..6e17a96 100644 --- a/cmd/zapret-autotune/managed_transaction_test.go +++ b/cmd/zapret-autotune/managed_transaction_test.go @@ -45,6 +45,7 @@ func TestManagedRender(t *testing.T) { t.Fatal(err) } s := string(b) + assertManagedFirewallConfig(t, s, "1") for _, want := range []string{"NFQWS2_ENABLE=1\n", "INIT_APPLY_FW=1\n", "NFQWS2_PORTS_TCP=80,443\n", "NFQWS2_PORTS_UDP=443\n", "--hostlist-domains=www.youtube.com --lua-desync=multisplit --new --filter-tcp=443 --filter-l7=tls --hostlist-domains=^rr1---sn-test.googlevideo.com --lua-desync=fake"} { if !strings.Contains(s, want) { t.Fatalf("missing %q in %s", want, s) @@ -62,6 +63,23 @@ func TestManagedRender(t *testing.T) { } } +func assertManagedFirewallConfig(t *testing.T, config, enabled string) { + t.Helper() + for _, line := range []string{ + "NFQWS2_ENABLE=" + enabled, "INIT_APPLY_FW=1", + "NFQWS2_PORTS_TCP=80,443", "NFQWS2_PORTS_UDP=443", + "NFQWS2_TCP_PKT_OUT=15", "NFQWS2_TCP_PKT_IN=15", + "NFQWS2_UDP_PKT_OUT=15", "NFQWS2_UDP_PKT_IN=15", + } { + if strings.Count(config, "\n"+line+"\n") != 1 { + t.Fatalf("expected exactly one %q in %s", line, config) + } + } + if strings.Contains(config, "QNUM=") { + t.Fatal("queue default overridden") + } +} + func TestManagedRenderRejectsUnsafeArgs(t *testing.T) { for _, args := range []string{"--lua-desync=fake'", "--lua-desync=fake\n", "--lua-desync=fake;touch /tmp/x", "--lua-desync=$(id)", "--lua-desync=fake`id`", "--lua-desync=fake\"", "--lua-desync=fake --new", "--hostlist-domains=googlevideo.com"} { t.Run(args, func(t *testing.T) {