-
Notifications
You must be signed in to change notification settings - Fork 9
/
wifi_manager.py
944 lines (778 loc) · 33.8 KB
/
wifi_manager.py
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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
WiFi Manager
Load and connect to configured networks based on an encrypted JSON file.
In case no network has been configured or no connection could be established
to any of the configured networks within the timeout of each 5 seconds an
AccessPoint is created.
A simple Picoweb webserver is hosting the webpages to connect to new networks,
to remove already configured networks from the list of connections to
establish and to get the latest available networks as JSON
"""
# system packages
import gc
import json
import machine
import _thread
import time
import ubinascii
import ucryptolib
# pip installed packages
# https://github.com/miguelgrinberg/microdot
from microdot.microdot_asyncio import Microdot, redirect, Request, Response, \
send_file
from microdot import URLPattern
from microdot.microdot_utemplate import render_template, init_templates
# custom packages
from be_helpers.generic_helper import GenericHelper
from be_helpers.message import Message
from be_helpers.path_helper import PathHelper
from be_helpers.wifi_helper import WifiHelper
# typing not natively supported on micropython
from be_helpers.typing import List, Tuple, Union, Callable
class WiFiManager(object):
"""docstring for WiFiManager"""
ERROR = 0
SUCCESS = 1
CONNECTION_ISSUE_TIMEOUT = 1
CONNECTION_ISSUE_NOT_CONFIGURED = 2
Response.default_content_type = 'text/html'
def __init__(self, logger=None, quiet=False, name=__name__):
# setup and configure logger if none is provided
if logger is None:
logger = GenericHelper.create_logger(
logger_name=self.__class__.__name__)
GenericHelper.set_level(logger, 'debug')
self.logger = logger
self.logger.disabled = quiet
self._config_file = 'wifi-secure.json'
self.app = Microdot()
init_templates(template_dir='lib/templates')
self.wh = WifiHelper()
self.event_sinks = set()
self._available_urls = dict()
self._add_app_routes()
# other setup and init here
# AES key shall be 16 or 32 bytes
required_len = 16
uuid = ubinascii.hexlify(machine.unique_id())
amount = required_len // len(uuid) + (required_len % len(uuid) > 0)
self._enc_key = (uuid * amount).decode('ascii')[:required_len]
self._configured_networks = list()
self._selected_network_bssid = ''
self._connection_timeout = 5
self._connection_result = self.ERROR
# WiFi scan specific defines
self._scan_lock = _thread.allocate_lock()
self._scan_interval = 5000 # milliseconds
# Queue also works, but in this case there is no need for a history
self._scan_net_msg = Message()
self._scan_net_msg.set([]) # empty list, required by save_wifi_config
self._latest_scan = None
self._stop_scanning_timer = None
# start the WiFi scanning thread as soon as "start_config" is called
self.scanning = False
@property
def available_urls(self) -> dict:
"""
Get public available URLs.
:returns: Dict of available URLs as keys and description as values
:rtype: dict
"""
return self._available_urls
@available_urls.setter
def available_urls(self, value: dict) -> None:
"""
Set public available URLs.
This is used to render the index page content
:param value: New URL for index page
:type value: Dict
"""
if isinstance(value, dict):
self._available_urls.update(value)
@property
def connection_timeout(self) -> int:
"""
Get the WiFi connection timeout in seconds.
:returns: Timeout if WiFi connection can not be established within
:rtype: int
"""
return self._connection_timeout
@connection_timeout.setter
def connection_timeout(self, value: int) -> None:
"""
Set the WiFi connection timeout in seconds.
Values below 3 sec are set to 3 sec.
:param value: Timeout of WiFi connection per network in seconds
:type value: int
"""
if isinstance(value, int):
if value < 3:
value = 3
self._connection_timeout = value
@property
def connection_result(self) -> int:
"""
Get connection issue error code
:returns: Connection issue error
:rtype: int
"""
return self._connection_result
def load_and_connect(self) -> bool:
"""
Load configured network credentials and try to connect to those
:returns: Result of connection
:rtype: bool
"""
result = False
# check wifi config file existance
if PathHelper.exists(path=self._config_file):
self.logger.debug('Encrypted wifi config file exists')
loaded_cfg = self._load_wifi_config_data(path=self._config_file,
encrypted=True)
private_cfg = None
ssids = list()
passwords = list()
if isinstance(loaded_cfg, list):
# deepcopy would be necessary but not builtin in micropython
private_cfg = json.loads(json.dumps(loaded_cfg))
for net in private_cfg:
if 'ssid' in net:
ssids.append(net['ssid'])
if 'password' in net:
passwords.append(net['password'])
net['password'] = '*' * 8
else:
passwords.append('')
self._configured_networks = list(ssids).copy()
elif isinstance(loaded_cfg, dict):
private_cfg = loaded_cfg.copy()
if 'ssid' in loaded_cfg:
ssids = loaded_cfg['ssid']
if 'password' in private_cfg:
passwords = loaded_cfg['password']
private_cfg['password'] = '*' * 8
self._configured_networks = ssids
# self._configured_networks = list(ssids).copy()
# self.logger.debug('All SSIDs: {}'.format(ssids))
self.logger.debug('Config content: {}'.format(loaded_cfg))
self.logger.debug('Private config content: {}'.
format(private_cfg))
self.logger.debug('Configured networks: {}'.
format(self._configured_networks))
self.logger.info('Connecting to loaded network config...')
result = WifiHelper.connect(ssid=ssids,
password=passwords,
timeout=self.connection_timeout,
reconnect=False)
self.logger.debug('Result of connection: {}'.format(result))
if result is False:
self._connection_result = self.CONNECTION_ISSUE_TIMEOUT
else:
self.logger.debug('WiFi config file does not (yet) exist')
self._connection_result = self.CONNECTION_ISSUE_NOT_CONFIGURED
return result
def start_config(self) -> None:
"""Start WiFi manager accesspoint and webserver."""
ap_name = 'WiFiManager_{}'.format(
GenericHelper.get_uuid(-4).decode('ascii'))
self.logger.info('Starting WiFiManager as AccessPoint "{}"'.
format(ap_name))
result = self.wh.create_ap(ssid=ap_name,
password='',
channel=11,
timeout=5)
self.logger.debug('Created AP: {}'.format(result))
ifconfig = self.wh.ifconfig_ap
self.logger.debug(ifconfig)
# start scanning for available networks
self.scanning = True
# finally
self.run(host=ifconfig.ip, port=80, debug=True)
self.logger.debug('Finished running the Webserver application')
self.scanning = False
self.logger.debug('Stopped scanning thread')
# wait some time to end all threads savely
time.sleep(5)
gc.collect()
self.logger.debug('Goodbye from WiFiManager')
def add_url_rule(self,
url: str,
func: Callable[[Request], None],
methods: Tuple[str, ...] = ['GET']) -> None:
"""
Register a function as a request handler for a given URL
:param url: URL pattern
:type url: str
:param func: The function to be called
:type func: Callable[[Request], None]
:param methods: HTTP methods handled by the function
:type methods: Tuple[str, ...]
"""
# @app.route('/')
# def index(self, req): pass
# https://github.com/miguelgrinberg/microdot/blob/81394980234f24aac834faf8e2e8225231e9014b/src/microdot.py#L785-L786
self.app.url_map.append((methods, URLPattern(url), func))
def _add_app_routes(self) -> None:
"""Add all application routes to the webserver."""
self.add_url_rule(url='/', func=self.landing_page)
self.add_url_rule(url='/select', func=self.wifi_selection)
self.add_url_rule(url='/render_network_inputs',
func=self.render_network_inputs)
self.add_url_rule(url='/configure', func=self.wifi_configs)
self.add_url_rule(url='/save_wifi_config',
func=self.save_wifi_config,
methods=['POST'])
self.add_url_rule(url='/remove_wifi_config',
func=self.remove_wifi_config,
methods=['POST'])
self.add_url_rule(url='/scan_result', func=self.scan_result)
self.add_url_rule(url=r'<re:(.*)\.css|(.*)\.js:path>',
func=self.serve_static)
self.add_url_rule(url='/favicon.ico', func=self.serve_favicon)
self.add_url_rule(url='/shutdown', func=self.shutdown)
self.app.error_handlers[404] = self.not_found
available_urls = {
"/select": {
"title": "Select WiFi",
"color": "border-primary",
"text": "Configure WiFi networks",
},
"/configure": {
"title": "Manage WiFi networks",
"color": "border-primary",
"text": "Remove credentials of configured WiFi networks",
},
"/scan_result": {
"title": "Scan data",
"color": "text-white bg-info",
"text": "Latest WiFi scan data as JSON",
}
}
self.available_urls = available_urls
def _encrypt_data(self, data: Union[str, list, dict]) -> bytes:
"""
Encrypt data with encryption key
:param data: The data
:type data: Union[str, list, dict]
:returns: Encrypted data
:rtype: bytes
"""
# https://forum.micropython.org/viewtopic.php?t=6726
# create bytes array of the data and encrypt it
if not isinstance(data, str):
data = str(data)
data_bytes = data.encode()
enc = ucryptolib.aes(self._enc_key, 1)
# add '\x00' to fill up the data string to reach a multiple of 16
encrypted_data = enc.encrypt(data_bytes + b'\x00' * ((16 - (len(data_bytes) % 16)) % 16)) # noqa: E501
return encrypted_data
def _decrypt_data(self, data: bytes) -> str:
"""
Decrypt data with decryption key
:param data: The data
:type data: bytes
:returns: Decrypted data
:rtype: str
"""
# https://forum.micropython.org/viewtopic.php?t=6726
# decrypt bytes array
dec = ucryptolib.aes(self._enc_key, 1)
decrypted_data = dec.decrypt(data)
# remove added '\x00' stuff after decoding it to ascii
decrypted_data_str = decrypted_data.decode('ascii').rstrip('\x00')
return decrypted_data_str
def extend_wifi_config_data(self,
data: Union[dict, List[dict]],
path: str,
encrypted: bool = False) -> None:
"""
Extend WiFi configuration data of file.
:param data: The data
:type data: Union[dict, List[dict]]
:param path: The full path to the file
:type path: str
:param encrypted: Flag to save data encrypted
:type encrypted: bool, optional
"""
# in case the file already exists, extend its data content
if PathHelper.exists(path=path):
existing_data = self._load_wifi_config_data(path=path,
encrypted=encrypted)
self.logger.debug('Existing WiFi config data: {}'.
format(existing_data))
if isinstance(existing_data, dict):
if isinstance(data, dict):
data = [existing_data, data]
elif isinstance(data, list):
data = [existing_data] + data
elif isinstance(existing_data, list):
if isinstance(data, dict):
existing_data.append(data)
data = existing_data
elif isinstance(data, list):
data = existing_data + data
else:
# unknown content, overwrite it
pass
self.logger.debug('Updated data: {}'.format(data))
ssids = list()
if isinstance(data, list):
for net in data:
if 'ssid' in net:
ssids.append(net['ssid'])
elif isinstance(data, dict):
if 'ssid' in data:
ssids = [data['ssid']]
self._configured_networks = ssids.copy()
if encrypted:
# create bytes array of the dict and encrypt it
encrypted_data = self._encrypt_data(data=data)
# save data to file as binary as it contains encrypted data
GenericHelper.save_file(data=encrypted_data,
path=path,
mode='wb')
self.logger.debug('Saved encrypted data as json: {}'.
format(encrypted_data))
else:
# save data to file, no need for binary mode
GenericHelper.save_json(data=data,
path=path,
mode='w')
self.logger.debug('Saved data as json: {}'.format(data))
def _load_wifi_config_data(self,
path: str,
encrypted: bool = False) -> Union[dict,
List[dict]]:
"""
Load WiFi configuration data from file.
:param path: The full path to the file
:type path: str
:param encrypted: Flag to decrypt data
:type encrypted: bool, optional
:returns: The loaded data
:rtype: Union[dict, List[dict]]
"""
data = dict()
if encrypted:
# read file in binary as it contains encrypted data
encrypted_read_data = GenericHelper.load_file(path=path,
mode='rb')
self.logger.debug('Read encrypted data: {}'.
format(encrypted_read_data))
# decrypt read data
decrypted_data_str = self._decrypt_data(data=encrypted_read_data)
self.logger.debug('Decrypted data str: {}'.
format(decrypted_data_str))
# convert string to dict
data = GenericHelper.str_to_dict(data=decrypted_data_str)
self.logger.debug('Decrypted data dict: {}'.format(data))
else:
data = GenericHelper.load_json(path=path, mode='r')
self.logger.debug('Read non encrypted data: {}'.
format(data))
return data
@property
def configured_networks(self) -> List[str]:
"""
Get SSIDs of all configured networks
:returns: SSIDs of configured networks
:rtype: List[str]
"""
return self._configured_networks
def _scan(self,
wh: WifiHelper,
msg: Message,
scan_interval: int,
lock: int) -> None:
"""
Scan for available networks.
:param wh: Wifi helper object
:type wh: WifiHelper
:param msg: The shared message from this thread
:type msg: Message
:param scan_interval: The scan interval in milliseconds
:type scan_interval: int
:param lock: The lock object
:type lock: _thread.lock
"""
while lock.locked():
try:
# rescan for available networks
found_nets = wh.get_wifi_networks_sorted(rescan=True,
scan_if_empty=True)
msg.set(found_nets)
# wait for specified time
time.sleep_ms(scan_interval)
except KeyboardInterrupt:
break
print('Finished scanning')
@property
def scan_interval(self) -> int:
"""
Get the WiFi scan interval in milliseconds.
:returns: Interval of WiFi scans in milliseconds
:rtype: int
"""
return self._scan_interval
@scan_interval.setter
def scan_interval(self, value: int) -> None:
"""
Set the WiFi scan interval in milliseconds.
Values below 1000 ms are set to 1000 ms.
One scan takes around 3 sec, which leads to maximum 15 scans per min
:param value: Interval of WiFi scans in milliseconds
:type value: int
"""
if isinstance(value, int):
if value < 1000:
value = 1000
self._scan_interval = value
@property
def scanning(self) -> bool:
"""
Get the scanning status.
:returns: Flag whether WiFi network scan is running or not.
:rtype: bool
"""
return self._scan_lock.locked()
@scanning.setter
def scanning(self, value: int) -> None:
"""
Start or stop scanning for available WiFi networks.
:param value: The value
:type value: int
"""
if value and (not self._scan_lock.locked()):
# start scanning if not already scanning
self._scan_lock.acquire()
# parameters of the _scan function
params = (
self.wh,
self._scan_net_msg,
self._scan_interval,
self._scan_lock
)
_thread.start_new_thread(self._scan, params)
self.logger.info('Scanning started')
elif (value is False) and self._scan_lock.locked():
# stop scanning if not already stopped
self._scan_lock.release()
self.logger.info('Scanning stoppped')
if isinstance(self._stop_scanning_timer, machine.Timer):
self._stop_scanning_timer.deinit()
def _stop_scanning_cb(self, tim: machine.Timer) -> None:
"""
Timer callback function to stop the WiFi scan thread.
:param tim: The timer calling this function.
:type tim: machine.Timer
"""
self.scanning = False
@property
def latest_scan(self) -> Union[List[dict], str]:
"""
Get lastest scanned networks.
Start scanning if not already scanning, set a oneshot timer to 10.5x
of @see scan_interval to stop scanning again in order to reduce CPU
load and avoid unused scans
:returns: Dictionary of available networks
:rtype: Union[List[dict], str]
"""
if not self.scanning:
self.scanning = True
if self._stop_scanning_timer is None:
self._stop_scanning_timer = machine.Timer(-1)
cb_period = int(self.scan_interval * 10 + self.scan_interval / 2)
self._stop_scanning_timer.init(mode=machine.Timer.ONE_SHOT,
period=cb_period,
callback=self._stop_scanning_cb)
return self._scan_net_msg.value()
def _render_index_page(self, available_pages: dict) -> str:
"""
Render HTML list of available pages
:param available_pages: All available pages
:type available_pages: dict
:returns: Sub content of landing page
:rtype: str
"""
content = ""
# sort dict by 'title' of the dict values
for url, info in sorted(available_pages.items(),
key=lambda item: item[1]['title']):
content += """
<div class="col-sm-4 py-2"><div class="card h-100 {color}"><div class="card-body">
<h3 class="card-title">{title}</h3><p class="card-text">{text}</p>
<a href="{url}" class="stretched-link"></a></div></div></div>
""".format(color=info['color'], # noqa: E501
title=info['title'],
text=info['text'],
url=url)
return content
def _render_network_inputs(self,
available_nets: dict,
selected_bssid: str = '') -> str:
"""
Render HTML list of selectable networks
:param available_nets: All available nets
:type available_nets: dict
:param selected_bssid: Currently selected network on the webpage
:type selected_bssid: str
:returns: Sub content of WiFi selection page
:rtype: str
"""
content = ""
if len(available_nets):
for ele in available_nets:
selected = ''
if ele['bssid'].decode('ascii') == selected_bssid:
selected = "checked"
content += """
<input class="list-group-item-check" type="radio" name="bssid" id="{bssid}" value="{bssid}" onclick="remember_selected_element(this)" {state}>
<label class="list-group-item py-3" for="{bssid}">
{ssid}
<span class="d-block small opacity-50">
Signal quality {quality}%, BSSID {bssid}
</span>
</label>
""".format(bssid=ele['bssid'], # noqa: E501
state=selected,
ssid=ele['ssid'],
quality=ele['quality'])
else:
# as long as no networks are available show a spinner
content = """
<div class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>
"""
return content
def _save_wifi_config(self, form_data: dict) -> None:
"""
Save a new WiFi configuration to the WiFi configuration file.
:param form_data: The form data
:type form_data: dict
"""
network_cfg = dict()
available_nets = self.latest_scan
self.logger.info('Available nets: {}'.format(available_nets))
# [
# {
# 'ssid': 'TP-LINK_FBFC3C',
# 'RSSI': -21,
# 'bssid': 'a0f3c1fbfc3c',
# 'authmode': 'WPA/WPA2-PSK',
# 'quality': 9,
# 'channel': 1,
# 'hidden': False
# },
# {
# 'ssid': 'FRITZ!Box 7490',
# 'RSSI': -17,
# 'bssid': '3810d517eb39',
# 'authmode': 'WPA2-PSK',
# 'quality': 27,
# 'channel': 11,
# 'hidden': False
# }
# ]
# find SSID of network based on given bssid value
if form_data['ssid'] != '':
network_cfg['ssid'] = form_data['ssid']
else:
if 'bssid' not in form_data:
return
# selected_bssid = form_data['wifi_network']
selected_bssid = form_data['bssid']
for ele in available_nets:
if (isinstance(selected_bssid, str) and
selected_bssid.startswith("b'")):
# actually a bytes element used as string
# this is a bug due to the XMLHttpRequest updated list as
# JSON which does not handle bytes format
this_bssid = str(ele['bssid'])
else:
this_bssid = ele['bssid'].decode('ascii')
if this_bssid == selected_bssid:
# use string, json loading will fail otherwise later
network_cfg['ssid'] = ele['ssid'].decode('ascii')
break
network_cfg['password'] = form_data['password']
self.logger.info('Network cfg: {}'.format(network_cfg))
# Network cfg: {'ssid': 'TP-LINK_FBFC3C', 'password': 'qwertz'}
if 'ssid' in network_cfg:
if isinstance(network_cfg['ssid'], bytes):
network_cfg['ssid'] = network_cfg['ssid'].decode('ascii')
self.logger.info('Raw network config: {}'.format(network_cfg))
# save data in encrypted mode
self.extend_wifi_config_data(data=network_cfg,
path=self._config_file,
encrypted=True)
self.logger.info('Saving of network config to {} done'.
format(self._config_file))
else:
self.logger.info('No valid SSID found, will not save this net')
def _remove_wifi_config(self, form_data: dict) -> None:
"""
Remove a WiFi network from the WiFi configuration file.
:param form_data: The form data
:type form_data: dict
"""
if len(form_data):
loaded_cfg = self._load_wifi_config_data(path=self._config_file,
encrypted=True)
updated_cfg = list()
updated_ssids = list()
for net in loaded_cfg:
if net['ssid'] not in form_data:
updated_cfg.append(net)
updated_ssids.append(net['ssid'])
self._configured_networks = updated_ssids.copy()
# create bytes array of the dict and encrypt it
encrypted_data = self._encrypt_data(data=updated_cfg)
# save to file as binary
GenericHelper.save_file(data=encrypted_data,
path=self._config_file,
mode='wb')
self.logger.debug('Saved encrypted data as json: {}'.
format(encrypted_data))
# -------------------------------------------------------------------------
# Webserver functions
# @app.route('/')
async def landing_page(self, req: Request) -> None:
"""
Provide landing page aka index page
List of available subpages is rendered from all elements of the
@see available_urls property
"""
available_pages = self.available_urls
content = self._render_index_page(available_pages)
return render_template(template='index.tpl', req=None, content=content)
# @app.route('/scan_result')
async def scan_result(self, req: Request) -> None:
"""Provide latest found networks as JSON"""
# https://microdot.readthedocs.io/en/latest/intro.html#json-responses
return self.latest_scan
# @app.route('/select')
async def wifi_selection(self, req: Request) -> None:
"""
Provide webpage to select WiFi network from list of available networks
Scanning just in time of accessing the page would block all processes
for approx. 2.5sec.
Using the result provided by the scan thread via a message takes only
0.02sec to complete
"""
available_nets = self.latest_scan
content = self._render_network_inputs(
available_nets=available_nets
)
# do not stop scanning as page is updating scan results on the fly
# with XMLHTTP requests to @see scan_result
# stop scanning thread
# self.logger.info('Stopping scanning thread')
# self.scanning = False
return render_template(template='select.tpl', req=0, content=content)
# @app.route('/render_network_inputs')
async def render_network_inputs(self, req: Request) -> str:
"""Return rendered network inputs content to webpage"""
available_nets = self.latest_scan
selected_bssid = self._selected_network_bssid
content = self._render_network_inputs(
available_nets=available_nets,
selected_bssid=selected_bssid
)
return content
# @app.route('/configure')
async def wifi_configs(self, req: Request) -> None:
"""Provide webpage with table of configured networks"""
configured_nets = self.configured_networks
self.logger.debug('Existing config content: {}'.
format(configured_nets))
if isinstance(configured_nets, str):
configured_nets = [configured_nets]
return render_template(template='remove.tpl',
req=None,
wifi_nets=configured_nets,
button_mode='disabled')
# @app.route('/save_wifi_config')
async def save_wifi_config(self, req: Request) -> None:
"""Process saving the specified WiFi network to the WiFi config file"""
form_data = req.form
# Whether form data comes from GET or POST request, once parsed,
# it's available as req.form dictionary
self.logger.info('WiFi user input content: {}'.format(form_data))
# {'ssid': '', 'wifi_network': 'a0f3c1fbfc3c', 'password': 'qwertz'}
self._save_wifi_config(form_data=form_data)
# empty response to avoid any redirects or errors due to none response
return None, 204, {'Content-Type': 'application/json; charset=UTF-8'}
# @app.route('/remove_wifi_config')
async def remove_wifi_config(self, req: Request) -> None:
"""Remove a network from the list of configured networks"""
form_data = req.form
self.logger.info('Remove networks: {}'.format(form_data))
# Remove networks: {'FRITZ!Box 7490': 'FRITZ!Box 7490'}
self._remove_wifi_config(form_data=form_data)
# redirect to '/configure'
return redirect('/configure')
# @app.route('/static/<path:path>')
async def serve_static(self,
req: Request,
path: str) -> Union[str,
Tuple[str, int],
Tuple[str, int, dict]]:
if '..' in path:
# directory traversal is not allowed
return 'Not found', 404
response_header = {
'Cache-Control': 'max-age=86400',
}
ext = path.split('.')[-1]
complete_file_path = '/lib/' + 'static/' + ext + '/' + path
response_header['Content-Type'] = Response.types_map.get(
ext, 'application/octet-stream')
if 'gzip' in req.headers.get('Accept-Encoding', ''):
self.logger.debug('gzip accepted for {} file'.format(ext))
complete_file_path += '.gz'
response_header['Content-Encoding'] = 'gzip'
self.logger.debug('Send file {}'.format(complete_file_path))
self.logger.debug('Response header {}'.format(response_header))
f = open(complete_file_path, 'rb')
return f, 200, response_header
# @app.route('/favicon.ico')
async def serve_favicon(self, req: Request) -> None:
# return None, 204, {'Content-Type': 'application/json; charset=UTF-8'}
return send_file(filename='/lib/static/favicon.ico',
status_code=200,
content_type='image/x-icon')
# @app.route('/shutdown')
async def shutdown(self, req: Request) -> None:
"""Shutdown webserver"""
req.app.shutdown()
return 'The server is shutting down...'
async def not_found(self, req: Request) -> None:
return {'error': 'resource not found'}, 404
def run(self,
host: str = '0.0.0.0',
port: int = 80,
debug: bool = False) -> None:
"""
Run the web application
:param host: The hostname to listen on
:type host: str, optional
:param port: The port of the webserver
:type port: int, optional
:param debug: Flag to automatically reload for code changes and
show debugger content
:type debug: bool, optional
"""
self.logger.debug('Run app on {}:{} with debug: {}'.format(host,
port,
debug))
try:
# self.app.run()
# self.app.run(debug=debug)
self.app.run(host=host, port=port, debug=debug)
except KeyboardInterrupt:
self.logger.debug('Catched KeyboardInterrupt at run of web app')
except Exception as e:
self.logger.warning(e)