mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-20 16:52:18 +01:00
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / web-coverage (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Set [tool.pylint.similarities] min-similarity-lines = 8 (as diet-guard) and refactor the remaining R0801 hits: the store and total-block iptables modules now share create_fresh_chain / add_drop_rule / drop_chain in _store_tools. No duplicate-code disable or exclude. Run pylint serially (pyproject jobs = 1, hook --jobs=1): parallel mode pickles PyLinter with dill, which crashes on Python 3.15 (co_lnotab). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
92 lines
2.5 KiB
Python
92 lines
2.5 KiB
Python
"""Steam-store blocking at the iptables/DNS layer.
|
|
|
|
These call nothing else in the blocker, so they extract cleanly: the
|
|
one-way dependency is what keeps store_blocker importable.
|
|
Split to keep both files under the 250-line cap.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import contextlib
|
|
import logging
|
|
import socket
|
|
import subprocess
|
|
|
|
from steam_backlog_enforcer._store_tools import (
|
|
IPTABLES,
|
|
IPTABLES_CHAIN,
|
|
SUDO,
|
|
add_drop_rule,
|
|
create_fresh_chain,
|
|
drop_chain,
|
|
ensure_chain_in_output,
|
|
run_quiet,
|
|
)
|
|
from steam_backlog_enforcer.config import (
|
|
BLOCKED_DOMAINS,
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _is_iptables_blocked() -> bool:
|
|
"""Check if our iptables chain exists and has rules."""
|
|
try:
|
|
result = subprocess.run(
|
|
[SUDO, IPTABLES, "-L", IPTABLES_CHAIN, "-n"],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=5,
|
|
check=False,
|
|
)
|
|
except OSError, subprocess.SubprocessError:
|
|
return False
|
|
return result.returncode == 0 and "DROP" in result.stdout
|
|
|
|
|
|
def _block_store_iptables() -> bool:
|
|
"""Block Steam Store domains using iptables (IP-based)."""
|
|
try:
|
|
create_fresh_chain(IPTABLES_CHAIN)
|
|
|
|
# Resolve domains and block their IPs.
|
|
blocked_ips: set[str] = set()
|
|
for domain in BLOCKED_DOMAINS:
|
|
with contextlib.suppress(socket.gaierror):
|
|
ips = socket.getaddrinfo(domain, 443, socket.AF_INET)
|
|
for _, _, _, _, addr in ips:
|
|
blocked_ips.add(addr[0])
|
|
|
|
for ip in blocked_ips:
|
|
add_drop_rule(IPTABLES_CHAIN, ip)
|
|
|
|
ensure_chain_in_output(IPTABLES_CHAIN)
|
|
except OSError, subprocess.SubprocessError:
|
|
logger.exception("Failed to block store via iptables")
|
|
return False
|
|
logger.info("Steam Store blocked via iptables (%d IPs).", len(blocked_ips))
|
|
return True
|
|
|
|
|
|
def _unblock_store_iptables() -> bool:
|
|
"""Remove iptables-based block."""
|
|
try:
|
|
drop_chain(IPTABLES_CHAIN)
|
|
except OSError, subprocess.SubprocessError:
|
|
logger.exception("Failed to unblock iptables")
|
|
return False
|
|
logger.info("Steam Store unblocked from iptables.")
|
|
return True
|
|
|
|
|
|
def flush_dns_cache() -> None:
|
|
"""Flush the system DNS cache."""
|
|
commands = [
|
|
["systemd-resolve", "--flush-caches"],
|
|
["resolvectl", "flush-caches"],
|
|
["nscd", "--invalidate=hosts"],
|
|
]
|
|
for cmd in commands:
|
|
with contextlib.suppress(FileNotFoundError, OSError):
|
|
run_quiet(cmd)
|