Skip to content

Commit 5aab2d1

Browse files
committed
webrepl: Changes for more webrepl features while making it smaller.
This change: - Moves the password checking to python - Removes the special file transfer protocol - Moves the REPL data to websocket binary packages Signed-off-by: Felix Dörre <felix@dogcraft.de>
1 parent ffb07db commit 5aab2d1

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

micropython/net/webrepl/manifest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
metadata(description="WebREPL server.", version="0.1.0")
1+
metadata(description="WebREPL server.", version="1.0.0")
22

33
module("webrepl.py", opt=3)
44
module("webrepl_setup.py", opt=3)

micropython/net/webrepl/webrepl.py

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,72 @@
77
import socket
88
import sys
99
import websocket
10-
import _webrepl
10+
import io
1111

1212
listen_s = None
1313
client_s = None
1414

1515
DEBUG = 0
1616

17-
_DEFAULT_STATIC_HOST = const("https://micropython.org/webrepl/")
17+
_DEFAULT_STATIC_HOST = const("https://felix.dogcraft.de/webrepl/")
18+
_WELCOME_PROMPT = const("\r\nWebREPL connected\r\n>>> ")
1819
static_host = _DEFAULT_STATIC_HOST
20+
webrepl_pass = None
21+
22+
23+
class WebreplWrapper(io.IOBase):
24+
def __init__(self, sock):
25+
self.sock = sock
26+
self.sock.ioctl(9, 2)
27+
if webrepl_pass is not None:
28+
self.pw = bytearray(16)
29+
self.pwPos = 0
30+
self.sock.write("Password: ")
31+
else:
32+
self.pw = None
33+
self.sock.write(_WELCOME_PROMPT)
34+
35+
def readinto(self, buf):
36+
if self.pw is not None:
37+
buf = bytearray(1)
38+
while True:
39+
l = self.sock.readinto(buf)
40+
if l is None:
41+
continue
42+
if l <= 0:
43+
return l
44+
if buf[0] == 10 or buf[0] == 13:
45+
print("Authenticating with:")
46+
print(self.pw[0 : self.pwPos])
47+
if bytes(self.pw[0 : self.pwPos]) == webrepl_pass:
48+
self.pw = None
49+
del self.pwPos
50+
self.sock.write(_WELCOME_PROMPT)
51+
break
52+
else:
53+
print(bytes(self.pw[0 : self.pwPos]))
54+
print(webrepl_pass)
55+
self.sock.write("\r\nAccess denied\r\n")
56+
return 0
57+
else:
58+
if self.pwPos < len(self.pw):
59+
self.pw[self.pwPos] = buf[0]
60+
self.pwPos = self.pwPos + 1
61+
return self.sock.readinto(buf)
62+
63+
def write(self, buf):
64+
if self.pw is not None:
65+
return len(buf)
66+
return self.sock.write(buf)
67+
68+
def ioctl(self, kind, arg):
69+
if kind == 4:
70+
self.sock.close()
71+
return 0
72+
return -1
73+
74+
def close(self):
75+
self.sock.close()
1976

2077

2178
def server_handshake(cl):
@@ -84,7 +141,7 @@ def send_html(cl):
84141
cl.send(static_host)
85142
cl.send(
86143
b"""\"></base>\r
87-
<script src="webrepl_content.js"></script>\r
144+
<script src="webreplv2_content.js"></script>\r
88145
"""
89146
)
90147
cl.close()
@@ -127,7 +184,7 @@ def accept_conn(listen_sock):
127184
client_s = cl
128185

129186
ws = websocket.websocket(cl, True)
130-
ws = _webrepl._webrepl(ws)
187+
ws = WebreplWrapper(ws)
131188
cl.setblocking(False)
132189
# notify REPL on socket incoming data (ESP32/ESP8266-only)
133190
if hasattr(os, "dupterm_notify"):
@@ -147,10 +204,10 @@ def stop():
147204

148205

149206
def start(port=8266, password=None, accept_handler=accept_conn):
150-
global static_host
207+
global static_host, webrepl_pass
151208
stop()
152209
webrepl_pass = password
153-
if webrepl_pass is None:
210+
if password is None:
154211
try:
155212
import webrepl_cfg
156213

@@ -160,7 +217,9 @@ def start(port=8266, password=None, accept_handler=accept_conn):
160217
except:
161218
print("WebREPL is not configured, run 'import webrepl_setup'")
162219

163-
_webrepl.password(webrepl_pass)
220+
if webrepl_pass is not None:
221+
webrepl_pass = webrepl_pass.encode()
222+
164223
s = setup_conn(port, accept_handler)
165224

166225
if accept_handler is None:

0 commit comments

Comments
 (0)