Repository navigation
Expand file tree
/
Copy pathnetwork_utils.py
More file actions
300 lines (247 loc) · 11.9 KB
/
Copy pathnetwork_utils.py
File metadata and controls
300 lines (247 loc) · 11.9 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
"""
Network utilities for WiFi Auto Auth - Multi-Network Support
Handles network detection, SSID identification, and network profile management.
"""
import subprocess
import platform
import re
import json
import os
from typing import Optional, Dict, List, Tuple
from config.logging_config import get_logger
logger = get_logger(__name__)
class NetworkDetector:
"""Handles network detection and SSID identification across different platforms."""
def __init__(self):
self.platform = platform.system().lower()
logger.debug(f"Initialized NetworkDetector for platform: {self.platform}")
def get_current_ssid(self) -> Optional[str]:
"""
Get the SSID of the currently connected WiFi network.
Returns:
str: SSID of current network, or None if not connected to WiFi
"""
try:
if self.platform == "windows":
return self._get_ssid_windows()
elif self.platform == "darwin": # macOS
return self._get_ssid_macos()
elif self.platform == "linux":
return self._get_ssid_linux()
else:
logger.warning(f"Unsupported platform: {self.platform}")
return None
except Exception as e:
logger.error(f"Failed to get current SSID: {e}")
return None
def _get_ssid_windows(self) -> Optional[str]:
"""Get SSID on Windows using netsh command."""
try:
# Use netsh to get WiFi profile information
cmd = ["netsh", "wlan", "show", "profiles"]
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
# Get the currently connected profile
cmd_interfaces = ["netsh", "wlan", "show", "interfaces"]
interfaces_result = subprocess.run(cmd_interfaces, capture_output=True, text=True, check=True)
# Parse the SSID from the interfaces output
for line in interfaces_result.stdout.split('\n'):
if 'SSID' in line and 'BSSID' not in line:
# Extract SSID (format: " SSID : NetworkName")
match = re.search(r'SSID\s*:\s*(.+)', line.strip())
if match:
ssid = match.group(1).strip()
logger.debug(f"Detected Windows SSID: {ssid}")
return ssid
logger.debug("No active WiFi connection found on Windows")
return None
except subprocess.CalledProcessError as e:
logger.error(f"Windows netsh command failed: {e}")
return None
except Exception as e:
logger.error(f"Error getting Windows SSID: {e}")
return None
def _get_ssid_macos(self) -> Optional[str]:
"""Get SSID on macOS using airport utility."""
try:
# Try using networksetup first
cmd = ["networksetup", "-getairportnetwork", "en0"]
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
# Parse output (format: "Current Wi-Fi Network: NetworkName")
if "Current Wi-Fi Network:" in result.stdout:
ssid = result.stdout.split("Current Wi-Fi Network:")[-1].strip()
if ssid and ssid != "You are not associated with an AirPort network.":
logger.debug(f"Detected macOS SSID: {ssid}")
return ssid
# Fallback to airport utility
airport_cmd = ["/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport", "-I"]
airport_result = subprocess.run(airport_cmd, capture_output=True, text=True, check=True)
for line in airport_result.stdout.split('\n'):
if 'SSID:' in line:
ssid = line.split('SSID:')[-1].strip()
logger.debug(f"Detected macOS SSID via airport: {ssid}")
return ssid
logger.debug("No active WiFi connection found on macOS")
return None
except subprocess.CalledProcessError as e:
logger.error(f"macOS network command failed: {e}")
return None
except Exception as e:
logger.error(f"Error getting macOS SSID: {e}")
return None
def _get_ssid_linux(self) -> Optional[str]:
"""Get SSID on Linux using various methods."""
methods = [
self._linux_iwgetid,
self._linux_nmcli,
self._linux_iwconfig
]
for method in methods:
try:
ssid = method()
if ssid:
logger.debug(f"Detected Linux SSID: {ssid}")
return ssid
except Exception as e:
logger.debug(f"Linux method {method.__name__} failed: {e}")
continue
logger.debug("No active WiFi connection found on Linux")
return None
def _linux_iwgetid(self) -> Optional[str]:
"""Get SSID using iwgetid command."""
cmd = ["iwgetid", "-r"]
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
ssid = result.stdout.strip()
return ssid if ssid else None
def _linux_nmcli(self) -> Optional[str]:
"""Get SSID using NetworkManager's nmcli."""
cmd = ["nmcli", "-t", "-f", "active,ssid", "dev", "wifi"]
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
for line in result.stdout.split('\n'):
if line.startswith('yes:'):
ssid = line.split(':', 1)[1]
return ssid if ssid else None
return None
def _linux_iwconfig(self) -> Optional[str]:
"""Get SSID using iwconfig command."""
cmd = ["iwconfig"]
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
for line in result.stdout.split('\n'):
if 'ESSID:' in line:
match = re.search(r'ESSID:"([^"]*)"', line)
if match:
return match.group(1)
return None
class NetworkProfileManager:
"""Manages network profiles and configuration loading."""
def __init__(self, config_path: str = "config.json"):
self.config_path = config_path
self.detector = NetworkDetector()
logger.debug(f"Initialized NetworkProfileManager with config: {config_path}")
def load_config(self) -> Dict:
"""Load and parse configuration file."""
if not os.path.exists(self.config_path):
raise FileNotFoundError(
f"Missing {self.config_path}. Please copy config.example.json to {self.config_path} and configure your networks."
)
with open(self.config_path, "r") as f:
config = json.load(f)
logger.debug(f"Loaded configuration from {self.config_path}")
return config
def get_available_networks(self) -> List[str]:
"""Get list of configured network profile names."""
try:
config = self.load_config()
# Handle both new multi-network format and legacy format
if "networks" in config:
networks = list(config["networks"].keys())
logger.debug(f"Found configured networks: {networks}")
return networks
else:
# Legacy single network configuration
logger.debug("Using legacy single network configuration")
return ["default"]
except Exception as e:
logger.error(f"Error getting available networks: {e}")
return []
def get_network_profile(self, network_name: Optional[str] = None, auto_detect: bool = True) -> Tuple[str, Dict]:
"""
Get network configuration for specified network or auto-detect current network.
Args:
network_name: Specific network profile to use
auto_detect: Whether to auto-detect current network if network_name is None
Returns:
Tuple of (network_name, network_config)
"""
config = self.load_config()
# Handle legacy configuration format
if "networks" not in config:
logger.info("Using legacy configuration format")
legacy_config = {
"ssid": config.get("ssid", "Unknown"),
"wifi_url": config["wifi_url"],
"username": config["username"],
"password": config["password"],
"product_type": config.get("product_type", "0"),
"description": "Legacy configuration"
}
return "legacy", legacy_config
networks = config["networks"]
# If specific network requested, use it
if network_name:
if network_name in networks:
logger.info(f"Using specified network profile: {network_name}")
return network_name, networks[network_name]
else:
raise ValueError(f"Network profile '{network_name}' not found in configuration")
# Auto-detect current network
if auto_detect:
current_ssid = self.detector.get_current_ssid()
if current_ssid:
logger.info(f"Detected current SSID: {current_ssid}")
# Find matching network profile by SSID
for profile_name, profile_config in networks.items():
if profile_config.get("ssid") == current_ssid:
logger.info(f"Found matching network profile: {profile_name}")
return profile_name, profile_config
logger.warning(f"No network profile found for SSID: {current_ssid}")
else:
logger.warning("Could not detect current network SSID")
# Fall back to default network
default_network = config.get("default_network", list(networks.keys())[0])
if default_network in networks:
logger.info(f"Using default network profile: {default_network}")
return default_network, networks[default_network]
# If default not found, use first available
first_network = list(networks.keys())[0]
logger.info(f"Using first available network profile: {first_network}")
return first_network, networks[first_network]
def list_networks(self) -> Dict[str, Dict]:
"""Get detailed information about all configured networks."""
try:
config = self.load_config()
if "networks" not in config:
# Legacy format
return {
"legacy": {
"ssid": config.get("ssid", "Unknown"),
"wifi_url": config["wifi_url"],
"description": "Legacy configuration"
}
}
return config["networks"]
except Exception as e:
logger.error(f"Error listing networks: {e}")
return {}
# Convenience functions for backward compatibility
def get_current_ssid() -> Optional[str]:
"""Get the SSID of the currently connected WiFi network."""
detector = NetworkDetector()
return detector.get_current_ssid()
def get_network_profile(network_name: Optional[str] = None, auto_detect: bool = True) -> Tuple[str, Dict]:
"""Get network configuration for specified network or auto-detect current network."""
manager = NetworkProfileManager()
return manager.get_network_profile(network_name, auto_detect)
def list_available_networks() -> List[str]:
"""Get list of configured network profile names."""
manager = NetworkProfileManager()
return manager.get_available_networks()