7
7
import socket
8
8
import sys
9
9
import websocket
10
- import _webrepl
10
+ import io
11
11
12
12
listen_s = None
13
13
client_s = None
14
14
15
15
DEBUG = 0
16
16
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 >>> " )
18
19
static_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 ()
20
75
21
76
def server_handshake (cl ):
22
77
req = cl .makefile ("rwb" , 0 )
@@ -84,7 +139,7 @@ def send_html(cl):
84
139
cl .send (static_host )
85
140
cl .send (
86
141
b"""\" ></base>\r
87
- <script src="webrepl_content .js"></script>\r
142
+ <script src="webreplv2_content .js"></script>\r
88
143
"""
89
144
)
90
145
cl .close ()
@@ -127,7 +182,7 @@ def accept_conn(listen_sock):
127
182
client_s = cl
128
183
129
184
ws = websocket .websocket (cl , True )
130
- ws = _webrepl . _webrepl (ws )
185
+ ws = WebreplWrapper (ws )
131
186
cl .setblocking (False )
132
187
# notify REPL on socket incoming data (ESP32/ESP8266-only)
133
188
if hasattr (os , "dupterm_notify" ):
@@ -147,10 +202,10 @@ def stop():
147
202
148
203
149
204
def start (port = 8266 , password = None , accept_handler = accept_conn ):
150
- global static_host
205
+ global static_host , webrepl_pass
151
206
stop ()
152
207
webrepl_pass = password
153
- if webrepl_pass is None :
208
+ if password is None :
154
209
try :
155
210
import webrepl_cfg
156
211
@@ -160,7 +215,9 @@ def start(port=8266, password=None, accept_handler=accept_conn):
160
215
except :
161
216
print ("WebREPL is not configured, run 'import webrepl_setup'" )
162
217
163
- _webrepl .password (webrepl_pass )
218
+ if webrepl_pass is not None :
219
+ webrepl_pass = webrepl_pass .encode ()
220
+
164
221
s = setup_conn (port , accept_handler )
165
222
166
223
if accept_handler is None :
0 commit comments