methods optimized

This commit is contained in:
Désiré Werner Menrath 2025-04-28 12:35:19 +02:00
parent 4eb9d6acd4
commit eadc2a06bf
2 changed files with 32 additions and 38 deletions

View File

@ -333,16 +333,11 @@ class FrameWidgets(ttk.Frame):
self.l_box.insert("end", tunnels[:-5])
self.l_box.update()
# Button Vpn
if self.a != "":
self.stop()
wg_read = f"/tmp/tlecdcwg/{self.a}.conf"
with open(wg_read, "r", encoding="utf-8") as file:
data = Tunnel.con_to_dict(file)
# Address Label
self.init_and_report(data)
self.show_data()
data = self.handle_tunnel_data(self.a)
else:
self.start()
@ -350,7 +345,7 @@ class FrameWidgets(ttk.Frame):
self.add = tk.StringVar()
self.DNS = tk.StringVar()
self.enp = tk.StringVar()
self.label_empty()
self.reset_fields()
self.show_data()
# Button Import
@ -413,10 +408,7 @@ class FrameWidgets(ttk.Frame):
self.start()
self.l_box.update()
# Address Label
self.add.set("")
self.DNS.set("")
self.enp.set("")
self.reset_fields()
except IndexError:
@ -591,7 +583,7 @@ class FrameWidgets(ttk.Frame):
new_conf = f"/tmp/tlecdcwg/{path_split}"
if self.a != "":
check_call(["nmcli", "connection", "down", Tunnel.active()])
self.label_empty()
self.reset_fields()
subprocess.check_output(["nmcli", "connection", "import", "type",
"wireguard", "file", new_conf], text=True)
@ -602,7 +594,7 @@ class FrameWidgets(ttk.Frame):
shutil.copy(filepath, "/tmp/tlecdcwg/")
if self.a != "":
check_call(["nmcli", "connection", "down", Tunnel.active()])
self.label_empty()
self.reset_fields()
subprocess.check_output(["nmcli", "connection", "import", "type",
"wireguard", "file", filepath], text=True)
@ -635,15 +627,8 @@ class FrameWidgets(ttk.Frame):
self.str_var.set(self.a)
self.color_label()
self.stop()
wg_read = f"/tmp/tlecdcwg/{self.a}.conf"
with open(wg_read, "r", encoding="utf-8") as file_for_key:
data = Tunnel.con_to_dict(file_for_key)
# Address Label
self.init_and_report(data)
self.show_data()
data = self.handle_tunnel_data(self.a)
check_call(["nmcli", "con", "mod", self.a, "connection.autoconnect", "no"])
Path.chmod(wg_read, 0o600)
if ("PrivateKey = " in read) and ("Endpoint = " in read):
pass
@ -661,6 +646,23 @@ class FrameWidgets(ttk.Frame):
except subprocess.CalledProcessError:
print("Tunnel exist!")
def handle_tunnel_data(self, tunnel_name: str) -> dict[str, str]:
"""_summary_
Args:
tunnel_name (str): name of tunnel
Returns:
Dict[str, str]: dictionary with tunneldata
"""
wg_read = f"/tmp/tlecdcwg/{tunnel_name}.conf"
with open(wg_read, "r", encoding="utf-8") as file:
data = Tunnel.con_to_dict(file)
self.init_and_report(data)
self.show_data()
return data
def box_set(self):
"""
This Method will display the autostart label which
@ -736,13 +738,14 @@ class FrameWidgets(ttk.Frame):
self.enp = tk.StringVar()
self.enp.set(f"{_("Endpoint: ")}{data[2]}")
def label_empty(self):
def reset_fields(self) -> None:
"""
empty labels
reset data from labels
"""
self.add.set("")
self.DNS.set("")
self.enp.set("")
fields = [self.add, self.DNS, self.enp]
for field in fields:
field.set("")
def show_data(self):
"""
@ -814,13 +817,7 @@ class FrameWidgets(ttk.Frame):
self.select_tunnel = self.l_box.curselection()
select_tl = self.l_box.get(self.select_tunnel[0])
check_call(["nmcli", "connection", "up", select_tl])
wg_read = f"/tmp/tlecdcwg/{select_tl}.conf"
with open(wg_read, "r", encoding="utf-8") as file:
data = Tunnel.con_to_dict(file)
# Address Label
self.init_and_report(data)
self.show_data()
data = self.handle_tunnel_data(self.a)
# Button Start/Stop
self.stop()
@ -841,10 +838,7 @@ class FrameWidgets(ttk.Frame):
self.str_var.set("")
self.color_label()
# Address Label
self.add.set("")
self.DNS.set("")
self.enp.set("")
self.reset_fields()
self.show_data()
except IndexError: