punix
c157154b1c
for Buttons and Listbox with Scrollbar. all Widgets new format delete works now of disable checkbox when Listbox empty (part two)
326 lines
12 KiB
Python
Executable File
326 lines
12 KiB
Python
Executable File
# Wireguard functions for Wire-Py
|
|
import os
|
|
import shutil
|
|
from datetime import datetime
|
|
from tkinter import filedialog, ttk
|
|
import tkinter as tk
|
|
|
|
font_color = '#4011a7'
|
|
|
|
|
|
class Message(tk.Tk):
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.warning_pic = None
|
|
self.x_width = 300
|
|
self.y_height = 120
|
|
self.monitor_center_x = self.winfo_screenwidth() / 2 - (self.x_width / 2)
|
|
self.monitor_center_y = self.winfo_screenheight() / 2 - (self.y_height / 2)
|
|
self.resizable(width=False, height=False)
|
|
self.title('Import error!')
|
|
self.configure()
|
|
self.geometry('%dx%d+%d+%d' % (self.x_width, self.y_height, self.monitor_center_x, self.monitor_center_y))
|
|
self.columnconfigure(0, weight=1)
|
|
|
|
self.label = tk.Label(self, image=self.warning_pic,
|
|
text='Oh... no valid Wireguard File!\nPlease select a valid Wireguard File')
|
|
self.label.config(font=('Ubuntu', 11), padx=15, pady=15)
|
|
self.label.grid(column=0, row=0)
|
|
self.button = tk.Button(self, text='OK', command=self.destroy)
|
|
self.button.config(padx=15, pady=5)
|
|
self.button.grid(column=0, row=1)
|
|
|
|
|
|
class GreenLabel(tk.Tk):
|
|
def __init__(self, container, **kwargs):
|
|
super().__init__(container, **kwargs)
|
|
self.StrVar = None
|
|
self.lb_tunnel = None
|
|
|
|
def green_show_label(self):
|
|
self.lb_tunnel = tk.Label(self, textvariable=self.StrVar, fg='green')
|
|
self.lb_tunnel.config(font=('Ubuntu', 11, 'bold'))
|
|
self.lb_tunnel.grid(column=2, padx=10, row=0)
|
|
self.columnconfigure(2, weight=1)
|
|
self.rowconfigure(0, weight=1)
|
|
|
|
|
|
class StartStopBTN(tk.Tk):
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.lb_frame_buttons = None
|
|
self.wg_switch = None
|
|
self.btn_stst = None
|
|
self.wg_vpn_start = tk.PhotoImage(file=r'icons/wg-vpn-start-48.png')
|
|
self.wg_vpn_stop = tk.PhotoImage(file=r'icons/wg-vpn-stop-48.png')
|
|
|
|
def button_stop(self):
|
|
self.btn_stst = tk.Button(self.lb_frame_buttons, image=self.wg_vpn_stop, bd=0, command=self.wg_switch)
|
|
self.btn_stst.grid(column=0, row=0, padx=15, pady=8)
|
|
#self.columnconfigure(0, weight=1)
|
|
#self.rowconfigure(0, weight=1)
|
|
|
|
def button_start(self):
|
|
self.btn_stst = tk.Button(self.lb_frame_buttons, image=self.wg_vpn_start, bd=0, command=self.wg_switch)
|
|
self.btn_stst.grid(column=0, row=0, padx=15, pady=8)
|
|
#self.columnconfigure(0, weight=1)
|
|
#self.rowconfigure(0, weight=1)
|
|
|
|
|
|
class ConToDict:
|
|
@classmethod
|
|
def covert_to_dict(cls, file):
|
|
dictlist = []
|
|
for lines in file.readlines():
|
|
line_plit = lines.split()
|
|
dictlist = dictlist + line_plit
|
|
dictlist.remove('[Interface]')
|
|
dictlist.remove('[Peer]')
|
|
for items in dictlist:
|
|
if items == '=':
|
|
dictlist.remove(items)
|
|
for _ 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]]
|
|
d = [dictlist[6], dictlist[7]]
|
|
e = [dictlist[8], dictlist[9]]
|
|
f = [dictlist[10], dictlist[11]]
|
|
g = [dictlist[12], dictlist[13]]
|
|
h = [dictlist[14], dictlist[15]]
|
|
new_list = [a, b, c, d, e, f, g, h]
|
|
final_dict = {}
|
|
for elements in new_list:
|
|
final_dict[elements[0]] = elements[1]
|
|
|
|
# end... result a Dictionary
|
|
address = final_dict['Address']
|
|
dns = final_dict['DNS']
|
|
if ',' in dns:
|
|
dns = dns[:-1]
|
|
endpoint = final_dict['Endpoint']
|
|
return address, dns, endpoint
|
|
|
|
|
|
class TunnelActiv:
|
|
@staticmethod
|
|
def active(): # Shows the active tunnel
|
|
active = os.popen('nmcli con show --active | grep -iPo "(.*)(wireguard)"').read().split()
|
|
if not active:
|
|
active = ''
|
|
else:
|
|
active = active[0]
|
|
|
|
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=4, sticky='w', padx=10, pady=6)
|
|
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=6)
|
|
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=7, sticky='w', padx=10, pady=10)
|
|
self.endpoint.config(font=('Ubuntu', 9))
|
|
|
|
|
|
class ListTunnels:
|
|
@staticmethod
|
|
def tl_list():
|
|
wg_s = os.popen('nmcli con show | grep -iPo "(.*)(wireguard)"').read().split()
|
|
tl = wg_s[::3] # tl = Tunnel list # Show of 4.Element in list
|
|
return tl
|
|
|
|
|
|
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
|
|
self.lb_tunnel = None
|
|
self.StrVar = None
|
|
self.a = None
|
|
self.l_box = None
|
|
|
|
def wg_import_select(self):
|
|
try:
|
|
filepath = filedialog.askopenfilename(initialdir=os.environ['HOME'], title='Select Wireguard config File',
|
|
filetypes=[('WG config files', '*.conf')])
|
|
file = open(filepath, 'r')
|
|
read = file.read()
|
|
file.close()
|
|
path_split = filepath.split('/')
|
|
path_split1 = path_split[-1]
|
|
if 'PrivateKey = ' in read and 'PublicKey = ' in read:
|
|
if len(path_split1) > 17:
|
|
p1 = shutil.copy(filepath, os.environ['HOME'] + '/tester/')
|
|
path_split = path_split1[len(path_split1) - 17:]
|
|
os.rename(p1, os.environ['HOME'] + '/tester/' + str(path_split))
|
|
if self.a != '':
|
|
os.system('nmcli connection down ' + str(TunnelActiv.active()))
|
|
os.system('nmcli connection import type wireguard file ' + os.environ['HOME'] + '/tester/' +
|
|
str(path_split))
|
|
else:
|
|
shutil.copy(filepath, os.environ['HOME'] + '/tester/')
|
|
if self.a != '':
|
|
os.system('nmcli connection down ' + str(TunnelActiv.active()))
|
|
os.system('nmcli connection import type wireguard file ' + str(filepath))
|
|
|
|
self.StrVar.set('')
|
|
self.a = TunnelActiv.active()
|
|
self.l_box.insert(0, self.a)
|
|
self.l_box.update()
|
|
self.StrVar = tk.StringVar()
|
|
self.StrVar.set(self.a)
|
|
GreenLabel.green_show_label(self)
|
|
StartStopBTN.button_stop(self)
|
|
wg_read = os.environ['HOME'] + '/tester/' + str(self.a) + '.conf'
|
|
file = open(wg_read, 'r')
|
|
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')
|
|
new_tl = open('wg_py.xml', 'a')
|
|
new_tl.write('false ' + str(self.a) + '\n')
|
|
new_tl.close()
|
|
if 'PrivateKey = ' not in read:
|
|
Message()
|
|
except EOFError:
|
|
pass
|
|
except TypeError:
|
|
pass
|
|
except FileNotFoundError:
|
|
pass
|
|
|
|
|
|
class FileHandle:
|
|
|
|
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
|
|
self.select_tunnel = None
|
|
|
|
def box_set(self):
|
|
word_replace = ''
|
|
try:
|
|
self.select_tunnel = self.l_box.curselection()
|
|
select_tl = self.l_box.get(self.select_tunnel[0])
|
|
if self.selected_option.get() == 0:
|
|
set_on = open('wg_py.xml', 'r')
|
|
# using the for loop
|
|
for line in set_on:
|
|
line = line.strip()
|
|
changes = line.replace('true ' + select_tl, 'false ' + select_tl)
|
|
os.system('nmcli con mod ' + str(self.auto_con) + ' connection.autoconnect no')
|
|
word_replace = word_replace + changes + "\n"
|
|
set_on.close()
|
|
if self.selected_option.get() >= 1:
|
|
set_on = open('wg_py.xml', 'r')
|
|
# using the for loop
|
|
for line in set_on:
|
|
line = line.strip()
|
|
changes = line.replace('false ' + select_tl, 'true ' + select_tl)
|
|
os.system('nmcli con mod ' + str(select_tl) + ' connection.autoconnect yes')
|
|
word_replace = word_replace + changes + "\n"
|
|
set_on.close()
|
|
set_off = open('wg_py.xml', 'w')
|
|
set_off.write(word_replace)
|
|
set_off.close()
|
|
|
|
except IndexError:
|
|
self.selected_option.set(1)
|
|
|
|
OnOff.on_off(self)
|
|
|
|
|
|
class OnOff(tk.Tk, FileHandle):
|
|
def __init__(self, container, **kwargs):
|
|
super().__init__(container, **kwargs)
|
|
self.lb_frame_buttons = None
|
|
|
|
def on_off(self):
|
|
a_connect = {}
|
|
on_or_off = open('wg_py.xml', 'r')
|
|
for line in on_or_off.readlines():
|
|
line_splitted = line.split()
|
|
a_connect[line_splitted[0]] = line_splitted[1]
|
|
if 'true' in a_connect:
|
|
self.selected_option.set(1)
|
|
self.autoconnect_var.set('')
|
|
self.auto_con = a_connect['true']
|
|
|
|
else:
|
|
self.wg_autostart.configure(state='disabled')
|
|
self.auto_con = 'no Autoconnect'
|
|
on_or_off.close()
|
|
|
|
self.autoconnect_var = tk.StringVar()
|
|
self.autoconnect_var.set(self.auto_con)
|
|
self.autoconnect = tk.Label(self, textvariable=self.autoconnect_var, fg='blue', padx=5)
|
|
self.autoconnect.config(font=('Ubuntu', 11))
|
|
self.autoconnect.grid(column=0, row=4, sticky='ne', pady=20)
|
|
|
|
|
|
class ExportTunnels:
|
|
@staticmethod
|
|
def wg_export():
|
|
now_time = datetime.now()
|
|
now_datetime = now_time.strftime('/wg-exp-' + '%m-%d-%Y' + '-' + '%H:%M')
|
|
tl = ListTunnels.tl_list()
|
|
try:
|
|
if len(tl) != 0:
|
|
wg_tar = os.environ['HOME'] + now_datetime
|
|
p_to_conf = os.environ['HOME'] + '/tester'
|
|
shutil.make_archive(wg_tar, 'zip', p_to_conf)
|
|
#if zip_full != 0:
|
|
#print('Export erfolgraeich')
|
|
#else:
|
|
#print('ups etwwas ging schief bitte Export wiederholen')
|
|
else:
|
|
print('No Tunnel for Export')
|
|
except TypeError:
|
|
pass
|