forked from PierreGode/Ragnar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwifi_manager.py
More file actions
executable file
·2769 lines (2370 loc) · 128 KB
/
wifi_manager.py
File metadata and controls
executable file
·2769 lines (2370 loc) · 128 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# wifi_manager.py
# Features:
# - Auto-connect to known Wi-Fi networks on boot
# - Fall back to AP mode if connection fails
# - Web interface for Wi-Fi configuration
# - Robust connection monitoring with proper timing
# - Network scanning and credential management
# - Integration with wpa_supplicant and NetworkManager
# - SQLite database caching for improved scan performance
# - Connection history tracking and analytics
import os
import time
import json
import subprocess
import threading
import logging
import re
import signal
from datetime import datetime, timedelta
from logger import Logger
from db_manager import get_db
from wifi_interfaces import gather_wifi_interfaces, get_active_ethernet_interface
class WiFiManager:
"""Manages Wi-Fi connections, AP mode, and configuration for Ragnar"""
def __init__(self, shared_data):
self.shared_data = shared_data
self.logger = Logger(name="WiFiManager", level=logging.INFO)
# Initialize database for WiFi caching and analytics
try:
self.db = get_db(currentdir=shared_data.currentdir)
self.logger.info("WiFi database manager initialized")
except Exception as e:
self.logger.error(f"Failed to initialize WiFi database: {e}")
self.db = None
# Setup dedicated AP mode logging
self.setup_ap_logger()
# WiFi analytics tracking
self.current_connection_id = None # Track current connection for duration logging
# State management
self.wifi_connected = False
self.ap_mode_active = False
self.connection_attempts = 0
self.last_connection_attempt = None
self.connection_check_interval = 10 # Check every 10 seconds for responsive monitoring
self.connection_timeout = shared_data.config.get('wifi_initial_connection_timeout', 120) # 2 minutes initial wait
self.max_connection_attempts = 3
# Endless Loop Timing Configuration
self.endless_loop_active = False
self.endless_loop_start_time = None
self.boot_completed_time = None
self.wifi_search_timeout = 120 # 2 minutes to search for WiFi
self.ap_mode_timeout = 180 # 3 minutes in AP mode
self.wifi_validation_interval = 180 # 3 minutes between WiFi validations
self.wifi_validation_retries = 5 # 5 validation attempts (changed from 3)
self.wifi_validation_retry_interval = 10 # 10 seconds between validation retries
self.last_wifi_validation = None
self.wifi_validation_failures = 0
self.consecutive_validation_cycles_failed = 0 # Track consecutive full validation cycle failures
# Smart AP mode management
self.ap_mode_start_time = None
self.ap_timeout = self.ap_mode_timeout # Use endless loop timeout
self.ap_idle_timeout = self.ap_mode_timeout # 3 minutes in AP mode
self.reconnect_interval = self.wifi_search_timeout # 2 minutes to search for WiFi
self.ap_cycle_enabled = True # Always enable cycling for endless loop
self.last_ap_stop_time = None
self.ap_clients_connected = False
self.ap_clients_count = 0
self.cycling_mode = False # Track if we're in AP/reconnect cycle
self.user_connected_to_ap = False # Track if user has connected to AP
self.ap_user_connection_time = None # When user connected to AP
self.force_exit_ap_mode = False # Flag to force exit AP mode from web interface
# Network management
self.known_networks = []
self.available_networks = []
self.default_wifi_interface = shared_data.config.get('wifi_default_interface', 'wlan0')
self.interface_scan_cache = {}
self.interface_cache_time = {}
self.last_scan_interface = None
self.last_interface_reenable_time = 0
self.interface_reenable_interval = shared_data.config.get('wifi_interface_reenable_interval', 30)
self.current_ssid = None
self._pending_ping_sweep_ssid = None
self._ping_sweep_thread = None
self._last_ping_sweep_time = 0
self._last_ping_sweep_ssid = None
self.ping_sweep_cooldown = shared_data.config.get('wifi_ping_sweep_cooldown', 120)
# Failsafe cycle tracking (cycles with no WiFi and no AP clients)
# This counter tracks prolonged disconnections (>5 minutes each) to prevent endless loops
# It will only trigger a reboot after many consecutive long-term failures
self.no_connection_cycles = 0
self.failsafe_cycle_limit = shared_data.config.get('wifi_failsafe_cycle_limit', 20) # Increased from 10 to 20 for safety
self.failsafe_disconnect_threshold = 300 # 5 minutes of disconnection before counting as a cycle
# AP mode settings
self.ap_ssid = shared_data.config.get('wifi_ap_ssid', 'Ragnar')
self.ap_password = shared_data.config.get('wifi_ap_password', 'ragnarconnect')
self.ap_interface = "wlan0"
self.ap_ip = "192.168.4.1"
self.ap_subnet = "192.168.4.0/24"
# Control flags
self.should_exit = False
self.monitoring_thread = None
self.startup_complete = False
# Connection tracking
self.last_connection_type = None # 'wifi', 'ethernet', or None
self.last_ethernet_interface = None
# Load configuration
self.load_wifi_config()
def load_wifi_config(self):
"""Load Wi-Fi configuration from shared data"""
try:
config = self.shared_data.config
# Load known networks
self.known_networks = config.get('wifi_known_networks', [])
# Load AP settings
self.ap_ssid = config.get('wifi_ap_ssid', 'Ragnar')
self.ap_password = config.get('wifi_ap_password', 'ragnarconnect')
self.connection_timeout = config.get('wifi_connection_timeout', 60)
self.max_connection_attempts = config.get('wifi_max_attempts', 3)
self.logger.info(f"Wi-Fi config loaded: {len(self.known_networks)} known networks")
except Exception as e:
self.logger.error(f"Error loading Wi-Fi config: {e}")
def _resolve_scan_interface(self, interface=None):
"""Return a safe interface name for scanning commands."""
if isinstance(interface, str):
candidate = interface.strip()
if candidate:
return candidate
return self.default_wifi_interface
def _find_secondary_wifi_interface(self):
"""Find a WiFi interface that is NOT being used for AP mode.
Returns the interface name (e.g. 'wlan1') or None if only one
WiFi adapter is present.
"""
try:
interfaces = gather_wifi_interfaces(self.default_wifi_interface)
for iface in interfaces:
name = iface.get('name', '')
if name and name != self.ap_interface:
self.logger.info(f"Secondary WiFi interface found: {name}")
return name
except Exception as exc:
self.logger.debug(f"Unable to detect secondary WiFi interface: {exc}")
return None
def _cache_interface_networks(self, interface, networks):
"""Cache scan results per interface while preserving legacy attributes."""
target_iface = self._resolve_scan_interface(interface)
normalized = networks or []
self.interface_scan_cache[target_iface] = normalized
self.interface_cache_time[target_iface] = time.time()
self.last_scan_interface = target_iface
# Preserve legacy behavior for components that read the flat attribute
self.available_networks = normalized
def _get_cached_interface_networks(self, interface):
target_iface = self._resolve_scan_interface(interface)
return (
self.interface_scan_cache.get(target_iface),
self.interface_cache_time.get(target_iface)
)
def _get_known_ssids(self):
"""Return a deduplicated list of Ragnar-configured SSIDs."""
known_ssids = []
seen = set()
try:
for entry in self.known_networks or []:
if isinstance(entry, dict):
ssid = entry.get('ssid')
else:
ssid = str(entry).strip()
if ssid and ssid not in seen:
known_ssids.append(ssid)
seen.add(ssid)
except Exception as exc:
self.logger.debug(f"Unable to enumerate known SSIDs: {exc}")
return known_ssids
def _run_iwlist_scan(self, interface, *, system_profiles=None, known_ssids=None, log_target=None):
"""Execute iwlist scan and return normalized network list."""
logger_target = log_target or self.logger
system_profiles = set(system_profiles or [])
known_set = set(known_ssids or self._get_known_ssids())
cmd = ['sudo', 'iwlist', interface, 'scan']
logger_target.debug(f"Running iwlist scan on {interface}: {' '.join(cmd)}")
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=20)
except FileNotFoundError:
logger_target.warning("iwlist command not available; cannot perform fallback scan")
return []
except Exception as exc:
logger_target.warning(f"iwlist scan execution failed: {exc}")
return []
if result.returncode != 0:
stderr_snippet = (result.stderr or '').strip()
logger_target.warning(f"iwlist scan returned code {result.returncode} on {interface}: {stderr_snippet}")
return []
parsed_networks = self._parse_iwlist_output(result.stdout or '') or []
if not parsed_networks:
logger_target.info(f"iwlist scan on {interface} returned no parseable networks")
return []
deduped = {}
for network in parsed_networks:
ssid = network.get('ssid')
if not ssid or ssid == self.ap_ssid:
continue
try:
signal = int(network.get('signal', 0) or 0)
except (TypeError, ValueError):
signal = 0
existing = deduped.get(ssid)
if not existing or signal > existing.get('signal', 0):
enriched = {
'ssid': ssid,
'signal': signal,
'security': network.get('security', 'Unknown'),
'known': ssid in known_set or ssid in system_profiles,
'has_system_profile': ssid in system_profiles,
'scan_method': 'iwlist'
}
deduped[ssid] = enriched
final_networks = sorted(deduped.values(), key=lambda x: x.get('signal', 0), reverse=True)
logger_target.info(f"iwlist scan discovered {len(final_networks)} networks on {interface}")
return final_networks
def _set_current_ssid(self, ssid):
"""Update current SSID and notify shared storage manager."""
self.current_ssid = ssid
if hasattr(self.shared_data, 'set_active_network'):
try:
self.shared_data.set_active_network(ssid)
except Exception as exc:
self.logger.warning(f"Failed to propagate network change to storage manager: {exc}")
def _trigger_initial_ping_sweep(self, ssid):
"""Schedule a post-connection ping sweep to refresh network data."""
if not ssid:
self.logger.debug("Ping sweep trigger skipped - SSID unavailable")
return
now = time.time()
cooldown = max(0, self.ping_sweep_cooldown or 0)
if (self._last_ping_sweep_ssid == ssid and
now - self._last_ping_sweep_time < cooldown):
remaining = int(cooldown - (now - self._last_ping_sweep_time))
self.logger.info(f"Ping sweep for {ssid} suppressed - cooldown {remaining}s remaining")
return
if self._ping_sweep_thread and self._ping_sweep_thread.is_alive():
self.logger.info("Ping sweep already running - skipping additional trigger")
return
self.logger.info(f"Scheduling initial ping sweep for SSID '{ssid}'")
self._last_ping_sweep_time = now
self._last_ping_sweep_ssid = ssid
self._ping_sweep_thread = threading.Thread(
target=self._run_initial_ping_sweep,
args=(ssid,),
daemon=True
)
self._ping_sweep_thread.start()
def _run_initial_ping_sweep(self, ssid):
"""Background worker that runs the lightweight NetworkScanner ping sweep."""
try:
from actions.scanning import NetworkScanner
except ImportError as import_error:
self.logger.error(f"Unable to import NetworkScanner for ping sweep: {import_error}")
return
try:
scanner = NetworkScanner(self.shared_data)
summary = scanner.run_initial_ping_sweep(include_arp_scan=True)
if summary:
total_hosts = summary.get('arp_hosts', 0) + summary.get('ping_hosts', 0)
cidrs = ', '.join(summary.get('target_cidrs', []))
self.logger.info(
f"Initial ping sweep for {ssid} completed - {total_hosts} hosts touched across {cidrs}"
)
else:
self.logger.warning(f"Ping sweep for {ssid} finished without summary data")
except Exception as exc:
import traceback
self.logger.error(f"Ping sweep thread failed: {exc}")
self.logger.debug(traceback.format_exc())
def save_wifi_config(self):
"""Save Wi-Fi configuration to shared data"""
try:
self.shared_data.config['wifi_known_networks'] = self.known_networks
self.shared_data.config['wifi_ap_ssid'] = self.ap_ssid
self.shared_data.config['wifi_ap_password'] = self.ap_password
self.shared_data.config['wifi_connection_timeout'] = self.connection_timeout
self.shared_data.config['wifi_max_attempts'] = self.max_connection_attempts
self.shared_data.save_config()
self.logger.info("Wi-Fi configuration saved")
except Exception as e:
self.logger.error(f"Error saving Wi-Fi config: {e}")
def setup_ap_logger(self):
"""Setup dedicated logger for AP mode operations"""
try:
# Create a dedicated logger for AP mode
self.ap_logger = logging.getLogger('WiFiManager_AP')
self.ap_logger.setLevel(logging.DEBUG)
# Remove existing handlers to avoid duplication
for handler in self.ap_logger.handlers[:]:
self.ap_logger.removeHandler(handler)
# Create file handler for /var/log/ap.log
try:
ap_log_file = '/var/log/ap.log'
# Ensure log directory exists
os.makedirs(os.path.dirname(ap_log_file), exist_ok=True)
file_handler = logging.FileHandler(ap_log_file)
except (PermissionError, OSError):
# Fallback to local log file if /var/log is not writable
ap_log_file = os.path.join(self.shared_data.logsdir, 'ap.log')
os.makedirs(os.path.dirname(ap_log_file), exist_ok=True)
file_handler = logging.FileHandler(ap_log_file)
# Create formatter for detailed logging
formatter = logging.Formatter(
'%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
file_handler.setFormatter(formatter)
# Add handler to logger
self.ap_logger.addHandler(file_handler)
# Prevent propagation to avoid duplicate logging
self.ap_logger.propagate = False
# Log startup message
self.ap_logger.info("="*50)
self.ap_logger.info("AP Mode Logger Initialized")
self.ap_logger.info(f"Log file: {ap_log_file}")
self.ap_logger.info("="*50)
except Exception as e:
self.logger.error(f"Failed to setup AP logger: {e}")
# Create a fallback logger that writes to the main logger
self.ap_logger = self.logger
def start(self):
"""Start the Wi-Fi management system with endless loop behavior"""
self.logger.info("Starting Wi-Fi Manager with Endless Loop behavior...")
# Mark boot completion time for endless loop timing
self.boot_completed_time = time.time()
# Create a restart detection file to help identify service restarts
self._create_restart_marker()
# Start monitoring thread
if not self.monitoring_thread or not self.monitoring_thread.is_alive():
self.monitoring_thread = threading.Thread(target=self._endless_loop_monitoring, daemon=True)
self.monitoring_thread.start()
# Initial connection assessment and endless loop startup
self._initial_endless_loop_sequence()
def stop(self):
"""Stop the Wi-Fi management system"""
self.logger.info("Stopping Wi-Fi Manager...")
self.ap_logger.info("WiFi Manager stopping - shutting down AP logger")
# Save current connection state before stopping
current_ssid = self.get_current_ssid()
is_connected = self.check_wifi_connection()
self._save_connection_state(current_ssid, is_connected)
self.should_exit = True
# Clean up restart marker
self._cleanup_restart_marker()
if self.ap_mode_active:
self.stop_ap_mode()
if self.monitoring_thread and self.monitoring_thread.is_alive():
self.monitoring_thread.join(timeout=5)
# Close AP logger
try:
if hasattr(self, 'ap_logger') and hasattr(self.ap_logger, 'handlers'):
self.ap_logger.info("AP logger shutting down")
for handler in self.ap_logger.handlers[:]:
handler.close()
self.ap_logger.removeHandler(handler)
except Exception as e:
self.logger.warning(f"Error closing AP logger: {e}")
def _save_connection_state(self, ssid=None, connected=False):
"""Save current connection state to help with service restarts"""
try:
state_file = '/tmp/ragnar_wifi_state.json'
state = {
'timestamp': time.time(),
'connected': connected,
'ssid': ssid,
'ap_mode': self.ap_mode_active
}
with open(state_file, 'w') as f:
json.dump(state, f)
except Exception as e:
self.logger.warning(f"Could not save connection state: {e}")
def _load_connection_state(self):
"""Load previous connection state"""
try:
state_file = '/tmp/ragnar_wifi_state.json'
if os.path.exists(state_file):
with open(state_file, 'r') as f:
state = json.load(f)
# Only use state if it's recent (less than 10 minutes old)
if time.time() - state.get('timestamp', 0) < 600:
return state
except Exception as e:
self.logger.warning(f"Could not load connection state: {e}")
return None
def _cleanup_connection_state(self):
"""Clean up connection state file"""
try:
state_file = '/tmp/ragnar_wifi_state.json'
if os.path.exists(state_file):
os.remove(state_file)
except Exception as e:
self.logger.warning(f"Could not clean up connection state: {e}")
def _create_restart_marker(self):
"""Create a marker file to help detect service restarts"""
try:
marker_file = '/tmp/ragnar_wifi_manager.pid'
with open(marker_file, 'w') as f:
f.write(f"{os.getpid()}\n{time.time()}\n")
except Exception as e:
self.logger.warning(f"Could not create restart marker: {e}")
def _cleanup_restart_marker(self):
"""Clean up the restart marker file"""
try:
marker_file = '/tmp/ragnar_wifi_manager.pid'
if os.path.exists(marker_file):
os.remove(marker_file)
except Exception as e:
self.logger.warning(f"Could not clean up restart marker: {e}")
def _was_recently_running(self):
"""Check if WiFi manager was recently running (indicates service restart)"""
try:
marker_file = '/tmp/ragnar_wifi_manager.pid'
if os.path.exists(marker_file):
with open(marker_file, 'r') as f:
lines = f.readlines()
if len(lines) >= 2:
last_start_time = float(lines[1].strip())
# If the marker is less than 5 minutes old, consider it a recent restart
if time.time() - last_start_time < 300:
self.logger.info("Found recent WiFi manager marker - service restart detected")
return True
return False
except Exception as e:
self.logger.warning(f"Could not check restart marker: {e}")
return False
def _initial_endless_loop_sequence(self):
"""Handle initial Wi-Fi connection with endless loop behavior"""
self.logger.info("Starting Endless Loop Wi-Fi management...")
self._ensure_wifi_interfaces_up()
# Wait 5 seconds after boot before starting the endless loop (reduced from 30s)
if self.boot_completed_time:
boot_wait_time = 5 # 5 seconds – interfaces are ready by now
elapsed_since_boot = time.time() - self.boot_completed_time
remaining_wait = boot_wait_time - elapsed_since_boot
if remaining_wait > 0:
self.logger.info(f"Waiting {remaining_wait:.1f}s more before starting endless loop (5 seconds after boot)")
time.sleep(remaining_wait)
# Check if we're already connected before starting the loop (Ethernet preferred)
if self.check_network_connectivity():
if self.last_connection_type == 'wifi':
self.wifi_connected = True
self.shared_data.wifi_connected = True
self._set_current_ssid(self.get_current_ssid())
self._trigger_initial_ping_sweep(self.current_ssid)
self.logger.info(f"Already connected to Wi-Fi network: {self.current_ssid}")
self._save_connection_state(self.current_ssid, True)
elif self.last_connection_type == 'ethernet':
self.wifi_connected = False
self.shared_data.wifi_connected = False
self.logger.info("Active Ethernet connection detected; using LAN as default and skipping Wi-Fi search.")
self.last_wifi_validation = time.time()
self.startup_complete = True
self.endless_loop_active = True
return
# Start the endless loop
self.endless_loop_start_time = time.time()
self.startup_complete = True
self.logger.info("Endless Loop started - beginning WiFi search phase")
# Try to connect to known networks for 2 minutes
self._endless_loop_wifi_search()
def _endless_loop_wifi_search(self):
"""Search for and connect to known WiFi networks - simply enable WiFi and let system auto-reconnect"""
self.logger.info("Endless Loop: Starting WiFi search phase (1 minute)")
search_start_time = time.time()
self._ensure_wifi_interfaces_up()
# First check if already connected
if self.check_wifi_connection():
self.wifi_connected = True
self.shared_data.wifi_connected = True
self._set_current_ssid(self.get_current_ssid())
self.logger.info(f"Endless Loop: Already connected to {self.current_ssid}")
self._save_connection_state(self.current_ssid, True)
self.last_wifi_validation = time.time()
self.consecutive_validation_cycles_failed = 0
self._trigger_initial_ping_sweep(self.current_ssid)
return True
# Enable WiFi and let Linux/NetworkManager auto-reconnect to known networks
self.logger.info("Endless Loop: Enabling WiFi mode - system will auto-reconnect to known networks")
try:
# Return interface to NetworkManager control (if it was in AP mode)
subprocess.run(['sudo', 'nmcli', 'dev', 'set', self.ap_interface, 'managed', 'yes'],
capture_output=True, text=True, timeout=10)
# Enable WiFi radio (equivalent to user toggling WiFi on)
subprocess.run(['sudo', 'nmcli', 'radio', 'wifi', 'on'],
capture_output=True, text=True, timeout=10)
self.logger.info("Endless Loop: WiFi enabled - waiting up to 60 seconds for auto-connection")
except Exception as e:
self.logger.error(f"Error enabling WiFi: {e}")
# Wait up to 60 seconds for automatic connection (check every 5 seconds)
timeout = 60 # 1 minute to connect
check_interval = 5
elapsed = 0
while elapsed < timeout and not self.should_exit:
time.sleep(check_interval)
elapsed += check_interval
if self.check_wifi_connection():
self.wifi_connected = True
self.shared_data.wifi_connected = True
self._set_current_ssid(self.get_current_ssid())
self.logger.info(f"Endless Loop: Successfully auto-connected to {self.current_ssid} after {elapsed}s")
self._save_connection_state(self.current_ssid, True)
self.last_wifi_validation = time.time()
self.consecutive_validation_cycles_failed = 0
self.no_connection_cycles = 0 # Reset failsafe counter on success
self._trigger_initial_ping_sweep(self.current_ssid)
return True
else:
self.logger.debug(f"Endless Loop: Waiting for auto-connection... ({elapsed}s/{timeout}s)")
# Strengthened validation before AP fallback: perform a strong connectivity check (ping 8.8.8.8)
self.logger.info("Endless Loop: Performing strong connectivity verification before AP fallback")
if self._strong_wifi_presence_check():
self.logger.info("Endless Loop: Strong check indicates WiFi connectivity; aborting AP fallback")
self.wifi_connected = True
self.shared_data.wifi_connected = True
self._set_current_ssid(self.get_current_ssid())
self._save_connection_state(self.current_ssid, True)
self.last_wifi_validation = time.time()
self.no_connection_cycles = 0
self._trigger_initial_ping_sweep(self.current_ssid)
return True
# No WiFi connected after 1 minute — try open networks before AP fallback
if self._try_open_network_connect():
self.logger.info("Endless Loop: Connected via open network — skipping AP mode")
return True
# No WiFi connected after 1 minute, switch to AP mode
self.logger.info(f"Endless Loop: No auto-connection established after {timeout}s, switching to AP mode")
self._endless_loop_start_ap_mode()
return False
def _endless_loop_start_ap_mode(self):
"""Start AP mode as part of endless loop"""
self.logger.info("Endless Loop: Starting AP mode (3 minutes)")
if self.start_ap_mode():
self.ap_mode_start_time = time.time()
self.user_connected_to_ap = False
self.ap_user_connection_time = None
self.force_exit_ap_mode = False
self.logger.info("Endless Loop: AP mode started, waiting for connections or timeout")
else:
self.logger.error("Endless Loop: Failed to start AP mode, retrying WiFi search")
# If AP fails, try WiFi search again after short delay
time.sleep(30)
self._endless_loop_wifi_search()
def _endless_loop_monitoring(self):
"""Main endless loop monitoring thread"""
last_state_save = 0
while not self.should_exit:
try:
current_time = time.time()
# Wait for endless loop to be active
if not self.endless_loop_active:
time.sleep(5)
continue
# Check current connection status
was_connected = self.wifi_connected
self.wifi_connected = self.check_wifi_connection()
self.shared_data.wifi_connected = self.wifi_connected
# Handle WiFi connection state changes
if was_connected and not self.wifi_connected:
self.logger.warning("Endless Loop: Wi-Fi connection lost!")
self._save_connection_state(None, False)
self.wifi_validation_failures = 0 # Reset validation failures
self._pending_ping_sweep_ssid = None
# Only increment failsafe counter if we're in a problematic state:
# - Not in AP mode (as AP mode is the normal recovery mechanism)
# - Have been disconnected for an extended period (default: 5 minutes)
if not self.ap_mode_active:
# Only count this as a failure cycle if we've been disconnected for a while
# This prevents momentary connection blips from triggering failsafe
if not hasattr(self, '_disconnect_timestamp'):
self._disconnect_timestamp = current_time
self.logger.info("Endless Loop: Connection lost, starting disconnect timer")
else:
disconnect_duration = current_time - self._disconnect_timestamp
# Only count as a cycle failure if disconnected for more than threshold (default 5 minutes)
if disconnect_duration > self.failsafe_disconnect_threshold:
self.no_connection_cycles += 1
self.logger.warning(f"Failsafe counter increment (disconnected for {disconnect_duration:.0f}s): {self.no_connection_cycles}/{self.failsafe_cycle_limit}")
self._disconnect_timestamp = current_time # Reset for next cycle
if self.no_connection_cycles >= self.failsafe_cycle_limit:
self.logger.error(f"Failsafe threshold reached ({self.failsafe_cycle_limit} cycles) - initiating reboot as last resort")
self._failsafe_reboot()
self._endless_loop_wifi_search()
elif not was_connected and self.wifi_connected:
self.logger.info("Endless Loop: Wi-Fi connection established!")
current_ssid = self.get_current_ssid()
# Immediately propagate the SSID so the per-network
# database switches before the next scan cycle.
self._set_current_ssid(current_ssid)
self._save_connection_state(current_ssid, True)
self.last_wifi_validation = current_time
self.wifi_validation_failures = 0
self.no_connection_cycles = 0 # Reset failsafe counter on success
self._pending_ping_sweep_ssid = current_ssid
# Clear disconnect timestamp when reconnected
if hasattr(self, '_disconnect_timestamp'):
delattr(self, '_disconnect_timestamp')
if self.ap_mode_active:
self.logger.info("Endless Loop: Stopping AP mode due to successful WiFi connection")
self.stop_ap_mode()
# Handle WiFi validation every 3 minutes when connected
if (self.wifi_connected and self.last_wifi_validation and
(current_time - self.last_wifi_validation) >= self.wifi_validation_interval):
self._perform_wifi_validation()
# Handle AP mode timeout and user connection monitoring
if self.ap_mode_active:
self._handle_ap_mode_monitoring(current_time)
# Handle force exit AP mode from web interface
if self.force_exit_ap_mode and self.ap_mode_active:
self.logger.info("Endless Loop: Force exit AP mode requested from web interface")
self.stop_ap_mode()
self.force_exit_ap_mode = False
self._endless_loop_wifi_search()
# Update current SSID if connected
if self.wifi_connected:
connected_ssid = self.get_current_ssid()
self._set_current_ssid(connected_ssid)
if self._pending_ping_sweep_ssid:
self._trigger_initial_ping_sweep(self._pending_ping_sweep_ssid)
self._pending_ping_sweep_ssid = None
# Periodically save connection state (every 2 minutes)
if current_time - last_state_save > 120:
ssid = self.current_ssid if self.wifi_connected else None
self._save_connection_state(ssid, self.wifi_connected)
last_state_save = current_time
time.sleep(self.connection_check_interval)
except Exception as e:
self.logger.error(f"Error in endless loop monitoring: {e}")
time.sleep(5)
def _perform_wifi_validation(self):
"""Perform 5 WiFi validation checks, 10 seconds apart. All 5 must fail to trigger AP mode."""
self.logger.info("Endless Loop: Starting WiFi validation (5 checks, 10s apart)")
validation_failures = 0
for i in range(self.wifi_validation_retries):
if not self.check_wifi_connection():
validation_failures += 1
self.logger.warning(f"Endless Loop: WiFi validation failed ({validation_failures}/{self.wifi_validation_retries})")
else:
self.logger.info(f"Endless Loop: WiFi validation passed ({i+1}/{self.wifi_validation_retries})")
# Don't wait after the last check
if i < self.wifi_validation_retries - 1:
time.sleep(self.wifi_validation_retry_interval)
# Update validation time
self.last_wifi_validation = time.time()
# If ALL 5 validations failed, disconnect and start endless loop
if validation_failures == self.wifi_validation_retries:
self.consecutive_validation_cycles_failed += 1
self.logger.warning(f"Endless Loop: All {self.wifi_validation_retries} WiFi validations failed! Switching to AP mode")
self.wifi_connected = False
self.shared_data.wifi_connected = False
self.disconnect_wifi() # Disconnect from current network
self._endless_loop_start_ap_mode()
else:
# Reset consecutive failure counter if any check passed
self.consecutive_validation_cycles_failed = 0
self.logger.info(f"Endless Loop: WiFi validation completed - {self.wifi_validation_retries - validation_failures}/{self.wifi_validation_retries} passed")
def _strong_wifi_presence_check(self):
"""Perform a strong connectivity check (ping external host) to verify real internet access"""
try:
self.logger.debug("Performing strong WiFi presence check (ping 8.8.8.8)")
result = subprocess.run(['ping', '-c', '2', '-W', '3', '8.8.8.8'],
capture_output=True, timeout=8)
if result.returncode == 0:
self.logger.info("Strong check: Successfully pinged 8.8.8.8 - WiFi connected")
return True
else:
self.logger.warning("Strong check: Failed to ping 8.8.8.8")
return False
except Exception as e:
self.logger.error(f"Strong check error: {e}")
return False
def _failsafe_reboot(self):
"""Reboot the system as a last resort failsafe mechanism"""
try:
self.logger.critical("FAILSAFE: Initiating system reboot due to persistent connectivity issues")
self.ap_logger.critical("FAILSAFE: System reboot triggered - persistent connectivity failures")
# Save state before reboot
self._save_connection_state(None, False)
# Log the failsafe trigger
try:
with open('/var/log/ragnar_failsafe.log', 'a') as f:
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
f.write(f"{timestamp} - Failsafe reboot triggered: {self.no_connection_cycles} consecutive connection failures\n")
except:
pass
# Clean shutdown of services
self.should_exit = True
if self.ap_mode_active:
self.stop_ap_mode()
# Wait a moment for cleanup
time.sleep(2)
# Initiate reboot
self.logger.critical("FAILSAFE: Executing reboot command")
subprocess.run(['sudo', 'reboot'], timeout=5)
except Exception as e:
self.logger.error(f"Error during failsafe reboot: {e}")
# Try alternative reboot method
try:
subprocess.run(['sudo', 'systemctl', 'reboot'], timeout=5)
except:
pass
def _handle_ap_mode_monitoring(self, current_time):
"""Handle AP mode monitoring for endless loop with improved recovery"""
if not self.ap_mode_active or not self.ap_mode_start_time:
return
ap_uptime = current_time - self.ap_mode_start_time
# Check for client connections
current_client_count = self.check_ap_clients()
# Detect new user connection
if current_client_count > 0 and not self.user_connected_to_ap:
self.user_connected_to_ap = True
self.ap_user_connection_time = current_time
self.logger.info("Endless Loop: User connected to AP - monitoring user activity")
# Periodically check for known WiFi networks while in AP mode (every 30 seconds)
# BUT only after the initial 3-minute grace period to give user time to connect
# This allows recovery even when no user is connected, but not too aggressively
if ap_uptime >= 180 and int(ap_uptime) % 30 == 0: # Start checking after 3 minutes
self.logger.info("Endless Loop: Checking for available known WiFi networks while in AP mode (after 3-min grace period)...")
if self._check_known_networks_available():
self.logger.info("Endless Loop: Known WiFi network detected! Attempting to connect...")
self.stop_ap_mode()
time.sleep(2) # Brief pause for clean transition
if self._endless_loop_wifi_search():
self.logger.info("Endless Loop: Successfully reconnected to known WiFi from AP mode")
return
else:
# If reconnection fails, restart AP mode
self.logger.warning("Endless Loop: Failed to reconnect to WiFi, restarting AP mode")
self._endless_loop_start_ap_mode()
return
# Handle user-connected AP mode
if self.user_connected_to_ap and self.ap_user_connection_time:
user_connection_time = current_time - self.ap_user_connection_time
# Check if user is still connected every 30 seconds
if current_client_count == 0:
self.logger.info("Endless Loop: User disconnected from AP - switching back to WiFi search")
self.stop_ap_mode()
self._endless_loop_wifi_search()
return
# After 3 minutes with user connected, validate user still connected
if user_connection_time >= self.ap_mode_timeout:
if current_client_count > 0:
self.logger.info("Endless Loop: User still connected after 3 minutes - continuing AP mode")
# Reset the timer to check again in 3 minutes
self.ap_user_connection_time = current_time
else:
self.logger.info("Endless Loop: User no longer connected after 3 minutes - switching to WiFi search")
self.stop_ap_mode()
self._endless_loop_wifi_search()
return
# Handle AP timeout when no user connected
elif not self.user_connected_to_ap and ap_uptime >= self.ap_mode_timeout:
self.logger.info("Endless Loop: AP mode timeout (3 minutes) - no users connected, switching to WiFi search")
self.stop_ap_mode()
self._endless_loop_wifi_search()
def get_system_wifi_profiles(self):
"""Get all WiFi connection profiles from NetworkManager (system-wide)"""
try:
result = subprocess.run(['nmcli', '-t', '-f', 'NAME,TYPE', 'con', 'show'],
capture_output=True, text=True, timeout=10)
wifi_profiles = []
if result.returncode == 0:
for line in result.stdout.strip().split('\n'):
if line and ':' in line:
parts = line.split(':')
if len(parts) >= 2 and parts[1] == '802-11-wireless':
wifi_profiles.append(parts[0])
self.logger.debug(f"Found {len(wifi_profiles)} system WiFi profiles: {wifi_profiles}")
return wifi_profiles
except Exception as e:
self.logger.error(f"Error getting system WiFi profiles: {e}")
return []
def _check_known_networks_available(self):
"""Check if any known WiFi networks (Ragnar or system) are currently available without disrupting AP mode"""
try:
# Get both Ragnar's known networks AND system profiles
ragnar_known = [net['ssid'] for net in self.known_networks]
system_profiles = self.get_system_wifi_profiles()
all_known = list(set(ragnar_known + system_profiles)) # Combine and deduplicate
if not all_known:
return False
# Strategy 1: Use secondary adapter for a reliable nmcli scan
secondary = self._find_secondary_wifi_interface()
if secondary:
try:
subprocess.run(
['nmcli', 'dev', 'wifi', 'rescan', 'ifname', secondary],
capture_output=True, text=True, timeout=15
)
time.sleep(1)
result = subprocess.run(
['nmcli', '-t', '-f', 'SSID', 'dev', 'wifi', 'list', 'ifname', secondary],
capture_output=True, text=True, timeout=15
)
if result.returncode == 0:
available_ssids = [
line.strip() for line in result.stdout.strip().split('\n')
if line.strip()
]
for known_ssid in all_known:
if known_ssid in available_ssids:
self.logger.info(f"Known network '{known_ssid}' detected via secondary adapter {secondary}")
return True
except Exception as e:
self.logger.debug(f"Secondary adapter scan failed during known-network check: {e}")
# Strategy 2: Try a quick scan using iwlist on AP interface (less disruptive)
try:
result = subprocess.run(['sudo', 'iwlist', self.ap_interface, 'scan'],
capture_output=True, text=True, timeout=10)
if result.returncode == 0:
available_ssids = []
for line in result.stdout.split('\n'):
if 'ESSID:' in line:
ssid = line.split('ESSID:')[1].strip('"')
if ssid and ssid != '<hidden>':
available_ssids.append(ssid)
# Check if any known networks (Ragnar or system) are available
for known_ssid in all_known:
if known_ssid in available_ssids:
self.logger.info(f"Known network '{known_ssid}' detected while in AP mode")
return True
except Exception as e:
self.logger.debug(f"iwlist scan failed: {e}")
return False
except Exception as e:
self.logger.error(f"Error checking for known networks: {e}")
return False
def exit_ap_mode_from_web(self):
"""Exit AP mode and start WiFi search (called from web interface)"""
self.logger.info("Endless Loop: Exit AP mode requested from web interface")
self.force_exit_ap_mode = True
return True
def _is_fresh_boot(self):
"""Determine if this is a fresh system boot or just a service restart"""
try:
# First check if we were recently running (service restart)
if self._was_recently_running():
self.logger.info("WiFi manager was recently running - treating as service restart")
return False
# Check system uptime
with open('/proc/uptime', 'r') as f:
uptime_seconds = float(f.read().split()[0])
# If system has been up for less than 5 minutes, consider it a fresh boot
if uptime_seconds < 300: # 5 minutes
self.logger.info(f"System uptime: {uptime_seconds:.1f}s - treating as fresh boot")
return True
# Check if this is the first time WiFi manager has started since boot
# by checking if NetworkManager was recently started
result = subprocess.run(['systemctl', 'show', 'NetworkManager', '--property=ActiveEnterTimestamp'],
capture_output=True, text=True, timeout=5)