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
19-
20+ webrepl_pass = None
21+
22+ class WebreplWrapper (io .IOBase ):
23+ def __init__ (self , sock ):
24+ self .sock = sock
25+ self .sock .ioctl (9 , 2 )
26+ if webrepl_pass is not None :
27+ self .pw = bytearray (16 )
28+ self .pwPos = 0
29+ self .sock .write ("Password: " )
30+ else :
31+ self .pw = None
32+ self .sock .write (_WELCOME_PROMPT )
33+
34+ def readinto (self , buf ):
35+ if self .pw is not None :
36+ buf = bytearray (1 )
37+ while True :
38+ l = self .sock .readinto (buf )
39+ if l is None :
40+ continue
41+ if l <= 0 :
42+ return l
43+ if buf [0 ] == 10 or buf [0 ] == 13 :
44+ print ("Authenticating with:" )
45+ print (self .pw [0 :self .pwPos ])
46+ if bytes (self .pw [0 :self .pwPos ]) == webrepl_pass :
47+ self .pw = None
48+ del self .pwPos
49+ self .sock .write (_WELCOME_PROMPT )
50+ break
51+ else :
52+ print (bytes (self .pw [0 :self .pwPos ]))
53+ print (webrepl_pass )
54+ self .sock .write ("\r \n Access denied\r \n " )
55+ return 0
56+ else :
57+ if self .pwPos < len (self .pw ):
58+ self .pw [self .pwPos ] = buf [0 ]
59+ self .pwPos = self .pwPos + 1
60+ return self .sock .readinto (buf )
61+
62+ def write (self , buf ):
63+ if self .pw is not None :
64+ return len (buf )
65+ return self .sock .write (buf )
66+
67+ def ioctl (self , kind , arg ):
68+ if kind == 4 :
69+ self .sock .close ()
70+ return 0
71+ return - 1
72+
73+ def close (self ):
74+ self .sock .close ()
2075
2176def server_handshake (cl ):
2277 req = cl .makefile ("rwb" , 0 )
@@ -84,7 +139,7 @@ def send_html(cl):
84139 cl .send (static_host )
85140 cl .send (
86141 b"""\" ></base>\r
87- <script src="webrepl_content .js"></script>\r
142+ <script src="webreplv2_content .js"></script>\r
88143"""
89144 )
90145 cl .close ()
@@ -127,7 +182,7 @@ def accept_conn(listen_sock):
127182 client_s = cl
128183
129184 ws = websocket .websocket (cl , True )
130- ws = _webrepl . _webrepl (ws )
185+ ws = WebreplWrapper (ws )
131186 cl .setblocking (False )
132187 # notify REPL on socket incoming data (ESP32/ESP8266-only)
133188 if hasattr (os , "dupterm_notify" ):
@@ -147,10 +202,10 @@ def stop():
147202
148203
149204def start (port = 8266 , password = None , accept_handler = accept_conn ):
150- global static_host
205+ global static_host , webrepl_pass
151206 stop ()
152207 webrepl_pass = password
153- if webrepl_pass is None :
208+ if password is None :
154209 try :
155210 import webrepl_cfg
156211
@@ -160,7 +215,9 @@ def start(port=8266, password=None, accept_handler=accept_conn):
160215 except :
161216 print ("WebREPL is not configured, run 'import webrepl_setup'" )
162217
163- _webrepl .password (webrepl_pass )
218+ if webrepl_pass is not None :
219+ webrepl_pass = webrepl_pass .encode ()
220+
164221 s = setup_conn (port , accept_handler )
165222
166223 if accept_handler is None :
0 commit comments