11# This module should be imported from REPL, not run from command line.
22import binascii
33import hashlib
4- from micropython import const
54import network
65import os
76import socket
87import sys
98import websocket
10- import _webrepl
9+ import io
1110
1211listen_s = None
1312client_s = None
1413
1514DEBUG = 0
1615
17- _DEFAULT_STATIC_HOST = const ("https://micropython.org/webrepl/" )
16+ _DEFAULT_STATIC_HOST = const ("https://felix.dogcraft.de/webrepl/" )
17+ _WELCOME_PROMPT = const ("\r \n WebREPL connected\r \n >>> " )
1818static_host = _DEFAULT_STATIC_HOST
19-
19+ webrepl_pass = None
20+
21+ class WebreplWrapper (io .IOBase ):
22+ def __init__ (self , sock ):
23+ self .sock = sock
24+ self .sock .ioctl (9 , 2 )
25+ if webrepl_pass is not None :
26+ self .pw = bytearray (16 )
27+ self .pwPos = 0
28+ self .sock .write ("Password: " )
29+ else :
30+ self .pw = None
31+ self .sock .write (_WELCOME_PROMPT );
32+
33+ def readinto (self , buf ):
34+ if self .pw is not None :
35+ buf = bytearray (1 )
36+ while True :
37+ l = self .sock .readinto (buf )
38+ if l is None :
39+ continue
40+ if l <= 0 :
41+ return l
42+ if buf [0 ] == 10 or buf [0 ] == 13 :
43+ print ("Authenticating with:" )
44+ print (self .pw [0 :self .pwPos ])
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+ print (bytes (self .pw [0 :self .pwPos ]))
52+ print (webrepl_pass )
53+ self .sock .write ("\r \n Access denied\r \n " )
54+ return 0
55+ else :
56+ if self .pwPos < len (self .pw ):
57+ self .pw [self .pwPos ] = buf [0 ]
58+ self .pwPos = self .pwPos + 1
59+ return self .sock .readinto (buf )
60+
61+ def write (self , buf ):
62+ if self .pw is not None :
63+ return len (buf )
64+ return self .sock .write (buf )
65+
66+ def ioctl (self , kind , arg ):
67+ if kind == 4 :
68+ self .sock .close ()
69+ return 0
70+ return - 1
71+
72+ def close (self ):
73+ self .sock .close ()
2074
2175def server_handshake (cl ):
2276 req = cl .makefile ("rwb" , 0 )
@@ -84,7 +138,7 @@ def send_html(cl):
84138 cl .send (static_host )
85139 cl .send (
86140 b"""\" ></base>\r
87- <script src="webrepl_content .js"></script>\r
141+ <script src="webreplv2_content .js"></script>\r
88142"""
89143 )
90144 cl .close ()
@@ -127,7 +181,7 @@ def accept_conn(listen_sock):
127181 client_s = cl
128182
129183 ws = websocket .websocket (cl , True )
130- ws = _webrepl . _webrepl (ws )
184+ ws = WebreplWrapper (ws )
131185 cl .setblocking (False )
132186 # notify REPL on socket incoming data (ESP32/ESP8266-only)
133187 if hasattr (os , "dupterm_notify" ):
@@ -147,10 +201,10 @@ def stop():
147201
148202
149203def start (port = 8266 , password = None , accept_handler = accept_conn ):
150- global static_host
204+ global static_host , webrepl_pass
151205 stop ()
152206 webrepl_pass = password
153- if webrepl_pass is None :
207+ if password is None :
154208 try :
155209 import webrepl_cfg
156210
@@ -160,7 +214,9 @@ def start(port=8266, password=None, accept_handler=accept_conn):
160214 except :
161215 print ("WebREPL is not configured, run 'import webrepl_setup'" )
162216
163- _webrepl .password (webrepl_pass )
217+ if webrepl_pass is not None :
218+ webrepl_pass = webrepl_pass .encode ()
219+
164220 s = setup_conn (port , accept_handler )
165221
166222 if accept_handler is None :
0 commit comments