From a92d7d9b0b5aa78fa1a0b10645a4926c4a8c1208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80?= <78488229+viktor138irk@users.noreply.github.com> Date: Thu, 7 May 2026 01:06:28 +0900 Subject: [PATCH] Add installer patch for VM bridge guard --- scripts/patch_vm_network_guard.py | 53 +++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 scripts/patch_vm_network_guard.py diff --git a/scripts/patch_vm_network_guard.py b/scripts/patch_vm_network_guard.py new file mode 100644 index 0000000..dbfea73 --- /dev/null +++ b/scripts/patch_vm_network_guard.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +from pathlib import Path +import sys + +app_path = Path(sys.argv[1]) if len(sys.argv) > 1 else Path('/opt/virtuality/web/app.py') +if not app_path.exists(): + raise SystemExit(f'app.py not found: {app_path}') + +text = app_path.read_text() +changed = [] + +helper = ''' + +def bridge_exists(name: str) -> bool: + if not name or not re.fullmatch(r"[a-zA-Z0-9_.:-]+", name): + return False + return run_cmd(["ip", "link", "show", name], timeout=5)["ok"] +''' + +if 'def bridge_exists(name: str) -> bool:' not in text: + marker = '\n\ndef default_network_mode() -> str:' + if marker not in text: + raise SystemExit('default_network_mode marker not found') + text = text.replace(marker, helper + marker, 1) + changed.append('bridge_exists helper added') +else: + changed.append('bridge_exists helper already present') + +old_validation = ''' elif network_mode == "bridge" and (not bridge or not re.fullmatch(r"[a-zA-Z0-9_.:-]+", bridge)): + error = "Некорректное имя bridge." + else: + iso = Path(iso_path).resolve() +''' +new_validation = ''' elif network_mode == "bridge" and (not bridge or not re.fullmatch(r"[a-zA-Z0-9_.:-]+", bridge)): + error = "Некорректное имя bridge." + elif network_mode == "bridge" and not bridge_exists(bridge): + error = f"Bridge {bridge} не найден на сервере. Для VPS выбери режим NAT Router — virtuality-nat, либо сначала создай bridge {bridge}." + else: + iso = Path(iso_path).resolve() +''' + +if old_validation in text: + text = text.replace(old_validation, new_validation, 1) + changed.append('bridge existence validation added') +elif new_validation in text: + changed.append('bridge existence validation already present') +else: + raise SystemExit('bridge validation marker not found') + +app_path.write_text(text) +print('vm network guard patch applied:') +for item in changed: + print(f'- {item}')