2024-10-04 18:37:21 +02:00
|
|
|
""" Wireguard Classes and Method for Wire-Py """
|
|
|
|
|
2024-08-14 22:05:00 +02:00
|
|
|
import os
|
2024-08-25 15:39:23 +02:00
|
|
|
import shutil
|
2024-09-22 00:56:08 +02:00
|
|
|
import subprocess
|
2024-09-28 14:13:07 +02:00
|
|
|
import tkinter as tk
|
2024-09-20 14:55:15 +02:00
|
|
|
import zipfile
|
2024-08-26 19:50:06 +02:00
|
|
|
from datetime import datetime
|
2024-09-08 20:31:46 +02:00
|
|
|
from pathlib import Path
|
|
|
|
from subprocess import check_call
|
2024-10-12 13:36:27 +02:00
|
|
|
from tkinter import filedialog, ttk
|
|
|
|
import requests
|
|
|
|
|
|
|
|
''' 1 = 1. Year, 09 = Month of the Year, 2924 = Day and Year of the Year '''
|
2024-10-16 20:38:36 +02:00
|
|
|
version = 'v. 1.10.1624'
|
2024-08-18 22:33:16 +02:00
|
|
|
|
2024-09-17 20:57:11 +02:00
|
|
|
path_to_file = Path('/etc/wire_py/wg_py')
|
2024-10-12 13:36:27 +02:00
|
|
|
path_to_file2 = Path('/etc/wire_py/settings')
|
|
|
|
path_to_file3 = Path('/etc/wire_py/theme')
|
2024-09-19 22:23:34 +02:00
|
|
|
_u = Path.read_text(Path('/tmp/_u'))
|
2024-09-14 23:25:48 +02:00
|
|
|
|
2024-10-12 13:36:27 +02:00
|
|
|
UPDATE_API_URL = 'https://git.ilunix.de/api/v1/repos/punix/Wire-Py/releases'
|
|
|
|
|
2024-10-16 20:38:36 +02:00
|
|
|
|
2024-10-12 13:36:27 +02:00
|
|
|
class WirePyUpdate:
|
|
|
|
@staticmethod
|
|
|
|
def api_down():
|
2024-10-16 20:38:36 +02:00
|
|
|
try:
|
|
|
|
response = requests.get(UPDATE_API_URL)
|
|
|
|
response_dict = response.json()
|
|
|
|
response_dict = response_dict[0]
|
|
|
|
with open(path_to_file2, 'r') as set_file:
|
|
|
|
set_file = set_file.read()
|
|
|
|
if 'Update on' in set_file:
|
|
|
|
if version[3:] != response_dict['tag_name']:
|
|
|
|
return response_dict['tag_name']
|
2024-10-12 13:36:27 +02:00
|
|
|
else:
|
2024-10-16 20:38:36 +02:00
|
|
|
return 'No Updates'
|
|
|
|
else:
|
|
|
|
return 'False'
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
return 'No Internet Connection!'
|
2024-10-12 13:36:27 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def download():
|
|
|
|
try:
|
|
|
|
url = f'https://git.ilunix.de/punix/Wire-Py/archive/{res}.zip'
|
|
|
|
to_down = 'wget -qP ' + str(_u) + ' ' + url
|
|
|
|
result = subprocess.call(to_down, shell=True)
|
|
|
|
if result == 0:
|
|
|
|
shutil.chown(str(_u) + f'/{res}.zip', 1000, 1000)
|
|
|
|
"""img_w, img_i, w_title, w_txt hand over"""
|
|
|
|
iw = r'/usr/share/icons/wp-icons/64/info.png'
|
|
|
|
ii = r'/usr/share/icons/wp-icons/48/wg_vpn.png'
|
|
|
|
wt = 'Download Successful'
|
|
|
|
msg_t = 'Your zip file is in home directory'
|
|
|
|
msg_window(iw, ii, wt, msg_t)
|
|
|
|
|
|
|
|
else:
|
|
|
|
"""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 = 'Download error'
|
|
|
|
msg_t = 'Download failed! Please try again'
|
|
|
|
msg_window(iw, ii, wt, msg_t)
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
"""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 = 'Download error'
|
|
|
|
msg_t = 'Download failed! No internet connection!'
|
|
|
|
msg_window(iw, ii, wt, msg_t)
|
|
|
|
|
|
|
|
|
|
|
|
res = WirePyUpdate.api_down()
|
|
|
|
|
2024-10-04 18:37:21 +02:00
|
|
|
def msg_window(img_w, img_i, w_title, w_txt):
|
2024-09-22 16:24:22 +02:00
|
|
|
"""
|
2024-09-22 17:17:02 +02:00
|
|
|
Function for different message windows for the user. with 4 arguments to be passed.
|
|
|
|
To create messages with your own images, icons, and titles. As an alternative to Python Messagebox.
|
|
|
|
Paths to images must be specified: r'/usr/share/icons/wp-icons/64/info.png'
|
2024-09-22 16:24:22 +02:00
|
|
|
img_w = Image for Tk Window
|
|
|
|
img_i = Image for Icon
|
|
|
|
w_title = Windows Title
|
|
|
|
w_txt = Text for Tk Window
|
|
|
|
"""
|
2024-10-12 13:36:27 +02:00
|
|
|
|
2024-09-14 23:25:48 +02:00
|
|
|
msg = tk.Toplevel()
|
|
|
|
msg.resizable(width=False, height=False)
|
2024-09-22 16:24:22 +02:00
|
|
|
msg.title(w_title)
|
2024-10-16 20:38:36 +02:00
|
|
|
msg.configure(pady=15, padx=15)
|
2024-09-22 16:24:22 +02:00
|
|
|
msg.img = tk.PhotoImage(file=img_w)
|
2024-10-16 20:38:36 +02:00
|
|
|
msg.i_window = tk.Label(msg, image=msg.img)
|
2024-09-22 16:24:22 +02:00
|
|
|
msg.i_window.grid(column=0, row=0)
|
|
|
|
label = tk.Label(msg, text=w_txt)
|
2024-10-16 20:38:36 +02:00
|
|
|
label.config(font=('Ubuntu', 11), padx=15)
|
2024-09-14 23:25:48 +02:00
|
|
|
label.grid(column=1, row=0)
|
2024-10-12 13:36:27 +02:00
|
|
|
button = ttk.Button(msg, text='OK', command=msg.destroy, padding=4)
|
|
|
|
button.config()
|
2024-09-14 23:25:48 +02:00
|
|
|
button.grid(column=0, columnspan=2, row=1)
|
2024-09-22 16:24:22 +02:00
|
|
|
img_i = tk.PhotoImage(file=img_i)
|
|
|
|
msg.iconphoto(True, img_i)
|
2024-09-26 21:18:48 +02:00
|
|
|
msg.columnconfigure(0, weight=1)
|
|
|
|
msg.rowconfigure(0, weight=1)
|
2024-09-20 16:06:15 +02:00
|
|
|
msg.winfo_toplevel()
|
|
|
|
|
|
|
|
|
2024-09-14 23:25:48 +02:00
|
|
|
class GreenLabel:
|
2024-09-22 17:17:02 +02:00
|
|
|
"""
|
|
|
|
Show the active tunnel in green in the label
|
|
|
|
"""
|
2024-09-26 21:18:48 +02:00
|
|
|
|
2024-09-14 23:25:48 +02:00
|
|
|
def __init__(self):
|
2024-08-21 11:50:01 +02:00
|
|
|
self.StrVar = None
|
|
|
|
self.lb_tunnel = None
|
|
|
|
|
|
|
|
def green_show_label(self):
|
2024-10-16 20:38:36 +02:00
|
|
|
self.lb_tunnel = ttk.Label(self, textvariable=self.StrVar)
|
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-09-14 23:25:48 +02:00
|
|
|
def columnconfigure(self, param, weight):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def rowconfigure(self, param, weight):
|
|
|
|
pass
|
2024-08-21 11:50:01 +02:00
|
|
|
|
2024-09-14 23:25:48 +02:00
|
|
|
|
|
|
|
class StartStopBTN:
|
2024-09-22 17:17:02 +02:00
|
|
|
"""
|
|
|
|
Show Start and Stop Button in Label
|
|
|
|
"""
|
2024-09-26 21:18:48 +02:00
|
|
|
|
2024-09-14 23:25:48 +02:00
|
|
|
def __init__(self):
|
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
|
2024-09-17 20:57:11 +02:00
|
|
|
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')
|
2024-08-21 12:51:07 +02:00
|
|
|
|
|
|
|
def button_stop(self):
|
2024-10-16 20:38:36 +02:00
|
|
|
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)
|
2024-08-21 12:51:07 +02:00
|
|
|
|
|
|
|
def button_start(self):
|
2024-10-16 20:38:36 +02:00
|
|
|
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)
|
2024-08-21 12:51:07 +02:00
|
|
|
|
|
|
|
|
2024-08-21 11:50:01 +02:00
|
|
|
class ConToDict:
|
2024-09-22 17:17:02 +02:00
|
|
|
"""
|
|
|
|
The config file is packed into a dictionary,
|
|
|
|
to display the values Address , DNS and Peer in the labels
|
|
|
|
"""
|
2024-09-26 21:18:48 +02:00
|
|
|
|
2024-08-21 11:50:01 +02:00
|
|
|
@classmethod
|
|
|
|
def covert_to_dict(cls, file):
|
2024-10-04 18:37:21 +02:00
|
|
|
|
2024-08-21 11:50:01 +02:00
|
|
|
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-10-04 18:37:21 +02:00
|
|
|
|
|
|
|
''' Here is the beginning (Loop) of convert List to Dictionary '''
|
|
|
|
for _ in dictlist:
|
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-10-04 18:37:21 +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-09-22 00:56:08 +02:00
|
|
|
if 'PresharedKey' in final_dict:
|
|
|
|
pre_key = final_dict['PresharedKey']
|
|
|
|
else:
|
|
|
|
pre_key = final_dict['PreSharedKey']
|
|
|
|
return address, dns, endpoint, pre_key
|
2024-08-21 11:50:01 +02:00
|
|
|
|
|
|
|
|
2024-08-18 23:23:43 +02:00
|
|
|
class TunnelActiv:
|
2024-09-22 17:17:02 +02:00
|
|
|
"""
|
|
|
|
Shows the Active Tunnel
|
|
|
|
"""
|
2024-09-26 21:18:48 +02:00
|
|
|
|
2024-08-18 23:23:43 +02:00
|
|
|
@staticmethod
|
2024-10-04 18:37:21 +02:00
|
|
|
def active():
|
|
|
|
|
2024-08-18 23:23:43 +02:00
|
|
|
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-09-14 23:25:48 +02:00
|
|
|
class ShowAddress:
|
2024-09-22 17:17:02 +02:00
|
|
|
"""
|
|
|
|
Displays the value address, DNS and peer in the labels
|
|
|
|
or empty it again
|
|
|
|
"""
|
2024-09-26 21:18:48 +02:00
|
|
|
|
2024-09-14 23:25:48 +02:00
|
|
|
def __init__(self):
|
2024-08-23 02:00:24 +02:00
|
|
|
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):
|
2024-10-04 18:37:21 +02:00
|
|
|
""" Address Label """
|
2024-08-23 12:57:07 +02:00
|
|
|
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):
|
2024-10-04 18:37:21 +02:00
|
|
|
""" Address Label """
|
2024-10-16 20:38:36 +02:00
|
|
|
self.address = ttk.Label(self.lb_frame, textvariable=self.add, foreground='#0071ff')
|
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-10-04 18:37:21 +02:00
|
|
|
|
|
|
|
''' DNS Label '''
|
2024-10-16 20:38:36 +02:00
|
|
|
self.dns = ttk.Label(self.lb_frame, textvariable=self.DNS, foreground='#0071ff')
|
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-10-04 18:37:21 +02:00
|
|
|
|
|
|
|
''' Endpoint Label '''
|
2024-10-16 20:38:36 +02:00
|
|
|
self.endpoint = ttk.Label(self.lb_frame2, textvariable=self.enp, foreground='#0071ff')
|
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:
|
2024-09-22 17:17:02 +02:00
|
|
|
"""
|
|
|
|
Shows all existing Wireguard tunnels
|
|
|
|
"""
|
2024-09-26 21:18:48 +02:00
|
|
|
|
2024-08-19 08:29:35 +02:00
|
|
|
@staticmethod
|
|
|
|
def tl_list():
|
|
|
|
wg_s = os.popen('nmcli con show | grep -iPo "(.*)(wireguard)"').read().split()
|
2024-10-04 18:37:21 +02:00
|
|
|
|
|
|
|
''' tl = Tunnel list # Show of 4.Element in list '''
|
|
|
|
tl = wg_s[::3]
|
2024-08-19 08:29:35 +02:00
|
|
|
return tl
|
|
|
|
|
2024-08-17 15:42:57 +02:00
|
|
|
|
2024-09-14 23:25:48 +02:00
|
|
|
class ImportTunnel:
|
2024-09-22 17:17:02 +02:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2024-09-14 23:25:48 +02:00
|
|
|
|
|
|
|
def __init__(self):
|
2024-09-08 20:31:46 +02:00
|
|
|
|
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):
|
2024-10-04 18:37:21 +02:00
|
|
|
|
2024-08-19 08:29:35 +02:00
|
|
|
try:
|
2024-09-19 22:23:34 +02:00
|
|
|
filepath = filedialog.askopenfilename(initialdir=str(_u), title='Select Wireguard config File',
|
2024-09-20 14:55:15 +02:00
|
|
|
filetypes=[('WG config files', '*.conf')], )
|
2024-09-19 10:33:45 +02:00
|
|
|
|
2024-09-14 23:25:48 +02:00
|
|
|
with open(filepath, 'r') as file:
|
|
|
|
read = file.read()
|
2024-08-31 17:50:42 +02:00
|
|
|
path_split = filepath.split('/')
|
|
|
|
path_split1 = path_split[-1]
|
2024-09-10 20:06:33 +02:00
|
|
|
self.a = TunnelActiv.active()
|
2024-10-04 18:37:21 +02:00
|
|
|
|
2024-08-31 17:50:42 +02:00
|
|
|
if 'PrivateKey = ' in read and 'PublicKey = ' in read:
|
2024-09-22 00:56:08 +02:00
|
|
|
with open(filepath, 'r') as file:
|
|
|
|
key = ConToDict.covert_to_dict(file)
|
|
|
|
pre_key = key[3]
|
|
|
|
if len(pre_key) != 0:
|
|
|
|
with open('/etc/wire_py/.keys', 'r') as readfile:
|
2024-09-22 16:24:22 +02:00
|
|
|
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'
|
2024-10-04 18:37:21 +02:00
|
|
|
msg_window(iw, ii, wt, msg_t)
|
|
|
|
|
2024-09-22 00:56:08 +02:00
|
|
|
else:
|
2024-10-04 18:37:21 +02:00
|
|
|
|
2024-09-22 00:56:08 +02:00
|
|
|
with open('/etc/wire_py/.keys', 'a') as keyfile:
|
2024-09-22 16:24:22 +02:00
|
|
|
keyfile.write(pre_key + '\r')
|
2024-09-22 00:56:08 +02:00
|
|
|
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', TunnelActiv.active()])
|
|
|
|
ShowAddress.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', TunnelActiv.active()])
|
|
|
|
ShowAddress.label_empty(self)
|
|
|
|
|
|
|
|
subprocess.check_output(['nmcli', 'connection', 'import', 'type',
|
|
|
|
'wireguard', 'file', filepath], text=True)
|
|
|
|
|
|
|
|
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 = Path('/etc/wire_py') / str(self.a + '.conf')
|
|
|
|
with open(wg_read, 'r') as file_for_key:
|
|
|
|
data = ConToDict.covert_to_dict(file_for_key)
|
2024-10-04 18:37:21 +02:00
|
|
|
|
|
|
|
''' Address Label '''
|
2024-09-22 00:56:08 +02:00
|
|
|
ShowAddress.init_and_report(self, data)
|
|
|
|
ShowAddress.show_data(self)
|
|
|
|
check_call(['nmcli', 'con', 'mod', self.a, 'connection.autoconnect', 'no'])
|
2024-09-28 14:13:07 +02:00
|
|
|
Path.chmod(wg_read, 0o600)
|
2024-09-22 00:56:08 +02:00
|
|
|
|
2024-08-31 17:50:42 +02:00
|
|
|
if 'PrivateKey = ' not in read:
|
2024-10-04 18:37:21 +02:00
|
|
|
"""img_w, img_i, w_title, w_txt hand over"""
|
2024-09-22 16:24:22 +02:00
|
|
|
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'
|
2024-10-04 18:37:21 +02:00
|
|
|
msg_window(iw, ii, wt, msg_t)
|
|
|
|
|
2024-08-19 08:29:35 +02:00
|
|
|
except EOFError:
|
|
|
|
pass
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2024-09-22 00:56:08 +02:00
|
|
|
except subprocess.CalledProcessError:
|
2024-10-04 18:37:21 +02:00
|
|
|
|
2024-09-22 00:56:08 +02:00
|
|
|
print('Tunnel exist!')
|
2024-08-14 22:05:00 +02:00
|
|
|
|
|
|
|
|
2024-09-03 21:27:36 +02:00
|
|
|
class FileHandle:
|
2024-09-22 17:17:02 +02:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2024-09-26 21:18:48 +02:00
|
|
|
|
2024-09-03 21:27:36 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
def box_set(self):
|
2024-10-04 18:37:21 +02:00
|
|
|
|
2024-09-03 21:27:36 +02:00
|
|
|
try:
|
2024-09-14 23:25:48 +02:00
|
|
|
select_tunnel = self.l_box.curselection()
|
|
|
|
select_tl = self.l_box.get(select_tunnel[0])
|
2024-10-04 18:37:21 +02:00
|
|
|
|
2024-09-03 21:27:36 +02:00
|
|
|
if self.selected_option.get() == 0:
|
2024-09-15 02:09:43 +02:00
|
|
|
Path.unlink(path_to_file)
|
2024-09-10 20:06:33 +02:00
|
|
|
tl = ListTunnels.tl_list()
|
2024-10-04 18:37:21 +02:00
|
|
|
|
2024-09-10 20:06:33 +02:00
|
|
|
if len(tl) == 0:
|
|
|
|
self.wg_autostart.configure(state='disabled')
|
2024-10-04 18:37:21 +02:00
|
|
|
|
2024-09-04 20:52:44 +02:00
|
|
|
if self.selected_option.get() >= 1:
|
2024-09-15 02:09:43 +02:00
|
|
|
Path.write_text(path_to_file, select_tl)
|
2024-10-04 18:37:21 +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)
|
|
|
|
|
|
|
|
|
2024-09-14 23:25:48 +02:00
|
|
|
class OnOff:
|
2024-09-22 17:17:02 +02:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
"""
|
2024-09-26 21:18:48 +02:00
|
|
|
|
2024-09-14 23:25:48 +02:00
|
|
|
def __init__(self):
|
2024-10-04 18:37:21 +02:00
|
|
|
|
2024-09-14 23:25:48 +02:00
|
|
|
self.wg_autostart = None
|
|
|
|
self.selected_option = None
|
|
|
|
self.auto_con = None
|
|
|
|
self.autoconnect = None
|
|
|
|
self.autoconnect_var = None
|
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-10-04 18:37:21 +02:00
|
|
|
|
2024-09-15 02:09:43 +02:00
|
|
|
if Path.exists(path_to_file):
|
|
|
|
self.selected_option.set(1)
|
|
|
|
self.autoconnect_var.set('')
|
2024-09-17 20:57:11 +02:00
|
|
|
if not Path.is_dir(Path('/etc/wire_py')):
|
|
|
|
Path.mkdir(Path('/etc/wire_py'))
|
2024-09-15 02:09:43 +02:00
|
|
|
self.auto_con = Path.read_text(path_to_file)
|
2024-10-04 18:37:21 +02:00
|
|
|
|
2024-09-15 02:09:43 +02:00
|
|
|
else:
|
2024-10-04 18:37:21 +02:00
|
|
|
|
2024-09-15 02:09:43 +02:00
|
|
|
self.wg_autostart.configure(state='disabled')
|
|
|
|
self.auto_con = 'no Autoconnect'
|
|
|
|
self.autoconnect_var.set('')
|
2024-09-04 09:18:47 +02:00
|
|
|
self.autoconnect_var = tk.StringVar()
|
|
|
|
self.autoconnect_var.set(self.auto_con)
|
2024-10-16 20:38:36 +02:00
|
|
|
self.autoconnect = ttk.Label(self, textvariable=self.autoconnect_var)
|
2024-09-04 09:18:47 +02:00
|
|
|
self.autoconnect.config(font=('Ubuntu', 11))
|
2024-10-16 20:38:36 +02:00
|
|
|
self.autoconnect.grid(column=0, row=4, sticky='ne', pady=22)
|
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:
|
2024-09-22 17:17:02 +02:00
|
|
|
"""
|
|
|
|
This will export the tunnels.
|
|
|
|
A zipfile with current date and time is created
|
|
|
|
in the user's home directory with correct right
|
|
|
|
"""
|
2024-09-26 21:18:48 +02:00
|
|
|
|
2024-08-19 08:29:35 +02:00
|
|
|
@staticmethod
|
|
|
|
def wg_export():
|
2024-09-20 21:25:51 +02:00
|
|
|
_u1 = str(_u[6:])
|
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-10-04 18:37:21 +02:00
|
|
|
|
2024-08-19 08:29:35 +02:00
|
|
|
try:
|
2024-08-26 19:50:06 +02:00
|
|
|
if len(tl) != 0:
|
2024-09-19 22:23:34 +02:00
|
|
|
wg_tar = str(_u) + '/' + now_datetime
|
2024-09-28 14:13:07 +02:00
|
|
|
shutil.copytree('/etc/wire_py', '/tmp/wire_py', dirs_exist_ok=True)
|
|
|
|
source = Path('/tmp/wire_py')
|
|
|
|
Path.unlink(Path(source) / 'wg_py', missing_ok=True)
|
|
|
|
Path.unlink(Path(source) / '.keys', missing_ok=True)
|
|
|
|
shutil.make_archive(wg_tar, 'zip', source)
|
|
|
|
shutil.chown(wg_tar + '.zip', 1000, 1000)
|
|
|
|
shutil.rmtree(source)
|
2024-09-20 14:55:15 +02:00
|
|
|
with zipfile.ZipFile((wg_tar + '.zip'), 'r') as zf:
|
|
|
|
if len(zf.namelist()) != 0:
|
2024-10-04 18:37:21 +02:00
|
|
|
|
2024-09-22 16:24:22 +02:00
|
|
|
"""img_w, img_i, w_title, w_txt hand over"""
|
|
|
|
iw = r'/usr/share/icons/wp-icons/64/info.png'
|
|
|
|
ii = r'/usr/share/icons/wp-icons/48/wg_vpn.png'
|
2024-09-26 12:37:36 +02:00
|
|
|
wt = 'Export Successful'
|
2024-09-22 16:24:22 +02:00
|
|
|
msg_t = 'Your zip file is in home directory'
|
2024-10-04 18:37:21 +02:00
|
|
|
msg_window(iw, ii, wt, msg_t)
|
|
|
|
|
2024-09-20 14:55:15 +02:00
|
|
|
else:
|
2024-10-04 18:37:21 +02:00
|
|
|
|
2024-09-22 16:24:22 +02:00
|
|
|
"""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 = 'Export error'
|
|
|
|
msg_t = 'Export failed! Please try again'
|
2024-10-04 18:37:21 +02:00
|
|
|
msg_window(iw, ii, wt, msg_t)
|
|
|
|
|
2024-09-26 21:42:22 +02:00
|
|
|
else:
|
2024-10-04 18:37:21 +02:00
|
|
|
|
|
|
|
"""img_w, img_i, w_title, w_txt hand over"""
|
2024-09-26 21:42:22 +02:00
|
|
|
iw = r'/usr/share/icons/wp-icons/64/info.png'
|
|
|
|
ii = r'/usr/share/icons/wp-icons/48/wg_msg.png'
|
|
|
|
wt = 'Select tunnel'
|
2024-09-28 14:13:07 +02:00
|
|
|
msg_t = 'Please first import tunnel.'
|
2024-10-04 18:37:21 +02:00
|
|
|
msg_window(iw, ii, wt, msg_t)
|
2024-09-20 21:25:51 +02:00
|
|
|
|
2024-08-25 20:28:32 +02:00
|
|
|
except TypeError:
|
2024-10-16 20:38:36 +02:00
|
|
|
pass
|