2024-08-14 22:05:00 +02:00
|
|
|
# Wireguard functions for Wire-Py
|
|
|
|
import os
|
2024-08-25 15:39:23 +02:00
|
|
|
import shutil
|
2024-08-26 19:50:06 +02:00
|
|
|
from datetime import datetime
|
2024-09-05 22:17:31 +02:00
|
|
|
from tkinter import filedialog, ttk
|
2024-08-17 00:30:42 +02:00
|
|
|
import tkinter as tk
|
2024-09-08 20:31:46 +02:00
|
|
|
from pathlib import Path
|
|
|
|
from subprocess import check_call
|
2024-08-18 22:33:16 +02:00
|
|
|
|
2024-09-08 20:31:46 +02:00
|
|
|
home = Path.home()
|
2024-08-31 17:50:42 +02:00
|
|
|
font_color = '#4011a7'
|
2024-09-08 20:31:46 +02:00
|
|
|
path_to_file = home / '.config/wire_py/wg_py'
|
2024-08-23 02:00:24 +02:00
|
|
|
|
2024-08-18 22:33:16 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2024-09-08 20:31:46 +02:00
|
|
|
self.label = tk.Label(self,
|
|
|
|
image=self.warning_pic,
|
2024-08-19 23:28:53 +02:00
|
|
|
text='Oh... no valid Wireguard File!\nPlease select a valid Wireguard File')
|
2024-08-31 17:50:42 +02:00
|
|
|
self.label.config(font=('Ubuntu', 11), padx=15, pady=15)
|
2024-08-18 22:33:16 +02:00
|
|
|
self.label.grid(column=0, row=0)
|
2024-08-31 17:50:42 +02:00
|
|
|
self.button = tk.Button(self, text='OK', command=self.destroy)
|
2024-08-18 22:33:16 +02:00
|
|
|
self.button.config(padx=15, pady=5)
|
|
|
|
self.button.grid(column=0, row=1)
|
2024-08-17 00:30:42 +02:00
|
|
|
|
|
|
|
|
2024-08-21 11:50:01 +02:00
|
|
|
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')
|
2024-08-31 17:50:42 +02:00
|
|
|
self.lb_tunnel.config(font=('Ubuntu', 11, 'bold'))
|
2024-09-05 22:17:31 +02:00
|
|
|
self.lb_tunnel.grid(column=2, padx=10, row=0)
|
2024-08-27 19:14:49 +02:00
|
|
|
self.columnconfigure(2, weight=1)
|
2024-09-05 22:17:31 +02:00
|
|
|
self.rowconfigure(0, weight=1)
|
2024-08-21 11:50:01 +02:00
|
|
|
|
|
|
|
|
2024-08-21 12:51:07 +02:00
|
|
|
class StartStopBTN(tk.Tk):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2024-09-06 18:20:29 +02:00
|
|
|
self.lb_frame_btn_lbox = None
|
2024-08-21 12:51:07 +02:00
|
|
|
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):
|
2024-09-06 18:20:29 +02:00
|
|
|
self.btn_stst = tk.Button(self.lb_frame_btn_lbox, image=self.wg_vpn_stop, bd=0, command=self.wg_switch)
|
2024-09-05 22:17:31 +02:00
|
|
|
self.btn_stst.grid(column=0, row=0, padx=15, pady=8)
|
2024-08-21 12:51:07 +02:00
|
|
|
|
|
|
|
def button_start(self):
|
2024-09-06 18:20:29 +02:00
|
|
|
self.btn_stst = tk.Button(self.lb_frame_btn_lbox, image=self.wg_vpn_start, bd=0, command=self.wg_switch)
|
2024-09-05 22:17:31 +02:00
|
|
|
self.btn_stst.grid(column=0, row=0, padx=15, pady=8)
|
2024-08-21 12:51:07 +02:00
|
|
|
|
|
|
|
|
2024-08-21 11:50:01 +02:00
|
|
|
class ConToDict:
|
|
|
|
@classmethod
|
|
|
|
def covert_to_dict(cls, file):
|
|
|
|
dictlist = []
|
|
|
|
for lines in file.readlines():
|
2024-08-31 17:50:42 +02:00
|
|
|
line_plit = lines.split()
|
|
|
|
dictlist = dictlist + line_plit
|
2024-08-21 11:50:01 +02:00
|
|
|
dictlist.remove('[Interface]')
|
|
|
|
dictlist.remove('[Peer]')
|
|
|
|
for items in dictlist:
|
|
|
|
if items == '=':
|
|
|
|
dictlist.remove(items)
|
2024-08-31 17:50:42 +02:00
|
|
|
for _ in dictlist: # Here is the beginning (Loop) of convert List to Dictionary
|
2024-08-21 11:50:01 +02:00
|
|
|
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]]
|
2024-08-31 17:50:42 +02:00
|
|
|
new_list = [a, b, c, d, e, f, g, h]
|
|
|
|
final_dict = {}
|
|
|
|
for elements in new_list:
|
|
|
|
final_dict[elements[0]] = elements[1]
|
2024-08-24 12:34:44 +02:00
|
|
|
|
2024-08-23 02:00:24 +02:00
|
|
|
# end... result a Dictionary
|
2024-08-31 17:50:42 +02:00
|
|
|
address = final_dict['Address']
|
|
|
|
dns = final_dict['DNS']
|
2024-08-25 11:03:15 +02:00
|
|
|
if ',' in dns:
|
|
|
|
dns = dns[:-1]
|
2024-08-31 17:50:42 +02:00
|
|
|
endpoint = final_dict['Endpoint']
|
2024-08-23 02:00:24 +02:00
|
|
|
return address, dns, endpoint
|
2024-08-21 11:50:01 +02:00
|
|
|
|
|
|
|
|
2024-08-18 23:23:43 +02:00
|
|
|
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]
|
2024-08-17 15:42:57 +02:00
|
|
|
|
2024-08-18 23:23:43 +02:00
|
|
|
return active
|
2024-08-17 15:42:57 +02:00
|
|
|
|
|
|
|
|
2024-08-23 02:00:24 +02:00
|
|
|
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
|
|
|
|
|
2024-08-23 12:57:07 +02:00
|
|
|
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('')
|
|
|
|
|
2024-08-23 02:00:24 +02:00
|
|
|
def show_data(self):
|
|
|
|
# Address Label
|
|
|
|
self.address = tk.Label(self.lb_frame, textvariable=self.add, fg='blue')
|
2024-09-05 22:17:31 +02:00
|
|
|
self.address.grid(column=0, row=4, sticky='w', padx=10, pady=6)
|
2024-08-31 17:50:42 +02:00
|
|
|
self.address.config(font=('Ubuntu', 9))
|
2024-08-23 02:00:24 +02:00
|
|
|
# DNS Label
|
|
|
|
self.dns = tk.Label(self.lb_frame, textvariable=self.DNS, fg='blue')
|
2024-09-05 22:17:31 +02:00
|
|
|
self.dns.grid(column=0, row=6, sticky='w', padx=10, pady=6)
|
2024-08-31 17:50:42 +02:00
|
|
|
self.dns.config(font=('Ubuntu', 9))
|
2024-08-23 02:00:24 +02:00
|
|
|
# Endpoint Label
|
|
|
|
self.endpoint = tk.Label(self.lb_frame2, textvariable=self.enp, fg='blue')
|
2024-09-05 22:17:31 +02:00
|
|
|
self.endpoint.grid(column=0, row=7, sticky='w', padx=10, pady=10)
|
2024-08-31 17:50:42 +02:00
|
|
|
self.endpoint.config(font=('Ubuntu', 9))
|
2024-08-23 02:00:24 +02:00
|
|
|
|
|
|
|
|
2024-08-19 08:29:35 +02:00
|
|
|
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
|
|
|
|
|
2024-08-17 15:42:57 +02:00
|
|
|
|
2024-09-08 20:31:46 +02:00
|
|
|
class ImportTunnel(tk.Tk):
|
|
|
|
def __init__(self, container, **kwargs):
|
|
|
|
super().__init__(container, **kwargs)
|
|
|
|
|
2024-08-19 23:28:53 +02:00
|
|
|
self.select_tunnel = None
|
2024-08-19 08:29:35 +02:00
|
|
|
self.wg_switch = None
|
|
|
|
self.btn_stst = None
|
|
|
|
self.lb_tunnel = None
|
|
|
|
self.StrVar = None
|
|
|
|
self.a = None
|
|
|
|
self.l_box = None
|
2024-08-17 15:42:57 +02:00
|
|
|
|
2024-08-19 08:29:35 +02:00
|
|
|
def wg_import_select(self):
|
|
|
|
try:
|
2024-09-08 20:31:46 +02:00
|
|
|
filepath = filedialog.askopenfilename(initialdir=home, title='Select Wireguard config File',
|
2024-08-31 17:50:42 +02:00
|
|
|
filetypes=[('WG config files', '*.conf')])
|
2024-08-19 08:29:35 +02:00
|
|
|
file = open(filepath, 'r')
|
|
|
|
read = file.read()
|
|
|
|
file.close()
|
2024-08-31 17:50:42 +02:00
|
|
|
path_split = filepath.split('/')
|
|
|
|
path_split1 = path_split[-1]
|
|
|
|
if 'PrivateKey = ' in read and 'PublicKey = ' in read:
|
|
|
|
if len(path_split1) > 17:
|
2024-09-08 20:31:46 +02:00
|
|
|
p1 = shutil.copy(filepath, home / 'tester/')
|
2024-08-31 17:50:42 +02:00
|
|
|
path_split = path_split1[len(path_split1) - 17:]
|
2024-09-08 20:31:46 +02:00
|
|
|
os.rename(p1, home / 'tester/' / str(path_split))
|
2024-08-23 14:37:19 +02:00
|
|
|
if self.a != '':
|
2024-09-08 20:31:46 +02:00
|
|
|
check_call(['nmcli', 'connection', 'down', TunnelActiv.active()])
|
|
|
|
check_call(['nmcli', 'connection', 'import', 'type', 'wireguard', 'file', home / 'tester' /
|
|
|
|
path_split])
|
2024-08-19 08:29:35 +02:00
|
|
|
else:
|
2024-09-08 20:31:46 +02:00
|
|
|
shutil.copy(filepath, home / 'tester/')
|
2024-08-23 14:37:19 +02:00
|
|
|
if self.a != '':
|
2024-09-08 20:31:46 +02:00
|
|
|
check_call(['nmcli', 'connection', 'down', TunnelActiv.active()])
|
|
|
|
check_call(['nmcli', 'connection', 'import', 'type', 'wireguard', 'file', filepath])
|
2024-08-23 14:37:19 +02:00
|
|
|
|
|
|
|
self.StrVar.set('')
|
2024-08-19 08:29:35 +02:00
|
|
|
self.a = TunnelActiv.active()
|
|
|
|
self.l_box.insert(0, self.a)
|
|
|
|
self.l_box.update()
|
|
|
|
self.StrVar = tk.StringVar()
|
|
|
|
self.StrVar.set(self.a)
|
2024-08-21 22:39:18 +02:00
|
|
|
GreenLabel.green_show_label(self)
|
2024-08-21 12:51:07 +02:00
|
|
|
StartStopBTN.button_stop(self)
|
2024-09-08 20:31:46 +02:00
|
|
|
wg_read = home / 'tester/' / str(self.a + '.conf')
|
|
|
|
with open(wg_read, 'r') as file:
|
|
|
|
data = ConToDict.covert_to_dict(file)
|
2024-08-23 14:37:19 +02:00
|
|
|
# Address Label
|
|
|
|
ShowAddress.init_and_report(self, data)
|
2024-08-23 02:00:24 +02:00
|
|
|
ShowAddress.show_data(self)
|
2024-08-19 23:28:53 +02:00
|
|
|
file.close()
|
2024-09-08 20:31:46 +02:00
|
|
|
check_call(['nmcli', 'con', 'mod', self.a, 'connection.autoconnect', 'no'])
|
2024-08-31 17:50:42 +02:00
|
|
|
if 'PrivateKey = ' not in read:
|
2024-08-19 08:29:35 +02:00
|
|
|
Message()
|
|
|
|
except EOFError:
|
|
|
|
pass
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2024-08-14 22:05:00 +02:00
|
|
|
|
|
|
|
|
2024-09-03 21:27:36 +02:00
|
|
|
class FileHandle:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
2024-09-04 09:18:47 +02:00
|
|
|
self.wg_autostart = None
|
|
|
|
self.autoconnect = None
|
2024-09-03 21:27:36 +02:00
|
|
|
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):
|
|
|
|
try:
|
|
|
|
self.select_tunnel = self.l_box.curselection()
|
|
|
|
select_tl = self.l_box.get(self.select_tunnel[0])
|
|
|
|
if self.selected_option.get() == 0:
|
2024-09-08 20:31:46 +02:00
|
|
|
off = open(path_to_file, 'w')
|
|
|
|
off.write('false ' + '\n')
|
2024-09-07 23:52:16 +02:00
|
|
|
off.close()
|
2024-09-04 20:52:44 +02:00
|
|
|
if self.selected_option.get() >= 1:
|
2024-09-08 20:31:46 +02:00
|
|
|
set_on = open(path_to_file, 'w')
|
|
|
|
set_on.write('true ' + select_tl)
|
2024-09-03 21:27:36 +02:00
|
|
|
set_on.close()
|
2024-09-05 22:17:31 +02:00
|
|
|
|
2024-09-03 21:27:36 +02:00
|
|
|
except IndexError:
|
|
|
|
self.selected_option.set(1)
|
|
|
|
|
2024-09-04 20:52:44 +02:00
|
|
|
OnOff.on_off(self)
|
|
|
|
|
|
|
|
|
|
|
|
class OnOff(tk.Tk, FileHandle):
|
|
|
|
def __init__(self, container, **kwargs):
|
|
|
|
super().__init__(container, **kwargs)
|
2024-09-05 22:17:31 +02:00
|
|
|
self.lb_frame_buttons = None
|
2024-09-04 20:52:44 +02:00
|
|
|
|
|
|
|
def on_off(self):
|
2024-09-08 20:31:46 +02:00
|
|
|
with open(path_to_file, 'r') as file:
|
|
|
|
for line in file.readlines():
|
|
|
|
a_connect = line
|
|
|
|
if 'true' in a_connect:
|
|
|
|
self.selected_option.set(1)
|
|
|
|
self.autoconnect_var.set('')
|
|
|
|
self.auto_con = a_connect[5:]
|
|
|
|
else:
|
|
|
|
self.wg_autostart.configure(state='disabled')
|
|
|
|
self.auto_con = 'no Autoconnect'
|
|
|
|
file.close()
|
2024-09-04 09:18:47 +02:00
|
|
|
|
|
|
|
self.autoconnect_var = tk.StringVar()
|
|
|
|
self.autoconnect_var.set(self.auto_con)
|
2024-09-05 22:17:31 +02:00
|
|
|
self.autoconnect = tk.Label(self, textvariable=self.autoconnect_var, fg='blue', padx=5)
|
2024-09-04 09:18:47 +02:00
|
|
|
self.autoconnect.config(font=('Ubuntu', 11))
|
2024-09-05 22:17:31 +02:00
|
|
|
self.autoconnect.grid(column=0, row=4, sticky='ne', pady=20)
|
2024-09-04 09:18:47 +02:00
|
|
|
|
2024-09-03 21:27:36 +02:00
|
|
|
|
2024-08-19 08:29:35 +02:00
|
|
|
class ExportTunnels:
|
|
|
|
@staticmethod
|
|
|
|
def wg_export():
|
2024-08-26 19:50:06 +02:00
|
|
|
now_time = datetime.now()
|
2024-09-08 20:31:46 +02:00
|
|
|
now_datetime = now_time.strftime('wg-exp-' + '%m-%d-%Y' + '-' + '%H:%M')
|
2024-08-26 19:50:06 +02:00
|
|
|
tl = ListTunnels.tl_list()
|
2024-08-19 08:29:35 +02:00
|
|
|
try:
|
2024-08-26 19:50:06 +02:00
|
|
|
if len(tl) != 0:
|
2024-09-08 20:31:46 +02:00
|
|
|
wg_tar = home / now_datetime
|
|
|
|
p_to_conf = home / 'tester/'
|
2024-08-26 19:50:06 +02:00
|
|
|
shutil.make_archive(wg_tar, 'zip', p_to_conf)
|
|
|
|
#if zip_full != 0:
|
2024-09-03 21:27:36 +02:00
|
|
|
#print('Export erfolgraeich')
|
2024-08-26 19:50:06 +02:00
|
|
|
#else:
|
2024-09-03 21:27:36 +02:00
|
|
|
#print('ups etwwas ging schief bitte Export wiederholen')
|
2024-08-26 19:50:06 +02:00
|
|
|
else:
|
|
|
|
print('No Tunnel for Export')
|
2024-08-25 20:28:32 +02:00
|
|
|
except TypeError:
|
2024-08-19 08:29:35 +02:00
|
|
|
pass
|