2024-08-14 22:05:00 +02:00
|
|
|
# Wireguard functions for Wire-Py
|
|
|
|
import os
|
2024-08-17 00:30:42 +02:00
|
|
|
import subprocess
|
|
|
|
from tkinter import filedialog
|
|
|
|
import tkinter as tk
|
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)
|
|
|
|
|
|
|
|
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)
|
2024-08-17 00:30:42 +02:00
|
|
|
|
|
|
|
|
2024-08-17 15:42:57 +02:00
|
|
|
def active(): # Shows the active tunnel
|
|
|
|
a = os.popen('nmcli con show --active | grep -iPo "(.*)(wireguard)"').read().split()
|
|
|
|
if not a:
|
|
|
|
a = ''
|
|
|
|
return a
|
|
|
|
else:
|
|
|
|
a = a[0]
|
|
|
|
return a
|
|
|
|
|
|
|
|
|
|
|
|
a = active()
|
|
|
|
|
|
|
|
|
|
|
|
def tl_list():
|
|
|
|
wg_s = os.popen('nmcli con show | grep -iPo "(.*)(wireguard)"').read().split()
|
2024-08-18 22:33:16 +02:00
|
|
|
tl = wg_s[::3] # tl = Tunnel list # Show of 4.Element in list
|
2024-08-17 15:42:57 +02:00
|
|
|
return tl
|
|
|
|
|
|
|
|
|
2024-08-17 00:30:42 +02:00
|
|
|
def wg_import_select():
|
|
|
|
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()
|
|
|
|
pathsplit = filepath.split("/")
|
2024-08-17 15:42:57 +02:00
|
|
|
pathsplit1 = pathsplit[-1]
|
2024-08-17 00:30:42 +02:00
|
|
|
if "PrivateKey = " in read and "PublicKey = " in read:
|
2024-08-17 15:42:57 +02:00
|
|
|
if len(pathsplit1) > 17:
|
|
|
|
pathsplit = pathsplit1[len(pathsplit1) - 17:]
|
|
|
|
os.rename(filepath, os.environ['HOME'] + '/tester/' + str(pathsplit))
|
|
|
|
os.system('nmcli connection down ' + str(a))
|
|
|
|
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(a))
|
|
|
|
os.system('nmcli connection import type wireguard file ' + str(filepath))
|
|
|
|
os.system('nmcli con mod ' + str(pathsplit1[:-5]) + ' connection.autoconnect no')
|
|
|
|
if "PrivateKey = " not in read:
|
2024-08-18 22:33:16 +02:00
|
|
|
Message()
|
2024-08-17 00:30:42 +02:00
|
|
|
except EOFError:
|
|
|
|
pass
|
|
|
|
except TypeError:
|
|
|
|
pass
|
2024-08-14 22:05:00 +02:00
|
|
|
|
|
|
|
|
2024-08-17 00:30:42 +02:00
|
|
|
def wg_export():
|
|
|
|
try:
|
|
|
|
wg_exp = os.popen('nmcli con show | grep -iPo "(.*)(wireguard)"').read().split()
|
|
|
|
wg_exp = wg_exp[1]
|
|
|
|
return wg_exp
|
|
|
|
except IndexError:
|
|
|
|
pass
|