-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
666 lines (601 loc) · 22.6 KB
/
main.py
File metadata and controls
666 lines (601 loc) · 22.6 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
#################################################################################
# LEGAL NOTICE AT THE BEGINNING
#################################################################################
# The graphical elements in this program were generated by the Tkinter Designer by Parth Jadhav
# https://github.com/ParthJadhav/Tkinter-Designer
# BSD 3-Clause License
# Copyright (c) 2021, Parth Jadhav
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# from tkinter import *
# Explicit imports to satisfy Flake8
#################################################################################
# WELCOME TO 0trace
#################################################################################
import os
import sys
import subprocess
import Otrace.sys.file_mngr as file_mngr
import Otrace.gui as gui
#################################################################################
try:
os.system("cls" if os.name == "nt" else "clear")
skip_warning = False
if file_mngr.check(
os.path.join(
os.path.dirname(os.path.abspath(__file__)), "Otrace", "cache"
)
):
file_mngr.folder_create(
os.path.join(
os.path.dirname(os.path.abspath(__file__)), "Otrace", "cache"
)
)
if file_mngr.check(
os.path.join(
os.path.dirname(os.path.abspath(__file__)), "Otrace", "cache", "warning"
)
):
with open(
os.path.join(
os.path.dirname(os.path.abspath(__file__)), "Otrace", "cache", "warning"
),
"r",
) as file:
skip_warning = bool(file.read(1) == "n")
else:
file_mngr.create(
os.path.join(
os.path.dirname(os.path.abspath(__file__)), "Otrace", "cache", "warning"
)
)
if file_mngr.empty(
os.path.join(
os.path.dirname(os.path.abspath(__file__)), "Otrace", "cache", "warning"
)
):
skip_warning = None
if skip_warning == True:
print("--- Welcome to 0trace ---")
print("")
print(
"The program is automatically starting up and skipping the routine warning."
)
print("")
elif skip_warning == False:
gui.startup_warning.main()
else:
answer = gui.startup_warning_query.main()
if answer == "yes":
with open(
os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"Otrace",
"cache",
"warning",
),
"w",
) as file:
file.write("y")
print("")
print("(*) Warning enabled.")
print("")
elif answer == "no":
try:
with open(
os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"Otrace",
"cache",
"warning",
),
"w",
) as file:
file.write("n")
print("")
print("(*) Warning disabled.")
print("")
except FileNotFoundError:
gui.cache_error.main()
else:
gui.invalid_error.main()
os.system("cls" if os.name == "nt" else "clear")
delete_pycache = False
if file_mngr.check(
os.path.join(
os.path.dirname(os.path.abspath(__file__)), "Otrace", "cache", "del_pycache"
)
):
with open(
os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"Otrace",
"cache",
"del_pycache",
),
"r",
) as file:
delete_pycache = bool(file.read(1) == "y")
else:
file_mngr.create(
os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"Otrace",
"cache",
"del_pycache",
)
)
empty = file_mngr.empty(
os.path.join(
os.path.dirname(os.path.abspath(__file__)), "Otrace", "cache", "del_pycache"
)
)
if empty == True:
answer = gui.pycache.main()
if answer == "enable":
with open(
os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"Otrace",
"cache",
"del_pycache",
),
"w",
) as file:
file.write("y")
print("")
print("(*) Automatic deletion of the __pycache__ enabled.")
print("")
if answer == "disable":
try:
with open(
os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"Otrace",
"cache",
"del_pycache",
),
"w",
) as file:
file.write("n")
print("")
print("(*) Automatic deletion of the __pycache__ disabled.")
print("")
except FileNotFoundError:
gui.cache_error.main()
if delete_pycache == True:
print("")
print("Deleting __pycache__ as the user configured")
print("")
elif delete_pycache == False:
print("")
print("Won't delete __pycache__ as the user configured")
print("")
else:
answer = gui.pycache.main()
if answer.lower() == "enable":
with open(
os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"Otrace",
"cache",
"del_pycache",
),
"w",
) as file:
file.write("y")
print("")
print("(*) Automatic deletion of the __pycache__ enabled.")
print("")
if answer.lower() == "disable":
try:
with open(
os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"Otrace",
"cache",
"del_pycache",
),
"w",
) as file:
file.write("n")
print("")
print("(*) Automatic deletion of the __pycache__ disabled.")
print("")
except FileNotFoundError:
gui.cache_error.main()
else:
gui.invalid_error.main()
os.system("cls" if os.name == "nt" else "clear")
print("Starting up...")
print("")
os_name = sys.platform
if os_name.startswith("win"):
client_os = "Windows"
script_file_ending = "bat"
elif os_name.startswith("darwin"):
client_os = "MacOS"
script_file_ending = "sh"
elif os_name.startswith("linux"):
client_os = "Linux"
script_file_ending = "sh"
else:
client_os = "Unknown"
script_file_ending = "unknown"
version = "0.0.7.5"
author = "CodingPengu007"
program = "0trace"
publicity = "Closed Early Alpha"
github_link = "https://github.com/CodingPengu007/0trace"
main_dir = os.path.dirname(os.path.abspath(__file__))
venv_dir = os.path.join(main_dir, "Otrace_venv")
shell_script_path = os.path.join(
main_dir, f"start_{client_os.lower()}.{script_file_ending}"
)
home_dir_path = os.path.join(main_dir, "Otrace", "local", "home")
passwd_path = os.path.join(main_dir, "Otrace", "local", "etc", "passwd")
shadow_path = os.path.join(main_dir, "Otrace", "local", "etc", "shadow")
hostname_path = os.path.join(main_dir, "Otrace", "local", "etc", "hostname")
warning_path = os.path.join(main_dir, "Otrace", "cache", "warning")
del_pycache_path = os.path.join(main_dir, "Otrace", "cache", "del_pycache")
venv_dir = os.path.join(main_dir, "Otrace_venv")
apt_sources_path = os.path.join(main_dir, "Otrace", "programs", "apt", "sources")
sudoers_path = os.path.join(main_dir, "Otrace", "local", "etc", "sudoers")
opt_dir_path = os.path.join(main_dir, "Otrace", "local", "opt")
print(f"Detected operating system: {client_os}")
### Virtual Environment Management ###
print("")
print("Checking virtual environment:")
if not file_mngr.check(venv_dir):
print(
"(!) Virtual environment not found. (This is normal when starting for the first time)"
)
print("")
print(
"--------------------------------------------------------------------------------------"
)
print("This is the first time you are running this program, thanks for that!")
print(
"But please be aware that the first time will take a little bit longer than usual. :)"
)
print("So please wait while the virtual environment is set up:")
print(
"--------------------------------------------------------------------------------------"
)
print("")
print(
"(!) Program starting up for the first time! Removing saves of developers..."
)
print("Flushing all files storing user data...")
file_paths = [
passwd_path,
shadow_path,
hostname_path,
warning_path,
del_pycache_path,
apt_sources_path,
sudoers_path,
]
for file_path in file_paths:
try:
with open(file_path, "w") as file:
file.truncate()
except IOError as e:
if file_path == passwd_path or file_path == shadow_path:
file_path = "a file conaining sensitive user data"
print(f"Error opening or writing to {file_path}: {e}")
print("Flushed all files!")
print("")
print("Flushing all folders storing user data...")
folder_paths_ = [home_dir_path, opt_dir_path]
for folder in folder_paths_:
file_mngr.remove_lower(folder)
print("Flushed all folders!")
print("")
### Remove __pycache__ directories ###
print("")
print("Removing __pycache__ directories:")
print("")
file_mngr.remove_pycache(main_dir)
print("")
print("Removed __pycache__ directories.")
print("")
### ------------------------------ ###
else:
print("The virtual environment exists and has been found!")
print("")
### ----------------------------- ###
### Running the startup script ###
print("")
print("Running the startup script...")
try:
if client_os == "Windows":
subprocess.run(["cmd", "/c", shell_script_path], check=True)
elif client_os in ["MacOS", "Linux"]:
result = subprocess.run(
["bash", shell_script_path], check=True, capture_output=True, text=True
)
print("Startup script output:")
print(result.stdout)
else:
print("Unsupported operating system. Skipping script execution.")
except subprocess.CalledProcessError as e:
os.system("clear" if os.name == "posix" else "cls")
print(f"(!) Error occurred while running the script: {e}")
print("")
print(
f"(!) There was an error with one of the start scripts, please create an issue on GitHub ({github_link})."
)
sys.exit(1)
print("Startup script executed successfully.")
print("")
### ------------------------- ###
### Testing Imports ###
from importlib.util import find_spec
requirements = [
"bcrypt",
"requests",
"maskpass",
"textual",
"textual_textarea",
]
if not client_os == "Windows":
requirements.append("readline")
for package in requirements:
if find_spec(package) is not None:
print(f"{package} is installed")
else:
os.system("clear" if os.name == "posix" else "cls")
print("")
print("(!) An error occurred when importing modules!")
print(f"(!) {package} is not installed.")
print("")
print("(!) Please ensure that the virtual environment is activated.")
print("(!) You can do this by running the following command:")
print("")
if client_os == "Windows":
print(rf"{venv_dir}\Scripts\activate.bat")
print(rf"python {main_dir}\main.py")
elif client_os == "Linux" or client_os == "MacOS":
print(f"source {venv_dir}/bin/activate")
print(f"python3 {main_dir}/main.py")
else:
print(
f"(!) You are using an unknown operating system, please create an issue on GitHub ({github_link})."
)
print("")
print("Please activate the virtual environment and run the program again.")
print("")
sys.exit(1)
print("All imports successful.")
print("")
### -------------- ###
import Otrace as game
### Remove __pycache__ directories ###
if delete_pycache == True:
print("")
print("Removing __pycache__ directories:")
print("")
game.sys.file_mngr.remove_pycache(main_dir)
print("")
print("Removed __pycache__ directories.")
print("")
### ------------------------ ###
### Check if essential system files exist ###
print("")
print("Checking if essential system files exist:")
print("")
files_to_check = [
("hostname", os.path.join(main_dir, "Otrace", "local", "etc", "hostname")),
("username", os.path.join(main_dir, "Otrace", "local", "etc", "passwd")),
("password", os.path.join(main_dir, "Otrace", "local", "etc", "shadow")),
("sudoers", os.path.join(main_dir, "Otrace", "local", "etc", "sudoers")),
("apt_sources", os.path.join(main_dir, "Otrace", "programs", "apt", "sources")),
]
for file_desc, file_path in files_to_check:
print(f"Checking for {file_desc} file")
if not game.sys.file_mngr.check(file_path):
print(f"(!) {file_desc} file not found")
print(f"Creating {file_desc} file...")
game.sys.file_mngr.create(file_path)
print(f"{file_desc} file created successfully.")
else:
print(f"{file_desc} file found.")
print("")
### ------------------------------------ ###
### Check if essential system files exist ###
print("")
print("Checking if essential system files exist:")
print("")
folders_to_check = [
("opt", os.path.join(main_dir, "Otrace", "local", "opt")),
("cache", os.path.join(main_dir, "Otrace", "cache")),
("home", os.path.join(main_dir, "Otrace", "local", "home")),
]
for folder_desc, folder_path in folders_to_check:
print(f"Checking for {folder_desc} folder")
if not game.sys.file_mngr.check(folder_path):
print(f"(!) {folder_desc} folder not found")
print(f"Creating {folder_desc} folder...")
game.sys.file_mngr.folder_create(folder_path)
print(f"{folder_desc} folder created successfully.")
else:
print(f"{folder_desc} folder found.")
print("")
### ------------------------------------ ###
### Check if an account exists ###
print("")
print("Checking for accounts:")
if game.sys.accnt_mngr.check(main_dir):
print("Account(s) found.")
else:
print("(!) No account found.")
print("")
print("Starting account creation...")
game.sys.accnt_mngr.signup(main_dir, "sudo")
print("Account created successfully.")
print("")
### -------------------------- ###
### Check if an hostname exists ###
print("")
print("Checking for hostname:")
if game.sys.file_mngr.empty(hostname_path):
print("(!) No hostname found.")
print("")
print("Starting hostname creation...")
game.sys.accnt_mngr.create_hostname(main_dir)
print("Hostname created successfully.")
print("")
### -------------------------- ###
### Check if an hostname exists ###
print("")
print("Checking for hostname:")
if game.sys.file_mngr.empty(hostname_path):
print("(!) No hostname found.")
print("")
print("Starting hostname creation...")
game.sys.accnt_mngr.create_hostname(main_dir)
print("Hostname created successfully.")
print("")
### -------------------------- ###
### Check for account directories directory ###
print("")
print("Checking for account directories:")
username_file_path = os.path.join(main_dir, "Otrace", "local", "etc", "passwd")
with open(username_file_path, "r") as user_file:
usernames = [line.strip() for line in user_file.readlines()]
for users in usernames:
if not game.sys.file_mngr.check(
os.path.join(main_dir, "Otrace", "local", "home", users)
):
print(f"(!) Directory for {users} not found.")
print("Creating directory...")
os.mkdir(os.path.join(main_dir, "Otrace", "local", "home", users))
print("Directory created.")
else:
print(f"Directory for {users} found.")
print("")
### ------------------------------------- ###
### Load account information ###
print("")
print("Loading account information:")
print("(?) Login / Sign Up ...")
print("")
os.system("cls" if os.name == "nt" else "clear")
# Store the result of login_or_signup in a variable
user_choice = game.sys.accnt_mngr.login_or_signup()
if user_choice == "login":
os.system("cls" if os.name == "nt" else "clear")
print("Starting login...")
print("Loading username...")
os.system("cls" if os.name == "nt" else "clear")
username = game.sys.accnt_mngr.login(main_dir)
os.system("cls" if os.name == "nt" else "clear")
print("Username loaded.")
print("Login successful.")
elif user_choice == "signup":
os.system("cls" if os.name == "nt" else "clear")
print("Starting sign up...")
os.system("cls" if os.name == "nt" else "clear")
game.sys.accnt_mngr.signup(main_dir, "non_sudo")
os.system("cls" if os.name == "nt" else "clear")
print("Sign up successful.")
print("Starting login...")
print("Loading username...")
os.system("cls" if os.name == "nt" else "clear")
username = game.sys.accnt_mngr.login(main_dir)
os.system("cls" if os.name == "nt" else "clear")
print("Username loaded.")
print("Login successful.")
else:
os.system("cls" if os.name == "nt" else "clear")
print("")
print("Crash report:")
print("----------------------------")
print(f"ERROR: Invalid output from login_or_signup function.")
print("----------------------------")
print("")
print("")
print("(!) Please report this issue to the developers.")
print("")
print("--> Please create an issue on Github:")
print(f"--> {github_link}")
print("")
print("Loading hostname...")
hostname = game.sys.accnt_mngr.load_hostname(main_dir)
print("Hostname loaded.")
print("Loading local directory...")
local_dir = main_dir + "/Otrace/local"
print("Local directory loaded.")
print("Loading current directory...")
current_dir = f"{local_dir}/home/{username}"
print("Current directory loaded.")
print("")
### ------------------------ ###
print("")
except KeyboardInterrupt:
print("Exiting...")
os.system("cls" if os.name == "nt" else "clear")
sys.exit(0)
except Exception as e:
print("")
print("The Startup of 0trace failed.")
print("")
print("Crash report:")
print("----------------------------")
print(f"ERROR: {e}")
print("----------------------------")
print("")
print("")
print("(!) Please report this issue to the developers.")
print("")
print("--> Please create an issue on Github:")
print(f"--> {github_link}")
print("")
print("")
input("Press Enter to exit...")
os.system("cls" if os.name == "nt" else "clear")
sys.exit(1)
print("Startup complete.")
os.system("cls" if os.name == "nt" else "clear")
print(f"Welcome to {program}!")
print("")
print(f"Version: {version}")
print(f"Author: {author}")
print(f"Publicity: {publicity}")
print("")
os.system("cls" if os.name == "nt" else "clear")
#################################################################################
try:
game.prgms.cmd.line(username, hostname, current_dir, local_dir, main_dir)
except KeyboardInterrupt:
print("Exiting...")
os.system("cls" if os.name == "nt" else "clear")
sys.exit(0)
except Exception as e:
print(f"(!) An error occurred: {e}")
print("")
print(
f"(!) Please report this bug to the developers and create an issue on GitHub ({github_link})."
)
#################################################################################