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
+ 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
+ self .sock .write ("\r \n Access denied\r \n " )
52
+ return 0
53
+ else :
54
+ if self .pwPos < len (self .pw ):
55
+ self .pw [self .pwPos ] = buf [0 ]
56
+ self .pwPos = self .pwPos + 1
57
+ return self .sock .readinto (buf )
58
+
59
+ def write (self , buf ):
60
+ if self .pw is not None :
61
+ return len (buf )
62
+ return self .sock .write (buf )
63
+
64
+ def ioctl (self , kind , arg ):
65
+ if kind == 4 :
66
+ self .sock .close ()
67
+ return 0
68
+ return - 1
69
+
70
+ def close (self ):
71
+ self .sock .close ()
19
72
20
73
21
74
def server_handshake (cl ):
@@ -84,7 +137,7 @@ def send_html(cl):
84
137
cl .send (static_host )
85
138
cl .send (
86
139
b"""\" ></base>\r
87
- <script src="webrepl_content .js"></script>\r
140
+ <script src="webreplv2_content .js"></script>\r
88
141
"""
89
142
)
90
143
cl .close ()
@@ -95,10 +148,7 @@ def setup_conn(port, accept_handler):
95
148
listen_s = socket .socket ()
96
149
listen_s .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
97
150
98
- ai = socket .getaddrinfo ("0.0.0.0" , port )
99
- addr = ai [0 ][4 ]
100
-
101
- listen_s .bind (addr )
151
+ listen_s .bind (("" , port ))
102
152
listen_s .listen (1 )
103
153
if accept_handler :
104
154
listen_s .setsockopt (socket .SOL_SOCKET , 20 , accept_handler )
@@ -127,7 +177,7 @@ def accept_conn(listen_sock):
127
177
client_s = cl
128
178
129
179
ws = websocket .websocket (cl , True )
130
- ws = _webrepl . _webrepl (ws )
180
+ ws = WebreplWrapper (ws )
131
181
cl .setblocking (False )
132
182
# notify REPL on socket incoming data (ESP32/ESP8266-only)
133
183
if hasattr (os , "dupterm_notify" ):
@@ -147,10 +197,10 @@ def stop():
147
197
148
198
149
199
def start (port = 8266 , password = None , accept_handler = accept_conn ):
150
- global static_host
200
+ global static_host , webrepl_pass
151
201
stop ()
152
202
webrepl_pass = password
153
- if webrepl_pass is None :
203
+ if password is None :
154
204
try :
155
205
import webrepl_cfg
156
206
@@ -160,7 +210,9 @@ def start(port=8266, password=None, accept_handler=accept_conn):
160
210
except :
161
211
print ("WebREPL is not configured, run 'import webrepl_setup'" )
162
212
163
- _webrepl .password (webrepl_pass )
213
+ if webrepl_pass is not None :
214
+ webrepl_pass = webrepl_pass .encode ()
215
+
164
216
s = setup_conn (port , accept_handler )
165
217
166
218
if accept_handler is None :
0 commit comments