From bd4ad4ad54d955dd151e0383fbb2192ada3ad88e Mon Sep 17 00:00:00 2001 From: simon <652327+volt-sec@users.noreply.github.com> Date: Sun, 17 Dec 2023 21:38:33 +0100 Subject: [PATCH 1/3] extend gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 9f11b75..2483976 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .idea/ +__pycache__/ From 101a5b6526203d28fdc9ce430311a45b5603d176 Mon Sep 17 00:00:00 2001 From: simon <652327+volt-sec@users.noreply.github.com> Date: Sun, 17 Dec 2023 21:39:20 +0100 Subject: [PATCH 2/3] add wifi manager utils and fix encoding of ssid and password --- wifimgr.py | 34 ++++++++++++++++++---------------- wifimgr_utils.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 wifimgr_utils.py diff --git a/wifimgr.py b/wifimgr.py index 650e88d..3ad1f9b 100644 --- a/wifimgr.py +++ b/wifimgr.py @@ -1,11 +1,12 @@ import network import socket -import ure import time +import ure +import wifimgr_utils -ap_ssid = "WifiManager" -ap_password = "tayfunulu" -ap_authmode = 3 # WPA2 +AP_SSID = "wifimanager" +AP_PASSWORD = "wifimanager" +AP_AUTHMODE = 3 # WPA2 NETWORK_PROFILES = 'wifi.dat' @@ -14,7 +15,6 @@ server_socket = None - def get_connection(): """return a working WLAN(STA_IF) instance or None""" @@ -22,12 +22,13 @@ def get_connection(): if wlan_sta.isconnected(): return wlan_sta - connected = False + # ESP connecting to WiFi takes time, wait a bit and try again: + time.sleep(3) + if wlan_sta.isconnected(): + return wlan_sta + try: - # ESP connecting to WiFi takes time, wait a bit and try again: - time.sleep(3) - if wlan_sta.isconnected(): - return wlan_sta + connected = False # Read known network profiles from file profiles = read_profiles() @@ -187,11 +188,12 @@ def handle_configure(client, request): return False # version 1.9 compatibility try: - ssid = match.group(1).decode("utf-8").replace("%3F", "?").replace("%21", "!") - password = match.group(2).decode("utf-8").replace("%3F", "?").replace("%21", "!") + ssid = wifimgr_utils.unquote(match.group(1).decode("utf-8")) + password = wifimgr_utils.unquote(match.group(2).decode("utf-8")) except Exception: - ssid = match.group(1).replace("%3F", "?").replace("%21", "!") - password = match.group(2).replace("%3F", "?").replace("%21", "!") + # Not both groups of ssid and password captured + send_response(client, "SSID and/or password not provided", status_code=400) + return False if len(ssid) == 0: send_response(client, "SSID must be provided", status_code=400) @@ -266,13 +268,13 @@ def start(port=80): wlan_sta.active(True) wlan_ap.active(True) - wlan_ap.config(essid=ap_ssid, password=ap_password, authmode=ap_authmode) + wlan_ap.config(essid=AP_SSID, password=AP_PASSWORD, authmode=AP_AUTHMODE) server_socket = socket.socket() server_socket.bind(addr) server_socket.listen(1) - print('Connect to WiFi ssid ' + ap_ssid + ', default password: ' + ap_password) + print('Connect to WiFi ssid ' + AP_SSID + ', default password: ' + AP_PASSWORD) print('and access the ESP via your favorite web browser at 192.168.4.1.') print('Listening on:', addr) diff --git a/wifimgr_utils.py b/wifimgr_utils.py new file mode 100644 index 0000000..795909d --- /dev/null +++ b/wifimgr_utils.py @@ -0,0 +1,43 @@ +_hexdig = '0123456789ABCDEFabcdef' +_hextobyte = None + +def unquote(string): + """ + @brief Remove URL encoding from a string, i.e. + unquote('abc%20def') -> 'abc def', see + urllib.parse.unquote_to_bytes() + @param string String that might contain URL/percent encoding + @return string with URL encoding replaced by original characters + """ + global _hextobyte + + # Note: strings are encoded as UTF-8. This is only an issue if it contains + # unescaped non-ASCII characters, which URIs should not. + if not string: + return "" + + if isinstance(string, str): + string = string.encode('utf-8') + + bits = string.split(b'%') + if len(bits) == 1: + return string.decode('utf-8') + + res = [bits[0]] + append = res.append + + # Delay the initialization of the table to not waste memory + # if the function is never called + if _hextobyte is None: + _hextobyte = {(a + b).encode(): bytes([int(a + b, 16)]) + for a in _hexdig for b in _hexdig} + + for item in bits[1:]: + try: + append(_hextobyte[item[:2]]) + append(item[2:]) + except KeyError: + append(b'%') + append(item) + + return (b''.join(res)).decode('utf-8') From 06578afda77d4c43089f76c889e0976dd41f59d9 Mon Sep 17 00:00:00 2001 From: simon <652327+volt-sec@users.noreply.github.com> Date: Sun, 17 Dec 2023 21:53:08 +0100 Subject: [PATCH 3/3] revert gitignore, SSID and default password --- .gitignore | 1 - wifimgr.py | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 2483976..9f11b75 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ .idea/ -__pycache__/ diff --git a/wifimgr.py b/wifimgr.py index 3ad1f9b..b0982c9 100644 --- a/wifimgr.py +++ b/wifimgr.py @@ -4,8 +4,8 @@ import ure import wifimgr_utils -AP_SSID = "wifimanager" -AP_PASSWORD = "wifimanager" +AP_SSID = "WifiManager" +AP_PASSWORD = "tayfunulu" AP_AUTHMODE = 3 # WPA2 NETWORK_PROFILES = 'wifi.dat'