forked from MatrixTM/MHDDoS
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb_gui.py
More file actions
137 lines (121 loc) · 4.93 KB
/
web_gui.py
File metadata and controls
137 lines (121 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import os
import subprocess
import sys
import time
import webbrowser
import argparse
from socket import AF_INET, SOCK_STREAM, socket
from typing import Optional, Tuple
import requests
API_URL = "http://127.0.0.1:8000"
HEALTH_ENDPOINT = f"{API_URL}/api/health"
def get_process_on_port(port: int) -> Tuple[Optional[int], Optional[str]]:
"""Identifies the PID and Name of the process using the specified port on Windows."""
try:
# Get PID using netstat
output = subprocess.check_output(f"netstat -ano | findstr LISTENING | findstr :{port}", shell=True).decode()
for line in output.strip().split('\n'):
parts = line.split()
if parts and parts[1].endswith(f":{port}"):
pid = int(parts[-1])
# Get Process Name using tasklist
task_output = subprocess.check_output(f"tasklist /FI \"PID eq {pid}\" /NH", shell=True).decode()
name = task_output.split()[0] if task_output.strip() else "Unknown"
return pid, name
except Exception:
pass
return None, None
def is_api_running() -> Tuple[bool, bool]:
"""
Checks if something is already listening on port 8000.
Returns (is_busy, is_our_api)
"""
with socket(AF_INET, SOCK_STREAM) as s:
if s.connect_ex(('127.0.0.1', 8000)) == 0:
try:
response = requests.get(HEALTH_ENDPOINT, timeout=2)
if response.status_code == 200:
data = response.json()
if data.get("status") == "online" and "version" in data:
return True, True # It is definitely our API
except Exception:
pass
return True, False # Port is busy but not by a responsive MHDDoS API
return False, False
def kill_process(pid: int):
"""Kills a process and its children."""
try:
print(f"[*] Terminating conflicting process (PID: {pid})...")
subprocess.run(f"taskkill /F /PID {pid} /T", shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except Exception as e:
print(f"[!] Failed to kill process: {e}")
def wait_for_api(timeout: float = 10.0) -> bool:
"""Blocks until the API is ready or timeout is reached."""
start_time = time.time()
while time.time() - start_time < timeout:
busy, ours = is_api_running()
if busy and ours:
return True
time.sleep(0.5)
return False
def main() -> None:
parser = argparse.ArgumentParser(description="MHDDoS Professional Web Launcher")
parser.add_argument("--force", action="store_true", help="Force restart the API server by killing any process on port 8000")
args = parser.parse_args()
print("[*] Initializing MHDDoS Professional Web Launcher v1.2.1...")
# 1. Handle Conflict
busy, ours = is_api_running()
if args.force and busy:
pid, name = get_process_on_port(8000)
if pid:
kill_process(pid)
time.sleep(1)
busy, ours = is_api_running()
if busy:
if ours:
print("[*] Tactical API Server already active. Redirecting to existing instance...")
webbrowser.open(API_URL)
return
else:
pid, name = get_process_on_port(8000)
print(f"[!] Port Conflict: Port 8000 is occupied by '{name}' (PID: {pid}).")
print("[!] Please close that application or run with --force to terminate it.")
return
# 2. Prepare paths
base_dir = os.path.dirname(os.path.abspath(__file__))
api_path = os.path.join(base_dir, "api.py")
# 3. Launch server process
server_process: Optional[subprocess.Popen[bytes]] = None
try:
print("[*] Starting background API engine...")
# Use CREATE_NEW_PROCESS_GROUP to ensure we can manage it
server_process = subprocess.Popen(
[sys.executable, api_path],
cwd=base_dir,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
# 4. Wait for readiness via health check
print("[*] Synchronizing with tactical engine...", end="", flush=True)
if wait_for_api(timeout=30.0):
print(" SUCCESS.")
print(f"[*] Opening {API_URL} in your web browser...")
webbrowser.open(API_URL)
# Keep the main process alive
server_process.wait()
else:
print(" FAILED.")
print("[!] Critical Error: Tactical API server failed to start within timeout.")
if server_process:
server_process.terminate()
except KeyboardInterrupt:
print("\n[*] Stopping launcher...")
if server_process:
server_process.terminate()
server_process.wait()
except Exception as e:
print(f"\n[!] Critical Launcher Error: {e}")
if server_process:
server_process.terminate()
if __name__ == "__main__":
main()