2016-04-24 16:04:21 -04:00
|
|
|
# This module should be imported from REPL, not run from command line.
|
2022-07-19 22:09:24 -04:00
|
|
|
import binascii
|
|
|
|
import hashlib
|
2016-04-25 17:59:21 -04:00
|
|
|
import network
|
2022-07-19 22:09:24 -04:00
|
|
|
import os
|
|
|
|
import socket
|
|
|
|
import sys
|
|
|
|
import websocket
|
2016-04-30 13:39:35 -04:00
|
|
|
import _webrepl
|
2016-04-24 16:04:21 -04:00
|
|
|
|
|
|
|
listen_s = None
|
|
|
|
client_s = None
|
|
|
|
|
2022-07-19 22:09:24 -04:00
|
|
|
DEBUG = 0
|
|
|
|
|
|
|
|
_DEFAULT_STATIC_HOST = const("https://micropython.org/webrepl/")
|
|
|
|
static_host = _DEFAULT_STATIC_HOST
|
|
|
|
|
|
|
|
|
|
|
|
def server_handshake(cl):
|
|
|
|
req = cl.makefile("rwb", 0)
|
|
|
|
# Skip HTTP GET line.
|
|
|
|
l = req.readline()
|
|
|
|
if DEBUG:
|
|
|
|
sys.stdout.write(repr(l))
|
|
|
|
|
|
|
|
webkey = None
|
|
|
|
upgrade = False
|
|
|
|
websocket = False
|
|
|
|
|
|
|
|
while True:
|
|
|
|
l = req.readline()
|
|
|
|
if not l:
|
|
|
|
# EOF in headers.
|
|
|
|
return False
|
|
|
|
if l == b"\r\n":
|
|
|
|
break
|
|
|
|
if DEBUG:
|
|
|
|
sys.stdout.write(l)
|
|
|
|
h, v = [x.strip() for x in l.split(b":", 1)]
|
|
|
|
if DEBUG:
|
|
|
|
print((h, v))
|
|
|
|
if h == b"Sec-WebSocket-Key":
|
|
|
|
webkey = v
|
|
|
|
elif h == b"Connection" and b"Upgrade" in v:
|
|
|
|
upgrade = True
|
|
|
|
elif h == b"Upgrade" and v == b"websocket":
|
|
|
|
websocket = True
|
|
|
|
|
|
|
|
if not (upgrade and websocket and webkey):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if DEBUG:
|
|
|
|
print("Sec-WebSocket-Key:", webkey, len(webkey))
|
|
|
|
|
|
|
|
d = hashlib.sha1(webkey)
|
|
|
|
d.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
|
|
|
|
respkey = d.digest()
|
|
|
|
respkey = binascii.b2a_base64(respkey)[:-1]
|
|
|
|
if DEBUG:
|
|
|
|
print("respkey:", respkey)
|
|
|
|
|
|
|
|
cl.send(
|
|
|
|
b"""\
|
|
|
|
HTTP/1.1 101 Switching Protocols\r
|
|
|
|
Upgrade: websocket\r
|
|
|
|
Connection: Upgrade\r
|
|
|
|
Sec-WebSocket-Accept: """
|
|
|
|
)
|
|
|
|
cl.send(respkey)
|
|
|
|
cl.send("\r\n\r\n")
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def send_html(cl):
|
|
|
|
cl.send(
|
|
|
|
b"""\
|
|
|
|
HTTP/1.0 200 OK\r
|
|
|
|
\r
|
|
|
|
<base href=\""""
|
|
|
|
)
|
|
|
|
cl.send(static_host)
|
|
|
|
cl.send(
|
|
|
|
b"""\"></base>\r
|
|
|
|
<script src="webrepl_content.js"></script>\r
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
cl.close()
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2016-04-30 13:41:09 -04:00
|
|
|
def setup_conn(port, accept_handler):
|
2016-05-08 12:59:22 -04:00
|
|
|
global listen_s
|
2016-04-24 16:04:21 -04:00
|
|
|
listen_s = socket.socket()
|
|
|
|
listen_s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
|
2016-04-25 11:44:37 -04:00
|
|
|
ai = socket.getaddrinfo("0.0.0.0", port)
|
2016-04-24 16:04:21 -04:00
|
|
|
addr = ai[0][4]
|
|
|
|
|
|
|
|
listen_s.bind(addr)
|
|
|
|
listen_s.listen(1)
|
2016-05-08 12:59:22 -04:00
|
|
|
if accept_handler:
|
|
|
|
listen_s.setsockopt(socket.SOL_SOCKET, 20, accept_handler)
|
2016-04-25 17:59:21 -04:00
|
|
|
for i in (network.AP_IF, network.STA_IF):
|
|
|
|
iface = network.WLAN(i)
|
|
|
|
if iface.active():
|
2022-07-19 22:09:24 -04:00
|
|
|
print("WebREPL server started on http://%s:%d/" % (iface.ifconfig()[0], port))
|
2016-05-08 12:59:22 -04:00
|
|
|
return listen_s
|
2016-04-24 16:04:21 -04:00
|
|
|
|
|
|
|
|
2016-04-24 17:31:43 -04:00
|
|
|
def accept_conn(listen_sock):
|
|
|
|
global client_s
|
|
|
|
cl, remote_addr = listen_sock.accept()
|
2022-07-19 22:09:24 -04:00
|
|
|
|
|
|
|
if not server_handshake(cl):
|
|
|
|
send_html(cl)
|
|
|
|
return False
|
|
|
|
|
|
|
|
prev = os.dupterm(None)
|
|
|
|
os.dupterm(prev)
|
2017-10-13 05:01:57 -04:00
|
|
|
if prev:
|
2016-10-20 09:49:45 -04:00
|
|
|
print("\nConcurrent WebREPL connection from", remote_addr, "rejected")
|
|
|
|
cl.close()
|
2022-07-19 22:09:24 -04:00
|
|
|
return False
|
2016-04-25 18:00:28 -04:00
|
|
|
print("\nWebREPL connection from:", remote_addr)
|
2016-04-24 17:31:43 -04:00
|
|
|
client_s = cl
|
2022-07-19 22:09:24 -04:00
|
|
|
|
|
|
|
ws = websocket.websocket(cl, True)
|
2016-04-30 13:39:35 -04:00
|
|
|
ws = _webrepl._webrepl(ws)
|
2016-04-24 16:04:21 -04:00
|
|
|
cl.setblocking(False)
|
2019-10-21 00:55:18 -04:00
|
|
|
# notify REPL on socket incoming data (ESP32/ESP8266-only)
|
2022-07-19 22:09:24 -04:00
|
|
|
if hasattr(os, "dupterm_notify"):
|
|
|
|
cl.setsockopt(socket.SOL_SOCKET, 20, os.dupterm_notify)
|
|
|
|
os.dupterm(ws)
|
|
|
|
|
|
|
|
return True
|
2016-04-24 17:31:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
def stop():
|
|
|
|
global listen_s, client_s
|
2022-07-19 22:09:24 -04:00
|
|
|
os.dupterm(None)
|
2016-04-24 17:31:43 -04:00
|
|
|
if client_s:
|
|
|
|
client_s.close()
|
|
|
|
if listen_s:
|
|
|
|
listen_s.close()
|
|
|
|
|
|
|
|
|
2022-05-14 09:00:20 -04:00
|
|
|
def start(port=8266, password=None, accept_handler=accept_conn):
|
2022-07-19 22:09:24 -04:00
|
|
|
global static_host
|
2016-04-24 17:31:43 -04:00
|
|
|
stop()
|
2022-05-14 09:00:20 -04:00
|
|
|
webrepl_pass = password
|
|
|
|
if webrepl_pass is None:
|
2016-05-07 14:59:02 -04:00
|
|
|
try:
|
2016-11-06 02:01:48 -05:00
|
|
|
import webrepl_cfg
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2022-05-14 09:00:20 -04:00
|
|
|
webrepl_pass = webrepl_cfg.PASS
|
2022-07-19 22:09:24 -04:00
|
|
|
if hasattr(webrepl_cfg, "BASE"):
|
|
|
|
static_host = webrepl_cfg.BASE
|
2016-05-07 14:59:02 -04:00
|
|
|
except:
|
2016-11-06 02:01:48 -05:00
|
|
|
print("WebREPL is not configured, run 'import webrepl_setup'")
|
2022-05-14 09:00:20 -04:00
|
|
|
|
|
|
|
_webrepl.password(webrepl_pass)
|
|
|
|
s = setup_conn(port, accept_handler)
|
|
|
|
|
|
|
|
if accept_handler is None:
|
|
|
|
print("Starting webrepl in foreground mode")
|
2022-07-19 22:09:24 -04:00
|
|
|
# Run accept_conn to serve HTML until we get a websocket connection.
|
|
|
|
while not accept_conn(s):
|
|
|
|
pass
|
2022-05-14 09:00:20 -04:00
|
|
|
elif password is None:
|
|
|
|
print("Started webrepl in normal mode")
|
2016-05-07 14:59:02 -04:00
|
|
|
else:
|
2016-11-06 02:01:48 -05:00
|
|
|
print("Started webrepl in manual override mode")
|
2016-05-08 12:59:22 -04:00
|
|
|
|
|
|
|
|
2022-05-14 09:00:20 -04:00
|
|
|
def start_foreground(port=8266, password=None):
|
|
|
|
start(port, password, None)
|