Skip to content

Commit

Permalink
Update and rename two.py to usb_management.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Akash-Nipunge committed Oct 26, 2023
1 parent 2197f0c commit f2a72ee
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 80 deletions.
80 changes: 0 additions & 80 deletions two.py

This file was deleted.

79 changes: 79 additions & 0 deletions usb_management.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

def create_usb_port_management_frame(parent_frame,ctk):
rules_conf_file = '/etc/usbguard/rules.conf'
lines_list = []

try:
with open(rules_conf_file, 'r') as file:
for line in file:
lines_list.append(line.strip())
except FileNotFoundError:
print(f"File not found: {rules_conf_file}")
except Exception as e:
print(f"An error occurred: {e}")

objects = []

for line in lines_list:
if line != "":
temp_list = line.split('"')
temp_0 = temp_list[0]
temp_0_split = temp_0.split(' ')
temp_dict = {}
temp_dict["status"] = temp_0_split[0]
temp_dict["id"] = temp_0_split[2]
temp_dict["serial"] = temp_list[1]
temp_dict["name"] = temp_list[3]
objects.append(temp_dict)

def change_rule_status(id_to_block, new_status):
with open(rules_conf_file, 'r') as file:
lines = file.readlines()

for i, line in enumerate(lines):
if id_to_block in line:
if "allow" in line:
lines[i] = line.replace("allow", new_status)
elif "block" in line:
lines[i] = line.replace("block", new_status)

with open(rules_conf_file, 'w') as file:
file.writelines(lines)
subprocess.run("pkexec service usbguard restart", shell=True)

def update_status(index, new_status):
update_display()
change_rule_status(objects[index]["id"], new_status)
objects[index]["status"] = new_status
update_display()

title = ctk.CTkLabel(parent_frame, text="USB PORTS MANAGEMENT", font=("Arial", 24), pady=14)
title.pack()
frame = ctk.CTkFrame(parent_frame)
frame.pack()

circle_labels = []

for index, obj in enumerate(objects):
circle = ctk.CTkLabel(frame, text="●", font=("Arial", 24), width=1, anchor="w")
circle.grid(row=index, column=0, padx=10, pady=5)
circle_labels.append(circle)

label = ctk.CTkLabel(frame, font=("Arial", 16), text=f"ID: {obj['id']}, Name: {obj['name']}")
label.grid(row=index, column=1, padx=10, pady=8)

allow_button = ctk.CTkButton(frame, text="Allow", command=lambda i=index: update_status(i, "allow"))
allow_button.grid(row=index, column=2, padx=5, pady=8)

block_button = ctk.CTkButton(frame, text="Block", command=lambda i=index: update_status(i, "block"))
block_button.grid(row=index, column=3, padx=5, pady=8)

def update_display():
for i, obj in enumerate(objects):
if obj["status"] == "block":
circle_labels[i].configure(text_color="red")
else:
circle_labels[i].configure(text_color="green")

update_display()

0 comments on commit f2a72ee

Please sign in to comment.