TTOPM commited on
Commit
b39be4c
·
verified ·
1 Parent(s): 0ac7ee4

Create controls.py

Browse files
Files changed (1) hide show
  1. belel-shield/dashboard/controls.py +90 -0
belel-shield/dashboard/controls.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, subprocess, signal, sys, json
2
+ from pathlib import Path
3
+
4
+ HOME = Path.home()
5
+ DATA_DIR = HOME / ".belel"
6
+ DATA_DIR.mkdir(parents=True, exist_ok=True)
7
+ PID_FILE = DATA_DIR / "gideon_scanner.pid"
8
+
9
+ def _python_executable():
10
+ # Prefer the Python running Streamlit, else fallback to python3
11
+ return sys.executable or "python3"
12
+
13
+ def scanner_is_running():
14
+ if not PID_FILE.exists(): return False
15
+ try:
16
+ pid = int(PID_FILE.read_text().strip())
17
+ except Exception:
18
+ return False
19
+ try:
20
+ os.kill(pid, 0) # check signal
21
+ return True
22
+ except Exception:
23
+ PID_FILE.unlink(missing_ok=True)
24
+ return False
25
+
26
+ def start_scanner(scanner_path: str) -> str:
27
+ """
28
+ Launch gideon_scanner.py detached, record its PID.
29
+ Returns a status string.
30
+ """
31
+ if scanner_is_running():
32
+ return "Scanner is already running."
33
+ try:
34
+ py = _python_executable()
35
+ proc = subprocess.Popen(
36
+ [py, scanner_path],
37
+ stdout=subprocess.DEVNULL,
38
+ stderr=subprocess.DEVNULL,
39
+ start_new_session=True
40
+ )
41
+ PID_FILE.write_text(str(proc.pid))
42
+ return f"Scanner started (pid {proc.pid})."
43
+ except Exception as e:
44
+ return f"Failed to start scanner: {e}"
45
+
46
+ def stop_scanner() -> str:
47
+ """
48
+ Stop the running scanner if PID file exists and process is alive.
49
+ """
50
+ if not PID_FILE.exists():
51
+ return "No PID file; scanner not tracked."
52
+ try:
53
+ pid = int(PID_FILE.read_text().strip())
54
+ except Exception as e:
55
+ PID_FILE.unlink(missing_ok=True)
56
+ return f"Invalid PID file removed ({e})."
57
+
58
+ try:
59
+ os.kill(pid, signal.SIGTERM)
60
+ except ProcessLookupError:
61
+ PID_FILE.unlink(missing_ok=True)
62
+ return "Scanner process not found; cleaned PID file."
63
+ except Exception as e:
64
+ return f"Failed to stop scanner: {e}"
65
+
66
+ PID_FILE.unlink(missing_ok=True)
67
+ return "Scanner stopped."
68
+
69
+ def run_update_blocklist(updater_path: str) -> str:
70
+ """
71
+ Run the manual updater once; return output combined (stdout+stderr).
72
+ """
73
+ try:
74
+ py = _python_executable()
75
+ out = subprocess.check_output([py, updater_path], stderr=subprocess.STDOUT, text=True)
76
+ return out.strip()
77
+ except subprocess.CalledProcessError as e:
78
+ return (e.output or "").strip() or f"Updater failed: {e}"
79
+ except Exception as e:
80
+ return f"Updater error: {e}"
81
+
82
+ def firewall_apply_hint():
83
+ # Print the exact commands; user enters sudo password themselves in a terminal.
84
+ return (
85
+ "To apply firewall sinkhole/tarpit rules (Linux):\n"
86
+ " sudo ./resilience/firewall_rules.sh\n\n"
87
+ "To remove current DROP rules manually (examples):\n"
88
+ " sudo iptables -D OUTPUT -d <IP> -j DROP\n"
89
+ " sudo iptables -D INPUT -s <IP> -j DROP\n"
90
+ )