-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32thing.py
359 lines (336 loc) · 13.5 KB
/
esp32thing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import network
global MyName, debug, debugDNS, ip, runREPL, runDNS, runWeb, threaded
from config import *
if(debug==True):
q = input("run access point? (Y/N) ")
if q in ['y','Y']:
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(dhcp_hostname=MyName, essid=MyName, authmode=network.AUTH_OPEN)
ip = ap.ifconfig()[0]
else:
q = input("connect to wifi? (Y/N) ")
if q in ['y','Y']:
from utils import wifi_connect
essid = input("ESSID? ")
password = input("Password? ")
ip = wifi_connect(essid,password)
else:
pass
q = input("start webrepl?")
if q in ['Y','y']:
runREPL = True
else:
try:
f = open("/client.cfg")
param = f.read().split(':')
param.append(None)
print("client.cfg found and debug not set. using wifi settings.\n", \
"connecting to",param[0],param[1])
from utils import wifi_connect
if(param[1] == None):
ip = wifi_connect(param[0].strip())
else:
ip = wifi_connect(param[0].strip(),param[1].strip())
except:
ap = network.WLAN(network.AP_IF)
ap.active(True)
from time import sleep
sleep(1)
ap.config(dhcp_hostname=MyName, essid=MyName, authmode=network.AUTH_OPEN)
ip = ap.ifconfig()[0]
try:
print('my IP is',ip)
except NameError:
print("Not online.")
if(runREPL):
import webrepl
from utils import replaceRe
pat = "ws://(.*)\:8266/"
replaceRe('/www/webrepl.html',pat,"ws://"+ip+":8266/")
webrepl.start()
def response(packet):
global ip
bip = ip.split('.')
for i in range(0,len(bip)):
bip[i] = int(bip[i])
bip = bytes(bip)
return b''.join([
packet[:2], # Query identifier
b'\x85\x80', # Flags and codes
packet[4:6], # Query question count
b'\x00\x01', # Answer record count
b'\x00\x00', # Authority record count
b'\x00\x00', # Additional record count
packet[12:], # Query question
b'\xc0\x0c', # Answer name as pointer
b'\x00\x01', # Answer type A
b'\x00\x01', # Answer class IN
b'\x00\x00\x00\x1E', # Answer TTL 30 secondes
b'\x00\x04', # Answer data length
bip]) # Answer data
def run_dns():
global ip, debugDNS
try:
import usocket as socket
except:
import socket
dnsserver = socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)
dnsserver.setsockopt(socket.SOL_SOCKET,
socket.SO_REUSEADDR,
1)
dnsserver.bind(('0.0.0.0', 53))
#dnsserver.setblocking(True)
print("dns server starting...")
while True:
(packet, cliAddr) = dnsserver.recvfrom(256)
qs = str(packet[13:])
if debugDNS: print("<53",cliAddr, qs.replace('\\x03','.').replace('\\x0f','.').replace('\\x00',' ').replace('\\x07','.'))
packet = response(packet)
if packet:
dnsserver.sendto(packet, cliAddr)
qr = str(packet[13:])
if debugDNS: print(">53",cliAddr, ip)
def run_web(threaded):
from microWebSrv import MicroWebSrv
def delayed_reboot(delay=3.5):
from time import sleep
from machine import reset
sleep(delay)
reset()
####################################### URL Route handling #################################
@MicroWebSrv.route('/config')
def _httpHandlerConfig(httpClient, httpResponse, args={}) :
from utils import sys_config_html
content = """\
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset="UTF-8" />
<title>CONFIG DUMP</title>
</head>
<body><PRE>"""
content += sys_config_html()
content += '<div style="alignt: center; text-align: center;">\n'
content += '<P><h3><a href="/stats">status</a></h3></P>'
content += "<br /><h5><a href=\"/config/reboot\">REBOOT?</A></h5>\n"
content += "</div>"
content += """\
</PRE>
</body>
</html>
"""
httpResponse.WriteResponseOk( headers = None,
contentType = "text/html",
contentCharset = "UTF-8",
content = content )
##############################################
@MicroWebSrv.route('/config/reboot')
@MicroWebSrv.route('/config/restart')
def _httpHandlerReboot(httpClient, httpResponse, args={}) :
from _thread import start_new_thread
start_new_thread(delayed_reboot, ())
content = """\
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="8;url=/" />
<title>C U NEXT TUESDAY...</title>
</head>
<body><BR /><HR /><br />
<P><center><img src="/reboot.gif"></center></P>
<BR /><HR /><br /></body>
</html>
"""
httpResponse.WriteResponseOk( headers = None,
contentType = "text/html",
contentCharset = "UTF-8",
content = content )
############################################################
@MicroWebSrv.route('/config/toggleAP')
def _httpHandlerToggle(httpClient, httpResponse, args={}) :
from os import rename
apMode = None
try:
x = open('/client.cfg')
x.close()
apMode = False
rename("/client.cfg","/client.disable")
except:
try:
x = open('client.disable')
x.close()
apMode = True
rename("/client.disable","/client.cfg")
except:
return()
content = """\
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset="UTF-8" />
<title>TEST EDIT</title>
</head>
<body>
"""
if(apMode):
content += "<H2>Switched to Wifi Client Mode</H2>"
content += '<div style="alignt: center; text-align: center;">\n'
content += '<P><h3><a href="/config/">config</a></h3></P>'
content += "<br /><h5><a href=\"/config/reboot\">REBOOT?</A></h5>\n"
content += "</div>"
else:
content += "<H2>Switched to Wifi AP Mode</H2>"
content += '<div style="alignt: center; text-align: center;">\n'
content += '<P><h3><a href="/config/">config</a></h3></P>'
content += "<br /><h5><a href=\"/config/reboot\">REBOOT?</A></h5>\n"
content += "</div>"
content += """\
</body>
</html>
"""
httpResponse.WriteResponseOk( headers = None,
contentType = "text/html",
contentCharset = "UTF-8",
content = content )
##############################################
@MicroWebSrv.route('/config/<param>/<value>')
def _httpHandlerEditParams(httpClient, httpResponse, args={}) :
from utils import replaceLine, findValue
content = """\
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset="UTF-8" />
<title>-=DAT CONFIG=-</title>
</head>
<body>
"""
content += "<h3>CONFIG UPDATE</h3>"
if 'param' in args :
param = args['param'].strip()
oldline = findValue('/config.py',param)
content += "<p>[config: {}]</p>".format(oldline)
content += "<br />"
if 'value' in args :
if(type(args['value']) == int):
value = str(args['value'])
else:
value = args['value'].strip()
content += "<p>[new value is {}]</p>".format(value)
content += "<br />"
if( ('value' in args) and ('param' in args) ):
#re = args['param'].strip() + " = ([A-Za-z\"]+)"
re = oldline
if((value == str(1)) or ('rue' in value)):
value = "True"
elif((value == str(0)) or ('alse' in value)):
value = "False"
else:
value = '"' + value + '"'
newline = args['param'].strip() + " = " + value + "\n"
content += "<P>" + newline + "</P><BR />"
print("CONFIGURE:",param,' - ',value,' - ', re, '\n',newline)
replaceLine("/config.py",re,newline)
content += "<p>parameters updated.</p>"
content += '<div style="alignt: center; text-align: center;">\n'
content += '<P><h3><a href="/config/">config</a></h3></P>'
content += "<br /><h5><a href=\"/config/reboot\">REBOOT?</A></h5>\n"
content += "</div>"
content += """
</body>
</html>
"""
httpResponse.WriteResponseOk( headers = None,
contentType = "text/html",
contentCharset = "UTF-8",
content = content )
############################################################
@MicroWebSrv.route('/config/<param>')
def _httpHandlerEditParams(httpClient, httpResponse, args={}) :
from utils import findValue
content = """\
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset="UTF-8" />
<title>-=DAT CONFIG=-</title>
</head>
<body>
"""
content += "<h3>CONFIG REQUEST</h3>"
if 'param' in args :
param = args['param'].strip()
oldline = findValue('/config.py',param)
content += "<p>[config: {}]</p>".format(oldline)
content += "<br />"
content += '<div style="alignt: center; text-align: center;">\n'
content += "<P><h5><a href='/config/" + param + "/1'>enable</a> / <a href='/config/" + param + "/0'>disable</a></h5></P>"
content += '<P><h3><a href="/config/">config</a></h3></P>'
content += "</div>"
content += """
</body>
</html>
"""
httpResponse.WriteResponseOk( headers = None,
contentType = "text/html",
contentCharset = "UTF-8",
content = content )
############################################################
@MicroWebSrv.route('/stats')
@MicroWebSrv.route('/status')
def _httpHandlerStats(httpClient, httpResponse, args={}) :
from utils import sys_stats, sys_config
global MyName, debug, debugDNS, ip, runREPL, runDNS, runWeb, threaded
content = """\
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="2" />
<title>STATUS ORGASM</title>
</head>
<body>
"""
content += "<h2>SYSTEM STATS</h2>\n<PRE>" + sys_stats() + "</PRE>\n"
content += "<H2>SYSTEM CONFIG</H2>\n<PRE>" + sys_config() + "</PRE>\n"
content += "<h5><a href='/config'>configure</a></h5>"
content += "<h3><a href='/'>HOME</a></h5>"
content += """
</body>
</html>
"""
httpResponse.WriteResponseOk( headers = None,
contentType = "text/html",
contentCharset = "UTF-8",
content = content )
############################################ End of Routes ##################################################
mws = MicroWebSrv(port=80, bindIP='0.0.0.0', webPath="/www")
mws.SetNotFoundPageUrl("http://"+ip)
print("web server starting at http://" + ip + "/ .")
mws.Start(threaded=threaded)
def run():
global ip, debug, threaded
if(debug==True):
q = input("run dns server? (y/n) ")
if q in ['y','Y']:
from _thread import start_new_thread
start_new_thread(run_dns, ())
else:
if(runDNS):
from _thread import start_new_thread
start_new_thread(run_dns, ())
if(debug==True):
q = input("run web server? (y/n) ")
if q in ['y','Y']:
t = input("run www threaded? (y/n) ")
if(t in ['N','n']):
threaded = False
elif(t in ['Y','y']):
threaded = True
run_web(threaded)
else:
if(runWeb):
run_web(threaded)