Skip to content

Commit 47e1338

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 47e1338

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
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: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,76 @@
11
# This module should be imported from REPL, not run from command line.
22
import binascii
33
import hashlib
4-
from micropython import const
54
import network
65
import os
76
import socket
87
import sys
98
import websocket
10-
import _webrepl
9+
import io
1110

1211
listen_s = None
1312
client_s = None
1413

1514
DEBUG = 0
1615

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

2175
def server_handshake(cl):
2276
req = cl.makefile("rwb", 0)
@@ -84,7 +138,7 @@ def send_html(cl):
84138
cl.send(static_host)
85139
cl.send(
86140
b"""\"></base>\r
87-
<script src="webrepl_content.js"></script>\r
141+
<script src="webreplv2_content.js"></script>\r
88142
"""
89143
)
90144
cl.close()
@@ -127,7 +181,7 @@ def accept_conn(listen_sock):
127181
client_s = cl
128182

129183
ws = websocket.websocket(cl, True)
130-
ws = _webrepl._webrepl(ws)
184+
ws = WebreplWrapper(ws)
131185
cl.setblocking(False)
132186
# notify REPL on socket incoming data (ESP32/ESP8266-only)
133187
if hasattr(os, "dupterm_notify"):
@@ -147,10 +201,10 @@ def stop():
147201

148202

149203
def start(port=8266, password=None, accept_handler=accept_conn):
150-
global static_host
204+
global static_host, webrepl_pass
151205
stop()
152206
webrepl_pass = password
153-
if webrepl_pass is None:
207+
if password is None:
154208
try:
155209
import webrepl_cfg
156210

@@ -160,7 +214,9 @@ def start(port=8266, password=None, accept_handler=accept_conn):
160214
except:
161215
print("WebREPL is not configured, run 'import webrepl_setup'")
162216

163-
_webrepl.password(webrepl_pass)
217+
if webrepl_pass is not None:
218+
webrepl_pass = webrepl_pass.encode()
219+
164220
s = setup_conn(port, accept_handler)
165221

166222
if accept_handler is None:

0 commit comments

Comments
 (0)