From 6f2b5f11bd5f00269bd88f0b84d61019f7e0f2d7 Mon Sep 17 00:00:00 2001 From: Andrei Leibzon Date: Fri, 20 Nov 2026 17:08:18 +0300 Subject: [PATCH] =?UTF-8?q?guard:=20=D0=B7=D0=B0=D1=89=D0=B8=D1=82=D0=B0?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=B4=D0=BD=D0=B8=D0=BC=D0=B0=D0=B5=D1=82=D1=81?= =?UTF-8?q?=D1=8F=20=D1=81=D1=80=D0=B0=D0=B7=D1=83=20=D0=BF=D0=BE=D1=81?= =?UTF-8?q?=D0=BB=D0=B5=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B7=D0=B0=D0=B3=D1=80?= =?UTF-8?q?=D1=83=D0=B7=D0=BA=D0=B8=20=D1=81=D0=B5=D1=80=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Раньше nft-таблица создавалась таймером с RandomizedDelaySec=1h, поэтому после ребута статус «выключен» держался до часа. Теперь запуск на загрузке отдельным service, таблица и правила создаются до скачивания списка, старые юниты чинятся при update. Co-Authored-By: Claude Sonnet 5.5 --- server/phobos-guard.sh | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/server/phobos-guard.sh b/server/phobos-guard.sh index 7311d2b..ed1c6ba 100755 --- a/server/phobos-guard.sh +++ b/server/phobos-guard.sh @@ -21,15 +21,19 @@ ensure_nft() { update_list() { local url tmp nf + # старые установки: юнит без [Install] -> чиним, чтобы защита поднималась на загрузке + if [ -f "$STATE" ] && ! grep -q '^\[Install\]' /etc/systemd/system/phobos-guard.service 2>/dev/null; then + write_units; systemctl daemon-reload; systemctl enable phobos-guard.service >/dev/null 2>&1 + fi url="$(list_url)" tmp="$(mktemp)"; nf="$(mktemp)" - curl -fsSL -m60 "$url" -o "$tmp" || { rm -f "$tmp" "$nf"; echo "download failed: $url" >&2; return 1; } nft add table $TABLE 2>/dev/null || true nft add set $TABLE v4 '{ type ipv4_addr; flags interval; auto-merge; }' 2>/dev/null || true nft add set $TABLE v6 '{ type ipv6_addr; flags interval; auto-merge; }' 2>/dev/null || true nft add chain $TABLE input '{ type filter hook input priority raw; policy accept; }' 2>/dev/null || true nft list chain $TABLE input | grep -q '@v4' || nft add rule $TABLE input ip saddr @v4 counter drop nft list chain $TABLE input | grep -q '@v6' || nft add rule $TABLE input ip6 saddr @v6 counter drop + curl -fsSL -m60 "$url" -o "$tmp" || { rm -f "$tmp" "$nf"; echo "download failed: $url" >&2; return 1; } # collapse + skip private/loopback so a bad list can never cut LAN/tunnel/admin python3 - "$tmp" > "$nf" <<'PY' import ipaddress, sys @@ -57,9 +61,7 @@ PY return "$rc" } -enable() { - ensure_nft || { echo "nftables install failed" >&2; exit 1; } - [ -n "${1:-}" ] && echo "GUARD_URL=$1" > "$STATE" || touch "$STATE" +write_units() { cat > /etc/systemd/system/phobos-guard.service < /etc/systemd/system/phobos-guard.timer <<'EOF' [Unit] Description=Refresh PCA Phobos guard list daily [Timer] -OnBootSec=1min OnCalendar=daily RandomizedDelaySec=1h Persistent=true [Install] WantedBy=timers.target EOF +} + +enable() { + ensure_nft || { echo "nftables install failed" >&2; exit 1; } + [ -n "${1:-}" ] && echo "GUARD_URL=$1" > "$STATE" || touch "$STATE" + write_units systemctl daemon-reload - systemctl enable --now phobos-guard.timer >/dev/null 2>&1 + systemctl enable phobos-guard.service phobos-guard.timer >/dev/null 2>&1 + systemctl start phobos-guard.timer >/dev/null 2>&1 update_list }