From eadc2a06bf39ad7678a94604f2596fed7dcdf08b Mon Sep 17 00:00:00 2001 From: punix Date: Mon, 28 Apr 2025 12:35:19 +0200 Subject: [PATCH] methods optimized --- __pycache__/cls_mth_fc.cpython-312.pyc | Bin 23550 -> 23550 bytes wirepy.py | 70 +++++++++++-------------- 2 files changed, 32 insertions(+), 38 deletions(-) diff --git a/__pycache__/cls_mth_fc.cpython-312.pyc b/__pycache__/cls_mth_fc.cpython-312.pyc index 644a7f841069c11b4007a97d63e98e21a8afb09a..cad7fecc4342ea16b8e912544eb5d3f8f4c5111f 100644 GIT binary patch delta 21 bcmeyjo$=pxMy}Jmyj%=Gurg^Q*OzDjS;Yr? delta 21 bcmeyjo$=pxMy}Jmyj%=GAmFr->q|5MRgVV3 diff --git a/wirepy.py b/wirepy.py index 5f5d600..eb31c51 100755 --- a/wirepy.py +++ b/wirepy.py @@ -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: