-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
152 lines (131 loc) · 5.54 KB
/
main.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
#!/usr/bin/env python3
"""
PyWall - A simple firewall management tool for Windows.
Allows easy control of inbound and outbound connections for applications.
"""
import argparse
import os
import pathlib
import sys
from PyQt5.QtWidgets import QApplication
from src.cmdWorker import access_handler
from src.config import config_exists, document_folder, make_default, validate_config
from src.logger import actionLogger, logException
from src.shellHandler import createInternetAccessMenu, removeInternetAccessMenu
def checkExistingInstall():
"""Check if PyWall is already installed."""
document_folder_path = document_folder()
return os.path.exists(document_folder_path + "\\PyWall\\Executable.txt")
def saveCurrentFolder():
"""Save the current folder for context menu access."""
document_folder_path = document_folder()
if not os.path.exists(document_folder_path + "\\PyWall"):
os.makedirs(document_folder_path + "\\PyWall")
with open(document_folder_path + "\\PyWall\\Executable.txt", 'w') as f:
f.write(os.path.dirname(os.path.abspath(__file__)))
def main():
"""Main entry point for PyWall."""
# Verify config file
try:
if not config_exists():
make_default()
if not validate_config():
make_default()
parser = argparse.ArgumentParser(
description='PyWall - Firewall Management Tool')
parser.add_argument('-file',
help='Target file or directory path', type=str)
parser.add_argument('-allow',
choices=['true', 'True', 'false', 'False'],
help='Allow or deny internet access', type=str)
parser.add_argument('-rule_type',
choices=['in', 'out', 'both'],
help='Rule type: inbound, outbound, or both', type=str)
parser.add_argument('-install', action='store_true',
help='Install context menu')
parser.add_argument('-uninstall', action='store_true',
help='Uninstall context menu')
parser.add_argument('-config', action='store_true',
help='Open configuration file')
parser.add_argument("-c", help="Shell handler", type=str)
try:
all_args = parser.parse_known_args()
args = all_args[0]
except IndexError:
all_args = sys.argv
args = None
# Save the current folder for context menu access
if not checkExistingInstall():
saveCurrentFolder()
# Shell handler
if args.c:
argument = str(args.c)
# Access handling
if "allowAccess" in argument or "denyAccess" in argument:
print(all_args)
arg = all_args[1]
file_path = arg[0]
if "allowAccess" in argument:
action = "allow"
else:
action = "deny"
if ",in" in argument:
rule_type = "in"
elif ",out" in argument:
rule_type = "out"
else:
rule_type = "both"
shell_action = str(args.c).split(",")
actionLogger(
f"Shell action is {shell_action[0]}, filename is {file_path}, "
f"rule type is {rule_type}, proceeding...")
access_handler(pathlib.Path(file_path), action, rule_type)
return
if args.install:
actionLogger("Installing context menu")
createInternetAccessMenu()
return
if args.uninstall:
actionLogger("Uninstalling context menu")
removeInternetAccessMenu()
return
if args.config:
from src.cmdWorker import open_config
open_config()
return
if args.file and args.allow and args.rule_type:
allow_action = args.allow.lower() == 'true'
action = "allow" if allow_action else "deny"
file_path = pathlib.Path(str(args.file))
actionLogger(f'Argument "File" is {file_path}')
actionLogger(f'Argument "Allow" is {allow_action}')
actionLogger(f'Argument "Rule type" is {args.rule_type}')
if allow_action:
actionLogger(f'Attempting to allow "{file_path.stem}"')
else:
actionLogger(f'Attempting to block "{file_path.stem}"')
access_handler(file_path, action, args.rule_type)
return
except Exception as critical:
logException("unknown_argument_error", critical)
raise critical
# Launch GUI if no command line arguments are provided
try:
# Import and start the GUI
from src.configGui import start
app = QApplication(sys.argv)
try:
start() # Use the start function from configGui
except Exception as e:
logException("gui_error", e)
raise e
sys.exit(app.exec_())
except ImportError as e:
logException("import_error", e)
raise e
except Exception as critical:
logException("unknown_gui_error", critical)
raise critical # Re-raise the exception after logging it
if __name__ == "__main__":
main()
# Thanks for taking the time to read this script, you nerd \( ̄︶ ̄*\)) #