line formatted for better reading

This commit is contained in:
2025-04-25 17:37:04 +02:00
parent f9ecd54e0a
commit f6204c9071
7 changed files with 117 additions and 364 deletions

326
wirepy.py
View File

@ -30,9 +30,7 @@ userfile = Path("/tmp/.loguser")
# 1 = 1. Year, 09 = Month of the Year, 2924 = Day and Year of the Year
VERSION = "v. 2.04.1725"
res = GiteaUpdate.api_down(
"https://git.ilunix.de/api/v1/repos/punix/Wire-Py/releases", VERSION, set_file
)
res = GiteaUpdate.api_down("https://git.ilunix.de/api/v1/repos/punix/Wire-Py/releases", VERSION, set_file)
# Translate
APP = "wirepy"
@ -60,9 +58,7 @@ class Wirepy(tk.Tk):
self.monitor_center_y = int(self.winfo_screenheight() / 2 - (self.y_height / 2))
self.resizable(width=False, height=False)
self.title("Wire-Py")
self.geometry(
f"{self.x_width}x{self.y_height}+{self.monitor_center_x}+{self.monitor_center_y}"
)
self.geometry(f"{self.x_width}x{self.y_height}+{self.monitor_center_x}+{self.monitor_center_y}")
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
@ -98,22 +94,14 @@ class FrameWidgets(ttk.Frame):
self.dns = None
self.address = None
self.auto_con = None
self.wg_vpn_start = tk.PhotoImage(
file=r"/usr/share/icons/lx-icons/48/wg_vpn-start.png"
)
self.wg_vpn_stop = tk.PhotoImage(
file=r"/usr/share/icons/lx-icons/48/wg_vpn-stop.png"
)
self.wg_vpn_start = tk.PhotoImage(file=r"/usr/share/icons/lx-icons/48/wg_vpn-start.png")
self.wg_vpn_stop = tk.PhotoImage(file=r"/usr/share/icons/lx-icons/48/wg_vpn-stop.png")
self.imp_pic = tk.PhotoImage(file=r"/usr/share/icons/lx-icons/48/wg_import.png")
self.tr_pic = tk.PhotoImage(file=r"/usr/share/icons/lx-icons/48/wg_trash.png")
self.exp_pic = tk.PhotoImage(file=r"/usr/share/icons/lx-icons/48/wg_export.png")
self.warning_pic = tk.PhotoImage(file=r"/usr/share/icons/lx-icons/64/error.png")
self.wg_vpn_start = tk.PhotoImage(
file=r"/usr/share/icons/lx-icons/48/wg_vpn-start.png"
)
self.wg_vpn_stop = tk.PhotoImage(
file=r"/usr/share/icons/lx-icons/48/wg_vpn-stop.png"
)
self.wg_vpn_start = tk.PhotoImage(file=r"/usr/share/icons/lx-icons/48/wg_vpn-start.png")
self.wg_vpn_stop = tk.PhotoImage(file=r"/usr/share/icons/lx-icons/48/wg_vpn-stop.png")
def update():
"""
@ -185,12 +173,10 @@ class FrameWidgets(ttk.Frame):
iw = r"/usr/share/icons/lx-icons/48/wg_vpn.png"
ii = r"/usr/share/icons/lx-icons/48/wg_msg.png"
wt = _("Info")
msg_t = _(
"Wire-Py a simple Wireguard Gui for Linux systems.\n\n"
"Wire-Py is open source software written in Python.\n\n"
"Email: polunga40@unity-mail.de also likes for donation.\n\n"
"Use without warranty!\n"
)
msg_t = _("Wire-Py a simple Wireguard Gui for Linux systems.\n\n"
"Wire-Py is open source software written in Python.\n\n"
"Email: polunga40@unity-mail.de also likes for donation.\n\n"
"Use without warranty!\n")
txt2 = _("Go to Wire-Py git")
com = link_btn
@ -217,12 +203,8 @@ class FrameWidgets(ttk.Frame):
set_tip = tk.BooleanVar()
self.settings = tk.Menu(self, relief="flat")
self.options_btn.configure(menu=self.settings, style="Toolbutton")
self.settings.add_checkbutton(
label=_("Disable Updates"), command=update, variable=set_update
)
self.settings.add_checkbutton(
label=_("Disable Tooltips"), command=tooltip, variable=set_tip
)
self.settings.add_checkbutton(label=_("Disable Updates"), command=update, variable=set_update)
self.settings.add_checkbutton(label=_("Disable Tooltips"), command=tooltip, variable=set_tip)
self.settings.add_command(label=_("Light"), command=theme_change_light)
self.settings.add_command(label=_("Dark"), command=theme_change_dark)
@ -272,13 +254,9 @@ class FrameWidgets(ttk.Frame):
self.update_btn.configure(menu=self.download, style="Toolbutton")
self.download.add_command(
label=_("Download"),
command=lambda: GiteaUpdate.download(
f"https://git.ilunix.de/punix/Wire-Py/archive/{res}.zip",
r"/usr/share/icons/lx-icons/48/wg_vpn.png",
r"/usr/share/icons/lx-icons/48/wg_msg.png",
res,
),
)
command=lambda: GiteaUpdate.download(f"https://git.ilunix.de/punix/Wire-Py/archive/{res}.zip",
r"/usr/share/icons/lx-icons/48/wg_vpn.png",
r"/usr/share/icons/lx-icons/48/wg_msg.png", res))
# Show active Tunnel
self.a = Tunnel.active()
@ -301,8 +279,7 @@ class FrameWidgets(ttk.Frame):
# Bottom Frame 4
self.lb_frame3 = ttk.Frame(self)
self.lb_frame3.configure(relief="flat")
self.lb_frame3.grid(column=0, row=5, columnspan=4, sticky="snew",
padx=2, pady=2)
self.lb_frame3.grid(column=0, row=5, columnspan=4, sticky="snew", padx=2, pady=2)
# Bottom Frame 5
self.lb_frame4 = ttk.Frame(self)
@ -346,9 +323,7 @@ class FrameWidgets(ttk.Frame):
self.l_box.grid(column=1, rowspan=4, row=0, sticky="ns")
self.l_box.event_add("<<ClickEvent>>", "<Button-1>")
self.l_box.bind("<<ClickEvent>>", enable_check_box)
self.scrollbar = ttk.Scrollbar(
self.lb_frame_btn_lbox, orient="vertical", command=self.l_box.yview
)
self.scrollbar = ttk.Scrollbar(self.lb_frame_btn_lbox, orient="vertical", command=self.l_box.yview)
self.scrollbar.grid(column=1, rowspan=4, row=0, sticky="nse")
self.l_box.configure(yscrollcommand=self.scrollbar.set)
@ -379,8 +354,7 @@ class FrameWidgets(ttk.Frame):
self.show_data()
# Button Import
self.btn_i = ttk.Button(self.lb_frame_btn_lbox, image=self.imp_pic,
command=self.import_sl, padding=0)
self.btn_i = ttk.Button(self.lb_frame_btn_lbox, image=self.imp_pic, command=self.import_sl, padding=0)
self.btn_i.grid(column=0, row=1, padx=15, pady=8)
Tooltip(self.btn_i, _("Click to import a Wireguard Tunnel"), tips)
@ -392,8 +366,7 @@ class FrameWidgets(ttk.Frame):
try:
self.select_tunnel = self.l_box.curselection()
select_tl = self.l_box.get(self.select_tunnel[0])
with open(f"/tmp/tlecdcwg/{select_tl}.conf", "r+", encoding="utf-8"
) as file2:
with open(f"/tmp/tlecdcwg/{select_tl}.conf", "r+", encoding="utf-8") as file2:
key = Tunnel.con_to_dict(file2)
pre_key = key[3]
check_call(["nmcli", "connection", "delete", select_tl])
@ -427,7 +400,8 @@ class FrameWidgets(ttk.Frame):
if self.l_box.size() == 0:
self.wg_autostart.configure(state="disabled")
self.lb_rename.configure(state="disabled")
Tooltip(self.wg_autostart, _("You must have at least one\ntunnel in the list,to use the autostart"), tips)
Tooltip(self.wg_autostart, _("You must have at least one\ntunnel in the list,to use the autostart")
, tips)
Tooltip(self.btn_exp, _("No Tunnels in List for Export"), tips)
Tooltip(self.btn_stst, _("No tunnels to start in the list"), tips)
@ -465,36 +439,23 @@ class FrameWidgets(ttk.Frame):
LxTools.msg_window(iw, ii, wt, msg_t)
# Button Trash
self.btn_tr = ttk.Button(
self.lb_frame_btn_lbox,
image=self.tr_pic,
command=delete,
padding=0,
style="CButton.TButton",
)
self.btn_tr = ttk.Button(self.lb_frame_btn_lbox, image=self.tr_pic, command=delete, padding=0,
style="CButton.TButton")
self.btn_tr.grid(column=0, row=2, padx=15, pady=8)
if self.l_box.size() == 0:
Tooltip(self.btn_tr, _("No tunnels to delete in the list"), tips)
else:
Tooltip(self.btn_tr, _("Click to delete a Wireguard Tunnel\nSelect from the list!"),
tips,
)
Tooltip(self.btn_tr, _("Click to delete a Wireguard Tunnel\nSelect from the list!"), tips)
# Button Export
self.btn_exp = ttk.Button(
self.lb_frame_btn_lbox, image=self.exp_pic, command=Tunnel.export, padding=0
)
self.btn_exp = ttk.Button(self.lb_frame_btn_lbox, image=self.exp_pic, command=Tunnel.export, padding=0)
self.btn_exp.grid(column=0, row=3, padx=15, pady=8)
if self.l_box.size() == 0:
Tooltip(self.btn_exp, _("No Tunnels in List for Export"), tips)
else:
Tooltip(
self.btn_exp,
_("Click to export all\nWireguard Tunnel to Zipfile"),
tips,
)
Tooltip(self.btn_exp, _("Click to export all\nWireguard Tunnel to Zipfile"), tips)
# Label Entry
self.lb_rename = ttk.Entry(self.lb_frame4, width=20)
@ -503,17 +464,9 @@ class FrameWidgets(ttk.Frame):
self.lb_rename.config(state="disable")
if self.l_box.size() != 0:
Tooltip(
self.lb_rename,
_("To rename a tunnel, you need to\nselect a tunnel from the list"),
tips,
)
Tooltip(self.lb_rename, _("To rename a tunnel, you need to\nselect a tunnel from the list"), tips)
else:
Tooltip(
self.lb_rename,
_("To rename a tunnel, at least one must be in the list"),
tips,
)
Tooltip(self.lb_rename, _("To rename a tunnel, at least one must be in the list"), tips)
def tl_rename():
@ -543,9 +496,7 @@ class FrameWidgets(ttk.Frame):
iw = r"/usr/share/icons/lx-icons/64/info.png"
ii = r"/usr/share/icons/lx-icons/48/wg_msg.png"
wt = _("Renaming not possible")
msg_t = _(
"No valid sign. These must not be used.\nBlank, Slash, Backslash and { }\n"
)
msg_t = _("No valid sign. These must not be used.\nBlank, Slash, Backslash and { }\n")
LxTools.msg_window(iw, ii, wt, msg_t)
else:
@ -555,16 +506,7 @@ class FrameWidgets(ttk.Frame):
select_tl = self.l_box.get(self.select_tunnel[0])
# nmcli connection modify old connection.id iphone
check_call(
[
"nmcli",
"connection",
"modify",
select_tl,
"connection.id",
self.lb_rename.get(),
]
)
check_call(["nmcli", "connection", "modify", select_tl, "connection.id", self.lb_rename.get()])
source = Path(f"/tmp/tlecdcwg/{select_tl}.conf")
destination = source.with_name(f"{self.lb_rename.get()}.conf")
source.replace(destination)
@ -580,10 +522,7 @@ class FrameWidgets(ttk.Frame):
self.str_var.set(value=self.a)
with open(set_file, "r", encoding="utf-8") as set_f5:
lines5 = set_f5.readlines()
if (
select_tl == lines5[7].strip()
and "off\n" not in lines5[7].strip()
):
if select_tl == lines5[7].strip() and "off\n" not in lines5[7].strip():
lines5[7] = new_a_connect
with open(set_file, "w", encoding="utf-8") as theme_set5:
theme_set5.writelines(lines5)
@ -601,14 +540,8 @@ class FrameWidgets(ttk.Frame):
LxTools.msg_window(iw, ii, wt, msg_t)
# Button Rename
self.btn_rename = ttk.Button(
self.lb_frame4,
text=_("Rename"),
state="disable",
command=tl_rename,
padding=4,
style="RnButton.TButton",
)
self.btn_rename = ttk.Button(self.lb_frame4, text=_("Rename"), state="disable", command=tl_rename, padding=4,
style="RnButton.TButton")
self.btn_rename.grid(column=3, row=0, padx=5, pady=10, sticky="ne")
# Check Buttons
@ -617,40 +550,22 @@ class FrameWidgets(ttk.Frame):
self.autoconnect_var.set(self.auto_con)
# Frame for Labels, Entry and Button
self.autoconnect = ttk.Label(
self.lb_frame3, textvariable=self.autoconnect_var, width=15
)
self.autoconnect = ttk.Label(self.lb_frame3, textvariable=self.autoconnect_var, width=15)
self.autoconnect.config(font=("Ubuntu", 11))
self.autoconnect.grid(column=1, row=0, sticky="e", pady=19)
self.wg_autostart = ttk.Checkbutton(
self.lb_frame3,
text=_("Autoconnect on:"),
variable=self.selected_option,
command=self.box_set,
)
self.wg_autostart = ttk.Checkbutton(self.lb_frame3, text=_("Autoconnect on:"), variable=self.selected_option,
command=self.box_set)
self.wg_autostart.grid(column=0, row=0, pady=15, padx=15, sticky="nw")
if self.l_box.size() >= 1 and len(self.l_box.curselection()) >= 1:
Tooltip(
self.wg_autostart, _("To use the autostart, enable this Checkbox"), tips
)
Tooltip(self.wg_autostart, _("To use the autostart, enable this Checkbox"), tips)
if self.l_box.size() == 0:
Tooltip(
self.wg_autostart,
_(
"You must have at least one\ntunnel in the list,to use the autostart"
),
tips,
)
Tooltip(self.wg_autostart, _("You must have at least one\ntunnel in the list,to use the autostart"), tips)
else:
Tooltip(
self.wg_autostart,
_("To use the autostart, a tunnel must be selected from the list"),
tips,
)
Tooltip(self.wg_autostart, _("To use the autostart, a tunnel must be selected from the list"), tips)
self.on_off()
@ -672,11 +587,8 @@ class FrameWidgets(ttk.Frame):
Create.dir_and_files()
try:
filepath = filedialog.askopenfilename(
initialdir=f"{Path.home()}",
title=_("Select Wireguard config File"),
filetypes=[(_("WG config files"), "*.conf")],
)
filepath = filedialog.askopenfilename(initialdir=f"{Path.home()}", title=_("Select Wireguard config File"),
filetypes=[(_("WG config files"), "*.conf")])
with open(filepath, "r", encoding="utf-8") as file:
read = file.read()
@ -684,11 +596,7 @@ class FrameWidgets(ttk.Frame):
path_split1 = path_split[-1]
self.a = Tunnel.active()
if (
"PrivateKey = " in read
and "PublicKey = " in read
and "Endpoint =" in read
):
if "PrivateKey = " in read and "PublicKey = " in read and "Endpoint =" in read:
with open(filepath, "r", encoding="utf-8") as file:
key = Tunnel.con_to_dict(file)
pre_key = key[3]
@ -701,9 +609,7 @@ class FrameWidgets(ttk.Frame):
iw = r"/usr/share/icons/lx-icons/64/error.png"
ii = r"/usr/share/icons/lx-icons/48/wg_msg.png"
wt = _("Import Error")
msg_t = _(
"Tunnel already available!\nPlease use another file for import"
)
msg_t = _("Tunnel already available!\nPlease use another file for import")
LxTools.msg_window(iw, ii, wt, msg_t)
else:
@ -712,64 +618,26 @@ class FrameWidgets(ttk.Frame):
keyfile.write(f"{pre_key}\r")
if len(path_split1) > 17:
p1 = shutil.copy(filepath, "/tmp/tlecdcwg/")
path_split = path_split1[
len(path_split1) - 17:
]
os.rename(
p1, f"/tmp/tlecdcwg/{path_split}"
)
path_split = path_split1[len(path_split1) - 17:]
os.rename(p1, f"/tmp/tlecdcwg/{path_split}")
new_conf = f"/tmp/tlecdcwg/{path_split}"
if self.a != "":
check_call(
[
"nmcli",
"connection",
"down",
Tunnel.active(),
]
)
check_call(["nmcli", "connection", "down", Tunnel.active()])
self.label_empty()
subprocess.check_output(
[
"nmcli",
"connection",
"import",
"type",
"wireguard",
"file",
new_conf,
],
text=True,
)
subprocess.check_output(["nmcli", "connection", "import", "type",
"wireguard", "file", new_conf], text=True)
Create.encrypt()
else:
shutil.copy(filepath, "/tmp/tlecdcwg/")
if self.a != "":
check_call(
[
"nmcli",
"connection",
"down",
Tunnel.active(),
]
)
check_call(["nmcli", "connection", "down", Tunnel.active()])
self.label_empty()
subprocess.check_output(
[
"nmcli",
"connection",
"import",
"type",
"wireguard",
"file",
filepath,
],
text=True,
)
subprocess.check_output(["nmcli", "connection", "import", "type",
"wireguard", "file", filepath], text=True)
Create.encrypt()
@ -781,40 +649,18 @@ class FrameWidgets(ttk.Frame):
self.l_box.update()
self.l_box.selection_set(0)
Tooltip(
self.wg_autostart,
_("To use the autostart, enable this Checkbox"),
tips,
)
Tooltip(self.wg_autostart, _("To use the autostart, enable this Checkbox"), tips)
# Tooltip(self.l_box, _("List of available tunnels"))
Tooltip(
self.btn_tr,
_(
"Click to delete a Wireguard Tunnel\n"
"Select from the list!"
),
tips,
)
Tooltip(self.btn_tr, _("Click to delete a Wireguard Tunnel\nSelect from the list!")
, tips,)
Tooltip(
self.btn_exp,
_(
" Click to export all\n"
"Wireguard Tunnel to Zipfile"
),
tips,
)
Tooltip(self.btn_exp, _(" Click to export all\nWireguard Tunnel to Zipfile")
, tips)
Tooltip(
self.btn_rename,
_(
"To rename a tunnel, you need to\n"
"select a tunnel from the list"
),
tips,
)
Tooltip(self.btn_rename, _("To rename a tunnel, you need to\nselect a tunnel from"
" the list"), tips)
self.lb_rename.insert(0, "Max. 12 characters!")
self.str_var = tk.StringVar()
@ -822,24 +668,13 @@ class FrameWidgets(ttk.Frame):
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:
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()
check_call(
[
"nmcli",
"con",
"mod",
self.a,
"connection.autoconnect",
"no",
]
)
check_call(["nmcli", "con", "mod", self.a, "connection.autoconnect", "no"])
Path.chmod(wg_read, 0o600)
if ("PrivateKey = " in read) and ("Endpoint = " in read):
@ -850,9 +685,7 @@ class FrameWidgets(ttk.Frame):
iw = r"/usr/share/icons/lx-icons/64/error.png"
ii = r"/usr/share/icons/lx-icons/48/wg_msg.png"
wt = _("Import Error")
msg_t = _(
"Oh... no valid Wireguard File!\nPlease select a valid Wireguard File"
)
msg_t = _("Oh... no valid Wireguard File!\nPlease select a valid Wireguard File")
LxTools.msg_window(iw, ii, wt, msg_t)
except EOFError as e:
@ -921,12 +754,7 @@ class FrameWidgets(ttk.Frame):
self.autoconnect_var = tk.StringVar()
self.autoconnect_var.set(self.auto_con)
self.autoconnect = ttk.Label(
self.lb_frame3,
textvariable=self.autoconnect_var,
foreground="#0071ff",
width=15,
)
self.autoconnect = ttk.Label(self.lb_frame3, textvariable=self.autoconnect_var, foreground="#0071ff", width=15)
self.autoconnect.config(font=("Ubuntu", 11))
self.autoconnect.grid(column=1, row=0, sticky="e", pady=19)
@ -958,9 +786,7 @@ class FrameWidgets(ttk.Frame):
"""
# Address Label
self.address = ttk.Label(
self.lb_frame, textvariable=self.add, foreground="#0071ff"
)
self.address = ttk.Label(self.lb_frame, textvariable=self.add, foreground="#0071ff")
self.address.grid(column=0, row=5, sticky="w", padx=10, pady=6)
self.address.config(font=("Ubuntu", 9))
@ -970,9 +796,7 @@ class FrameWidgets(ttk.Frame):
self.dns.config(font=("Ubuntu", 9))
# Endpoint Label
self.endpoint = ttk.Label(
self.lb_frame2, textvariable=self.enp, foreground="#0071ff"
)
self.endpoint = ttk.Label(self.lb_frame2, textvariable=self.enp, foreground="#0071ff")
self.endpoint.grid(column=0, row=8, sticky="w", padx=10, pady=20)
self.endpoint.config(font=("Ubuntu", 9))
@ -980,12 +804,7 @@ class FrameWidgets(ttk.Frame):
"""
Stop Button
"""
self.btn_stst = ttk.Button(
self.lb_frame_btn_lbox,
image=self.wg_vpn_stop,
command=self.wg_switch,
padding=0,
)
self.btn_stst = ttk.Button(self.lb_frame_btn_lbox, image=self.wg_vpn_stop, command=self.wg_switch, padding=0)
self.btn_stst.grid(column=0, row=0, padx=5, pady=8)
Tooltip(self.btn_stst, _("Click to stop selected Wireguard Tunnel"), tips)
@ -994,12 +813,7 @@ class FrameWidgets(ttk.Frame):
"""
Start Button
"""
self.btn_stst = ttk.Button(
self.lb_frame_btn_lbox,
image=self.wg_vpn_start,
command=self.wg_switch,
padding=0,
)
self.btn_stst = ttk.Button(self.lb_frame_btn_lbox, image=self.wg_vpn_start, command=self.wg_switch, padding=0)
self.btn_stst.grid(column=0, row=0, padx=5, pady=8)
tl = Tunnel.list()
@ -1016,14 +830,10 @@ class FrameWidgets(ttk.Frame):
with open(set_file, "r", encoding="utf-8") as read_file:
lines = read_file.readlines()
if "light\n" in lines:
self.lb_tunnel = ttk.Label(
self, textvariable=self.str_var, foreground="green"
)
self.lb_tunnel = ttk.Label(self, textvariable=self.str_var, foreground="green")
else:
self.lb_tunnel = ttk.Label(
self, textvariable=self.str_var, foreground="yellow"
)
self.lb_tunnel = ttk.Label(self, textvariable=self.str_var, foreground="yellow")
self.lb_tunnel.config(font=("Ubuntu", 11, "bold"))
self.lb_tunnel.grid(column=2, padx=10, row=1)