remove classes and add methods to class FrameWidgets (removed self errors)

This commit is contained in:
Désiré Werner Menrath 2024-11-07 18:04:58 +01:00
parent 414a36b436
commit 0a5341fab7
2 changed files with 358 additions and 416 deletions

View File

@ -122,34 +122,9 @@ def msg_window(img_w, img_i, w_title, w_txt, txt2=None, com=None):
class Tunnel:
"""
Show the active tunnel in green or yellow in the label
Class of Methods for Wire-Py
"""
def __init__(self):
self.StrVar = None
self.lb_tunnel = None
self.lb_frame2 = None
self.lb_frame = None
self.endpoint = None
self.dns = None
self.address = None
self.enp = None
self.DNS = None
self.add = None
def color(self):
with open(path_to_file2, 'r') as read_file:
lines = read_file.readlines()
if 'light\n' in lines:
self.lb_tunnel = ttk.Label(self, textvariable=self.StrVar, foreground='green')
else:
self.lb_tunnel = ttk.Label(self, textvariable=self.StrVar, foreground='yellow')
self.lb_tunnel.config(font=('Ubuntu', 11, 'bold'))
self.lb_tunnel.grid(column=2, padx=10, row=1)
"""
The config file is packed into a dictionary,
to display the values Address , DNS and Peer in the labels
@ -212,44 +187,6 @@ class Tunnel:
return active
"""
Displays the value address, DNS and peer in the labels
or empty it again
"""
def init_and_report(self, data=None):
""" Address Label """
self.add = tk.StringVar()
self.add.set('Address: ' + data[0])
self.DNS = tk.StringVar()
self.DNS.set(' DNS: ' + data[1])
self.enp = tk.StringVar()
self.enp.set('Endpoint: ' + data[2])
def label_empty(self):
self.add.set('')
self.DNS.set('')
self.enp.set('')
def show_data(self):
""" Address Label """
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))
''' DNS Label '''
self.dns = ttk.Label(self.lb_frame, textvariable=self.DNS, foreground='#0071ff')
self.dns.grid(column=0, row=7, sticky='w', padx=10, pady=6)
self.dns.config(font=('Ubuntu', 9))
''' Endpoint Label '''
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))
"""
Shows all existing Wireguard tunnels
"""
@ -329,87 +266,5 @@ class Tunnel:
tips = Tunnel.if_tip()
class FileHandle:
"""
This class will display the autostart label which
Tunnel is automatically started regardless of the active tunnel.
The selected tunnel is written into a file to read it after the start of the system.
"""
def __init__(self):
self.wg_autostart = None
self.autoconnect = None
self.auto_con = None
self.autoconnect_var = None
self.tl = None
self.selected_option = None
self.l_box = None
def box_set(self):
try:
select_tunnel = self.l_box.curselection()
select_tl = self.l_box.get(select_tunnel[0])
if self.selected_option.get() == 0:
with open(path_to_file2, 'r') as set_file3:
lines3 = set_file3.readlines()
lines3[7] = 'off'
with open(path_to_file2, 'w') as set_file3:
set_file3.writelines(lines3)
tl = Tunnel.list()
if len(tl) == 0:
self.wg_autostart.configure(state='disabled')
if self.selected_option.get() >= 1:
with open(path_to_file2, 'r') as set_file3:
lines3 = set_file3.readlines()
lines3[7] = select_tl
with open(path_to_file2, 'w') as set_file3:
set_file3.writelines(lines3)
except IndexError:
self.selected_option.set(1)
OnOff.on_off(self)
class OnOff:
"""
Here it is checked whether the path to the file is there if not it is created.
Set (on), the selected tunnel is displayed in the label.
At (off) the label is first emptied then filled with No Autoconnect
"""
def __init__(self):
self.wg_autostart = None
self.selected_option = None
self.auto_con = None
self.autoconnect = None
self.autoconnect_var = None
self.lb_frame_buttons = None
def on_off(self):
with open(path_to_file2, 'r') as set_file4:
lines4 = set_file4.readlines()
if lines4[7] != 'off':
self.selected_option.set(1)
self.autoconnect_var.set('')
self.auto_con = lines4[7]
else:
self.wg_autostart.configure(state='disabled')
self.auto_con = 'no Autoconnect'
self.autoconnect_var.set('')
self.autoconnect_var = tk.StringVar()
self.autoconnect_var.set(self.auto_con)
self.autoconnect = ttk.Label(self, textvariable=self.autoconnect_var, foreground='#0071ff')
self.autoconnect.config(font=('Ubuntu', 11))
self.autoconnect.grid(column=0, row=4, sticky='ne', pady=19)

View File

@ -8,7 +8,7 @@ from subprocess import check_call
from tkinter import *
from tkinter import filedialog, ttk
from wg_func import (Tunnel, FileHandle, OnOff, msg_window, WirePyUpdate, res, _u, version, path_to_file2, tips)
from wg_func import (Tunnel, msg_window, WirePyUpdate, res, _u, version, path_to_file2, tips)
tcl_path = Path('/usr/share/TK-Themes')
@ -45,6 +45,7 @@ class MainWindow(tk.Tk):
self.iconphoto(True, self.wg_icon)
''' Set on or off in file '''
def update():
if set_update.get() == 1:
with open(path_to_file2, 'r') as set_file2:
@ -61,6 +62,7 @@ class MainWindow(tk.Tk):
set_file2.writelines(lines2)
''' Set on or off in file '''
def tooltip():
if set_tip.get():
with open(path_to_file2, 'r') as set_file2:
@ -77,6 +79,7 @@ class MainWindow(tk.Tk):
set_file2.writelines(lines2)
''' Set dark or light '''
def theme_change_light():
if self.tk.call("ttk::style", "theme", "use") == "water-dark":
''' Set light theme '''
@ -241,256 +244,13 @@ class MainWindow(tk.Tk):
FrameWidgets(self).grid()
class StSt:
"""
Show Start and Stop Button in Label
"""
def __init__(self):
self.lb_frame_btn_lbox = None
self.wg_switch = None
self.btn_stst = None
self.wg_vpn_start = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_vpn-start.png')
self.wg_vpn_stop = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_vpn-stop.png')
def stop(self):
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)
def stop_enter(event):
""" The mouse moves into the entry widget """
window.my_tool_tip = MyToolTip(event.x_root, event.y_root,
'Click to stop selected Wireguard Tunnel')
def stop_leave(_):
""" The mouse moves from the entry widget """
''' Remove Tool-Tip '''
window.my_tool_tip.destroy()
self.btn_stst.bind('<Enter>', stop_enter)
self.btn_stst.bind('<Leave>', stop_leave)
def start(self):
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)
def empty_list_start_enter(event):
""" The mouse moves into the entry widget """
window.my_tool_tip = MyToolTip(event.x_root, event.y_root,
'No tunnels to start in the list')
def empty_list_start_leave(_):
""" The mouse moves from the entry widget """
''' Remove Tool-Tip '''
window.my_tool_tip.destroy()
def start_enter(event):
""" The mouse moves into the entry widget """
window.my_tool_tip = MyToolTip(event.x_root, event.y_root,
'Click to start selected Wireguard Tunnel')
def start_leave(_):
""" The mouse moves from the entry widget """
''' Remove Tool-Tip '''
window.my_tool_tip.destroy()
tl = Tunnel.list()
if len(tl) == 0:
self.btn_stst.bind('<Enter>', empty_list_start_enter)
self.btn_stst.bind('<Leave>', empty_list_start_leave)
else:
self.btn_stst.bind('<Enter>', start_enter)
self.btn_stst.bind('<Leave>', start_leave)
class TLImp:
"""
Import Class for Wireguard config Files.
Before importing, it is checked whether PrivateKey and PublicKey are in the file.
If True then it is checked whether the PreSharedKey is already in the key file
to avoid an import error so that no double wgconf are imported.
Thus, tunnels can be renamed without the problems arise. If False then the key is written into the file.
Furthermore, it is checked whether the name is longer than 12 characters.
If True then the name is automatically shortened to 12 characters and then imported.
If in each case false comes out, a corresponding window comes to inform the user that something is wrong.
"""
def __init__(self):
self.lb_rename = None
self.btn_tr = None
self.btn_exp = None
self.wg_autostart = None
self.select_tunnel = None
self.wg_switch = None
self.btn_stst = None
self.lb_tunnel = None
self.StrVar = None
self.a = None
self.l_box = None
def import_sl(self):
try:
filepath = filedialog.askopenfilename(initialdir=str(_u), title='Select Wireguard config File',
filetypes=[('WG config files', '*.conf')], )
with open(filepath, 'r') as file:
read = file.read()
path_split = filepath.split('/')
path_split1 = path_split[-1]
self.a = Tunnel.active()
if 'PrivateKey = ' in read and 'PublicKey = ' in read and 'Endpoint =' in read:
with open(filepath, 'r') as file:
key = Tunnel.con_to_dict(file)
pre_key = key[3]
if len(pre_key) != 0:
with open('/etc/wire_py/.keys', 'r') as readfile:
p_key = readfile.readlines()
if pre_key in p_key or pre_key + '\n' in p_key:
"""img_w, img_i, w_title, w_txt hand over"""
iw = r'/usr/share/icons/wp-icons/64/error.png'
ii = r'/usr/share/icons/wp-icons/48/wg_msg.png'
wt = 'Import Error'
msg_t = 'Tunnel already available!\nPlease use another file for import'
msg_window(iw, ii, wt, msg_t)
else:
with open('/etc/wire_py/.keys', 'a') as keyfile:
keyfile.write(pre_key + '\r')
if len(path_split1) > 17:
p1 = shutil.copy(filepath, Path('/etc/wire_py/'))
path_split = path_split1[len(path_split1) - 17:]
os.rename(p1, Path('/etc/wire_py') / str(path_split))
new_conf = '/etc/wire_py/' + path_split
if self.a != '':
check_call(['nmcli', 'connection', 'down', Tunnel.active()])
Tunnel.label_empty(self)
subprocess.check_output(['nmcli', 'connection', 'import', 'type',
'wireguard', 'file', new_conf], text=True)
else:
shutil.copy(filepath, Path('/etc/wire_py/'))
if self.a != '':
check_call(['nmcli', 'connection', 'down', Tunnel.active()])
Tunnel.label_empty(self)
subprocess.check_output(['nmcli', 'connection', 'import', 'type',
'wireguard', 'file', filepath], text=True)
self.StrVar.set('')
self.a = Tunnel.active()
self.l_box.insert(0, self.a)
self.wg_autostart.configure(state='normal')
self.l_box.selection_clear(0, tk.END)
self.l_box.update()
self.l_box.selection_set(0)
def chk_a_enter(event):
""" The mouse moves into the entry widget """
window.my_tool_tip = MyToolTip(event.x_root, event.y_root,
'To use the autostart, enable this '
'Checkbox')
def chk_a_leave(_):
""" The mouse moves from the entry widget """
''' Remove Tool-Tip '''
window.my_tool_tip.destroy()
def list_info_enter(event):
""" The mouse moves into the entry widget """
window.my_tool_tip = MyToolTip(event.x_root, event.y_root,
'List of available tunnels')
def list_info_leave(_):
""" The mouse moves from the entry widget """
''' Remove Tool-Tip '''
window.my_tool_tip.destroy()
def del_enter(event):
""" The mouse moves into the entry widget """
window.my_tool_tip = MyToolTip(event.x_root, event.y_root,
'Click to delete a Wireguard '
'Tunnel\nSelect from the list!')
def del_leave(_):
""" The mouse moves from the entry widget """
''' Remove Tool-Tip '''
window.my_tool_tip.destroy()
def exp_enter(event):
""" The mouse moves into the entry widget """
window.my_tool_tip = MyToolTip(event.x_root, event.y_root,
' Click to export '
'all\nWireguard Tunnel to Zipfile')
def exp_leave(_):
""" The mouse moves from the entry widget """
''' Remove Tool-Tip '''
window.my_tool_tip.destroy()
def rename_enter(event):
""" The mouse moves into the entry widget """
window.my_tool_tip = MyToolTip(event.x_root, event.y_root,
'To rename a tunnel, you need to\n'
'select a tunnel from the list')
def rename_leave(_):
""" The mouse moves from the entry widget """
''' Remove Tool-Tip '''
window.my_tool_tip.destroy()
self.wg_autostart.bind('<Enter>', chk_a_enter)
self.wg_autostart.bind('<Leave>', chk_a_leave)
self.l_box.bind('<Enter>', list_info_enter)
self.l_box.bind('<Leave>', list_info_leave)
self.btn_tr.bind('<Enter>', del_enter)
self.btn_tr.bind('<Leave>', del_leave)
self.btn_exp.bind('<Enter>', exp_enter)
self.btn_exp.bind('<Leave>', exp_leave)
self.lb_rename.bind('<Enter>', rename_enter)
self.lb_rename.bind('<Leave>', rename_leave)
self.lb_rename.insert(0, 'Max. 12 characters!')
self.StrVar = tk.StringVar()
self.StrVar.set(self.a)
Tunnel.color(self)
StSt.stop(self)
wg_read = Path('/etc/wire_py') / str(self.a + '.conf')
with open(wg_read, 'r') as file_for_key:
data = Tunnel.con_to_dict(file_for_key)
''' Address Label '''
Tunnel.init_and_report(self, data)
Tunnel.show_data(self)
check_call(['nmcli', 'con', 'mod', self.a, 'connection.autoconnect', 'no'])
Path.chmod(wg_read, 0o600)
if 'PrivateKey = ' and 'Endpoint = ' not in read:
"""img_w, img_i, w_title, w_txt hand over"""
iw = r'/usr/share/icons/wp-icons/64/error.png'
ii = r'/usr/share/icons/wp-icons/48/wg_msg.png'
wt = 'Import Error'
msg_t = 'Oh... no valid Wireguard File!\nPlease select a valid Wireguard File'
msg_window(iw, ii, wt, msg_t)
except EOFError:
pass
except TypeError:
pass
except FileNotFoundError:
pass
except subprocess.CalledProcessError:
print('Tunnel exist!')
class FrameWidgets(ttk.Frame):
def __init__(self, container, **kwargs):
super().__init__(container, **kwargs)
self.endpoint = None
self.dns = None
self.address = None
self.btn_stst = None
self.auto_con = None
self.enp = None
@ -506,6 +266,8 @@ class FrameWidgets(ttk.Frame):
self.tr_pic = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_trash.png')
self.exp_pic = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_export.png')
self.warning_pic = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/64/error.png')
self.wg_vpn_start = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_vpn-start.png')
self.wg_vpn_stop = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_vpn-stop.png')
''' Show active Tunnel '''
self.a = Tunnel.active()
@ -541,7 +303,7 @@ class FrameWidgets(ttk.Frame):
''' Label to Show active Tunnel '''
self.StrVar = tk.StringVar(value=self.a)
Tunnel.color(self)
self.color_label()
''' Interface Label '''
self.interface = ttk.Label(self.lb_frame, text='Interface')
@ -607,27 +369,27 @@ class FrameWidgets(ttk.Frame):
''' Button Vpn '''
if self.a != '':
StSt.stop(self)
self.stop()
wg_read = Path('/etc/wire_py') / str(self.a + '.conf')
with open(wg_read, 'r') as file:
data = Tunnel.con_to_dict(file)
''' Address Label '''
Tunnel.init_and_report(self, data)
Tunnel.show_data(self)
self.init_and_report(data)
self.show_data()
else:
StSt.start(self)
self.start()
''' Address Label '''
self.add = tk.StringVar()
self.DNS = tk.StringVar()
self.enp = tk.StringVar()
Tunnel.label_empty(self)
Tunnel.show_data(self)
self.label_empty()
self.show_data()
''' Button Import '''
self.btn_i = ttk.Button(self.lb_frame_btn_lbox,
image=self.imp_pic, command=lambda: TLImp.import_sl(self),
image=self.imp_pic, command=self.import_sl,
padding=0)
self.btn_i.grid(column=0, row=1, padx=15, pady=8)
@ -705,7 +467,7 @@ class FrameWidgets(ttk.Frame):
if self.a != '' and self.a == select_tl:
self.StrVar.set(value='')
StSt.start(self)
self.start()
self.l_box.update()
''' Address Label '''
@ -912,10 +674,8 @@ class FrameWidgets(ttk.Frame):
self.autoconnect = ttk.Label(self, textvariable=self.autoconnect_var)
self.autoconnect.config(font=('Ubuntu', 11))
self.autoconnect.grid(column=0, row=4, sticky='ne', padx=10, pady=15)
self.wg_autostart = ttk.Checkbutton(self,
text='Autoconnect on:',
variable=self.selected_option,
command=lambda: FileHandle.box_set(self))
self.wg_autostart = ttk.Checkbutton(self, text='Autoconnect on:', variable=self.selected_option,
command=self.box_set)
self.wg_autostart.grid(column=0, row=4, pady=15, padx=15, sticky='nw')
def chk_enter(event):
@ -960,14 +720,341 @@ class FrameWidgets(ttk.Frame):
self.wg_autostart.bind('<Enter>', chk_a_enter)
self.wg_autostart.bind('<Leave>', chk_a_leave)
OnOff.on_off(self)
self.on_off()
"""
Import Methode for Wireguard config Files.
Before importing, it is checked whether PrivateKey and PublicKey are in the file.
If True then it is checked whether the PreSharedKey is already in the key file
to avoid an import error so that no double wgconf are imported.
Thus, tunnels can be renamed without the problems arise. If False then the key is written into the file.
Furthermore, it is checked whether the name is longer than 12 characters.
If True then the name is automatically shortened to 12 characters and then imported.
If in each case false comes out, a corresponding window comes to inform the user that something is wrong.
"""
def import_sl(self):
try:
filepath = filedialog.askopenfilename(initialdir=str(_u), title='Select Wireguard config File',
filetypes=[('WG config files', '*.conf')], )
with open(filepath, 'r') as file:
read = file.read()
path_split = filepath.split('/')
path_split1 = path_split[-1]
self.a = Tunnel.active()
if 'PrivateKey = ' in read and 'PublicKey = ' in read and 'Endpoint =' in read:
with open(filepath, 'r') as file:
key = Tunnel.con_to_dict(file)
pre_key = key[3]
if len(pre_key) != 0:
with open('/etc/wire_py/.keys', 'r') as readfile:
p_key = readfile.readlines()
if pre_key in p_key or pre_key + '\n' in p_key:
"""img_w, img_i, w_title, w_txt hand over"""
iw = r'/usr/share/icons/wp-icons/64/error.png'
ii = r'/usr/share/icons/wp-icons/48/wg_msg.png'
wt = 'Import Error'
msg_t = 'Tunnel already available!\nPlease use another file for import'
msg_window(iw, ii, wt, msg_t)
else:
with open('/etc/wire_py/.keys', 'a') as keyfile:
keyfile.write(pre_key + '\r')
if len(path_split1) > 17:
p1 = shutil.copy(filepath, Path('/etc/wire_py/'))
path_split = path_split1[len(path_split1) - 17:]
os.rename(p1, Path('/etc/wire_py') / str(path_split))
new_conf = '/etc/wire_py/' + path_split
if self.a != '':
check_call(['nmcli', 'connection', 'down', Tunnel.active()])
self.label_empty()
subprocess.check_output(['nmcli', 'connection', 'import', 'type',
'wireguard', 'file', new_conf], text=True)
else:
shutil.copy(filepath, Path('/etc/wire_py/'))
if self.a != '':
check_call(['nmcli', 'connection', 'down', Tunnel.active()])
self.label_empty()
subprocess.check_output(['nmcli', 'connection', 'import', 'type',
'wireguard', 'file', filepath], text=True)
self.StrVar.set('')
self.a = Tunnel.active()
self.l_box.insert(0, self.a)
self.wg_autostart.configure(state='normal')
self.l_box.selection_clear(0, tk.END)
self.l_box.update()
self.l_box.selection_set(0)
def chk_a_enter(event):
""" The mouse moves into the entry widget """
window.my_tool_tip = MyToolTip(event.x_root, event.y_root,
'To use the autostart, enable this '
'Checkbox')
def chk_a_leave(_):
""" The mouse moves from the entry widget """
''' Remove Tool-Tip '''
window.my_tool_tip.destroy()
def list_info_enter(event):
""" The mouse moves into the entry widget """
window.my_tool_tip = MyToolTip(event.x_root, event.y_root,
'List of available tunnels')
def list_info_leave(_):
""" The mouse moves from the entry widget """
''' Remove Tool-Tip '''
window.my_tool_tip.destroy()
def del_enter(event):
""" The mouse moves into the entry widget """
window.my_tool_tip = MyToolTip(event.x_root, event.y_root,
'Click to delete a Wireguard '
'Tunnel\nSelect from the list!')
def del_leave(_):
""" The mouse moves from the entry widget """
''' Remove Tool-Tip '''
window.my_tool_tip.destroy()
def exp_enter(event):
""" The mouse moves into the entry widget """
window.my_tool_tip = MyToolTip(event.x_root, event.y_root,
' Click to export '
'all\nWireguard Tunnel to Zipfile')
def exp_leave(_):
""" The mouse moves from the entry widget """
''' Remove Tool-Tip '''
window.my_tool_tip.destroy()
def rename_enter(event):
""" The mouse moves into the entry widget """
window.my_tool_tip = MyToolTip(event.x_root, event.y_root,
'To rename a tunnel, you need to\n'
'select a tunnel from the list')
def rename_leave(_):
""" The mouse moves from the entry widget """
''' Remove Tool-Tip '''
window.my_tool_tip.destroy()
self.wg_autostart.bind('<Enter>', chk_a_enter)
self.wg_autostart.bind('<Leave>', chk_a_leave)
self.l_box.bind('<Enter>', list_info_enter)
self.l_box.bind('<Leave>', list_info_leave)
self.btn_tr.bind('<Enter>', del_enter)
self.btn_tr.bind('<Leave>', del_leave)
self.btn_exp.bind('<Enter>', exp_enter)
self.btn_exp.bind('<Leave>', exp_leave)
self.lb_rename.bind('<Enter>', rename_enter)
self.lb_rename.bind('<Leave>', rename_leave)
self.lb_rename.insert(0, 'Max. 12 characters!')
self.StrVar = tk.StringVar()
self.StrVar.set(self.a)
self.color_label()
self.stop()
wg_read = Path('/etc/wire_py') / str(self.a + '.conf')
with open(wg_read, 'r') 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'])
Path.chmod(wg_read, 0o600)
if 'PrivateKey = ' and 'Endpoint = ' not in read:
"""img_w, img_i, w_title, w_txt hand over"""
iw = r'/usr/share/icons/wp-icons/64/error.png'
ii = r'/usr/share/icons/wp-icons/48/wg_msg.png'
wt = 'Import Error'
msg_t = 'Oh... no valid Wireguard File!\nPlease select a valid Wireguard File'
msg_window(iw, ii, wt, msg_t)
except EOFError:
pass
except TypeError:
pass
except FileNotFoundError:
pass
except subprocess.CalledProcessError:
print('Tunnel exist!')
'''
This Method will display the autostart label which
Tunnel is automatically started regardless of the active tunnel.
The selected tunnel is written into a file to read it after the start of the system.
'''
def box_set(self):
try:
select_tunnel = self.l_box.curselection()
select_tl = self.l_box.get(select_tunnel[0])
if self.selected_option.get() == 0:
with open(path_to_file2, 'r') as set_file3:
lines3 = set_file3.readlines()
lines3[7] = 'off'
with open(path_to_file2, 'w') as set_file3:
set_file3.writelines(lines3)
tl = Tunnel.list()
if len(tl) == 0:
self.wg_autostart.configure(state='disabled')
if self.selected_option.get() >= 1:
with open(path_to_file2, 'r') as set_file3:
lines3 = set_file3.readlines()
lines3[7] = select_tl
with open(path_to_file2, 'w') as set_file3:
set_file3.writelines(lines3)
except IndexError:
self.selected_option.set(1)
self.on_off()
'''
Here it is checked whether the path to the file is there if not it is created.
Set (on), the selected tunnel is displayed in the label.
At (off) the label is first emptied then filled with No Autoconnect
'''
def on_off(self):
with open(path_to_file2, 'r') as set_file4:
lines4 = set_file4.readlines()
if lines4[7] != 'off':
self.selected_option.set(1)
self.autoconnect_var.set('')
self.auto_con = lines4[7]
else:
self.wg_autostart.configure(state='disabled')
self.auto_con = 'no Autoconnect'
self.autoconnect_var.set('')
self.autoconnect_var = tk.StringVar()
self.autoconnect_var.set(self.auto_con)
self.autoconnect = ttk.Label(self, textvariable=self.autoconnect_var, foreground='#0071ff')
self.autoconnect.config(font=('Ubuntu', 11))
self.autoconnect.grid(column=0, row=4, sticky='ne', pady=19)
"""
Displays the value address, DNS and peer in the labels
or empty it again
"""
def init_and_report(self, data=None):
""" Address Label """
self.add = tk.StringVar()
self.add.set('Address: ' + data[0])
self.DNS = tk.StringVar()
self.DNS.set(' DNS: ' + data[1])
self.enp = tk.StringVar()
self.enp.set('Endpoint: ' + data[2])
def label_empty(self):
self.add.set('')
self.DNS.set('')
self.enp.set('')
def show_data(self):
""" Address Label """
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))
''' DNS Label '''
self.dns = ttk.Label(self.lb_frame, textvariable=self.DNS, foreground='#0071ff')
self.dns.grid(column=0, row=7, sticky='w', padx=10, pady=6)
self.dns.config(font=('Ubuntu', 9))
''' Endpoint Label '''
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))
def stop(self):
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)
def stop_enter(event):
""" The mouse moves into the entry widget """
window.my_tool_tip = MyToolTip(event.x_root, event.y_root,
'Click to stop selected Wireguard Tunnel')
def stop_leave(_):
""" The mouse moves from the entry widget """
''' Remove Tool-Tip '''
window.my_tool_tip.destroy()
self.btn_stst.bind('<Enter>', stop_enter)
self.btn_stst.bind('<Leave>', stop_leave)
def start(self):
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)
def empty_list_start_enter(event):
""" The mouse moves into the entry widget """
window.my_tool_tip = MyToolTip(event.x_root, event.y_root,
'No tunnels to start in the list')
def empty_list_start_leave(_):
""" The mouse moves from the entry widget """
''' Remove Tool-Tip '''
window.my_tool_tip.destroy()
def start_enter(event):
""" The mouse moves into the entry widget """
window.my_tool_tip = MyToolTip(event.x_root, event.y_root,
'Click to start selected Wireguard Tunnel')
def start_leave(_):
""" The mouse moves from the entry widget """
''' Remove Tool-Tip '''
window.my_tool_tip.destroy()
tl = Tunnel.list()
if len(tl) == 0:
self.btn_stst.bind('<Enter>', empty_list_start_enter)
self.btn_stst.bind('<Leave>', empty_list_start_leave)
else:
self.btn_stst.bind('<Enter>', start_enter)
self.btn_stst.bind('<Leave>', start_leave)
def color_label(self):
with open(path_to_file2, 'r') as read_file:
lines = read_file.readlines()
if 'light\n' in lines:
self.lb_tunnel = ttk.Label(self, textvariable=self.StrVar, foreground='green')
else:
self.lb_tunnel = ttk.Label(self, textvariable=self.StrVar, foreground='yellow')
self.lb_tunnel.config(font=('Ubuntu', 11, 'bold'))
self.lb_tunnel.grid(column=2, padx=10, row=1)
def wg_switch(self):
self.a = Tunnel.active()
try:
if self.a == '':
StSt.start(self)
self.start()
self.select_tunnel = self.l_box.curselection()
select_tl = self.l_box.get(self.select_tunnel[0])
check_call(['nmcli', 'connection', 'up', select_tl])
@ -976,33 +1063,33 @@ class FrameWidgets(ttk.Frame):
data = Tunnel.con_to_dict(file)
''' Address Label '''
Tunnel.init_and_report(self, data)
Tunnel.show_data(self)
self.init_and_report(data)
self.show_data()
''' Button Start/Stop '''
StSt.stop(self)
self.stop()
self.a = Tunnel.active()
self.StrVar = tk.StringVar()
self.StrVar.set(self.a)
Tunnel.color(self)
self.color_label()
elif self.a != '':
''' Button Start/Stop '''
StSt.stop(self)
self.stop()
check_call(['nmcli', 'connection', 'down', self.a])
''' Button Start/Stop '''
StSt.start(self)
self.start()
self.a = Tunnel.active()
self.StrVar.set('')
Tunnel.color(self)
self.color_label()
''' Address Label '''
self.add.set('')
self.DNS.set('')
self.enp.set('')
Tunnel.show_data(self)
self.show_data()
except IndexError: