mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-26 21:07:17 +01:00
Let store checks settle after a hosts edit
In vmbox, systemd-resolved answered nothing for about 2 s after an unblock, so the HTTPS check failed a working unblock. It also answers no address, not 0.0.0.0, for a sinkholed name. Probes now repeat (3 attempts, 2 s apart) until they agree with the wanted result, and a name with no answer counts as blocked. Todo: 85af719b Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
9ad424d777
commit
7fa0ebad6a
@ -12,7 +12,8 @@ When only ``https`` fails and ``https://steamcommunity.com/`` fails too, the
|
|||||||
machine is offline: the result is "network down", not an unblock failure.
|
machine is offline: the result is "network down", not an unblock failure.
|
||||||
|
|
||||||
Re-block checks (the reverse):
|
Re-block checks (the reverse):
|
||||||
dns -- every store domain resolves only to a sinkhole;
|
dns -- every store domain resolves only to a sinkhole (or to
|
||||||
|
nothing, which is how systemd-resolved answers one);
|
||||||
https -- the store does not answer.
|
https -- the store does not answer.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -22,6 +23,7 @@ from dataclasses import dataclass, field
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import time
|
||||||
from typing import TYPE_CHECKING, Final
|
from typing import TYPE_CHECKING, Final
|
||||||
|
|
||||||
from steam_backlog_enforcer._hosts_lines import (
|
from steam_backlog_enforcer._hosts_lines import (
|
||||||
@ -50,6 +52,12 @@ _HTTPS_SECONDS: Final = 8
|
|||||||
_GETENT_SECONDS: Final = 2
|
_GETENT_SECONDS: Final = 2
|
||||||
_JOURNAL_SECONDS: Final = 5
|
_JOURNAL_SECONDS: Final = 5
|
||||||
_JOURNAL_LINES: Final = 50
|
_JOURNAL_LINES: Final = 50
|
||||||
|
# Name resolution lags a hosts edit: systemd-resolved answered nothing for
|
||||||
|
# about 2 s after an unblock in vmbox, and a cache can still hold the real
|
||||||
|
# address right after a re-block. Probes repeat until they agree with the
|
||||||
|
# wanted result, at most this many times.
|
||||||
|
SETTLE_ATTEMPTS: Final = 3
|
||||||
|
_SETTLE_GAP_SECONDS: Final = 2.0
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
@ -150,6 +158,31 @@ def https_reachable(url: str) -> tuple[bool, str]:
|
|||||||
return rc == 0, f"curl rc={rc} {out}".strip()
|
return rc == 0, f"curl rc={rc} {out}".strip()
|
||||||
|
|
||||||
|
|
||||||
|
def _settled_https(url: str, *, want: bool) -> tuple[bool, str]:
|
||||||
|
"""Probe *url* until its reachability is *want* (or attempts run out)."""
|
||||||
|
for attempt in range(SETTLE_ATTEMPTS):
|
||||||
|
if attempt:
|
||||||
|
time.sleep(_SETTLE_GAP_SECONDS)
|
||||||
|
reachable, detail = https_reachable(url)
|
||||||
|
if reachable == want:
|
||||||
|
break
|
||||||
|
return reachable, detail
|
||||||
|
|
||||||
|
|
||||||
|
def _leaks() -> dict[str, str]:
|
||||||
|
"""Store domains resolving to a real address (no answer counts as blocked).
|
||||||
|
|
||||||
|
systemd-resolved answers a sinkholed name with no address at all rather
|
||||||
|
than ``0.0.0.0``; either way nothing can connect.
|
||||||
|
"""
|
||||||
|
leaks = {}
|
||||||
|
for domain in BLOCKED_DOMAINS:
|
||||||
|
addrs, _ = resolve(domain)
|
||||||
|
if not addrs <= SINKHOLES:
|
||||||
|
leaks[domain] = ",".join(sorted(addrs))
|
||||||
|
return leaks
|
||||||
|
|
||||||
|
|
||||||
def resolve(domain: str) -> tuple[set[str], str]:
|
def resolve(domain: str) -> tuple[set[str], str]:
|
||||||
"""Addresses *domain* resolves to via NSS (what curl and Steam see)."""
|
"""Addresses *domain* resolves to via NSS (what curl and Steam see)."""
|
||||||
_rc, out = _run([GETENT, "ahosts", domain], _GETENT_SECONDS)
|
_rc, out = _run([GETENT, "ahosts", domain], _GETENT_SECONDS)
|
||||||
@ -173,7 +206,7 @@ def check_unblock() -> StoreCheckReport:
|
|||||||
"""Run the unblock checks once."""
|
"""Run the unblock checks once."""
|
||||||
live = live_store_lines(_hosts_text())
|
live = live_store_lines(_hosts_text())
|
||||||
chain, _rules = _chain_rules()
|
chain, _rules = _chain_rules()
|
||||||
store_ok, store_detail = https_reachable(STORE_URL)
|
store_ok, store_detail = _settled_https(STORE_URL, want=True)
|
||||||
checks = [
|
checks = [
|
||||||
Check("hosts", not live, f"{len(live)} live store line(s)"),
|
Check("hosts", not live, f"{len(live)} live store line(s)"),
|
||||||
Check("iptables", not chain, "chain present" if chain else "chain gone"),
|
Check("iptables", not chain, "chain present" if chain else "chain gone"),
|
||||||
@ -186,13 +219,14 @@ def check_unblock() -> StoreCheckReport:
|
|||||||
|
|
||||||
|
|
||||||
def check_reblock() -> StoreCheckReport:
|
def check_reblock() -> StoreCheckReport:
|
||||||
"""Run the re-block checks once."""
|
"""Run the re-block checks once (each probe settles, see SETTLE_ATTEMPTS)."""
|
||||||
leaks = {}
|
leaks = _leaks()
|
||||||
for domain in BLOCKED_DOMAINS:
|
for _ in range(SETTLE_ATTEMPTS - 1):
|
||||||
addrs, _ = resolve(domain)
|
if not leaks:
|
||||||
if not addrs or not addrs <= SINKHOLES:
|
break
|
||||||
leaks[domain] = ",".join(sorted(addrs)) or "no answer"
|
time.sleep(_SETTLE_GAP_SECONDS)
|
||||||
store_open, detail = https_reachable(STORE_URL)
|
leaks = _leaks()
|
||||||
|
store_open, detail = _settled_https(STORE_URL, want=False)
|
||||||
leak_text = "; ".join(f"{d} -> {a}" for d, a in leaks.items())
|
leak_text = "; ".join(f"{d} -> {a}" for d, a in leaks.items())
|
||||||
checks = [
|
checks = [
|
||||||
Check("dns", not leaks, leak_text or "every domain sinkholed"),
|
Check("dns", not leaks, leak_text or "every domain sinkholed"),
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING, Any
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@ -25,6 +25,7 @@ class FakeHost:
|
|||||||
def __init__(self, hosts: Path) -> None:
|
def __init__(self, hosts: Path) -> None:
|
||||||
"""Start as a correctly unblocked, online machine."""
|
"""Start as a correctly unblocked, online machine."""
|
||||||
self.hosts = hosts
|
self.hosts = hosts
|
||||||
|
self.sleep = MagicMock()
|
||||||
self.chain = False
|
self.chain = False
|
||||||
self.reachable = {sv.STORE_URL: True, sv.CONTROL_URL: True}
|
self.reachable = {sv.STORE_URL: True, sv.CONTROL_URL: True}
|
||||||
self.resolves: dict[str, str] = dict.fromkeys(BLOCKED_DOMAINS, "1.2.3.4")
|
self.resolves: dict[str, str] = dict.fromkeys(BLOCKED_DOMAINS, "1.2.3.4")
|
||||||
@ -35,10 +36,10 @@ class FakeHost:
|
|||||||
self.calls.append(cmd)
|
self.calls.append(cmd)
|
||||||
out, rc = "", 0
|
out, rc = "", 0
|
||||||
if cmd[0] == sv.CURL:
|
if cmd[0] == sv.CURL:
|
||||||
rc = 0 if self.reachable[cmd[-1]] else 7
|
rc = 0 if _next(self.reachable, cmd[-1]) else 7
|
||||||
out = "200" if rc == 0 else "curl: (7) Failed to connect"
|
out = "200" if rc == 0 else "curl: (7) Failed to connect"
|
||||||
elif cmd[0] == sv.GETENT:
|
elif cmd[0] == sv.GETENT:
|
||||||
addr = self.resolves[cmd[-1]]
|
addr = _next(self.resolves, cmd[-1])
|
||||||
out = f"{addr} STREAM {cmd[-1]}\n" if addr else ""
|
out = f"{addr} STREAM {cmd[-1]}\n" if addr else ""
|
||||||
rc = 0 if addr else 2
|
rc = 0 if addr else 2
|
||||||
elif "-S" in cmd:
|
elif "-S" in cmd:
|
||||||
@ -50,6 +51,14 @@ class FakeHost:
|
|||||||
return MagicMock(returncode=rc, stdout=out, stderr="")
|
return MagicMock(returncode=rc, stdout=out, stderr="")
|
||||||
|
|
||||||
|
|
||||||
|
def _next(table: dict[str, Any], key: str) -> object:
|
||||||
|
"""The value for *key*; a list is a sequence of answers, one per call."""
|
||||||
|
value = table[key]
|
||||||
|
if isinstance(value, list):
|
||||||
|
return value.pop(0) if len(value) > 1 else value[0]
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="host")
|
@pytest.fixture(name="host")
|
||||||
def fixture_host(tmp_path: Path) -> Iterator[FakeHost]:
|
def fixture_host(tmp_path: Path) -> Iterator[FakeHost]:
|
||||||
"""A fake machine with an empty /etc/hosts."""
|
"""A fake machine with an empty /etc/hosts."""
|
||||||
@ -57,6 +66,7 @@ def fixture_host(tmp_path: Path) -> Iterator[FakeHost]:
|
|||||||
fake = FakeHost(hosts)
|
fake = FakeHost(hosts)
|
||||||
hosts.write_text("127.0.0.1 localhost\n", encoding="utf-8")
|
hosts.write_text("127.0.0.1 localhost\n", encoding="utf-8")
|
||||||
with (
|
with (
|
||||||
|
patch(f"{sv.__name__}.time.sleep") as fake.sleep,
|
||||||
patch(f"{sv.__name__}.subprocess.run", side_effect=fake.run),
|
patch(f"{sv.__name__}.subprocess.run", side_effect=fake.run),
|
||||||
patch(f"{sv.__name__}.HOSTS_FILE", hosts),
|
patch(f"{sv.__name__}.HOSTS_FILE", hosts),
|
||||||
):
|
):
|
||||||
@ -179,7 +189,7 @@ def test_verify_without_repair_checks_once(host: FakeHost) -> None:
|
|||||||
assert not report.ok
|
assert not report.ok
|
||||||
assert not report.repaired
|
assert not report.repaired
|
||||||
curls = [c for c in host.calls if c[0] == sv.CURL]
|
curls = [c for c in host.calls if c[0] == sv.CURL]
|
||||||
assert len(curls) == 1
|
assert len(curls) == sv.SETTLE_ATTEMPTS # One check pass, settling once.
|
||||||
|
|
||||||
|
|
||||||
def test_run_survives_missing_binary() -> None:
|
def test_run_survives_missing_binary() -> None:
|
||||||
@ -194,3 +204,35 @@ def test_error_carries_report(host: FakeHost) -> None:
|
|||||||
err = sv.StoreVerifyError(sv.check_unblock())
|
err = sv.StoreVerifyError(sv.check_unblock())
|
||||||
assert str(err) == "Store unblock failed: iptables"
|
assert str(err) == "Store unblock failed: iptables"
|
||||||
assert err.report.failed_check == "iptables"
|
assert err.report.failed_check == "iptables"
|
||||||
|
|
||||||
|
|
||||||
|
def test_unblock_https_settles_after_dns_reload(host: FakeHost) -> None:
|
||||||
|
"""Resolution fails for a moment after a hosts edit: retried, then ok."""
|
||||||
|
host.reachable[sv.STORE_URL] = [False, False, True]
|
||||||
|
report = sv.check_unblock()
|
||||||
|
assert report.ok
|
||||||
|
assert host.sleep.call_count == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_unblock_https_gives_up_after_attempts(host: FakeHost) -> None:
|
||||||
|
"""A store that stays unreachable fails after the last attempt."""
|
||||||
|
host.reachable[sv.STORE_URL] = False
|
||||||
|
assert sv.check_unblock().failed_check == "https"
|
||||||
|
store_probes = [c for c in host.calls if c[-1] == sv.STORE_URL]
|
||||||
|
assert len(store_probes) == sv.SETTLE_ATTEMPTS
|
||||||
|
|
||||||
|
|
||||||
|
def test_reblock_no_answer_counts_as_blocked(host: FakeHost) -> None:
|
||||||
|
"""systemd-resolved answers no address for a sinkholed name: blocked."""
|
||||||
|
host.resolves = dict.fromkeys(BLOCKED_DOMAINS, "")
|
||||||
|
host.reachable[sv.STORE_URL] = False
|
||||||
|
report = sv.check_reblock()
|
||||||
|
assert report.ok
|
||||||
|
|
||||||
|
|
||||||
|
def test_reblock_settles_stale_cache(host: FakeHost) -> None:
|
||||||
|
"""A cached real address and an answering store settle to blocked."""
|
||||||
|
host.resolves = dict.fromkeys(BLOCKED_DOMAINS, _V4)
|
||||||
|
host.resolves[_STORE] = ["2.18.70.115", _V4]
|
||||||
|
host.reachable[sv.STORE_URL] = [True, False]
|
||||||
|
assert sv.check_reblock().ok
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user