feat: Entfernen des wg_route Helper-Skripts und Versionsnummer auf 1.0.32 erhöht

This commit is contained in:
2026-05-09 12:57:26 +02:00
parent 0c14da3435
commit db5ca2f74f
2 changed files with 1 additions and 77 deletions

76
app.py
View File

@@ -327,40 +327,11 @@ def wg_update_state():
error=None, has_config=has_conf) error=None, has_config=has_conf)
# Helper-Script das wg-quick via PostUp/PreDown aufruft
_WG_ROUTE_SCRIPT = BASE_DIR / 'wg_route.py'
_WG_ROUTE_PY = '''\
#!/usr/bin/env python3
"""PiCopy: lokale IPs bei aktivem WireGuard-VPN erreichbar halten."""
import sys, subprocess, ipaddress
def local_nets():
r = subprocess.run(["ip", "-4", "addr", "show"], capture_output=True, text=True)
seen, res, iface = set(), [], ""
for ln in r.stdout.splitlines():
if not ln[:1].isspace():
iface = ln.split(":")[1].strip().split("@")[0] if ":" in ln else ""
elif iface and iface != "lo" and ln.strip().startswith("inet "):
try:
net = str(ipaddress.IPv4Interface(ln.split()[1]).network)
if net not in seen: seen.add(net); res.append(net)
except Exception: pass
return res
up = len(sys.argv) < 2 or sys.argv[1] != "down"
for i, net in enumerate(local_nets()):
cmd = "add" if up else "del"
subprocess.run(["ip","rule",cmd,"from",net,"table","main","priority",str(100+i*2)], capture_output=True)
subprocess.run(["ip","rule",cmd,"to", net,"table","main","priority",str(101+i*2)], capture_output=True)
'''
def wg_connect(): def wg_connect():
if not WG_CONF.exists(): if not WG_CONF.exists():
with wg_lock: with wg_lock:
wg_state['error'] = 'Keine Konfiguration vorhanden' wg_state['error'] = 'Keine Konfiguration vorhanden'
return False return False
_wg_inject_routing(WG_CONF.read_text(encoding='utf-8')) # PostUp/PreDown sicherstellen
r = subprocess.run(['wg-quick', 'up', WG_IFACE], r = subprocess.run(['wg-quick', 'up', WG_IFACE],
capture_output=True, text=True, timeout=30) capture_output=True, text=True, timeout=30)
if r.returncode == 0: if r.returncode == 0:
@@ -425,56 +396,9 @@ def wg_uninstall():
_wg_apt('remove', ['wireguard', 'wireguard-tools']) _wg_apt('remove', ['wireguard', 'wireguard-tools'])
def _wg_inject_routing(content: str) -> str:
"""Schreibt den Helper-Script und injiziert PostUp/PreDown in die Config,
falls 0.0.0.0/0 enthalten ist (Full-Tunnel). Gibt den ggf. geänderten
Config-Text zurück und schreibt ihn direkt in WG_CONF."""
if '0.0.0.0/0' not in content:
return content
try:
_WG_ROUTE_SCRIPT.write_text(_WG_ROUTE_PY, encoding='utf-8')
_WG_ROUTE_SCRIPT.chmod(0o755)
except Exception as e:
log.warning(f'wg_route.py konnte nicht geschrieben werden: {e}')
return content
post = f'PostUp = python3 {_WG_ROUTE_SCRIPT} up'
pred = f'PreDown = python3 {_WG_ROUTE_SCRIPT} down'
tag = str(_WG_ROUTE_SCRIPT)
# Bereits vorhanden → nichts tun
if tag in content:
return content
lines, new_lines, in_iface, done = content.splitlines(), [], False, False
for line in lines:
s = line.strip().lower()
# Alte PostUp/PreDown von uns entfernen (falls Pfad geändert)
if (s.startswith('postup') or s.startswith('predown')) and 'wg_route' in line:
continue
if line.strip() == '[Interface]':
in_iface = True
elif in_iface and line.strip().startswith('[') and not done:
new_lines += [post, pred, '']
in_iface = False
done = True
new_lines.append(line)
if in_iface and not done:
new_lines += [post, pred]
new_content = '\n'.join(new_lines)
try:
WG_CONF.write_text(new_content, encoding='utf-8')
WG_CONF.chmod(0o600)
except Exception as e:
log.warning(f'WG_CONF konnte nicht aktualisiert werden: {e}')
return new_content
def wg_save_config(content: str): def wg_save_config(content: str):
try: try:
WG_CONF.parent.mkdir(parents=True, exist_ok=True) WG_CONF.parent.mkdir(parents=True, exist_ok=True)
content = _wg_inject_routing(content)
WG_CONF.write_text(content, encoding='utf-8') WG_CONF.write_text(content, encoding='utf-8')
WG_CONF.chmod(0o600) WG_CONF.chmod(0o600)
return True, '' return True, ''

View File

@@ -1 +1 @@
1.0.31 1.0.32