This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemclient.py
159 lines (139 loc) · 5.01 KB
/
systemclient.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
#!/usr/bin/env python3
# PhilleConnect Systemclient v2 - main
# © 2019 Johannes Kreutz.
# Include dependencies
import sys
import os
import time
import ctypes
from flask import Flask, request
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QThreadPool
# Include modules
import modules.configreader as config
import modules.lockscreen as lock
import modules.essentials as ess
import modules.lockcaentf as lockcaentf
import modules.lockaccessibility as accessibility
import modules.upcommserver as upcommserver
import modules.upcommclient as upcommclient
import modules.smbfixup as smbfixup
import modules.watchdog as wd
import modules.shutdown as sd
import modules.request as rq
import modules.vncpasswd as vcpw
# Startup modes
if len(sys.argv) > 1:
if len(sys.argv) == 2:
watchdog = wd.watchdog(2, sys.argv[1])
else:
print("Wrong parameters. Aborting.")
sys.exit()
elif len(sys.argv) == 1:
watchdog = wd.watchdog(1)
# Check for admin rights
try:
isAdmin = os.getuid() == 0
except AttributeError:
isAdmin = ctypes.windll.shell32.IsUserAnAdmin() != 0
# Manager objects
config = config.reader()
# Main instance running in admin context
if isAdmin:
caentf = lockcaentf.lockcaentf()
acc = accessibility.lockaccessibility()
# Apply windows SMB workaround
smbfixup.smbfixup.fixHostsFile(config.get("smbserver"))
# Stop process on teacher machines as everything is done here
if config.get("isteachermachine") == "1":
sys.exit()
# Start watchdog
watchdog.start()
# Global status variables (why so complicated, you might think? wasn't able to get the easy way working...)
class lockstate:
def __init__(self):
self.__ls = False
def set(self, mode):
self.__ls = mode
def get(self):
return self.__ls
ls = lockstate()
# User context events
ups = upcommserver.upcommserver()
ups.start()
# Flask server definition (legacy mode)
legacy = Flask(__name__)
# LEGACY API ENDPOINTS
@legacy.route("/online", methods = ["GET", "POST"])
def onlineAnswer():
if ups.isConnected():
return "online"
return "semi"
@legacy.route("/lockstate", methods = ["GET", "POST"])
def lockstateAnswer():
if ls.get():
return "locked"
else:
return "unlocked"
@legacy.route("/<room>/<machine>/<command>", methods = ["GET", "POST"])
def handleCommand(room, machine, command):
if room == config.get("room") and machine == config.get("machinename"):
permissioncheck = rq.request(config.get("server"), {"usage": "checkteacher", "globalpw": config.get("global"), "req": request.remote_addr}, False)
result = permissioncheck.post()
if result == "success":
if command == "lock":
if ups.send("LOCK") == "SUCCESS":
caentf.enable()
acc.enable()
ls.set(True)
return "locked"
else:
return "error"
elif command == "unlock":
if ups.send("UNLOCK") == "SUCCESS":
caentf.disable()
acc.disable()
ls.set(False)
return "unlocked"
else:
return "error"
elif command == "shutdown":
sd.shutdown.now()
return "shutdown"
elif command == "screenshot":
return ups.send("SCREENSHOT")
elif command == "requestcontrol":
password = ess.essentials.randomString(8)
vcpw.vncpasswd.setPasswd(password)
if ups.send("CONTROL") == "SUCCESS":
return password
else:
return "error"
elif command == "cancelcontrol":
if ups.send("STOPCONTROL") == "SUCCESS":
return "success"
else:
return "error"
else:
return "Bad command."
else:
return "unauthorized."
else:
return "Bad request."
# Create server
if __name__ == "__main__":
legacy.run(debug=False, host="0.0.0.0", port=34567, threaded=True)
# Worker client running in user context
else:
# Stop process on teacher machines as everything is done here
if config.get("isteachermachine") == "1":
sys.exit()
# Start watchdog
watchdog.start()
# GUI and communication configuration
app = QApplication(sys.argv) # Create QApplication object
window = lock.lockscreen() # Create window
threadpool = QThreadPool() # Create thread pool
upc = upcommclient.upcommclient(window) # Create communication thread
threadpool.start(upc) # Add worker thread to thread pool
sys.exit(app.exec_())