-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.py
386 lines (342 loc) · 21.7 KB
/
terminal.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
'''
MIT LICENSE
© 2023 Maximilian Gründinger
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including, but not limited to, the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
NOTICE: IF you are not a member of the FIRST LEGO LEAGUE TEAM PaRaMeRoS or intend to use it for FIRST LEGO LEAGUE you are not allowed to use, copy, resell or take parts of the Software/Code!!!
There is an exception if I (Maximilian Gründinger) give you a written permission to do so. If you want to use the Software/Code for FIRST LEGO LEAGUE, please contact me via email ([email protected]).
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
DISCLAIMER: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This license is valid only for the year 2023. After 2023, the aforementioned terms and conditions will be considered invalid.
Users are not authorized to copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
excluding older or custom versions from third parties.
For older versions, the above legal terms and conditions will remain valid from the year 2023 onwards.
'''
# import Area
import time
import logging
import os
import sys
print("Python is starting...")
# Konfigurieren des Loggers
logger = logging.getLogger('backend_new.py')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('./backend.log')
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
# var Area Placeholder till config
file_input = "(system)"
running = True
User = "Team"
dir0 = "/home/web/website/PaRaMeRoS-Python/directory.fll"
in0 = "/home/web/website/PaRaMeRoS-Python/output.fll"
out0 = "/home/web/website/PaRaMeRoS-Python/input.fll"
start0 = "/home/web/website/PaRaMeRoS-Python/start.fll"
user0 = "/home/web/website/PaRaMeRoS-Python/user.fll"
config0 = "/home/web/website/PaRaMeRoS-Python/config.fll"
real_admin_user = ["ZRHztr65"]
admin_user = ["David", "Max"]
normal_user = ["Team", "Coach!"]
index = None
user_input = None
permision_input = None
time_wait = int(2)
#Listen von den DIR´s
DIR_LIST = ['terminal', 'terminal-en', 'terminal-en-conf', 'terminal-en-conf-sys', 'terminal-en-conf-sys-spi']
#Listen von Weiterführung zu DIR
WEITERDIR_LIST = ['end', 'exit', 'en', 'do', 'show', 'conf -t', 'sys -conf', 'conf -spi', ]
#Alle Befehle
ALL_LIST = ['en', 'conf -t', 'conf -sys', 'conf -spi', 'end', 'exit', 'help', 'update', 'do shutdown', 'do flush', 'do reboot', 'show files', 'show log', 'show status', "upload '{file_input}'", "download '{file_input}'", "remove", 'spike charge', 'change permision', "exit_script", "pwd", "grep file", "command", "Algorithmus"]
#valid combinations
VALIDS = [(0, 0), (1, 1), (2, 2), (3, 3), (0, 4), (1, 4), (2, 4), (3, 4), (4, 4), (1, 5), (2, 5), (3, 5), (4, 5), (0, 6), (1, 6), (2, 6), (3, 6), (4, 6), (0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (3, 8), (4, 8), (3, 9), (3, 10), (2, 11), (3, 11), (4, 11), (2, 12), (2, 13), (3, 13), (4, 13), (3, 14), (3, 15), (4, 15), (3, 16), (4, 17), (3, 18), (0,20), (1,20), (2,20), (3,20), (4,20), (2,21),(3,22),(0,23)]
#valid combinations with user
ADMIN_VALIDS = [(0, 0), (1, 1), (2, 2), (3, 3), (0, 4), (1, 4), (2, 4), (3, 4), (4, 4), (1, 5), (2, 5), (3, 5), (4, 5), (0, 6), (1, 6), (2, 6), (3, 6), (4, 6), (0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (3, 8), (4, 8), (3, 9), (3, 10), (2, 11), (3, 11), (4, 11), (2, 12), (2, 13), (3, 13), (4, 13), (3, 14), (3, 15), (4, 15), (3, 16), (4, 17)]
# Funktionen
OUT_LIST = {
(0, 0): "terminal-en",
(1, 1): "terminal-en-conf",
(2, 2): "terminal-en-conf-sys",
(3, 3): "terminal-en-conf-sys-spi",
(0, 4): "terminal",
(1, 4): "terminal",
(2, 4): "terminal",
(3, 4): "terminal",
(4, 4): "terminal",
(1, 5): '',
(2, 5): '',
(3, 5): '',
(4, 5): '',
(0, 6): f"en - Enables the system configuration mode\n some comands like delet file takes extra Argument with an , lease try to care for under scores",
(1, 6): f"exit - goes to terminal</br>\nend - goes to terminal</br>\nupdate - updates the Conntent</br>\nshow - goes to dir show (files/shows files, status/shows status)</br>\nconf -t/configure terminal - goes to terminal-en-conf</br>",
(2, 6): f"exit - goes to terminal</br>\nend - goes to terminal</br>\nupdate - updates the Conntent</br>\ndo - goes to dir do (shutdown/shutssdown the system, bootup/bootsup the system, reboot/reboots the system)</br>\nsys -conf - goes to terminal-#-conf-sys</br>",
(3, 6): f"exit - goes to terminal</br>\nend - goes to terminal</br>\nupdate - updates the Conntent</br>\ndo - goes to dir do (shutdown/shutssdown the system, bootup/bootsup the system, reboot/reboots the system)</br>\n<span class='color2'>upload file - uploads an file to the server</span></br>\n<span class='color2'>download file - downloads an file from the server</span></br>\n<span class='color2'>remove file - removes an file from the server, please enter file DIR</span></br>\n<span class='color2'>config file - configures an file from the server</span></br>\ncommand - admins can enter linux command</br>",
(4, 6): f"exit - goes to terminal</br>\nend - goes to terminal</br>\nupdate - updates the Conntent</br>\nshow - goes to dir show (files/shows files, status/shows status)</br>\nspike charge - shows spike charge</br>",
(0, 7): "Updating...",
(1, 7): "Updating...",
(2, 7): "Updating...",
(3, 7): "Updating...",
(4, 7): "Updating...",
(3, 8): "",
(4, 8): "",
(3, 9): "flusching...",
(3, 10): "Rebooting...",
(2, 12): "",
(2, 13): "Status: No Problems recorded",
(3, 13): "Status: Online and running",
(4, 13): "Status: Online",
(3, 14): "Drag your file into the commandline!",
(3, 15): "The file downloads into your Download folder!",
(4, 15): "The file will download in a few seconds",
(3, 16): "The file is getting removed please stand by!",
(4, 17): "Spike Charging",
(3, 18): "change user permission",
(0, 19): "exiting the Skript...",
(0, 20): "terminal",
(1, 20): "terminal-en",
(2, 20): "terminal-en-conf",
(3, 20): "terminal-en-conf-sys",
(4, 20): "terminal-en-conf-sys-spi",
(2, 21): f"loading...\n",
(2, 9): "flushing...",
(0, 23): "load -> load a project<br>create -> create a new project<br>exit -> exit the programm<br>help -> show this help<br>info -> show info about the programm<br>Version: v.2.2.1<br> Author: Maximilian Gründinger<br>Date: 2021-08-15<br>License: Custom MIT",
}
class User:
def __init__(self):
# alle Dateien werden gelesen und aufbereitet für die Verarbeitung durch die Funktion command
with open(user0, "r") as file:
User = file.read().strip()
with open(dir0, "r") as file:
dir1 = file.read().strip()
if not any(element in dir1 for element in DIR_LIST):
with open(out0, "w") as f:
input1 = f.read().strip()
with open(in0, "r") as file:
input234 = file.read().strip()
input1 = input234
user_input = None
try:
input234 = input234.split(", ")
user_input = input234[1]
imput1 = input234[0]
input1 = input1.replace(", ", "")
except IndexError:
imput1 = input234
user_input = None
if not any(element in input1 for element in ALL_LIST):
null = None
index = None
for j, element in enumerate(DIR_LIST):
if element == dir1:
index = j
index1 = None
for i, element in enumerate(ALL_LIST):
if element == input1:
index1 = i
together = (index, index1)
print(together)
self.command(together, out0, index, User, user_input) # runn's the main logik
def command(self, combination, file, index, User, variable_input=None):
if combination == (1,5) or combination == (2,5) or combination == (3,5) or combination == (4,5):
index -= 1
self.write_file(out0, "")
self.write_file(dir0,DIR_LIST[index])
self.write_file(start0, "Done!")
elif combination == (0,19):
self.write_file(in0, "exit")
running = False
elif combination == (2,12):
with open(out0, "w") as files:
with open("/home/web/.pm2/logs/server-error.log", "r") as f:
with open("/home/web/.pm2/logs/server-out.log", "r") as f1:
with open("/home/web/.pm2/logs/terminal-error.log", "r") as f2:
with open("/home/web/.pm2/logs/terminal-out.log", "r") as f3:
files.write(f.read())
files.write("<br>")
files.write("<br>")
files.write("<br>")
files.write(f1.read())
files.write("<br>")
files.write("<br>")
files.write("<br>")
files.write(f2.read())
files.write("<br>")
files.write("<br>")
files.write("<br>")
files.write(f3.read())
files.write("<br>")
files.write("<br>")
files.write("<br>")
self.write_file(start0, "Done!")
elif combination == (2,9) or combination == (3,9):
os.system("pm2 flush")
self.write_file(out0, OUT_LIST[combination])
self.write_file(start0, "Done!")
elif combination == (4,22):
self.command_os(variable_input)
self.write_file(start0, "Done!")
elif combination == (3,16):
self.file_remove(variable_input)
self.write_file(start0, "Done!")
elif combination == (2,21):
self.load_file_inhalt(variable_input)
self.write_file(start0, "Done!")
elif combination == (3,10):
if check_user_admin(User):
self.write_file(start0, "Done!")
self.reboot(User)
else:
logger.debug("invalid_permision")
self.write_file(start0, "Done!")
elif combination == (3,18):
if check_user_admin1(User):
usre = variable_input.split("-> ")
user_input = usre[0]
permision_input = usre[1]
user_input = user_input.replace("-> ", "")
self.change_user_permision(user_input, permision_input)
self.write_file(start0, "Done!")
else:
logger.debug("invalid_permision")
self.write_file(start0, "Done!")
elif combination == (2,13):
if self.check_user_admin(User):
with open("backend.log", "r") as f:
self.write_file(out0, f.read())
self.write_file(start0, "Done!")
else:
logger.debug("invalid_permision")
elif combination == (0,0) or combination == (1,1) or combination == (2,2) or combination == (3,3) or combination == (0,4) or combination == (1,4) or combination == (2,4) or combination == (3,4) or combination == (4,4):
self.write_file(dir0, OUT_LIST[combination])
self.write_file(out0, OUT_LIST[combination])
self.write_file(start0, "Done!")
elif combination == (0,23) or combination == (0,6) or combination == (1,6) or combination == (2,6) or combination == (3,6) or combination == (4,6) or combination == (0,7) or combination == (1,7) or combination == (2,7) or combination == (3,7) or combination == (4,7) or combination == (3,8) or combination == (4,8) or combination == (3,9) or combination == (3,10) or combination == (2,11) or combination == (3,11) or combination == (4,11) or combination == (2,12) or combination == (2,13) or combination == (3,13) or combination == (4,13) or combination == (3,14) or combination == (3,15) or combination == (4,15) or combination == (3,16) or combination == (4,17) or combination == (0,20) or combination == (1,20) or combination == (2,20) or combination == (3,20) or combination == (4,20):
self.write_file(file, OUT_LIST[combination])
self.write_file(start0, "Done!")
else:
logger.debug("invalid_command")
self.write_file(out0, "invalid command")
self.write_file(start0, "Done!")
def file_remove(self, file):
if check_user_admin(User):
logger.debug("file_remove_valid")
os.remove(file)
def command_os(self, command):
if "sudo" in command: # disables sudo for normal user
if check_user_admin1(User):
com = os.system(command)
with open(out0, "w") as f:
f.write(com)
with open(start0, "w") as f:
f.write("Done!")
with open(out0, "w") as f:
f.write("invalid command")
with open(start0, "w") as f:
f.write("Done!")
else:
com = os.system(command)
with open(out0, "w") as f:
f.write(com)
with open(start0, "w") as f:
f.write("Done!")
def load_file_inhalt(self, file):
if check_user_admin(User):
logger.debug("load_file_inhalt_valid")
with open(file, "r") as f:
self.write_file(out0, f.read())
else:
logger.debug("load_file_inhalt_invalid")
def file_removefile_path(self, file_path):
if check_user_admin1(User):
logger.debug("file_remove_valid")
os.remove(file_path)
def check_user_admin(self, current_user):
if current_user in admin_user:
logger.debug("check_user_valid")
return True
elif current_user in real_admin_user:
logger.debug("check_user_valid")
return True
else:
logger.debug("check_user_invalid")
return False
def check_user_admin1(self, current_user):
if current_user in admin_user:
logger.debug("check_user_valid")
return False
elif current_user in real_admin_user:
logger.debug("check_user_valid")
return True
else:
logger.debug("check_user_invalid")
return False
def hard_shutdown(self, User):
if check_user_admin(User):
logger.debug("hard_shutdown_valid")
os.system("reboot -f")
def reboot(self, User):
if check_user_admin(User):
logger.debug("reboot_valid")
os.system("reboot")
def write_file(self, file, text):
with open(file, "w") as f:
f.write(text)
with open(start0, "w") as f:
f.write("Done!")
def change_user_permision(self, user, permision):
if self.check_user_admin(User):
logger.debug("change_user_permision_valid")
if permision == "admin":
logger.debug("change_user_permision_valid")
admin_user.append(user)
normal_user.remove(user)
elif permision == "normal":
logger.debug("change_user_permision_valid")
normal_user.append(user)
admin_user.remove(user)
else:
logger.debug("change_user_permision_invalid")
class config:
# Hier Config auslese und conf einbauen
def __init__(self):
with open("config.config", "r") as f: #/home/web/website/PaRaMeRoS-Python/
lines = f.readlines()
read = lines[0].split(", ")
read[0] = read[0].replace(", ", "")
read[0] = str(out0)
read[1] = read[1].replace(", ", "")
in0 = str(read[1])
dir0 = str(read[2])
start0 = str(read[3])
user0 = str(read[4])
admin_user = []
real_admin_user = []
normal_user = []
admin_user.append(str(lines[1].strip()))
real_admin_user.append(str(lines[2].strip()))
normal_user.append(str(lines[3].strip()))
time_wait = int(lines[4].strip())
logger.debug("conf_valid")
logger.debug(in0)
logger.debug(dir0)
logger.debug(start0)
logger.debug(user0)
if ValueError or IndexError or NameError or BufferError or TypeError or SyntaxError:
logger.debug("conf_invalid")
pass
# run Area
if __name__ == "__main__":
while running:
config()
time.sleep(time_wait)
with open(start0, "r") as f:
start = f.read().strip()
if start == "Start!":
User()
if start == "Exit!":
logger.warning("Exit!")