Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix/volt-sec/ssid-password-encoding #38

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions wifimgr.py
Original file line number Diff line number Diff line change
@@ -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 = "tayfunulu"
AP_AUTHMODE = 3 # WPA2

NETWORK_PROFILES = 'wifi.dat'

Expand All @@ -14,20 +15,20 @@

server_socket = None


def get_connection():
"""return a working WLAN(STA_IF) instance or None"""

# First check if there already is any 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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
43 changes: 43 additions & 0 deletions wifimgr_utils.py
Original file line number Diff line number Diff line change
@@ -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')