-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCortanaWireless.py
More file actions
463 lines (389 loc) · 15.5 KB
/
Copy pathCortanaWireless.py
File metadata and controls
463 lines (389 loc) · 15.5 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
from flask import Flask, request, jsonify
import subprocess
import os
import time
app = Flask(__name__)
# Shared secret password
SHARED_SECRET = "C0RT4N4"
# Function to check authorization
def check_authorization(request):
auth_header = request.headers.get("Authorization")
if auth_header == SHARED_SECRET:
return True
return False
# Function to get the current WiFi status (Online/Offline)
def get_wifi_status():
try:
result = subprocess.check_output(['iwgetid']).decode('utf-8').strip()
if result:
return {"status": "Online", "network": result.split('ESSID:\"')[-1].strip('\"')}
else:
return {"status": "Offline"}
except subprocess.CalledProcessError:
return {"status": "Offline"}
# Function to get detailed connection information (SSID, Signal Strength)
def get_connection_info():
try:
# Get WiFi status
status_result = subprocess.check_output(['iwgetid']).decode('utf-8').strip()
status = "Online" if status_result else "Offline"
# Initialize variables
ssid = None
signal_strength = None
ip_address = None
if status == "Online":
# Get WiFi connection details
result = subprocess.check_output(['iwconfig', 'wlan0']).decode('utf-8')
for line in result.splitlines():
if "ESSID" in line:
ssid = line.split('ESSID:')[-1].strip('"')
elif "Signal level" in line:
signal_strength = line.split('Signal level=')[-1].split(' ')[0]
# Get IP address
ip_result = subprocess.check_output(['hostname', '-I']).decode('utf-8').strip()
ip_address = ip_result.split(' ')[0] if ip_result else None
return {"status": status, "ssid": ssid, "signal_strength": signal_strength, "ip_address": ip_address}
except subprocess.CalledProcessError:
return {"error": "Unable to retrieve connection information"}
# Function to disconnect from the current WiFi network
def disconnect_from_network():
try:
subprocess.call(['sudo', 'ifconfig', 'wlan0', 'down'])
subprocess.call(['sudo', 'ifconfig', 'wlan0', 'up'])
return {"message": "Disconnected from current network"}
except Exception as e:
return {"error": str(e)}
@app.route('/status', methods=['GET'])
def status():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(get_wifi_status())
@app.route('/connection_info', methods=['GET'])
def connection_info():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(get_connection_info())
@app.route('/scan', methods=['GET'])
def scan():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
try:
networks = subprocess.check_output(['sudo', 'iwlist', 'wlan0', 'scan']).decode('utf-8')
ssid_list = []
for line in networks.split('\n'):
if "ESSID:" in line:
ssid = line.split('ESSID:')[-1].strip('\"')
if ssid:
ssid_list.append(ssid)
return jsonify({"networks": ssid_list})
except Exception as e:
return jsonify({"error": str(e)})
@app.route('/connect', methods=['POST'])
def connect():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
data = request.json
ssid = data.get('ssid')
password = data.get('password')
if not ssid:
return jsonify({"error": "SSID is required"}), 400
try:
os.system("nmcli device disconnect wlan0")
os.system("sudo dhclient -r wlan0")
time.sleep(2)
with open('/etc/wpa_supplicant/wpa_supplicant.conf', 'r') as file:
lines = file.readlines()
with open('/etc/wpa_supplicant/wpa_supplicant.conf', 'w') as file:
file.writelines([line for line in lines if "network=" not in line])
config = f'network={{\n ssid=\"{ssid}\"\n psk=\"{password}\"\n}}'
with open('/etc/wpa_supplicant/wpa_supplicant.conf', 'a') as file:
file.write(config)
os.system("sudo ifdown wlan0")
time.sleep(1)
os.system("sudo ifup wlan0")
os.system("sudo dhclient wlan0")
result = os.popen(f"nmcli dev wifi connect '{ssid}' password '{password}'").read()
with open("/tmp/wifi_log.txt", "a") as log:
log.write(f"Attempted connection to SSID: {ssid}\n")
log.write(result + "\n")
return jsonify({"message": "Connection attempt in progress"})
except Exception as e:
with open("/tmp/wifi_log.txt", "a") as log:
log.write(f"Connection error: {str(e)}\n")
return jsonify({"error": str(e)})
@app.route('/disconnect', methods=['GET'])
def disconnect():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(disconnect_from_network())
# Bluetooth scanning function
def scan_bluetooth_devices():
try:
subprocess.call(['timeout', '10s', 'bluetoothctl', 'scan', 'on'])
time.sleep(5)
subprocess.call(['bluetoothctl', 'scan', 'off'])
devices = subprocess.check_output(['bluetoothctl', 'devices']).decode('utf-8')
device_list = []
for line in devices.splitlines():
if "Device" in line:
parts = line.split(' ')
mac_address = parts[1]
device_name = ' '.join(parts[2:])
device_list.append({'mac_address': mac_address, 'device_name': device_name})
return {"devices": device_list}
except subprocess.CalledProcessError as e:
return {"error": "Bluetooth scan failed: {}".format(str(e))}
except Exception as e:
return {"error": "An error occurred during Bluetooth scanning: {}".format(str(e))}
def connect_bluetooth_device(mac_address):
try:
devices = subprocess.check_output(['bluetoothctl', 'devices']).decode('utf-8')
if mac_address not in devices:
return {"error": f"Device {mac_address} not available in the device list."}
subprocess.call(['bluetoothctl', 'discoverable', 'on'])
time.sleep(1)
result = subprocess.check_output(['bluetoothctl', 'connect', mac_address]).decode('utf-8')
if "Connection successful" in result:
return {"message": f"Connected to {mac_address}"}
else:
return {"error": f"Failed to connect to {mac_address}: {result.strip()}"}
except subprocess.CalledProcessError as e:
return {"error": f"Failed to connect to {mac_address}: {str(e)}"}
except Exception as e:
return {"error": f"An error occurred while connecting to Bluetooth device: {str(e)}"}
@app.route('/bluetooth_scan', methods=['GET'])
def bluetooth_scan():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(scan_bluetooth_devices())
@app.route('/connect_bluetooth', methods=['POST'])
def connect_bluetooth():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
data = request.json
mac_address = data.get('mac_address')
if not mac_address:
return jsonify({"error": "MAC Address is required"}), 400
return jsonify(connect_bluetooth_device(mac_address))
# XLink & insigniaDNS section
def start_xlinkkai():
try:
subprocess.call(['sudo', 'systemctl', 'start', 'KaiEngine'])
return {"message": "Starting XLink Kai!"}
except Exception as e:
return {"error": str(e)}
def stop_xlinkkai():
try:
subprocess.call(['sudo', 'systemctl', 'stop', 'KaiEngine'])
return {"message": "Stopping XLink Kai!"}
except Exception as e:
return {"error": str(e)}
def enable_xlinkkai():
try:
subprocess.call(['sudo', 'systemctl', 'enable', 'KaiEngine'])
return {"message": "Enabling XLink Kai!"}
except Exception as e:
return {"error": str(e)}
def disable_xlinkkai():
try:
subprocess.call(['sudo', 'systemctl', 'disable', 'KaiEngine'])
return {"message": "Disabling XLink Kai!"}
except Exception as e:
return {"error": str(e)}
def start_insigniadns():
try:
subprocess.call(['sudo', 'systemctl', 'start', 'insigniaDNS'])
return {"message": "Starting insigniaDNS!"}
except Exception as e:
return {"error": str(e)}
def stop_insigniadns():
try:
subprocess.call(['sudo', 'systemctl', 'stop', 'insigniaDNS'])
return {"message": "Stopping insigniaDNS!"}
except Exception as e:
return {"error": str(e)}
def enable_insigniadns():
try:
subprocess.call(['sudo', 'systemctl', 'enable', 'insigniaDNS'])
return {"message": "Enabling insigniaDNS!"}
except Exception as e:
return {"error": str(e)}
def disable_insigniadns():
try:
subprocess.call(['sudo', 'systemctl', 'disable', 'insigniaDNS'])
return {"message": "Disabling insigniaDNS!"}
except Exception as e:
return {"error": str(e)}
def start_xbdStats():
try:
subprocess.call(['sudo', 'systemctl', 'start', 'xbdStats'])
return {"message": "Starting xbdStats!"}
except Exception as e:
return {"error": str(e)}
def stop_xbdStats():
try:
subprocess.call(['sudo', 'systemctl', 'stop', 'xbdStats'])
return {"message": "Stopping xbdStats!"}
except Exception as e:
return {"error": str(e)}
def enable_xbdStats():
try:
subprocess.call(['sudo', 'systemctl', 'enable', 'xbdStats'])
return {"message": "Enabling xbdStats!"}
except Exception as e:
return {"error": str(e)}
def disable_xbdStats():
try:
subprocess.call(['sudo', 'systemctl', 'disable', 'xbdStats'])
return {"message": "Disabling xbdStats!"}
except Exception as e:
return {"error": str(e)}
def start_smb():
try:
subprocess.call(['sudo', 'systemctl', 'start', 'smb'])
return {"message": "Starting SMB server!"}
except Exception as e:
return {"error": str(e)}
def stop_smb():
try:
subprocess.call(['sudo', 'systemctl', 'stop', 'smb'])
return {"message": "Stopping SMB server!"}
except Exception as e:
return {"error": str(e)}
def enable_smb():
try:
subprocess.call(['sudo', 'systemctl', 'enable', 'smb'])
return {"message": "Enabling SMB server!"}
except Exception as e:
return {"error": str(e)}
def disable_smb():
try:
subprocess.call(['sudo', 'systemctl', 'disable', 'smb'])
return {"message": "Disabling SMB server!"}
except Exception as e:
return {"error": str(e)}
def start_ftp():
try:
subprocess.call(['sudo', 'systemctl', 'start', 'ftp'])
return {"message": "Starting FTP server!"}
except Exception as e:
return {"error": str(e)}
def stop_ftp():
try:
subprocess.call(['sudo', 'systemctl', 'stop', 'ftp'])
return {"message": "Stopping FTP server!"}
except Exception as e:
return {"error": str(e)}
def enable_ftp():
try:
subprocess.call(['sudo', 'systemctl', 'enable', 'ftp'])
return {"message": "Enabling FTP server!"}
except Exception as e:
return {"error": str(e)}
def disable_ftp():
try:
subprocess.call(['sudo', 'systemctl', 'disable', 'ftp'])
return {"message": "Disabling FTP server!"}
except Exception as e:
return {"error": str(e)}
# XLink, insigniaDNS xbdStats, SMB and FTP App Routes
@app.route('/startxlinkkai', methods=['GET'])
def startxlinkkai():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(start_xlinkkai())
@app.route('/stopxlinkkai', methods=['GET'])
def stopxlinkkai():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(stop_xlinkkai())
@app.route('/enablexlinkkai', methods=['GET'])
def enablexlinkkai():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(enable_xlinkkai())
@app.route('/disablexlinkkai', methods=['GET'])
def disablexlinkkai():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(disable_xlinkkai())
@app.route('/startinsigniadns', methods=['GET'])
def startinsigniadns():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(start_insigniadns())
@app.route('/stopinsigniadns', methods=['GET'])
def stopinsigniadns():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(stop_insigniadns())
@app.route('/enableinsigniadns', methods=['GET'])
def enableinsigniadns():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(enable_insigniadns())
@app.route('/disableinsigniadns', methods=['GET'])
def disableinsigniadns():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(disable_insigniadns())
@app.route('/startxbdstats', methods=['GET'])
def startxbdstats():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(start_xbdStats())
@app.route('/stopxbdstats', methods=['GET'])
def stopxbdstats():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(stop_xbdStats())
@app.route('/enablexbdstats', methods=['GET'])
def enablexbdstats():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(enable_xbdStats())
@app.route('/disablexbdstats', methods=['GET'])
def disablexbdstats():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(disable_xbdStats())
@app.route('/startxbdstats', methods=['GET'])
def startsmb():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(start_smb())
@app.route('/startsmb', methods=['GET'])
def stopsmb():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(start_smb())
@app.route('/stopsmb', methods=['GET'])
def stopsmb():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(stop_smb())
@app.route('/startftp', methods=['GET'])
def stopsmb():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(start_ftp())
@app.route('/stopftp', methods=['GET'])
def stopsmb():
if not check_authorization(request):
return jsonify({"error": "Unauthorized"}), 401
return jsonify(stop_ftp())
# OUT OF DATE - NEED TO FIX!
@app.route('/', methods=['GET'])
def home():
return """
Welcome to the Raspberry Pi WiFi Manager API. Choose an option:
1. /status - Get Connection Status (Online/Offline)
2. /connection_info - Get Connection Information (SSID, Signal Strength)
3. /scan - Scan for available WiFi Networks
4. /connect - Connect to a new WiFi Network (POST with SSID and Password)
5. /disconnect - Disconnect from current WiFi Network
6. /bluetooth_scan - Scan for Bluetooth Devices
7. /connect_bluetooth - Connect to a Bluetooth Device (POST with MAC Address)
"""
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)