Refactor settings loading to use auto-detection and improve error handling

This commit is contained in:
Yaro Kasear 2025-04-30 14:34:11 -05:00
parent 0e49e636ce
commit f360ae79e8

View file

@ -1,19 +1,31 @@
#!/usr/bin/env python3
import os import os
import signal import signal
import time import time
from collections import defaultdict from collections import defaultdict
from scapy.all import sniff, Dot11, RadioTap from scapy.all import sniff, Dot11, RadioTap
from dotenv import dotenv_values from dotenv import load_dotenv
from pathlib import Path
settings_path = os.path.expanduser("~/wifi_test/settings.env") # --- Settings.env auto-detection logic ---
config = dotenv_values(settings_path) SCRIPT_DIRECTORY = Path(__file__).resolve().parent
if not config: # Try intelligent guess of real user home even under sudo
print(f"[!] Failed to load settings from {settings_path}") SUDO_USER = os.environ.get("SUDO_USER")
USER_HOME = Path(f"/home/{SUDO_USER}") if SUDO_USER else Path.home()
ENV_PATH = USER_HOME / "wifi_test" / "settings.env"
# Fallback if all else fails
if not ENV_PATH.exists():
ENV_PATH = Path("/home/yaro/wifi_test/settings.env")
if not ENV_PATH.exists():
print(f"[!] Failed to load settings from {ENV_PATH}")
exit(1) exit(1)
LISTEN_INTERFACE = config.get("LISTEN_INTERFACE", "wlan0") load_dotenv(dotenv_path=ENV_PATH)
# Now load settings
LISTEN_INTERFACE = os.getenv("LISTEN_INTERFACE", "wlan0")
print(f"[+] Using LISTEN_INTERFACE = {LISTEN_INTERFACE}") print(f"[+] Using LISTEN_INTERFACE = {LISTEN_INTERFACE}")
# === Globals === # === Globals ===