Skip to content

Commit faf61a6

Browse files
authored
Improve namespaces to handle non-present SSIDs and improve monitor mode handling (#108)
1 parent e8721bd commit faf61a6

File tree

6 files changed

+314
-56
lines changed

6 files changed

+314
-56
lines changed

wlanpi_core/app.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
CURRENT_CONFIG_FILE,
2727
SECRETS_DIR,
2828
SUPPORTED_MODELS,
29+
CREATE_MONITOR_PAIRS_DEFAULT,
30+
CREATE_MONITOR_PAIRS_UNINIT,
2931
)
3032
from wlanpi_core.core.config import endpoints, settings
3133
from wlanpi_core.core.database import DatabaseError, DatabaseManager
@@ -35,7 +37,7 @@
3537
from wlanpi_core.core.system import SystemManager
3638
from wlanpi_core.core.token import TokenManager
3739
from wlanpi_core.services.system_service import get_model
38-
from wlanpi_core.utils.network_config import activate_config, get_current_config
40+
from wlanpi_core.utils.network_config import activate_config, get_current_config, interfaces_in_root
3941
from wlanpi_core.views.api import router as views_router
4042

4143

@@ -330,12 +332,13 @@ async def initialize_components(self):
330332
self.log.info(f"Default config activated sucessfully")
331333

332334

333-
system_initialized = await self._initialize_system_manager("wlanpi")
334-
if not system_initialized:
335-
self.log.error(
336-
"System manager initialization failed - cannot proceed"
337-
)
338-
return False
335+
if CREATE_MONITOR_PAIRS_DEFAULT:
336+
system_initialized = await self._initialize_system_manager("wlanpi", exclusions=[])
337+
if not system_initialized:
338+
self.log.error(
339+
"System manager initialization failed - cannot proceed"
340+
)
341+
return False
339342
else:
340343
model = get_model()
341344
if model not in SUPPORTED_MODELS:
@@ -351,6 +354,14 @@ async def initialize_components(self):
351354

352355
else:
353356
self.log.info(f"Default config activated sucessfully")
357+
358+
if CREATE_MONITOR_PAIRS_DEFAULT:
359+
system_initialized = await self._initialize_system_manager("wlanpi", exclusions=[])
360+
if not system_initialized:
361+
self.log.error(
362+
"System manager initialization failed - cannot proceed"
363+
)
364+
return False
354365
else:
355366
self.log.info(
356367
f"Activating current network configuration: {current_config}"
@@ -363,6 +374,15 @@ async def initialize_components(self):
363374
)
364375
else:
365376
self.log.info(f"Config {current_config} activated sucessfully")
377+
378+
if CREATE_MONITOR_PAIRS_UNINIT:
379+
exclusions = interfaces_in_root(current_config)
380+
system_initialized = await self._initialize_system_manager("wlanpi", exclusions=[])
381+
if not system_initialized:
382+
self.log.error(
383+
"System manager initialization failed - cannot proceed"
384+
)
385+
return False
366386

367387
except FileNotFoundError:
368388
self.log.warning("No current network configuration found")
@@ -445,10 +465,10 @@ async def _initialize_token_manager(self):
445465
self.log.error(f"Token manager initialization failed: {e}")
446466
return False
447467

448-
async def _initialize_system_manager(self, iface_name: str):
468+
async def _initialize_system_manager(self, iface_name: str, exclusions: list[str] = []):
449469
"""Initialize the system manager"""
450470
try:
451-
self.app.state.system_manager = SystemManager(iface_name)
471+
self.app.state.system_manager = SystemManager(iface_name, exclusions=exclusions)
452472
self.log.debug("System manager initialized succcessfully")
453473
return True
454474
except Exception as e:

wlanpi_core/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
PID_DIR = "/home/wlanpi/.local/share/wlanpi-core/netcfg/pids"
6060
APPS_FILE = "/home/wlanpi/.local/share/wlanpi-core/netcfg/apps.json"
6161
WPA_LOG_FILE = "/tmp/wpa.log"
62+
CREATE_MONITOR_PAIRS_DEFAULT = True
63+
CREATE_MONITOR_PAIRS_UNINIT = True
6264

6365
#### Paths below here are relative to script dir or /tmp fixed paths ###
6466

wlanpi_core/core/system.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111

1212
class SystemManager:
13-
def __init__(self, iface_name: str = "wlanpi"):
13+
def __init__(self, iface_name: str = "wlanpi", exclusions: list[str] = []):
1414
self.iface_name = iface_name
15+
self.exclusions = exclusions
1516
self.sync_monitor_interfaces()
1617

1718
def _run(self, cmd, capture_output=False, suppress_output=False):
@@ -66,8 +67,10 @@ def _get_interfaces_by_type(self):
6667
current_iface = line.strip().split()[1]
6768
elif "type" in line and current_iface:
6869
iface_type = line.strip().split()[1]
69-
interfaces[current_iface] = iface_type
70-
current_iface = None
70+
if current_iface not in self.exclusions:
71+
interfaces[current_iface] = iface_type
72+
current_iface = None
73+
7174
return interfaces
7275

7376
def _create_monitor(self, name, index):
@@ -119,11 +122,12 @@ def sync_monitor_interfaces(self):
119122
expected_mon = f"{self.iface_name}{index}"
120123
if expected_mon not in monitor:
121124
log.info(f"Creating monitor interface for {iface}{expected_mon}")
125+
self._iface_up(iface)
122126
self._create_monitor(iface, index)
123127
driver = self._get_driver(iface)
124128
if driver == "iwlwifi":
129+
self._iface_up(expected_mon)
125130
log.info(f"Bringing up and scanning on {iface}...")
126-
self._iface_up(iface)
127131
def background_scan_with_timeout():
128132
time.sleep(1)
129133
try:
@@ -134,6 +138,7 @@ def background_scan_with_timeout():
134138
timeout=10
135139
)
136140
log.info(f"Scan on {iface} done")
141+
137142
self._iface_down(iface)
138143
except subprocess.TimeoutExpired:
139144
log.warning(f"Scan on {iface} timed out after 10s")

wlanpi_core/schemas/network/network.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class NetworkModeEnum(str, Enum):
7070
class SecurityTypes(str, Enum):
7171
wpa2 = "WPA2-PSK"
7272
wpa3 = "WPA3-PSK"
73+
open = "OPEN"
74+
owe = "OWE"
7375

7476

7577
class NetSecurity(BaseModel):

0 commit comments

Comments
 (0)