-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb.py
95 lines (67 loc) · 2.52 KB
/
usb.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
import json
import tkinter as tk
import subprocess
rules_conf_file = '/etc/usbguard/rules.conf'
#rules_conf_file = './sample.txt'
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):
change_rule_status(objects[index]["id"], new_status)
objects[index]["status"] = new_status
update_display()
root = tk.Tk()
root.title("USB")
frame = tk.Frame(root)
frame.pack()
circle_labels = []
for index, obj in enumerate(objects):
circle = tk.Label(frame, text="●", font=("Arial", 12), width=2, anchor="w")
circle.grid(row=index, column=0, padx=10, pady=5)
circle_labels.append(circle)
label = tk.Label(frame, text=f"ID: {obj['id']}, Name: {obj['name']}")
label.grid(row=index, column=1, padx=10, pady=5)
allow_button = tk.Button(frame, text="Allow", command=lambda i=index: update_status(i, "allow"))
allow_button.grid(row=index, column=2, padx=5, pady=5)
block_button = tk.Button(frame, text="Block", command=lambda i=index: update_status(i, "block"))
block_button.grid(row=index, column=3, padx=5, pady=5)
def update_display():
for i, obj in enumerate(objects):
if obj["status"] == "block":
circle_labels[i].config(fg="red")
else:
circle_labels[i].config(fg="green")
update_button = tk.Button(root, text="refresh", command=update_display)
update_button.pack()
update_display()
root.mainloop()