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