Remove open file function, add Show Address Labels with tk.StrVar when remove a not active Tunnel Labels of Active Tunnel removed!

This commit is contained in:
Désiré Werner Menrath 2024-08-23 02:00:24 +02:00
parent 2601b7b0ce
commit 357cae2d2a
3 changed files with 112 additions and 50 deletions

View File

@ -4,7 +4,7 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="940e1630-c825-4d4c-be80-bc11f543c122" name="Changes" comment="add Class StartStopBTN to wg_func.py"> <list default="true" id="940e1630-c825-4d4c-be80-bc11f543c122" name="Changes" comment="Set Style to Clam&#10;Improved layout of widgets">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/dict_to_address" beforeDir="false" afterPath="$PROJECT_DIR$/dict_to_address" afterDir="false" /> <change beforePath="$PROJECT_DIR$/dict_to_address" beforeDir="false" afterPath="$PROJECT_DIR$/dict_to_address" afterDir="false" />
<change beforePath="$PROJECT_DIR$/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/main.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/main.py" afterDir="false" />
@ -164,7 +164,15 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1724237471056</updated> <updated>1724237471056</updated>
</task> </task>
<option name="localTasksCounter" value="10" /> <task id="LOCAL-00010" summary="Set Style to Clam&#10;Improved layout of widgets">
<option name="closed" value="true" />
<created>1724272761629</created>
<option name="number" value="00010" />
<option name="presentableId" value="LOCAL-00010" />
<option name="project" value="LOCAL" />
<updated>1724272761629</updated>
</task>
<option name="localTasksCounter" value="11" />
<servers /> <servers />
</component> </component>
<component name="Vcs.Log.Tabs.Properties"> <component name="Vcs.Log.Tabs.Properties">
@ -200,6 +208,7 @@
<MESSAGE value="add Label Interface and Peer to new FrameWidget one and two. when import Tunnel then add to list works and remove of list when Tunnel delete.(with Label active on and off) &#10;add to class and methods open a avtive Tunnel when Wire-Py open, when import and start a Tunnel" /> <MESSAGE value="add Label Interface and Peer to new FrameWidget one and two. when import Tunnel then add to list works and remove of list when Tunnel delete.(with Label active on and off) &#10;add to class and methods open a avtive Tunnel when Wire-Py open, when import and start a Tunnel" />
<MESSAGE value="add file dict_to_address for &#10;address a active Tunnel. Label green pack in Class.&#10;add Class to convert fileoutput as String to Dictionary" /> <MESSAGE value="add file dict_to_address for &#10;address a active Tunnel. Label green pack in Class.&#10;add Class to convert fileoutput as String to Dictionary" />
<MESSAGE value="add Class StartStopBTN to wg_func.py" /> <MESSAGE value="add Class StartStopBTN to wg_func.py" />
<option name="LAST_COMMIT_MESSAGE" value="add Class StartStopBTN to wg_func.py" /> <MESSAGE value="Set Style to Clam&#10;Improved layout of widgets" />
<option name="LAST_COMMIT_MESSAGE" value="Set Style to Clam&#10;Improved layout of widgets" />
</component> </component>
</project> </project>

88
main.py
View File

