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-10-26 12:35:18 +02:00
|
|
|
import time
|
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-21 22:11:24 +02:00
|
|
|
from tkinter import ttk
|
2024-10-12 13:36:27 +02:00
|
|
|
import requests
|
|
|
|
|
2024-10-20 02:10:23 +02:00
|
|
|
|
2024-10-12 13:36:27 +02:00
|
|
|
''' 1 = 1. Year, 09 = Month of the Year, 2924 = Day and Year of the Year '''
|
2024-10-21 22:11:24 +02:00
|
|
|
version = 'v. 1.10.2124'
|
2024-08-18 22:33:16 +02:00
|
|
|
|
2024-10-28 07:13:44 +01:00
|
|
|
path_to_file2 = Path('/etc/wire_py/settings.conf')
|
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()
|
2024-10-28 07:13:44 +01:00
|
|
|
if 'on\n' in set_file:
|
2024-10-16 20:38:36 +02:00
|
|
|
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-20 02:10:23 +02:00
|
|
|
|
2024-10-28 07:13:44 +01:00
|
|
|
|
|
|
|
def msg_window(img_w, img_i, w_title, w_txt, txt2=None, com=None):
|
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-10-28 07:13:44 +01:00
|
|
|
|
2024-09-22 16:24:22 +02:00
|
|
|
label = tk.Label(msg, text=w_txt)
|
2024-10-28 07:13:44 +01:00
|
|
|
|
2024-09-14 23:25:48 +02:00
|
|
|
label.grid(column=1, row=0)
|
2024-10-28 07:13:44 +01:00
|
|
|
|
|
|
|
if txt2 != None and com != None:
|
|
|
|
label.config(font=('Ubuntu', 11), padx=15, justify='left')
|
|
|
|
msg.i_window.grid(column=0, row=0, sticky='nw')
|
|
|
|
button2 = ttk.Button(msg, text=f'{txt2}', command=com, padding=4)
|
|
|
|
button2.grid(column=0, row=1, sticky='e', columnspan=2)
|
|
|
|
button = ttk.Button(msg, text='OK', command=msg.destroy, padding=4)
|
|
|
|
button.grid(column=0, row=1, sticky='w', columnspan=2)
|
|
|
|
|
|
|
|
else:
|
|
|
|
label.config(font=('Ubuntu', 11), padx=15)
|
|
|
|
msg.i_window.grid(column=0, row=0)
|
|
|
|
button = ttk.Button(msg, text='OK', command=msg.destroy, padding=4)
|
|
|
|
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-10-26 12:35:18 +02:00
|
|
|
class Tunnel:
|
|
|
|
|
2024-09-22 17:17:02 +02:00
|
|
|
"""
|
2024-11-07 18:04:58 +01:00
|
|
|
Class of Methods for Wire-Py
|
2024-09-22 17:17:02 +02:00
|
|
|
"""
|
2024-09-26 21:18:48 +02:00
|
|
|
|
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
|
2024-10-26 12:35:18 +02:00
|
|
|
def con_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-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-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
|
2024-10-26 12:35:18 +02:00
|
|
|
def list():
|
2024-08-19 08:29:35 +02:00
|
|
|
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-10-26 12:35:18 +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
|
|
|
|
"""
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def export():
|
|
|
|
_u1 = str(_u[6:])
|
|
|
|
now_time = datetime.now()
|
|
|
|
now_datetime = now_time.strftime('wg-exp-' + '%m-%d-%Y' + '-' + '%H:%M')
|
|
|
|
tl = Tunnel.list()
|
|
|
|
|
|
|
|
try:
|
|
|
|
if len(tl) != 0:
|
|
|
|
wg_tar = str(_u) + '/' + now_datetime
|
|
|
|
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)
|
2024-10-28 07:13:44 +01:00
|
|
|
Path.unlink(Path(source) / 'settings.conf', missing_ok=True)
|
2024-10-26 12:35:18 +02:00
|
|
|
shutil.make_archive(wg_tar, 'zip', source)
|
|
|
|
shutil.chown(wg_tar + '.zip', 1000, 1000)
|
|
|
|
shutil.rmtree(source)
|
|
|
|
with zipfile.ZipFile((wg_tar + '.zip'), 'r') as zf:
|
|
|
|
if len(zf.namelist()) != 0:
|
|
|
|
|
|
|
|
"""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 = 'Export 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 = 'Export error'
|
|
|
|
msg_t = 'Export failed! Please try again'
|
|
|
|
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/info.png'
|
|
|
|
ii = r'/usr/share/icons/wp-icons/48/wg_msg.png'
|
|
|
|
wt = 'Select tunnel'
|
|
|
|
msg_t = 'Please first import tunnel.'
|
|
|
|
msg_window(iw, ii, wt, msg_t)
|
|
|
|
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
2024-10-28 07:13:44 +01:00
|
|
|
@staticmethod
|
|
|
|
def if_tip():
|
|
|
|
with open(path_to_file2, 'r') as set_file2:
|
|
|
|
lines2 = set_file2.readlines()
|
|
|
|
if 'False\n' in lines2:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
tips = Tunnel.if_tip()
|
2024-10-26 12:35:18 +02:00
|
|
|
|
2024-10-04 18:37:21 +02:00
|
|
|
|
2024-10-18 22:33:20 +02:00
|
|
|
|