wire-py-reformat-14-08-2024 #8

Merged
punix merged 6 commits from wire-py-reformat-14-08-2024 into main 2024-08-23 18:00:19 +02:00
4 changed files with 169 additions and 62 deletions

View File

@ -4,9 +4,8 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<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="little fixes in Class Import">
<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$/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/main.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/wg_func.py" beforeDir="false" afterPath="$PROJECT_DIR$/wg_func.py" afterDir="false" />
</list>
@ -164,7 +163,55 @@
<option name="project" value="LOCAL" />
<updated>1724237471056</updated>
</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>
<task id="LOCAL-00011" summary="Remove open file function, add Show Address Labels with tk.StrVar when remove a not active Tunnel Labels of Active Tunnel removed!">
<option name="closed" value="true" />
<created>1724371229008</created>
<option name="number" value="00011" />
<option name="presentableId" value="LOCAL-00011" />
<option name="project" value="LOCAL" />
<updated>1724371229008</updated>
</task>
<task id="LOCAL-00012" summary="fixed error when active tunnel is deleted,&#10;now also the Address Label is emptied">
<option name="closed" value="true" />
<created>1724400463791</created>
<option name="number" value="00012" />
<option name="presentableId" value="LOCAL-00012" />
<option name="project" value="LOCAL" />
<updated>1724400463791</updated>
</task>
<task id="LOCAL-00013" summary="In FrameWidged Class else Block StrVar, this resorts reference and variable assignment together">
<option name="closed" value="true" />
<created>1724401340512</created>
<option name="number" value="00013" />
<option name="presentableId" value="LOCAL-00013" />
<option name="project" value="LOCAL" />
<updated>1724401340512</updated>
</task>
<task id="LOCAL-00014" summary="add method init_and_report and label_empty to Class ShowAddress">
<option name="closed" value="true" />
<created>1724410630021</created>
<option name="number" value="00014" />
<option name="presentableId" value="LOCAL-00014" />
<option name="project" value="LOCAL" />
<updated>1724410630021</updated>
</task>
<task id="LOCAL-00015" summary="little fixes in Class Import">
<option name="closed" value="true" />
<created>1724416642213</created>
<option name="number" value="00015" />
<option name="presentableId" value="LOCAL-00015" />
<option name="project" value="LOCAL" />
<updated>1724416642213</updated>
</task>
<option name="localTasksCounter" value="16" />
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
@ -200,6 +247,12 @@
<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 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" />
<MESSAGE value="Remove open file function, add Show Address Labels with tk.StrVar when remove a not active Tunnel Labels of Active Tunnel removed!" />
<MESSAGE value="fixed error when active tunnel is deleted,&#10;now also the Address Label is emptied" />
<MESSAGE value="In FrameWidged Class else Block StrVar, this resorts reference and variable assignment together" />
<MESSAGE value="add method init_and_report and label_empty to Class ShowAddress" />
<MESSAGE value="little fixes in Class Import" />
<option name="LAST_COMMIT_MESSAGE" value="little fixes in Class Import" />
</component>
</project>

View File

@ -1,3 +0,0 @@
192.168.178.158/24
192.168.178.1,
rj2cexasg9b9oydd.myfritz.net:54775

75
main.py
View File

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

View File

@ -1,9 +1,11 @@
# Wireguard functions for Wire-Py
import os
import subprocess
from tkinter import filedialog, ttk
from tkinter import filedialog
import tkinter as tk
fontcolor = '#4011a7'
class Message(tk.Tk):
def __init__(self, *args, **kwargs):
@ -44,6 +46,7 @@ class GreenLabel(tk.Tk):
class StartStopBTN(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.tl = None
self.wg_switch = None
self.btn_stst = None
self.wg_vpn_start = tk.PhotoImage(file=r'icons/wg-vpn-start-48.png')
@ -51,11 +54,21 @@ class StartStopBTN(tk.Tk):
def button_stop(self):
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):
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.tl = ListTunnels.tl_list()
print(len(self.tl))
if len(self.tl) == 0:
self.btn_stst = tk.Button(self, image=self.wg_vpn_start, bd=0, command=self.wg_switch)
self.btn_stst.config(state=tk.DISABLED)
self.btn_stst.grid(column=0, row=1, padx=15, pady=10, sticky="s")
self.rowconfigure(0, weight=1)
else:
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=10, sticky="s")
self.rowconfigure(0, weight=1)
class ConToDict:
@ -70,7 +83,7 @@ class ConToDict:
for items in dictlist:
if 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]]
b = [dictlist[2], dictlist[3]]
c = [dictlist[4], dictlist[5]]
@ -83,15 +96,11 @@ class ConToDict:
finaldict = {}
for elements in newlist:
finaldict[elements[0]] = elements[1]
# end... result a Dictionary
address = finaldict['Address']
dns = finaldict['DNS']
endpoint = finaldict['Endpoint']
with open('dict_to_address', 'w+') as data:
data.write(address + '\n')
data.write(dns + '\n')
data.write(endpoint + '\n')
data.close()
return address, dns, endpoint
class TunnelActiv:
@ -106,6 +115,47 @@ class TunnelActiv:
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 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 = 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:
@staticmethod
def tl_list():
@ -116,6 +166,9 @@ class ListTunnels:
class ImportTunnel:
def __init__(self):
self.enp = None
self.DNS = None
self.add = None
self.select_tunnel = None
self.wg_switch = None
self.btn_stst = None
@ -137,16 +190,17 @@ class ImportTunnel:
if len(pathsplit1) > 17:
pathsplit = pathsplit1[len(pathsplit1) - 17:]
os.rename(filepath, os.environ['HOME'] + '/tester/' + str(pathsplit))
os.system('nmcli connection down ' + str(TunnelActiv.active()))
if self.a != '':
os.system('nmcli connection down ' + str(TunnelActiv.active()))
os.system('nmcli connection import type wireguard file ' + os.environ['HOME'] + '/tester/' +
str(pathsplit))
os.system('nmcli con mod ' + str(pathsplit[:-5]) + ' connection.autoconnect no')
else:
subprocess.call('cp ' + str(filepath) + ' ' + os.environ['HOME'] + '/tester/', shell=True)
os.system('nmcli connection down ' + str(TunnelActiv.active()))
if self.a != '':
os.system('nmcli connection down ' + str(TunnelActiv.active()))
os.system('nmcli connection import type wireguard file ' + str(filepath))
os.system('nmcli con mod ' + str(pathsplit1[:-5]) + ' connection.autoconnect no')
self.StrVar.set(value=' ')
self.StrVar.set('')
self.a = TunnelActiv.active()
self.l_box.insert(0, self.a)
self.l_box.update()
@ -156,8 +210,12 @@ class ImportTunnel:
StartStopBTN.button_stop(self)
wg_read = os.environ['HOME'] + '/tester/' + str(self.a) + '.conf'
file = open(wg_read, 'r')
ConToDict.covert_to_dict(file)
data = ConToDict.covert_to_dict(file)
# Address Label
ShowAddress.init_and_report(self, data)
ShowAddress.show_data(self)
file.close()
os.system('nmcli con mod ' + str(self.a) + ' connection.autoconnect no')
if "PrivateKey = " not in read:
Message()
except EOFError: