-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwebinterface.py
More file actions
executable file
·1305 lines (1030 loc) · 37.3 KB
/
Copy pathwebinterface.py
File metadata and controls
executable file
·1305 lines (1030 loc) · 37.3 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
import fnmatch
import json
import socket
import subprocess
import dbm
import logging
import re
from jinja2 import Environment, FileSystemLoader
from markupsafe import Markup
from configparser import ConfigParser
from datetime import datetime
from functools import wraps
from glob import glob
from werkzeug.wsgi import DispatcherMiddleware
from werkzeug.serving import run_simple
from flask import Flask, redirect, url_for, send_file, abort, Response, render_template, jsonify, send_from_directory, \
request
from flask_bcrypt import Bcrypt
from libs.Camera import *
from flask import g
import browsepy
import random
import string
try:
# generate a new machine id if one does not already exist
if not os.path.exists("/etc/machine-id"):
os.system("systemd-machine-id-setup")
os.system("chown -R tor:tor /home/tor_private ")
os.system("chown -R tor:tor /var/lib/tor ")
except:
print("something went wrong, oh well...")
browsepy.app.config.update(
APPLICATION_ROOT="/filesystem",
directory_base="/home/images",
directory_start="/home/images",
directory_remove="/home/images",
)
app = Flask(__name__, static_url_path='/static')
app.debug = True
bcrypt = Bcrypt(app)
if socket.gethostname() != "VorvadossTwo":
kmsghandler = logging.FileHandler("/dev/kmsg", 'w')
app.logger.addHandler(kmsghandler)
def setup_ap():
"""
Starts the wireless adapter in access point mode using create_ap
"""
env = Environment(loader=FileSystemLoader('templates'))
template = env.get_template("createap")
interface = os.popen("ip link | cut -c4- | grep ^w | sed 's/:.*//'").read().rstrip()
# Enumerates ip link, removes first 4 characters, filters lines that start with w then removes text following ':'
netprofile = open('/usr/lib/systemd/system/create_ap.service', 'w')
netprofile.write(template.render(interface=interface))
netprofile.close()
print("Starting AP")
os.system("systemctl start create_ap.service")
def sanitizeconfig(towriteconfig, filename: str):
"""
This method is meant to be a sanitiser for the configuration file, before it gets written.
:param configparser.ConfigParser towriteconfig: config object to write to disk.
:param str filename: filename to write to.
"""
with open(filename, 'w') as configfile:
towriteconfig.write(configfile)
def get_time() -> str:
"""
Almost iso8601 formatted time string.
:return: time string formatted with 'YYYY-MM-DD HH:mm:ss'
:rtype: str
"""
return str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
def get_hostname() -> str:
"""
Hostname of the system as a string.
:return: the current hostname
:rtype: str
"""
return str(socket.gethostname())
def get_version() -> str:
"""
Current git version of spc-eyepi.
:return: version
:rtype: str
"""
return subprocess.check_output(["/usr/bin/git describe --always"], shell=True).decode()
try:
app.jinja_env.globals.update(get_time=get_time)
app.jinja_env.globals.update(get_hostname=get_hostname)
app.jinja_env.globals.update(version=get_version())
except:
pass
def check_auth(username: str, password: str) -> bool:
"""
validataion of auth.
Username and password are checked against the bcrypt password hash in the database.
:param str username:
:param str password:
:return: whether the supplied password matches the hash of the one stored in the database
:rtype: bool
"""
ubytes = bytes(username, 'utf-8')
with dbm.open('db', 'r') as db:
if ubytes in db.keys() and bcrypt.check_password_hash(db[ubytes].decode('utf-8'), password):
return True
return False
def requires_auth(f):
"""
Decorator for wrapping a view and requiring auth.
:param types.FunctionType f: view function
:return decorated: wrapped
:rtype: types.FunctionType
"""
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated
@browsepy.app.before_request
@requires_auth
def require_login():
"""
Hackery to make browsepy work with login.
"""
return
def authenticate():
"""
really this should just return a 404 for it to be really secure.
But I use the message sometimes.
:return: 401 Access Denied Response
:rtype: Response
"""
return Response('Access DENIED!', 401, {'WWW-Authenticate': 'Basic realm="Login Required"'})
@app.errorhandler(404)
def not_found(error):
"""
404 error handler
:param error:
:return: 404 page
:rtype: Response
"""
return render_template('page_not_found.html'), 404
@app.errorhandler(500)
def server_error(error):
"""
500 error handler
:param error:
:return: 500 page
:rtype: Response
"""
return render_template('server_error.html'), 500
@app.errorhandler(401)
def bad_auth(error):
"""
401 error handler
:param error:
:return: 401 page
:rtype: Response
"""
return render_template('bad_auth.html'), 401
def add_user(username: str, password_to_set: str, adminpass: str = None) -> bool:
"""
Creates a new user in the small db, or changes the password if it exists.
If the admin password is provided and matches the 'admin' users password hash in the db, allows adding of new users
and modification of other users accounts.
Hashes passwords using :mod:`flask_bcrypt` before storing them in the db.
TODO: this should be moved to :mod:`api`
:param str username: username to modify
:param str password_to_set: plaintext password
:param str adminpass:
:return: whether the operation was sucessful
:rtype: bool
"""
password_hash = bcrypt.generate_password_hash(password_to_set)
db = dbm.open('db', 'c')
# later only allow users control over their own password and admin to add later.
# allow global admin password to change everything.
if b'admin' in db.keys() and bcrypt.check_password_hash(db[b'admin'], adminpass):
db[username] = password_hash
db.close()
return True
# for each username, only allow the correct hash to change the password
for username_, hash_ in db.items():
if username_ in db.keys() and bcrypt.check_password_hash(hash_, adminpass):
db[username] = password_hash
db.close()
return True
db.close()
return False
@app.route("/imgs/<path:path>")
def get_image(path):
"""
View to serve an image (like from a camera), from the static/temp directory.
the static/temp dir is normally symlinked to /dev/shm or /tmp, the location where the main capture script drops the
last image.
:param str path: path/name of the image, without the extension (.jpg is added)
:return: image response
:rtype: Response
"""
if '..' in path or path.startswith('/'):
abort(404)
return send_file(os.path.join("static", "temp", path + ".jpg"))
def cap_lock_wait(port: str, serialnumber: str) -> bool:
"""
captures and writes an image to static/temp with the file name of the serial number.
todo: Does this even work? this is old and prbably broken FIX MEEEEE!
:param str port: sub port
:param str serialnumber: serial number of the camera
:return: whether a frame has been written to disk
:rtype: bool
"""
try:
a = subprocess.check_output(
"gphoto2 --port=" + str(port) + " --capture-preview --force-overwrite --filename='static/temp/" + str(
serialnumber) + ".jpg'", shell=True).decode()
print(a)
return False
except subprocess.CalledProcessError as e:
print(e.output)
return True
def capture_preview(serialnumber: str) -> bool:
"""
capture a preview image once.
todo: see :func:`cap_lock_wait`
:param serialnumber:
:return: whether the capture was a success
:rtype: bool
"""
try:
a = subprocess.check_output("gphoto2 --auto-detect", shell=True).decode()
for port in re.finditer("usb:", a):
port = a[port.start():port.end() + 7]
cmdret = subprocess.check_output('gphoto2 --port "' + port + '" --get-config serialnumber',
shell=True).decode()
_serialnumber = cmdret[cmdret.find("Current: ") + 9: len(cmdret) - 1]
if _serialnumber == serialnumber:
tries = 0
while tries < 10 and cap_lock_wait(port, serialnumber):
tries += 1
time.sleep(1)
return True
except subprocess.CalledProcessError as e:
print(str(e))
return False
@app.route("/preview_cam", methods=["GET"])
def preview():
"""
This gets a preview image from a camera, based on the url parameter "serialnumber"
so the endpoint wouild be /preview_cam?serialnumber=dfjkaghsdfysadftiqw
todo: see :func:`cap_lock_wait`
:return: the image file or the string "fail"
:rtype: Response or str
"""
if request.method == 'GET':
if request.args.get("serialnumber"):
serialnumber = request.args.get("serialnumber")
preview = capture_preview(serialnumber)
return send_file("static/temp/" + str(serialnumber) + ".jpg")
else:
return "fail"
else:
return "fail"
@app.route("/available_networks", methods=["GET"])
def available_networks():
"""
Gets the available networks doing a scan with ... wlp6s0?
todo: get fluffybunny to fix this.
:return: Streamed response of networks as they are enumerated by the scan, newline separated.
:rtype: Response
"""
def generate_networks():
"""
generator function for streaming scan of wifi networks.
todo: get fluffybunny to fix this.
"""
networks = str.splitlines(os.popen('iw dev wlan0 scan | grep "SSID: " | cut -c 8- | sort |uniq').read())
networks = [x for x in networks if "x00" not in x]
for net in networks:
yield net + '\n'
return Response(generate_networks(), mimetype='text/plain')
@app.route("/wifi", methods=["GET"])
def wifi():
"""
wifi configuration view.
"""
return render_template("wifi.html")
@app.route("/focus_cams")
def focus():
"""
view function to focus the cameras.
todo: move to :mod:`api`
"""
a = subprocess.check_output("gphoto2 --auto-detect", shell=True).decode()
for port in re.finditer("usb:", a):
port = a[port.start():port.end() + 7]
cmdret = subprocess.check_output('gphoto2 --port "' + port + '" --get-config serialnumber',
shell=True).decode()
return "success"
@app.route("/sync_hwclock")
@requires_auth
def sync_hwclock():
"""
synchronises the hardware clock with the system clock, and redirects to 'config' endpoint.
todo: move to :mod:`api`
"""
print("Synchronising hwclock")
try:
cmd = subprocess.check_output("hwclock --systohc", shell=True)
except Exception as e:
print("There was a problem Synchronising the hwclock. Debug me please.")
print("Exception: " + str(e))
return render_template('server_error.html'), 500
return redirect(url_for('config'))
@app.route('/savetousb', methods=["POST"])
@requires_auth
def savetousb():
"""
moves files in the 'upload_dir' specified in the config file, to a disk.
this will only work to move files to /dev/sda1.
:return: whether the transfer was a success
:rtype: str
"""
config = ConfigParser()
name = request.form.get("name", None)
if not name:
abort(500)
config.read(os.path.join("configs_byserial", name + '.ini'))
try:
subprocess.call("mount /dev/sda1 /mnt/", shell=True)
shutil.copytree(config["localfiles"]["upload_dir"], os.path.join("/mnt/", config["camera"]["name"]))
except Exception as e:
subprocess.call("umount /mnt", shell=True)
print(str(e))
return "failure"
return "success"
def after_this_request(func):
"""
Call after request helper.
:param types.FunctionType func: function to call
:return: provided function
:rtype: types.FunctionType
"""
if not hasattr(g, 'call_after_request'):
g.call_after_request = []
g.call_after_request.append(func)
return func
@app.after_request
def per_request_callbacks(response):
"""
I have no idea what this does but it looks important
:param response: ???
:return: ???
"""
for func in getattr(g, 'call_after_request', ()):
response = func(response)
return response
def shutdown_server():
"""
Shuts down the webinterface
"""
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
@app.route('/restart')
@app.route('/reboot')
@requires_auth
def restart():
"""
Restarts the raspberry pi through `reboot now` system call.
Probably unsafe but whatever.
:return: Response about rebooting
:rtype: Response
"""
@after_this_request
def sd(response):
"""
After request callback to reboot the pi
I dont think this works.... maybe I'm wrong.
"""
print("SHUTTING DOWN!")
time.sleep(1)
os.system("reboot now")
return response
return "Rebooting... ", 200
@app.route("/update")
@requires_auth
def update():
"""
Pulls the current version of SPC-eyepi from github, and replaces the one running with it.
:return: string response indicating success.
:rtype: str
"""
@after_this_request
def update(response):
app.debug = False
os.system("git fetch --all;git reset --hard origin/master")
os.system("systemctl restart spc-eyepi_capture.service")
return response
app.debug = True
return "SUCCESS"
@app.route("/update_to_tag/<tag>")
@requires_auth
def update_tag(tag: str):
"""
the same as update, except this can take a git tag to update to.
:param str tag: git tag to update to.
:return: string response indicating success.
:rtype: str
"""
@after_this_request
def update(response):
app.debug = False
os.system("git fetch --tags --all;git reset --hard {}".format(tag))
os.system("systemctl restart eyepi-capture.service")
return response
app.debug = True
return "SUCCESS"
@app.route("/pip_install")
@requires_auth
def pip_install():
"""
installs a package using pip.
Doesnt reload/restart anything, so after this is called, python needs to be restarted.
:return: string response indicating success.
:rtype: str
"""
import pip
_, package = dict(request.args).popitem()
pip.main(["install", package])
return "SUCCESS"
@app.route("/wificonfig", methods=['POST'])
@requires_auth
def wificonfig():
"""
wifi configuration view.
Accepts a POST request with 'ssid' and 'key' which are used to create a netctl profile.
TODO: alter this to work with netctl-auto rather than vanilla netctl.
:return: string response indicating success or 400 response if request type is not POST.
:rtype: str
"""
if request.method == 'POST':
interface = os.popen("ip link | cut -c4- | grep ^w | sed 's/:.*//'").read().rstrip()
print("Interface: " + interface)
ssid = request.form["ssid"]
key = request.form["key"]
with open('/etc/netctl/netprofile', 'w') as netprofile:
netprofile.write(render_template("netprofile", interface=interface, ssid=ssid, key=key))
print("Stopping AP")
os.system("systemctl stop create_ap.service")
print("Putting interface down")
os.system("ifconfig " + interface + " down")
if os.system("netctl start netprofile") != 0:
print("Connection failed restarting AP")
print("\nLog:\n\n" + os.popen("systemctl status netctl@netprofile.service").read())
os.system("systemctl start create_ap.service")
return "success"
else:
return abort(400)
@app.route("/newuser", methods=['POST'])
@requires_auth
def newuser():
"""
POST endpoint for adding a user.
Accepts 'username', 'pass', 'adminpass' form arguments.
Password has a minimum of 5 chars.
todo: this should be moved to :mod:`api`, and should be jsonified like a real api.
:return: string response indicating success or 400 response if request type is not POST.
:rtype: str
"""
if request.method == 'POST':
username = request.form["username"]
password = request.form["pass"]
adminpass = request.form.get("adminpass", None)
if len(username) > 0 and len(password) > 5:
return "success" if add_user(username, password, adminpass) else "auth_error"
else:
return "invalid"
else:
return abort(400)
@app.route('/admin')
@requires_auth
def admin():
"""
Administration page view
"""
db = dbm.open('db', 'r')
usernames = []
k = db.firstkey()
while k is not None:
usernames.append(k)
k = db.nextkey(k)
return render_template("admin.html", usernames=usernames)
@app.route('/update_camera/<path:serialnumber>', methods=["GET", "POST"])
@requires_auth
def update_camera_config(serialnumber: str):
"""
Update camera config endpoint.
Exists to update the cameras configuration.
Parses many values.
:param serialnumber: serialnumber of the camera to update
:return: response indicating result of operation
:rtype: Response
"""
ser = None
config_map = {
'name': ('camera', 'name'),
'capture': ('camera', 'enabled'),
'upload': ('ftp', 'enabled'),
'username': ('ftp', 'username'),
'password': ('ftp', 'password'),
'server': ('ftp', 'server'),
'timestamp': ('ftp', 'timestamp'),
'replace': ('ftp', 'replace'),
'interval': ('timelapse', 'interval'),
'starttime': ('timelapse', 'starttime'),
'stoptime': ('timelapse', 'stoptime')
}
tf = {"True": "on", "False": "off"}
if request.method == "POST":
config = ConfigParser()
with open("/etc/machine-id") as f:
m_id = str(f.read())
m_id = m_id.strip('\n')
if m_id == serialnumber:
# modify picam file if machine id is the sn
config_path = "picam.ini"
config.read(config_path)
for key, value in request.form.items(multi=True):
if value in tf.keys():
# parse datetimes correctly, because they are gonna be messy.
if value in ["starttime", "stoptime"]:
dt = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.Z")
value = dt.strftime('%H:%M')
value = tf[value]
config[config_map[key][0]][config_map[key][1]] = value
try:
sanitizeconfig(config, config_path)
return "", 200
except Exception as e:
"", 500
if os.path.isfile(os.path.join("configs_byserial", serialnumber + ".ini")):
# modify camera by serial if available, otherwise 404.
config_path = os.path.join("configs_byserial", serialnumber + ".ini")
config.read(config_path)
for key, value in request.form.items(multi=True):
if value in tf.keys():
value = tf[value]
config[config_map[key][0]][config_map[key][1]] = value
try:
sanitizeconfig(config, config_path)
return "", 200
except Exception as e:
"", 500
else:
return "", 404
return "", 405
@app.route("/command", methods=["POST"])
@requires_auth
def run_command() -> str:
"""
runs arbitrary commands from post data.
form_key: value
command: space separayed list of arguments
:return: str, json response of each command and the corresponding stdout (and stderr).
:rtype: str
"""
response = {}
for command, argument in request.form.items():
try:
a = subprocess.check_output([" ".join([command, argument])], stderr=subprocess.STDOUT,
shell=True).decode()
response[command] = str(a)
except Exception as e:
response[command] = {}
response[command]['exc'] = str(e)
if hasattr(e, "output"):
response[command]['out'] = str(e.output.decode())
return str(json.dumps(response))
@app.route("/reset_machine_id")
@requires_auth
def reset_machine_id():
"""
Resets the machine-id to a random one generated by 'systemd-machine-id-setup'.
:return: str, json response {ERR: "error message"} if an error occurs, otherwise {}
:rtype: str
"""
resp = {}
print("Resetting machine ID")
try:
if os.path.isfile("/etc/machine-id"):
os.remove("/etc/machine-id")
os.system("systemd-machine-id-setup")
except Exception as e:
resp["ERR"] = str(e)
return str(json.dumps(resp))
@app.route('/net')
@requires_auth
def network():
"""
network view
"""
return render_template("network.html")
def trunc_at(s: str, d: str, n: int) -> str:
"""
:param str s: string to truncate
:param str d: delimiter
:param int n: number of occurrences to ignore before caring
:return: s truncated at the n'th occurrence of the delimiter, d.
:rtype: str
"""
return d.join(s.split(d)[:n])
def get_net_size(netmask):
binary_str = ''
for octet in netmask:
binary_str += bin(int(octet))[2:].zfill(8)
return str(len(binary_str.rstrip('0')))
def commit_ip(ipaddress: str = None, subnet: str = None, gateway: str = None, dev="eth0"):
# this is blank on purpose. It needs fixing so its not so shit.
pass
def make_dynamic(dev: str):
"""
disable static ip addressing using systemctl.
Dont use this, it is probably broken.
:param str dev: device to change (eth0, wlp3s0 etc).
"""
os.system("systemctl disable network@{}".format(dev))
def set_ip(ipaddress: str = None, subnet: str = None, gateway: str = None, dev: str = "eth0"):
"""
sets a static ip address manually to the device specified.
:param ipaddress: ip address to commit
:param subnet: subnet to user
:param gateway: gateway to template
:param dev: device to use TODO: actually make this do something
"""
if ipaddress is not None and subnet is not None and gateway is not None:
os.system("ip addr add {}/{} broadcast {}.255 dev {}".format(ipaddress, get_net_size(subnet),
trunc_at(ipaddress, ".", 3), dev))
os.system("ip route add default via " + gateway)
else:
make_dynamic(dev)
@app.route('/set-ip', methods=['POST'])
@requires_auth
def set_ips():
"""
POST endpoing for setting a static ip (or enabling dhcp).
the form accepts "ip-form-dynamic" as an on off flag to enable/disable dhcp.
Otherwise it requires ip-form-ipaddress, ip-form-subnet and ip-form-gateway to set the IP to them.
:return: strin response indicating success
:rtype: str
"""
try:
if "ip-form-dynamic" in request.form.keys():
if request.form['ip-form-dynamic'] == "on":
set_ip()
else:
return "fail"
else:
try:
socket.inet_aton(request.form["ip-form-ipaddress"])
socket.inet_aton(request.form["ip-form-subnet"])
socket.inet_aton(request.form["ip-form-gateway"])
set_ip(ipaddress=request.form["ip-form-ipaddress"],
subnet=request.form["ip-form-subnet"],
gateway=request.form["ip-form-gateway"])
return 'success'
except Exception as e:
return "fail"
except:
return "fail"
@app.route('/commit-ip', methods=['POST'])
@requires_auth
def commit_ip_():
if request.method == 'POST':
try:
if "ip-form-dynamic" in request.form.keys():
if request.form['ip-form-dynamic'] == "on":
set_ip()
else:
return "fail"
else:
try:
socket.inet_aton(request.form["ip-form-ipaddress"])
socket.inet_aton(request.form["ip-form-subnet"])
socket.inet_aton(request.form["ip-form-gateway"])
set_ip(ipaddress=request.form["ip-form-ipaddress"],
subnet=request.form["ip-form-subnet"],
gateway=request.form["ip-form-gateway"])
return 'success'
except Exception as e:
return "fail"
except:
return "fail"
else:
abort(400)
@app.route('/break_the_interface')
@requires_auth
def break_the_interface():
"""
Intentionally load a nonexistent template to start the
`werkezeug interactive debugger <http://werkzeug.pocoo.org/docs/0.11/debug/>`_
"""
return render_template("bljdg.html")
@app.route('/delcfg', methods=['POST'])
@requires_auth
def delcfg():
"""
deletes a configuration file from the config directory.
:return: string response indicating success or failure
:rtype: str
"""
try:
os.remove(os.path.join("configs_byserial", request.form["name"] + ".ini"))
return "success"
except:
return "FAILURE"
@app.route('/writecfg', methods=['POST'])
@requires_auth
def writecfg():
"""
Writes the data contained within the post form to a configuration file.
:return: string response indicating success or failure or 400 Response if something broke writing the config
:rtype: str or Response
"""
aconfig = ConfigParser()
config_name = request.form["config-name"] + ".ini"
if not config_name == "picam.ini":
config_path = os.path.join("configs_byserial", config_name)
else:
config_path = config_name
aconfig.read(config_path)
# this is required because the default behaviour of checkboxes is that they do not trigger if they are unchecked.
aconfig["camera"]["enabled"] = "off"
aconfig["ftp"]["upload"] = "off"
aconfig["ftp"]["replace"] = "off"
aconfig["ftp"]["timestamp"] = "off"
for key, value in request.form.items(multi=True):
# print"key:" + key +" value:"+value
if value != "" and key != "config-name":
sect = key.split('.')[0]
opt = key.split(".")[1]
aconfig[sect][opt] = value
# print("changed: " + sect + ':' + opt + ':' + value)
try:
sanitizeconfig(aconfig, config_path)
return "success"
except Exception as e:
abort(400)
@app.route('/change_hostname', methods=['POST'])
@requires_auth
def change_hostname():
"""
Changes the hostname on the machine, including the /etc/hosts file.
Also goes through all the configuration files looking for the old hostname.
:return: string response indicating success or 400 response if form is incomplete or if writing files failed.
:rtype: str or Response
"""
if not request.form.get('hostname', None):
abort(400)
hostname = request.form['hostname']
config = ConfigParser()
config_path = "eyepi.ini"
config.read(config_path)
config["camera"]["name"] = hostname
pi_config = ConfigParser()
pi_config_path = "picam.ini"
pi_config.read(config_path)
pi_config["camera"]["name"] = hostname + "-Picam"
try:
with open("/etc/hosts", 'w') as hostsfile:
hostsfile.write(render_template('hosts.j2', hostname=hostname))
with open("/etc/hostname", 'w') as hostnamefile:
hostnamefile.write(hostname + '\n')
os.system("hostname " + hostname)
except Exception as e:
print("Something went horribly wrong")
print(str(e))
abort(500)
try:
sanitizeconfig(config, config_path)
sanitizeconfig(pi_config, pi_config_path)
return "success"
except Exception as e:
abort(500)
def wrap_field(name, field, display_name=None):
if not display_name:
display_name = name
return '''<div class="input-group">
<div class="input-group-addon" style="text-align: left;">
{name}
</div>
<div class="input-group-addon">
{f}
</div></div>
'''.format(name=display_name, f=render_field(name, field))
def render_field(name, field):
if type(field) in {str, float, int}: