8000 webrepl: Allow specifying an SSLContext. · micropython/micropython-lib@a46c2fd · GitHub
[go: up one dir, main page]

Skip to content

Commit a46c2fd

Browse files
committed
webrepl: Allow specifying an SSLContext.
Signed-off-by: Felix Dörre <felix@dogcraft.de>
1 parent cd2a054 commit a46c2fd

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

micropython/net/webrepl/webrepl.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ def close(self):
7171
self.sock.close()
7272

7373

74-
def server_handshake(cl):
75-
req = cl.makefile("rwb", 0)
74+
def server_handshake(req):
7675
# Skip HTTP GET line.
7776
l = req.readline()
7877
if DEBUG:
@@ -114,28 +113,28 @@ def server_handshake(cl):
114113
if DEBUG:
115114
print("respkey:", respkey)
116115

117-
cl.send(
116+
req.write(
118117
b"""\
119118
HTTP/1.1 101 Switching Protocols\r
120119
Upgrade: websocket\r
121120
Connection: Upgrade\r
122121
Sec-WebSocket-Accept: """
123122
)
124-
cl.send(respkey)
125-
cl.send("\r\n\r\n")
123+
req.write(respkey)
124+
req.write("\r\n\r\n")
126125

127126
return True
128127

129128

130129
def send_html(cl):
131-
cl.send(
130+
cl.write(
132131
b"""\
133132
HTTP/1.0 200 OK\r
134133
\r
135134
<base href=\""""
136135
)
137-
cl.send(static_host)
138-
cl.send(
136+
cl.write(static_host)
137+
cl.write(
139138
b"""\"></base>\r
140139
<script src="webreplv2_content.js"></script>\r
141140
"""
@@ -160,11 +159,16 @@ def setup_conn(port, accept_handler):
160159

161160

162161
def accept_conn(listen_sock):
163-
global client_s
162+
global client_s, webrepl_ssl_context
164163
cl, remote_addr = listen_sock.accept()
164+
req = cl.makefile("rwb", 0)
165+
sock = cl
166+
if webrepl_ssl_context is not None:
167+
sock = webrepl_ssl_context.wrap_socket(sock)
168+
req = sock
165169

166-
if not server_handshake(cl):
167-
send_html(cl)
170+
if not server_handshake(req):
171+
send_html(sock)
168172
return False
169173

170174
prev = os.dupterm(None)
@@ -176,13 +180,13 @@ def accept_conn(listen_sock):
176180
print("\nWebREPL connection from:", remote_addr)
177181
client_s = cl
178182

179-
ws = websocket.websocket(cl, True)
180-
ws = WebreplWrapper(ws)
183+
sock = websocket.websocket(sock)
184+
sock = WebreplWrapper(sock)
181185
cl.setblocking(False)
182186
# notify REPL on socket incoming data (ESP32/ESP8266-only)
183187
if hasattr(os, "dupterm_notify"):
184188
cl.setsockopt(socket.SOL_SOCKET, 20, os.dupterm_notify)
185-
os.dupterm(ws)
189+
os.dupterm(sock)
186190

187191
return True
188192

@@ -196,9 +200,10 @@ def stop():
196200
listen_s.close()
197201

198202

199-
def start(port=8266, password=None, accept_handler=accept_conn):
200-
global static_host, webrepl_pass
203+
def start(port=8266, password=None, ssl_context=None, accept_handler=accept_conn):
204+
global static_host, webrepl_pass, webrepl_ssl_context
201205
stop()
206+
webrepl_ssl_context = ssl_context
202207
webrepl_pass = password
203208
if password is None:
204209
try:

0 commit comments

Comments
 (0)
155E
0