@ -3,8 +3,10 @@ import os
import tkinter as tk import tkinter as tk
from tkinter import ttk from tkinter import ttk
from wg_func import (TunnelActiv, ListTunnels, ImportTunnel, from wg_func import (TunnelActiv, ListTunnels, ImportTunnel, ConToDict, GreenLabel, StartStopBTN, ShowAddress,
ConToDict, GreenLabel, StartStopBTN, ExportTunnels) ExportTunnels)
fontcolor = '#4011a7'
class MainWindow(tk.Tk): class MainWindow(tk.Tk):
@ -24,7 +26,6 @@ class MainWindow(tk.Tk):
self.columnconfigure(2, weight=1) self.columnconfigure(2, weight=1)
self.rowconfigure(0, weight=1) self.rowconfigure(0, weight=1)
self.style = ttk.Style(self) self.style = ttk.Style(self)
print(self.style.theme_names())
self.style.theme_use('clam') self.style.theme_use('clam')
# Load the image file from disk. # Load the image file from disk.
self.wg_icon = tk.PhotoImage(file=r'icons/wg-vpn-48.png') self.wg_icon = tk.PhotoImage(file=r'icons/wg-vpn-48.png')
@ -38,6 +39,11 @@ class FrameWidgets(ttk.Frame):
def __init__(self, container, **kwargs): def __init__(self, container, **kwargs):
super().__init__(container, **kwargs) super().__init__(container, **kwargs)
self.enp = None
self.DNS = None
self.add = None
self.data = None
self.peer = None
self.lb_tunnel = None self.lb_tunnel = None
self.wg_read = None self.wg_read = None
self.wg_vpn_start = tk.PhotoImage(file=r'icons/wg-vpn-start-48.png') self.wg_vpn_start = tk.PhotoImage(file=r'icons/wg-vpn-start-48.png')
@ -50,13 +56,13 @@ class FrameWidgets(ttk.Frame):
# Show active Tunnel # Show active Tunnel
self.a = TunnelActiv.active() self.a = TunnelActiv.active()
# Label 2 # Label Frame 1
self.lb_frame = ttk.Frame(self) self.lb_frame = ttk.Frame(self)
self.lb_frame.configure(relief='solid') self.lb_frame.configure(relief='solid')
self.lb_frame.grid(column=2, row=2, sticky='snew', padx=20, pady=5) self.lb_frame.grid(column=2, row=2, sticky='snew', padx=20, pady=5)
self.columnconfigure(0, weight=1) self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1) self.rowconfigure(0, weight=1)
# Label Frame 2
self.lb_frame2 = ttk.Frame(self) self.lb_frame2 = ttk.Frame(self)
self.lb_frame2.configure(relief='solid') self.lb_frame2.configure(relief='solid')
self.lb_frame2.grid(column=2, row=3, sticky='snew', padx=20, pady=5) self.lb_frame2.grid(column=2, row=3, sticky='snew', padx=20, pady=5)
@ -67,60 +73,54 @@ class FrameWidgets(ttk.Frame):
self.lb = tk.Label(self, text='Active: ') self.lb = tk.Label(self, text='Active: ')
self.lb.config(font=("Ubuntu", 11, "bold")) self.lb.config(font=("Ubuntu", 11, "bold"))
self.lb.grid(column=2, row=1, padx=15, sticky="w") self.lb.grid(column=2, row=1, padx=15, sticky="w")
# Label to Show active Tunnel # Label to Show active Tunnel
self.StrVar = tk.StringVar(value=self.a) self.StrVar = tk.StringVar(value=self.a)
GreenLabel.green_show_label(self) GreenLabel.green_show_label(self)
datafile = open('dict_to_address', 'r')
address = datafile.readline()
dns = datafile.readline()
endpoint = datafile.readline()
datafile.close()
print(address, dns, endpoint)
# Interface Label # Interface Label
self.interface = tk.Label(self.lb_frame, text='Interface') self.interface = tk.Label(self.lb_frame, text='Interface', fg=fontcolor)
self.interface.grid(column=0, row=4, sticky="we", padx=120) self.interface.grid(column=0, row=4, sticky="we", padx=120)
self.interface.config(font=("Ubuntu", 9)) self.interface.config(font=("Ubuntu", 9))
self.rowconfigure(0, weight=1)
# Address Label
self.address = tk.Label(self.lb_frame, text='Address: ' + address)
self.address.grid(column=0, row=5, sticky="w", padx=10)
self.address.config(font=("Ubuntu", 9))
# DNS Label
self.dns = tk.Label(self.lb_frame, text='DNS: ' + dns)
self.dns.grid(column=0, row=6, sticky="w", padx=10, pady=5)
self.dns.config(font=("Ubuntu", 9))
# Peer Label # Peer Label
self.peer = tk.Label(self.lb_frame2, text='Peer') self.peer = tk.Label(self.lb_frame2, text='Peer', fg=fontcolor)
self.peer.config(font=("Ubuntu", 9)) self.peer.config(font=("Ubuntu", 9))
self.peer.grid(column=0, row=7, sticky="we", padx=130) self.peer.grid(column=0, row=7, sticky="we", padx=130)
# Endpoint Label # Listbox with Scrollbar
self.endpoint = tk.Label(self.lb_frame2, text='Endpoint: ' + endpoint)
self.endpoint.grid(column=0, row=8, sticky="w", padx=10, pady=5)
self.endpoint.config(font=("Ubuntu", 9))
self.scrollbar = tk.Scrollbar(self) self.scrollbar = tk.Scrollbar(self)
self.l_box = tk.Listbox(self, fg='#606060', selectmode='single') self.l_box = tk.Listbox(self, fg='#606060', selectmode='single')
self.l_box.config(highlightthickness=0, relief='flat') self.l_box.config(highlightthickness=0, relief='flat')
self.scrollbar.config(command=self.l_box.yview) self.scrollbar.config(command=self.l_box.yview)
self.l_box.config(font=("Ubuntu", 12, "bold")) self.l_box.config(font=("Ubuntu", 12, "bold"))
self.l_box.grid(column=1, rowspan=3, row=1, ) self.l_box.grid(column=1, rowspan=3, row=1, )
# Tunnel List
self.tl = ListTunnels.tl_list() self.tl = ListTunnels.tl_list()
for tunnels in self.tl: for tunnels in self.tl:
self.l_box.insert("end", tunnels) self.l_box.insert("end", tunnels)
self.l_box.update() self.l_box.update()
# Button Vpn # Button Vpn
if self.a != '': if self.a != '':
StartStopBTN.button_stop(self) StartStopBTN.button_stop(self)
wg_read = os.environ['HOME'] + '/tester/' + str(self.a) + '.conf' wg_read = os.environ['HOME'] + '/tester/' + str(self.a) + '.conf'
file = open(wg_read, 'r') file = open(wg_read, 'r')
ConToDict.covert_to_dict(file) data = ConToDict.covert_to_dict(file)
# 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])
ShowAddress.show_data(self)
file.close() file.close()
else: else:
StartStopBTN.button_start(self) StartStopBTN.button_start(self)
# Address Label
self.add = tk.StringVar()
self.add.set('')
self.DNS = tk.StringVar()
self.DNS.set('')
self.enp = tk.StringVar()
self.enp.set('')
ShowAddress.show_data(self)
# Button Import # Button Import
self.btn_i = tk.Button(self, image=self.imp_pic, bd=0, command=lambda: ImportTunnel.wg_import_select(self)) self.btn_i = tk.Button(self, image=self.imp_pic, bd=0, command=lambda: ImportTunnel.wg_import_select(self))
self.btn_i.grid(column=0, row=2, padx=15, pady=15) self.btn_i.grid(column=0, row=2, padx=15, pady=15)
@ -135,6 +135,11 @@ class FrameWidgets(ttk.Frame):
self.StrVar.set(value='') self.StrVar.set(value='')
StartStopBTN.button_start(self) StartStopBTN.button_start(self)
self.l_box.update() self.l_box.update()
# Address Label
self.add.set('')
self.DNS.set('')
self.enp.set('')
ShowAddress.show_data(self)
# Button Trash # Button Trash
self.btn_tr = tk.Button(self, image=self.tr_pic, bd=0, command=delete) self.btn_tr = tk.Button(self, image=self.tr_pic, bd=0, command=delete)
@ -142,7 +147,6 @@ class FrameWidgets(ttk.Frame):
# Button Export # Button Export
self.btn_exp = tk.Button(self, image=self.exp_pic, bd=0) self.btn_exp = tk.Button(self, image=self.exp_pic, bd=0)
self.btn_exp.grid(column=0, row=4, padx=15, pady=15) self.btn_exp.grid(column=0, row=4, padx=15, pady=15)
# Check Buttons # Check Buttons
self.wg_autostart = tk.Checkbutton(self, text='Autoconnect on PC Start') self.wg_autostart = tk.Checkbutton(self, text='Autoconnect on PC Start')
self.wg_autostart.grid(column=1, rowspan=3, row=3) self.wg_autostart.grid(column=1, rowspan=3, row=3)
@ -158,7 +162,15 @@ class FrameWidgets(ttk.Frame):
os.system('nmcli connection up ' + str(select_tl)) os.system('nmcli connection up ' + str(select_tl))
wg_read = os.environ['HOME'] + '/tester/' + str(select_tl) + '.conf' wg_read = os.environ['HOME'] + '/tester/' + str(select_tl) + '.conf'
file = open(wg_read, 'r') file = open(wg_read, 'r')
ConToDict.covert_to_dict(file) data = ConToDict.covert_to_dict(file)
# 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])
ShowAddress.show_data(self)
file.close() file.close()
# Button Start/Stop # Button Start/Stop
StartStopBTN.button_stop(self) StartStopBTN.button_stop(self)
@ -173,9 +185,13 @@ class FrameWidgets(ttk.Frame):
# Button Start/Stop # Button Start/Stop
StartStopBTN.button_start(self) StartStopBTN.button_start(self)
self.a = TunnelActiv.active() self.a = TunnelActiv.active()
self.StrVar = tk.StringVar() self.StrVar.set('')
self.StrVar.set(value=' ')
GreenLabel.green_show_label(self) GreenLabel.green_show_label(self)
# Address Label
self.add.set('')
self.DNS.set('')
self.enp.set('')
ShowAddress.show_data(self)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -1,9 +1,11 @@
# Wireguard functions for Wire-Py # Wireguard functions for Wire-Py
import os import os
import subprocess import subprocess
from tkinter import filedialog, ttk from tkinter import filedialog
import tkinter as tk import tkinter as tk
fontcolor = '#4011a7'
class Message(tk.Tk): class Message(tk.Tk):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -51,11 +53,13 @@ class StartStopBTN(tk.Tk):
def button_stop(self): def button_stop(self):
self.btn_stst = tk.Button(self, image=self.wg_vpn_stop, bd=0, command=self.wg_switch) self.btn_stst = tk.Button(self, image=self.wg_vpn_stop, bd=0, command=self.wg_switch)
self.btn_stst.grid(column=0, row=1, padx=15, pady=15, sticky="s") self.btn_stst.grid(column=0, row=1, padx=15, pady=10, sticky="s")
self.rowconfigure(0, weight=1)
def button_start(self): def button_start(self):
self.btn_stst = tk.Button(self, image=self.wg_vpn_start, bd=0, command=self.wg_switch) self.btn_stst = tk.Button(self, image=self.wg_vpn_start, bd=0, command=self.wg_switch)
self.btn_stst.grid(column=0, row=1, padx=15, pady=15, sticky="s") self.btn_stst.grid(column=0, row=1, padx=15, pady=10, sticky="s")
self.rowconfigure(0, weight=1)
class ConToDict: class ConToDict:
@ -70,7 +74,7 @@ class ConToDict:
for items in dictlist: for items in dictlist:
if items == '=': if items == '=':
dictlist.remove(items) dictlist.remove(items)
for i in dictlist: for i in dictlist: # Here is the beginning (Loop) of convert List to Dictionary
a = [dictlist[0], dictlist[1]] a = [dictlist[0], dictlist[1]]
b = [dictlist[2], dictlist[3]] b = [dictlist[2], dictlist[3]]
c = [dictlist[4], dictlist[5]] c = [dictlist[4], dictlist[5]]
@ -83,15 +87,11 @@ class ConToDict:
finaldict = {} finaldict = {}
for elements in newlist: for elements in newlist:
finaldict[elements[0]] = elements[1] finaldict[elements[0]] = elements[1]
# end... result a Dictionary
address = finaldict['Address'] address = finaldict['Address']
dns = finaldict['DNS'] dns = finaldict['DNS']
endpoint = finaldict['Endpoint'] endpoint = finaldict['Endpoint']
with open('dict_to_address', 'w+') as data: return address, dns, endpoint
data.write(address + '\n')
data.write(dns + '\n')
data.write(endpoint + '\n')
data.close()
class TunnelActiv: class TunnelActiv:
@ -106,6 +106,33 @@ class TunnelActiv:
return active return active
class ShowAddress(tk.Tk):
def __init__(self, container, **kwargs):
super().__init__(container, **kwargs)
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 show_data(self):
# Address Label
self.address = tk.Label(self.lb_frame, textvariable=self.add, fg='blue')
self.address.grid(column=0, row=5, sticky="w", padx=10)
self.address.config(font=("Ubuntu", 9))
# DNS Label
self.dns = tk.Label(self.lb_frame, textvariable=self.DNS, fg='blue')
self.dns.grid(column=0, row=6, sticky="w", padx=10, pady=3)
self.dns.config(font=("Ubuntu", 9))
# Endpoint Label
self.endpoint = tk.Label(self.lb_frame2, textvariable=self.enp, fg='blue')
self.endpoint.grid(column=0, row=8, sticky="w", padx=10, pady=5)
self.endpoint.config(font=("Ubuntu", 9))
class ListTunnels: class ListTunnels:
@staticmethod @staticmethod
def tl_list(): def tl_list():
@ -116,6 +143,9 @@ class ListTunnels:
class ImportTunnel: class ImportTunnel:
def __init__(self): def __init__(self):
self.enp = None
self.DNS = None
self.add = None
self.select_tunnel = None self.select_tunnel = None
self.wg_switch = None self.wg_switch = None
self.btn_stst = None self.btn_stst = None
@ -156,7 +186,14 @@ class ImportTunnel:
StartStopBTN.button_stop(self) StartStopBTN.button_stop(self)
wg_read = os.environ['HOME'] + '/tester/' + str(self.a) + '.conf' wg_read = os.environ['HOME'] + '/tester/' + str(self.a) + '.conf'
file = open(wg_read, 'r') file = open(wg_read, 'r')
ConToDict.covert_to_dict(file) data = ConToDict.covert_to_dict(file)
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])
ShowAddress.show_data(self)
file.close() file.close()
if "PrivateKey = " not in read: if "PrivateKey = " not in read:
Message() Message()