diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index d3fedc8..57987ad 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,7 +4,7 @@
-
+
@@ -478,7 +478,15 @@
1726860371820
-
+
+
+ 1726915238475
+
+
+
+ 1726915238475
+
+
@@ -519,7 +527,6 @@
-
@@ -544,6 +551,7 @@
-
+
+
\ No newline at end of file
diff --git a/wg_func.py b/wg_func.py
index 5f34f2b..c0a35d1 100755
--- a/wg_func.py
+++ b/wg_func.py
@@ -1,6 +1,7 @@
# Wireguard functions for Wire-Py
import os
import shutil
+import subprocess
import zipfile
from datetime import datetime
from tkinter import filedialog, ttk
@@ -156,7 +157,11 @@ class ConToDict:
if ',' in dns:
dns = dns[:-1]
endpoint = final_dict['Endpoint']
- return address, dns, endpoint
+ if 'PresharedKey' in final_dict:
+ pre_key = final_dict['PresharedKey']
+ else:
+ pre_key = final_dict['PreSharedKey']
+ return address, dns, endpoint, pre_key
class TunnelActiv:
@@ -242,40 +247,55 @@ class ImportTunnel:
path_split1 = path_split[-1]
self.a = TunnelActiv.active()
if 'PrivateKey = ' in read and 'PublicKey = ' in read:
- 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))
+ with open(filepath, 'r') as file:
+ key = ConToDict.covert_to_dict(file)
+ pre_key = key[3]
+ print(type(pre_key))
+ if len(pre_key) != 0:
+ with open('/etc/wire_py/.keys', 'r') as readfile:
+ if pre_key in readfile:
+ print('Tunnel already available! Please use another file for import')
+ else:
+ with open('/etc/wire_py/.keys', 'a') as keyfile:
+ keyfile.write('\r\n' + pre_key)
+ 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)
- if self.a != '':
- check_call(['nmcli', 'connection', 'down', TunnelActiv.active()])
- ShowAddress.label_empty(self)
- check_call(['nmcli', 'connection', 'import', 'type', 'wireguard', 'file', Path('/etc') / 'wire_py' /
- path_split])
+ 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)
- check_call(['nmcli', 'connection', 'import', 'type', 'wireguard', 'file', filepath])
+ 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)
+ # Address Label
+ ShowAddress.init_and_report(self, data)
+ ShowAddress.show_data(self)
+ check_call(['nmcli', 'con', 'mod', self.a, 'connection.autoconnect', 'no'])
+ os.chmod(str(wg_read), 0o600)
- 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:
- data = ConToDict.covert_to_dict(file)
- # Address Label
- ShowAddress.init_and_report(self, data)
- ShowAddress.show_data(self)
- check_call(['nmcli', 'con', 'mod', self.a, 'connection.autoconnect', 'no'])
- os.chmod(str(wg_read), 0o600)
if 'PrivateKey = ' not in read:
msg_window()
except EOFError:
@@ -284,6 +304,8 @@ class ImportTunnel:
pass
except FileNotFoundError:
pass
+ except subprocess.CalledProcessError:
+ print('Tunnel exist!')
class FileHandle:
@@ -363,6 +385,3 @@ class ExportTunnels:
except TypeError:
pass
-
-
-
diff --git a/wg_main.py b/wg_main.py
index e96024d..170c0c5 100755
--- a/wg_main.py
+++ b/wg_main.py
@@ -1,4 +1,5 @@
#!/usr/bin/python3
+import os
import tkinter as tk
from subprocess import check_call
from tkinter import *
@@ -18,7 +19,7 @@ class MainWindow(tk.Tk):
self.switch_on = None
self.switch_off = None
self.x_width = 600
- self.y_height = 340
+ self.y_height = 330
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)
@@ -156,6 +157,10 @@ class FrameWidgets(ttk.Frame):
try:
self.select_tunnel = self.l_box.curselection()
select_tl = self.l_box.get(self.select_tunnel[0])
+ with open('/etc/wire_py/' + select_tl + '.conf', 'r+') as file2:
+ key = ConToDict.covert_to_dict(file2)
+ pre_key = key[3]
+ print(pre_key)
check_call(['nmcli', 'connection', 'delete', select_tl])
self.l_box.delete(self.select_tunnel[0])
if Path.is_file(Path('/etc/wire_py/wg_py')):
@@ -167,11 +172,16 @@ class FrameWidgets(ttk.Frame):
self.wg_autostart.configure(state='disabled')
Path.unlink(path_to_file)
Path.unlink(Path('/etc/wire_py') / str(select_tl + '.conf'))
+ with open('/etc/wire_py/.keys', 'r') as readfile:
+ with open('/etc/wire_py/.keys2', 'w') as writefile:
+ for line in readfile:
+ if pre_key not in line.strip("\n"):
+ writefile.write(line)
+ os.replace('/etc/wire_py/.keys2', '/etc/wire_py/.keys')
# for disable checkbox when Listbox empty
tl = ListTunnels.tl_list()
if len(tl) == 0:
self.wg_autostart.configure(state='disabled')
-
if self.a != '' and self.a == select_tl:
self.StrVar.set(value='')
StartStopBTN.button_start(self)
@@ -180,7 +190,6 @@ class FrameWidgets(ttk.Frame):
self.add.set('')
self.DNS.set('')
self.enp.set('')
-
return select_tl
except IndexError:
pass
@@ -194,9 +203,9 @@ class FrameWidgets(ttk.Frame):
self.btn_exp.grid(column=0, row=3, padx=15, pady=8)
# Label Entry
- self.lb_rename = ttk.Entry(self, width=45)
- self.lb_rename.grid(column=2, row=4, padx=30, pady=15, sticky='snw')
- self.lb_rename.insert(0, 'Max. 12 characters without blanks!')
+ self.lb_rename = ttk.Entry(self, width=20)
+ self.lb_rename.grid(column=2, row=4, padx=30, pady=5, sticky='w')
+ self.lb_rename.insert(0, 'Max. 12 characters!')
self.lb_rename.config(state='disable')
def tl_rename():
@@ -222,7 +231,7 @@ class FrameWidgets(ttk.Frame):
# Button Rename
self.btn_rename = ttk.Button(self, text='Rename', state='disable', command=tl_rename)
- self.btn_rename.grid(column=2, row=4, padx=20, pady=15, sticky='e')
+ self.btn_rename.grid(column=2, row=4, padx=20, pady=10, sticky='e')
# Check Buttons
self.selected_option = tk.IntVar()
@@ -230,7 +239,7 @@ class FrameWidgets(ttk.Frame):
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)
+ self.autoconnect.grid(column=0, row=4, sticky='ne', pady=10)
self.wg_autostart = tk.Checkbutton(self,
text='Autoconnect on:',
variable=self.selected_option,