fix(apply): restart target firewall with strategy
Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
1 parent
cdeb67f0dd
commit
0b4b909a20
6 files changed
+91
-15
No files matched your search
+25
-5
@@ -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
|
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
|
the candidate DB read-only via `ZAPRET_AUTOTUNE_DB`; candidates are not active
|
||||||
state. Bindings/inputs are checked before pending transaction reconciliation.
|
state. Bindings/inputs are checked before pending transaction reconciliation.
|
||||||
Production reload remains exclusively the fixed init executable with
|
Production reload is exclusively the fixed argv
|
||||||
`restart-daemons`, no shell, with a 30-second deadline; no firewall restart.
|
`/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,
|
The transaction engine is unchanged: same-directory staging/fsync/rename,
|
||||||
durable journal/backup, advisory locking, external drift rejection, rollback
|
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.
|
and exact GGC filtering are unchanged.
|
||||||
|
|
||||||
No Docker socket, deployment/container/remote changes, old target paths, Xray
|
No Docker socket, deployment/container/remote changes, old target paths, Xray
|
||||||
edits, monitor loop or firewall provisioning are included. New-target image
|
edits, monitor loop or host firewall provisioning are included. Before deployment,
|
||||||
compatibility, actual config loading, NFQUEUE routing and rollback gates still
|
provisioning must verify the pinned image honors `ZAPRET_CONFIG`, supports all
|
||||||
need independent acceptance. Persistent volume durability is a provisioning
|
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.
|
requirement; mountinfo alone cannot establish the storage lifecycle.
|
||||||
|
|
||||||
Injected unit tests cover exact identity/path/config, mountpoint refusal,
|
Injected unit tests cover exact identity/path/config, mountpoint refusal,
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ const managedMarkerName = ".autotune-target.json"
|
|||||||
const managedTargetID = "zapret-autotune-managed-v1"
|
const managedTargetID = "zapret-autotune-managed-v1"
|
||||||
const managedTargetDirectory = "/var/lib/zapret-autotune/target"
|
const managedTargetDirectory = "/var/lib/zapret-autotune/target"
|
||||||
const managedTargetConfig = managedTargetDirectory + "/config"
|
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 {
|
type managedIdentity struct {
|
||||||
Role, Directory, ID string
|
Role, Directory, ID string
|
||||||
@@ -47,7 +47,7 @@ func managedDependencies(player playerRunner) managedDeps {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if err := exec.CommandContext(ctx, path, args...).Run(); err != nil {
|
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
|
return nil
|
||||||
},
|
},
|
||||||
@@ -263,7 +263,7 @@ func managedCLI(args []string, stdout, stderr io.Writer, d managedDeps) int {
|
|||||||
fmt.Fprintln(stderr, "error:", err)
|
fmt.Fprintln(stderr, "error:", err)
|
||||||
return 1
|
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" {
|
if command == "init" {
|
||||||
err = managedInitialize(m)
|
err = managedInitialize(m)
|
||||||
} else if err = managedCheckIdentity(m); err == nil {
|
} else if err = managedCheckIdentity(m); err == nil {
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func managedCLIFixture(t *testing.T) (managedDeps, map[string]string, *int) {
|
|||||||
},
|
},
|
||||||
run: func(path string, args ...string) error {
|
run: func(path string, args ...string) error {
|
||||||
*calls++
|
*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)
|
t.Fatalf("unexpected executable/argv: %q %q", path, args)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -75,6 +75,14 @@ func initManagedCLI(t *testing.T, d managedDeps) {
|
|||||||
if code, _, diagnostic := callManaged(t, d, "init"); code != 0 {
|
if code, _, diagnostic := callManaged(t, d, "init"); code != 0 {
|
||||||
t.Fatalf("init: %d %s", code, diagnostic)
|
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) {
|
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) {
|
func TestManagedCLIRoleAndDirectoryGuards(t *testing.T) {
|
||||||
for _, role := range []string{"", "dev", "managed-test ", "old-container"} {
|
for _, role := range []string{"", "dev", "managed-test ", "old-container"} {
|
||||||
d, env, _ := managedCLIFixture(t)
|
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)
|
ggc := managedCandidateFixture(t, db, "googlevideo", "rr1.googlevideo.com", "--lua-desync=multisplit", true)
|
||||||
d.openDB = func() (*sql.DB, error) { return db, nil }
|
d.openDB = func() (*sql.DB, error) { return db, nil }
|
||||||
config := filepath.Join(d.directory, "config")
|
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 {
|
switch mode {
|
||||||
case "health":
|
case "health":
|
||||||
d.player = func(_ context.Context, _ []string, stdout, _ io.Writer) error {
|
d.player = func(_ context.Context, _ []string, stdout, _ io.Writer) error {
|
||||||
@@ -230,6 +261,7 @@ func TestManagedCLIApplyAndRollback(t *testing.T) {
|
|||||||
wantCalls := 0
|
wantCalls := 0
|
||||||
if mode == "success" {
|
if mode == "success" {
|
||||||
wantCalls = 1
|
wantCalls = 1
|
||||||
|
assertManagedFirewallConfig(t, string(b), "1")
|
||||||
if !strings.Contains(string(b), "--hostlist-domains=^rr1.googlevideo.com") {
|
if !strings.Contains(string(b), "--hostlist-domains=^rr1.googlevideo.com") {
|
||||||
t.Fatalf("wrong applied scope: %s", b)
|
t.Fatalf("wrong applied scope: %s", b)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,12 @@ import (
|
|||||||
"strings"
|
"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.
|
// renderManagedConfig accepts identifiers, not caller-supplied strategies or scope.
|
||||||
// Qualification is the existing complete endpoint × TLS evidence rule.
|
// Qualification is the existing complete endpoint × TLS evidence rule.
|
||||||
func renderManagedConfig(db *sql.DB, ids []int64) ([]byte, error) {
|
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
|
// All values are shell-inert after validateArgs; no shell evaluation/escaping
|
||||||
// contract is extended here. QNUM is intentionally left at the target default.
|
// contract is extended here. QNUM is intentionally left at the target default.
|
||||||
return []byte("# Autotuner-owned standalone config; runtime acceptance pending.\n" +
|
return []byte("# Autotuner-owned standalone config; runtime acceptance pending.\n" +
|
||||||
"NFQWS2_ENABLE=1\nNFQWS2_PORTS_TCP=80,443\nNFQWS2_PORTS_UDP=443\n" +
|
"NFQWS2_ENABLE=1\n" + managedFirewallConfig +
|
||||||
"NFQWS2_TCP_PKT_OUT=15\nNFQWS2_TCP_PKT_IN=15\nNFQWS2_UDP_PKT_OUT=15\nNFQWS2_UDP_PKT_IN=15\n" +
|
"NFQWS2_OPT='" + strings.Join(profiles, " --new ") + "'\n"), nil
|
||||||
"INIT_APPLY_FW=1\nNFQWS2_OPT='" + strings.Join(profiles, " --new ") + "'\n"), nil
|
|
||||||
}
|
}
|
||||||
@@ -33,10 +33,11 @@ type managedJournal struct {
|
|||||||
Next string `json:"pending_hash,omitempty"`
|
Next string `json:"pending_hash,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hooks take no commands or arguments. The future target adapter must bind Reload
|
// Hooks take no commands or arguments. The target adapter binds Reload
|
||||||
// exclusively to /opt/zapret2/init.d/sysv/zapret2 restart-daemons and Health to
|
// exclusively to /opt/zapret2/init.d/sysv/zapret2 restart and Health to
|
||||||
// health youtube over its configured loopback SOCKS, returning exactly "OK".
|
// 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 {
|
type managedTransaction struct {
|
||||||
Dir, Name string
|
Dir, Name string
|
||||||
Reload func() error
|
Reload func() error
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ func TestManagedRender(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
s := string(b)
|
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"} {
|
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) {
|
if !strings.Contains(s, want) {
|
||||||
t.Fatalf("missing %q in %s", want, s)
|
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) {
|
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"} {
|
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) {
|
t.Run(args, func(t *testing.T) {
|
||||||
|
|||||||
Reference in new issue
Block a user