8000 webrepl: allow specifying an SSLContext · micropython/micropython-lib@9fafda2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9fafda2

Browse files
committed
webrepl: allow specifying an SSLContext
Signed-off-by: Felix Dörre <felix@dogcraft.de>
1 parent 626c2cc commit 9fafda2

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

micropython/net/webrepl/webrepl.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ def ioctl(self, kind, arg):
7070
def close(self):
7171
self.sock.close()
7272

73-
74-
def server_handshake(cl):
75-
req = cl.makefile("rwb", 0)
73+
def server_handshake(req):
7674
# Skip HTTP GET line.
7775
l = req.readline()
7876
if DEBUG:
@@ -114,28 +112,28 @@ def server_handshake(cl):
114112
if DEBUG:
115113
print("respkey:", respkey)
116114

117-
cl.send(
115+
req.write(
118116
b"""\
119117
HTTP/1.1 101 Switching Protocols\r
120118
Upgrade: websocket\r
121119
Connection: Upgrade\r
122120
Sec-WebSocket-Accept: """
123121
)
124-
cl.send(respkey)
125-
cl.send("\r\n\r\n")
122+
req.write(respkey)
123+
req.write("\r\n\r\n")
126124

127125
return True
128126

129127

130128
def send_html(cl):
131-
cl.send(
129+
cl.write(
132130
b"""\
133131
HTTP/1.0 200 OK\r
134132
\r
135133
<base href=\""""
136134
)
137-
cl.send(static_host)
138-
cl.send(
135+
cl.write(static_host)
136+
cl.write(
139137
b"""\"></base>\r
140138
<script src="webreplv2_content.js"></script>\r
141139
"""
@@ -160,11 +158,16 @@ def setup_conn(port, accept_handler):
160158

161159

162160
def accept_conn(listen_sock):
163-
global client_s
161+
global client_s, webrepl_ssl_context
164162
cl, remote_addr = listen_sock.accept()
163+
req = cl.makefile("rwb", 0)
164+
sock = cl
165+
if webrepl_ssl_context is not None:
166+
sock = webrepl_ssl_context.wrap_socket(sock)
167+
req = sock
165168

166-
if not server_handshake(cl):
167-
send_html(cl)
169+
if not server_handshake(req):
170+
send_html(sock)
168171
return False
169172

170173
prev = os.dupterm(None)
@@ -176,13 +179,13 @@ def accept_conn(listen_sock):
176179
print("\nWebREPL connection from:", remote_addr)
177180
client_s = cl
178181

179-
ws = websocket.websocket(cl, True)
180-
ws = WebreplWrapper(ws)
182+
sock = websocket.websocket(sock)
183+
sock = WebreplWrapper(sock)
181184
cl.setblocking(False)
182185
# notify REPL on socket incoming data (ESP32/ESP8266-only)
183186
if hasattr(os, "dupterm_notify"):
184187
cl.setsockopt(socket.SOL_SOCKET, 20, os.dupterm_notify)
185-
os.dupterm(ws)
188+
os.dupterm(sock)
186189

187190
return True
188191

@@ -196,9 +199,10 @@ def stop():
196199
listen_s.close()
197200

198201

199-
def start(port=8266, password=None, accept_handler=accept_conn):
200-
global static_host, webrepl_pass
202+
def start(port=8266, password=None, ssl_context = None, accept_handler=accept_conn):
203+
global static_host, webrepl_pass, webrepl_ssl_context
201204
stop()
205+
webrepl_ssl_context = ssl_context
202206
webrepl_pass = password
203207
if password is None:
204208
try:

0 commit comments

Comments
 (0)
0