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
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
+ print ("Authenticating with:" )
46
+ print (self .pw [0 : self .pwPos ])
47
+ if bytes (self .pw [0 : self .pwPos ]) == webrepl_pass :
48
+ self .pw = None
49
+ del self .pwPos
50
+ self .sock .write (_WELCOME_PROMPT )
51
+ break
52
+ else :
53
+ print (bytes (self .pw [0 : self .pwPos ]))
54
+ print (webrepl_pass )
55
+ self .sock .write ("\r \n Access denied\r \n " )
56
+ return 0
57
+ else :
58
+ if self .pwPos < len (self .pw ):
59
+ self .pw [self .pwPos ] = buf [0 ]
60
+ self .pwPos = self .pwPos + 1
61
+ return self .sock .readinto (buf )
62
+
63
+ def write (self , buf ):
64
+ if self .pw is not None :
65
+ return len (buf )
66
+ return self .sock .write (buf )
67
+
68
+ def ioctl (self , kind , arg ):
69
+ if kind == 4 :
70
+ self .sock .close ()
71
+ return 0
72
+ return - 1
73
+
74
+ def close (self ):
75
+ self .sock .close ()
19
76
20
77
21
78
def server_handshake (cl ):
@@ -84,7 +141,7 @@ def send_html(cl):
84
141
cl .send (static_host )
85
142
cl .send (
86
143
b"""\" ></base>\r
87
- <script src="webrepl_content .js"></script>\r
144
+ <script src="webreplv2_content .js"></script>\r
88
145
"""
89
146
)
90
147
cl .close ()
@@ -127,7 +184,7 @@ def accept_conn(listen_sock):
127
184
client_s = cl
128
185
129
186
ws = websocket .websocket (cl , True )
130
- ws = _webrepl . _webrepl (ws )
187
+ ws = WebreplWrapper (ws )
131
188
cl .setblocking (False )
132
189
# notify REPL on socket incoming data (ESP32/ESP8266-only)
133
190
if hasattr (os , "dupterm_notify" ):
@@ -147,10 +204,10 @@ def stop():
147
204
148
205
149
206
def start (port = 8266 , password = None , accept_handler = accept_conn ):
150
- global static_host
207
+ global static_host , webrepl_pass
151
208
stop ()
152
209
webrepl_pass = password
153
- if webrepl_pass is None :
210
+ if password is None :
154
211
try :
155
212
import webrepl_cfg
156
213
@@ -160,7 +217,9 @@ def start(port=8266, password=None, accept_handler=accept_conn):
160
217
except :
161
218
print ("WebREPL is not configured, run 'import webrepl_setup'" )
162
219
163
- _webrepl .password (webrepl_pass )
220
+ if webrepl_pass is not None :
221
+ webrepl_pass = webrepl_pass .encode ()
222
+
164
223
s = setup_conn (port , accept_handler )
165
224
166
225
if accept_handler is None :
0 commit comments