2016-04-24 21:04:21 +01:00
|
|
|
# This module should be imported from REPL, not run from command line.
|
|
|
|
import socket
|
|
|
|
import uos
|
2016-04-25 22:59:21 +01:00
|
|
|
import network
|
2019-02-10 20:35:18 +00:00
|
|
|
import uwebsocket
|
2016-04-24 21:04:21 +01:00
|
|
|
import websocket_helper
|
2016-04-30 18:39:35 +01:00
|
|
|
import _webrepl
|
2016-04-24 21:04:21 +01:00
|
|
|
|
|
|
|
listen_s = None
|
|
|
|
client_s = None
|
|
|
|
|
2020-02-27 04:36:53 +00:00
|
|
|
|
2016-04-30 18:41:09 +01:00
|
|
|
def setup_conn(port, accept_handler):
|
2016-05-08 17:59:22 +01:00
|
|
|
global listen_s
|
2016-04-24 21:04:21 +01:00
|
|
|
listen_s = socket.socket()
|
|
|
|
listen_s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
|
2016-04-25 16:44:37 +01:00
|
|
|
ai = socket.getaddrinfo("0.0.0.0", port)
|
2016-04-24 21:04:21 +01:00
|
|
|
addr = ai[0][4]
|
|
|
|
|
|
|
|
listen_s.bind(addr)
|
|
|
|
listen_s.listen(1)
|
2016-05-08 17:59:22 +01:00
|
|
|
if accept_handler:
|
|
|
|
listen_s.setsockopt(socket.SOL_SOCKET, 20, accept_handler)
|
2016-04-25 22:59:21 +01:00
|
|
|
for i in (network.AP_IF, network.STA_IF):
|
|
|
|
iface = network.WLAN(i)
|
|
|
|
if iface.active():
|
2016-04-26 10:47:24 +01:00
|
|
|
print("WebREPL daemon started on ws://%s:%d" % (iface.ifconfig()[0], port))
|
2016-05-08 17:59:22 +01:00
|
|
|
return listen_s
|
2016-04-24 21:04:21 +01:00
|
|
|
|
|
|
|
|
2016-04-24 22:31:43 +01:00
|
|
|
def accept_conn(listen_sock):
|
|
|
|
global client_s
|
|
|
|
cl, remote_addr = listen_sock.accept()
|
2017-10-13 10:01:57 +01:00
|
|
|
prev = uos.dupterm(None)
|
|
|
|
uos.dupterm(prev)
|
|
|
|
if prev:
|
2016-10-20 14:49:45 +01:00
|
|
|
print("\nConcurrent WebREPL connection from", remote_addr, "rejected")
|
|
|
|
cl.close()
|
|
|
|
return
|
2016-04-25 23:00:28 +01:00
|
|
|
print("\nWebREPL connection from:", remote_addr)
|
2016-04-24 22:31:43 +01:00
|
|
|
client_s = cl
|
2016-04-24 21:04:21 +01:00
|
|
|
websocket_helper.server_handshake(cl)
|
2019-02-10 20:35:18 +00:00
|
|
|
ws = uwebsocket.websocket(cl, True)
|
2016-04-30 18:39:35 +01:00
|
|
|
ws = _webrepl._webrepl(ws)
|
2016-04-24 21:04:21 +01:00
|
|
|
cl.setblocking(False)
|
2019-10-21 05:55:18 +01:00
|
|
|
# notify REPL on socket incoming data (ESP32/ESP8266-only)
|
|
|
|
if hasattr(uos, "dupterm_notify"):
|
|
|
|
cl.setsockopt(socket.SOL_SOCKET, 20, uos.dupterm_notify)
|
2016-04-24 21:04:21 +01:00
|
|
|
uos.dupterm(ws)
|
2016-04-24 22:31:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
def stop():
|
|
|
|
global listen_s, client_s
|
|
|
|
uos.dupterm(None)
|
|
|
|
if client_s:
|
|
|
|
client_s.close()
|
|
|
|
if listen_s:
|
|
|
|
listen_s.close()
|
|
|
|
|
|
|
|
|
2016-05-07 19:59:02 +01:00
|
|
|
def start(port=8266, password=None):
|
2016-04-24 22:31:43 +01:00
|
|
|
stop()
|
2016-05-07 19:59:02 +01:00
|
|
|
if password is None:
|
|
|
|
try:
|
2016-11-06 07:01:48 +00:00
|
|
|
import webrepl_cfg
|
2020-02-27 04:36:53 +00:00
|
|
|
|
2016-11-06 07:01:48 +00:00
|
|
|
_webrepl.password(webrepl_cfg.PASS)
|
2016-05-07 19:59:02 +01:00
|
|
|
setup_conn(port, accept_conn)
|
|
|
|
print("Started webrepl in normal mode")
|
|
|
|
except:
|
2016-11-06 07:01:48 +00:00
|
|
|
print("WebREPL is not configured, run 'import webrepl_setup'")
|
2016-05-07 19:59:02 +01:00
|
|
|
else:
|
|
|
|
_webrepl.password(password)
|
2016-04-30 18:41:09 +01:00
|
|
|
setup_conn(port, accept_conn)
|
2016-11-06 07:01:48 +00:00
|
|
|
print("Started webrepl in manual override mode")
|
2016-05-08 17:59:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
def start_foreground(port=8266):
|
|
|
|
stop()
|
|
|
|
s = setup_conn(port, None)
|
|
|
|
accept_conn(s)
|