-
Notifications
You must be signed in to change notification settings - Fork 0
/
rasp.py
executable file
·530 lines (424 loc) · 20.6 KB
/
rasp.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
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
#!/usr/bin/python3
"""
Rasp2PC - RASP Component
A program based on socket protocol that uses a Raspberry Pi with touchscreen to control a computer via shortcuts
Copyright (C) 2020 seepiol
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import socket
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox, QApplication
import sys
import logging
import json
import csv
import argparse
from Crypto.Cipher import AES
system_functions_icons = []
labels = []
keyboard_labels = []
# FIXME: the GUI code is a mess
# POPUPS FOR ERROR
# BrokenPipeError Exception popup
def connection_interrupted():
app = QApplication(sys.argv)
window = QMessageBox()
window.setIcon(QMessageBox.Critical)
window.setWindowTitle("Connection Interrupted")
window.setText(f"Connection has been interrupted")
window.setStandardButtons(QMessageBox.Ok)
window.exec()
# Connection denied popup
def connection_denied(socket, host):
app = QApplication(sys.argv)
window = QMessageBox()
window.setIcon(QMessageBox.Critical)
window.setWindowTitle("Connection Denied")
window.setText(f"Connection denied by {host}")
window.setStandardButtons(QMessageBox.Ok)
window.exec()
socket.close()
exit()
def not_reachable(socket, host):
app = QApplication(sys.argv)
window = QMessageBox()
window.setIcon(QMessageBox.Critical)
window.setWindowTitle("Not reachable")
window.setText(f"PC {host} is not reachable")
window.setStandardButtons(QMessageBox.Ok)
window.exec()
socket.close()
exit()
def not_found(socket, host):
app = QApplication(sys.argv)
window = QMessageBox()
window.setIcon(QMessageBox.Critical)
window.setWindowTitle("Not found")
window.setText(
f"PC not found. check the ip address and if the pc component is running."
)
window.setStandardButtons(QMessageBox.Ok)
window.exec()
socket.close()
exit()
def pc_turned_off(socket):
socket.close()
app = QApplication(sys.argv)
window = QMessageBox()
window.setIcon(QMessageBox.Critical)
window.setWindowTitle("PC Turned off")
window.setText(
f"PC is turning off. Closing the socket and application for prevent errors"
)
window.setStandardButtons(QMessageBox.Ok)
window.exec()
exit()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(451, 286)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.RASPcomponent_label = QtWidgets.QLabel(self.centralwidget)
self.RASPcomponent_label.setGeometry(QtCore.QRect(10, 10, 181, 18))
self.RASPcomponent_label.setObjectName("RASPcomponent_label")
self.connected_to_label = QtWidgets.QLabel(self.centralwidget)
self.connected_to_label.setGeometry(QtCore.QRect(10, 30, 101, 18))
self.connected_to_label.setObjectName("connected_to_label")
self.pc_info_label = QtWidgets.QLabel(self.centralwidget)
self.pc_info_label.setGeometry(QtCore.QRect(110, 30, 181, 18))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setItalic(True)
font.setWeight(75)
self.pc_info_label.setFont(font)
self.pc_info_label.setText("")
self.pc_info_label.setObjectName("pc_info_label")
# System Functions
self.sysf1_icon = QtGui.QIcon()
self.sysf1_icon.addPixmap(QtGui.QPixmap(f"icons/{system_functions_icons[0]}.png"))
self.sysf1_button = QtWidgets.QPushButton(self.centralwidget)
self.sysf1_button.setGeometry(QtCore.QRect(290, 10, 41, 37))
self.sysf1_button.setIcon(self.sysf1_icon)
self.sysf1_button.setToolTip("System Function 1")
self.sysf1_button.setObjectName("sysf1_button")
self.sysf2_icon = QtGui.QIcon()
self.sysf2_icon.addPixmap(QtGui.QPixmap(f"icons/{system_functions_icons[1]}.png"))
self.sysf2_button = QtWidgets.QPushButton(self.centralwidget)
self.sysf2_button.setGeometry(QtCore.QRect(340, 10, 41, 37))
self.sysf2_button.setIcon(self.sysf2_icon)
self.sysf2_button.setToolTip("System Function 2")
self.sysf2_button.setObjectName("sysf2_button")
self.sysf3_icon = QtGui.QIcon()
self.sysf3_icon.addPixmap(QtGui.QPixmap(f"icons/{system_functions_icons[2]}.png"))
self.sysf3_button = QtWidgets.QPushButton(self.centralwidget)
self.sysf3_button.setGeometry(QtCore.QRect(390, 10, 41, 37))
self.sysf3_button.setIcon(self.sysf3_icon)
self.sysf3_button.setToolTip("System Function 3")
self.sysf3_button.setObjectName("sysf3_button")
# Programs
self.app_frame = QtWidgets.QFrame(self.centralwidget)
self.app_frame.setGeometry(QtCore.QRect(10, 50, 211, 221))
self.app_frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.app_frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.app_frame.setObjectName("app_frame")
self.app2_button = QtWidgets.QPushButton(self.app_frame)
self.app2_button.setGeometry(QtCore.QRect(110, 10, 90, 37))
self.app2_button.setText("")
self.app2_button.setObjectName("app2_button")
self.app1_button = QtWidgets.QPushButton(self.app_frame)
self.app1_button.setGeometry(QtCore.QRect(10, 10, 90, 37))
self.app1_button.setObjectName("app1_button")
self.app3_button = QtWidgets.QPushButton(self.app_frame)
self.app3_button.setGeometry(QtCore.QRect(10, 50, 90, 37))
self.app3_button.setText("")
self.app3_button.setObjectName("app3_button")
self.app4_button = QtWidgets.QPushButton(self.app_frame)
self.app4_button.setGeometry(QtCore.QRect(110, 50, 90, 37))
self.app4_button.setText("")
self.app4_button.setObjectName("app4_button")
self.app5_button = QtWidgets.QPushButton(self.app_frame)
self.app5_button.setGeometry(QtCore.QRect(10, 90, 90, 37))
self.app5_button.setText("")
self.app5_button.setObjectName("app5_button")
self.app6_button = QtWidgets.QPushButton(self.app_frame)
self.app6_button.setGeometry(QtCore.QRect(110, 90, 90, 37))
self.app6_button.setText("")
self.app6_button.setObjectName("app6_button")
self.app7_button = QtWidgets.QPushButton(self.app_frame)
self.app7_button.setGeometry(QtCore.QRect(10, 130, 90, 37))
self.app7_button.setText("")
self.app7_button.setObjectName("app7_button")
self.app8_button = QtWidgets.QPushButton(self.app_frame)
self.app8_button.setGeometry(QtCore.QRect(110, 130, 90, 37))
self.app8_button.setText("")
self.app8_button.setObjectName("app8_button")
self.app9_button = QtWidgets.QPushButton(self.app_frame)
self.app9_button.setGeometry(QtCore.QRect(10, 170, 90, 37))
self.app9_button.setText("")
self.app9_button.setObjectName("app9_button")
self.app10_button = QtWidgets.QPushButton(self.app_frame)
self.app10_button.setGeometry(QtCore.QRect(110, 170, 90, 37))
self.app10_button.setText("")
self.app10_button.setObjectName("app10_button")
# Keyboard
self.keyboard_frame = QtWidgets.QFrame(self.centralwidget)
self.keyboard_frame.setGeometry(QtCore.QRect(230, 50, 211, 221))
self.keyboard_frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.keyboard_frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.keyboard_frame.setObjectName("keyboard_frame")
self.short1_button = QtWidgets.QPushButton(self.keyboard_frame)
self.short1_button.setGeometry(QtCore.QRect(10, 10, 90, 37))
self.short1_button.setText("")
self.short1_button.setShortcut("")
self.short1_button.setObjectName("short1_button")
self.short2_button = QtWidgets.QPushButton(self.keyboard_frame)
self.short2_button.setGeometry(QtCore.QRect(110, 10, 90, 37))
self.short2_button.setText("")
self.short2_button.setObjectName("short2_button")
self.short3_button = QtWidgets.QPushButton(self.keyboard_frame)
self.short3_button.setGeometry(QtCore.QRect(10, 50, 90, 37))
self.short3_button.setText("")
self.short3_button.setObjectName("short3_button")
self.short4_button = QtWidgets.QPushButton(self.keyboard_frame)
self.short4_button.setGeometry(QtCore.QRect(110, 50, 90, 37))
self.short4_button.setText("")
self.short4_button.setObjectName("short4_button")
self.short5_button = QtWidgets.QPushButton(self.keyboard_frame)
self.short5_button.setGeometry(QtCore.QRect(10, 90, 90, 37))
self.short5_button.setText("")
self.short5_button.setObjectName("short5_button")
self.short6_button = QtWidgets.QPushButton(self.keyboard_frame)
self.short6_button.setGeometry(QtCore.QRect(110, 90, 90, 37))
self.short6_button.setText("")
self.short6_button.setObjectName("short6_button")
self.short7_button = QtWidgets.QPushButton(self.keyboard_frame)
self.short7_button.setGeometry(QtCore.QRect(10, 130, 90, 37))
self.short7_button.setText("")
self.short7_button.setObjectName("short7_button")
self.short8_button = QtWidgets.QPushButton(self.keyboard_frame)
self.short8_button.setGeometry(QtCore.QRect(110, 130, 90, 37))
self.short8_button.setText("")
self.short8_button.setObjectName("short8_button")
self.short9_button = QtWidgets.QPushButton(self.keyboard_frame)
self.short9_button.setGeometry(QtCore.QRect(10, 170, 90, 37))
self.short9_button.setText("")
self.short9_button.setObjectName("short9_button")
self.short10_button = QtWidgets.QPushButton(self.keyboard_frame)
self.short10_button.setGeometry(QtCore.QRect(110, 170, 90, 37))
self.short10_button.setText("")
self.short10_button.setObjectName("short10_button")
# Function Connections
# System Function
self.sysf1_button.clicked.connect(lambda: self.send("sf1"))
self.sysf2_button.clicked.connect(lambda: self.send("sf2"))
self.sysf3_button.clicked.connect(lambda: self.send("sf3"))
# Apps
self.app1_button.clicked.connect(lambda: self.send("a1"))
self.app2_button.clicked.connect(lambda: self.send("a2"))
self.app3_button.clicked.connect(lambda: self.send("a3"))
self.app4_button.clicked.connect(lambda: self.send("a4"))
self.app5_button.clicked.connect(lambda: self.send("a5"))
self.app6_button.clicked.connect(lambda: self.send("a6"))
self.app7_button.clicked.connect(lambda: self.send("a7"))
self.app8_button.clicked.connect(lambda: self.send("a8"))
self.app9_button.clicked.connect(lambda: self.send("a9"))
self.app10_button.clicked.connect(lambda: self.send("a10"))
# Keyboard
self.short1_button.clicked.connect(lambda: self.send("s1"))
self.short2_button.clicked.connect(lambda: self.send("s2"))
self.short3_button.clicked.connect(lambda: self.send("s3"))
self.short4_button.clicked.connect(lambda: self.send("s4"))
self.short5_button.clicked.connect(lambda: self.send("s5"))
self.short6_button.clicked.connect(lambda: self.send("s6"))
self.short7_button.clicked.connect(lambda: self.send("s7"))
self.short8_button.clicked.connect(lambda: self.send("s8"))
self.short9_button.clicked.connect(lambda: self.send("s9"))
self.short10_button.clicked.connect(lambda: self.send("s10"))
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Rasp2Pc "))
self.RASPcomponent_label.setText(_translate("MainWindow", "RASP Component"))
self.connected_to_label.setText(_translate("MainWindow", "Connected to:"))
self.pc_info_label.setText(
_translate("MainWindow", f"{PC_HOST}:{str(PC_PORT)}")
)
# !!!BUTTONS TEXT HERE!!!
self.app1_button.setText(_translate("MainWindow", labels[0])) # app1
self.app1_button.setToolTip("Application 1")
self.app2_button.setText(_translate("MainWindow", labels[1])) # app2
self.app2_button.setToolTip("Application 2")
self.app3_button.setText(_translate("MainWindow", labels[2])) # app3
self.app3_button.setToolTip("Application 3")
self.app4_button.setText(_translate("MainWindow", labels[3])) # app4
self.app4_button.setToolTip("Application 4")
self.app5_button.setText(_translate("MainWindow", labels[4])) # app5
self.app5_button.setToolTip("Application 5")
self.app6_button.setText(_translate("MainWindow", labels[5])) # app6
self.app6_button.setToolTip("Application 6")
self.app7_button.setText(_translate("MainWindow", labels[6])) # app7
self.app7_button.setToolTip("Application 7")
self.app8_button.setText(_translate("MainWindow", labels[7])) # app8
self.app8_button.setToolTip("Application 8")
self.app9_button.setText(_translate("MainWindow", labels[8])) # app9
self.app9_button.setToolTip("Application 9")
self.app10_button.setText(_translate("MainWindow", labels[9])) # app10
self.app10_button.setToolTip("Application 10")
self.short1_button.setText(_translate("MainWindow", keyboard_labels[0])) # short1
self.short1_button.setToolTip("Keyboard Shortcut 1")
self.short2_button.setText(_translate("MainWindow", keyboard_labels[1])) # short2
self.short2_button.setToolTip("Keyboard Shortcut 2")
self.short3_button.setText(_translate("MainWindow", keyboard_labels[2])) # short3
self.short3_button.setToolTip("Keyboard Shortcut 3")
self.short4_button.setText(_translate("MainWindow", keyboard_labels[3])) # short4
self.short4_button.setToolTip("Keyboard Shortcut 4")
self.short5_button.setText(_translate("MainWindow", keyboard_labels[4])) # short5
self.short5_button.setToolTip("Keyboard Shortcut 5")
self.short6_button.setText(_translate("MainWindow", keyboard_labels[5])) # short6
self.short6_button.setToolTip("Keyboard Shortcut 6")
self.short7_button.setText(_translate("MainWindow", keyboard_labels[6])) # short7
self.short7_button.setToolTip("Keyboard Shortcut 7")
self.short8_button.setText(_translate("MainWindow", keyboard_labels[7])) # short8
self.short8_button.setToolTip("Keyboard Shortcut 8")
self.short9_button.setText(_translate("MainWindow", keyboard_labels[8])) # short9
self.short9_button.setToolTip("Keyboard Shortcut 9")
self.short10_button.setText(_translate("MainWindow", keyboard_labels[9])) # short10
self.short10_button.setToolTip("Keyboard Shortcut 10")
# App/keyboard shortcuts/system functions launch functions
# I know that that's not the best way to handle an exception, but it works and it's enough for me
def send(self, index):
try:
logging.info(f"Selected {index}")
encrypt_index(f"{index}")
response = raspsocket.recv(1024).decode("utf-8") # Recive the response
print(response) # Print the response
except BrokenPipeError:
print("Connection closed or denied by PC. Quitting.")
connection_interrupted()
raspsocket.close()
exit()
def encrypt_index(index):
"""
encrypt the index, add whitespace to make the string >= 16 bytes and send the index to PC.
Args:
index (string): the index to encrypt
"""
# AES encrypter / decrypter
# A casual 128bit key A casual 128bit Initialization vector
crytool = AES.new(b"ghnmXRHOwJ2j1Qfr", AES.MODE_CBC, b"127jH6VBnm09Lkqw")
logging.info(f"Encrypting the index {index}")
index = index + (16 - len(index)) * " " # make the index 16 bytes
cipherindex = crytool.encrypt(index.encode("utf-8")) # encrypting the index
logging.info("Index encrypted. Sending to PC")
raspsocket.send(cipherindex) # send the index
logging.info("Index sent")
def load_json():
global labels
# Loading labels
try:
with open("shortcuts.json", "r") as shortcuts_file:
shortcuts_json = json.load(shortcuts_file)
for label in shortcuts_json["app"]:
labels.append(label)
for icon in shortcuts_json["system_functions"]:
system_functions_icons.append(icon)
for label in shortcuts_json["keyboard"]:
keyboard_labels.append(label)
except Exception as E:
print(f"Error while reading shortcuts.json: {E}.\nQuitting.")
logging.critical("Error while reading shortcuts.json")
exit()
if __name__ == "__main__":
# Setting up the logger
logging.basicConfig(
filename="rasp.log",
filemode="w",
format="%(asctime)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
logging.info("Rasp component started")
# Loading configuration file
try:
logging.info("Opening rasp.conf")
with open("rasp.conf", newline="") as conf_file:
reader = csv.reader(conf_file, delimiter=",")
for row in reader:
PC_HOST = row[0]
PC_PORT = int(row[1])
break
logging.info(f"setting pc host to {PC_HOST} and pc port to {PC_PORT}")
except Exception as e:
print(f"Error with rasp.conf: {e}. Fix the error and restart.")
logging.critical(f"Error {e} about rasp.conf. quitting")
exit()
# Cli arguments parser
parser = argparse.ArgumentParser(description="Rasp2Pc RASP Component")
parser.add_argument(
"--host",
type=str,
default=PC_HOST,
help=f"the addess of the PC Component (default={PC_HOST})",
)
parser.add_argument(
"--port",
type=int,
default=PC_PORT,
help=f"the port of the PC Component (default={PC_PORT})",
)
logging.info("Getting cli args")
args = parser.parse_args()
PC_HOST = args.host
PC_PORT = args.port
load_json()
try:
logging.info("Creating socket...")
with socket.socket(
socket.AF_INET, socket.SOCK_STREAM
) as raspsocket: # Create socket object
# ATTENTION: You have to modify the line 25 with the PC ip address and the port (default:10000)
logging.info(f"Connecting to {PC_HOST}:{PC_PORT}...")
raspsocket.connect((PC_HOST, PC_PORT)) # Connect to the PC
logging.info("sending raspcomponent identifier")
raspsocket.send(
"rasp2pc_rasp_component".encode("utf-8")
) # Declare to PC that this is a """legit""" rasp component
print("Waiting for PC to accept the connection...")
connection = raspsocket.recv(1024).decode("utf-8")
if connection == "ConnectionAccepted":
logging.info("Connection accepted")
else:
logging.critical(f"connection denied by {PC_HOST}")
connection_denied(raspsocket, PC_HOST)
app = QtWidgets.QApplication(sys.argv)
ex = Ui_MainWindow()
w = QtWidgets.QMainWindow()
w.setWindowIcon(QtGui.QIcon("icons/icon.png"))
ex.setupUi(w)
w.show()
sys.exit(app.exec_())
except ConnectionRefusedError as e:
not_reachable(raspsocket, PC_HOST)
logging.critical(
f"Pc ({PC_HOST}:{PC_PORT}) is not reachable. ERROR:{e}. Quitting"
)
exit()
except OSError as e:
not_found(raspsocket, PC_HOST)
print(
"PC not found. check the ip address and if the pc component is running. Quitting"
)
logging.critical(f"pc ({PC_HOST}:{PC_PORT}) not found. ERROR:{e}. Quitting")
exit()