77import socket
88import sys
99import websocket
10- import _webrepl
10+ import io
1111
1212listen_s = None
1313client_s = None
1414
1515DEBUG = 0
1616
17- _DEFAULT_STATIC_HOST = const ("https://micropython.org/webrepl/" )
17+ _DEFAULT_STATIC_HOST = const ("https://felix.dogcraft.de/webrepl/" )
18+ _WELCOME_PROMPT = const ("\r \n WebREPL connected\r \n >>> " )
1819static_host = _DEFAULT_STATIC_HOST
20+ webrepl_pass = None
21+
22+
23+ class WebreplWrapper (io .IOBase ):
24+ def __init__ (self , sock ):
25+ self .sock = sock
26+ self .sock .ioctl (9 , 2 )
27+ if webrepl_pass is not None :
28+ self .pw = bytearray (16 )
29+ self .pwPos = 0
30+ self .sock .write ("Password: " )
31+ else :
32+ self .pw = None
33+ self .sock .write (_WELCOME_PROMPT )
34+
35+ def readinto (self , buf ):
36+ if self .pw is not None :
37+ buf = bytearray (1 )
38+ while True :
39+ l = self .sock .readinto (buf )
40+ if l is None :
41+ continue
42+ if l <= 0 :
43+ return l
44+ if buf [0 ] == 10 or buf [0 ] == 13 :
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+ self .sock .write ("\r \n Access denied\r \n " )
52+ return 0
53+ else :
54+ if self .pwPos < len (self .pw ):
55+ self .pw [self .pwPos ] = buf [0 ]
56+ self .pwPos = self .pwPos + 1
57+ return self .sock .readinto (buf )
58+
59+ def write (self , buf ):
60+ if self .pw is not None :
61+ return len (buf )
62+ return self .sock .write (buf )
63+
64+ def ioctl (self , kind , arg ):
65+ if kind == 4 :
66+ self .sock .close ()
67+ return 0
68+ return - 1
69+
70+ def close (self ):
71+ self .sock .close ()
1972
2073
2174def server_handshake (cl ):
@@ -84,7 +137,7 @@ def send_html(cl):
84137 cl .send (static_host )
85138 cl .send (
86139 b"""\" ></base>\r
87- <script src="webrepl_content .js"></script>\r
140+ <script src="webreplv2_content .js"></script>\r
88141"""
89142 )
90143 cl .close ()
@@ -95,10 +148,7 @@ def setup_conn(port, accept_handler):
95148 listen_s = socket .socket ()
96149 listen_s .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
97150
98- ai = socket .getaddrinfo ("0.0.0.0" , port )
99- addr = ai [0 ][4 ]
100-
101- listen_s .bind (addr )
151+ listen_s .bind (("" , port ))
102152 listen_s .listen (1 )
103153 if accept_handler :
104154 listen_s .setsockopt (socket .SOL_SOCKET , 20 , accept_handler )
@@ -127,7 +177,7 @@ def accept_conn(listen_sock):
127177 client_s = cl
128178
129179 ws = websocket .websocket (cl , True )
130- ws = _webrepl . _webrepl (ws )
180+ ws = WebreplWrapper (ws )
131181 cl .setblocking (False )
132182 # notify REPL on socket incoming data (ESP32/ESP8266-only)
133183 if hasattr (os , "dupterm_notify" ):
@@ -147,10 +197,10 @@ def stop():
147197
148198
149199def start (port = 8266 , password = None , accept_handler = accept_conn ):
150- global static_host
200+ global static_host , webrepl_pass
151201 stop ()
152202 webrepl_pass = password
153- if webrepl_pass is None :
203+ if password is None :
154204 try :
155205 import webrepl_cfg
156206
@@ -160,7 +210,9 @@ def start(port=8266, password=None, accept_handler=accept_conn):
160210 except :
161211 print ("WebREPL is not configured, run 'import webrepl_setup'" )
162212
163- _webrepl .password (webrepl_pass )
213+ if webrepl_pass is not None :
214+ webrepl_pass = webrepl_pass .encode ()
215+
164216 s = setup_conn (port , accept_handler )
165217
166218 if accept_handler is None :
0 commit comments