little fixes replace os.system with check_call

first steps in install Script add wg_start.service file
This commit is contained in:
2024-09-08 20:31:46 +02:00
parent 607589d44a
commit 35406026f0
6 changed files with 109 additions and 56 deletions

View File

@ -4,8 +4,12 @@ import shutil
from datetime import datetime
from tkinter import filedialog, ttk
import tkinter as tk
from pathlib import Path
from subprocess import check_call
home = Path.home()
font_color = '#4011a7'
path_to_file = home / '.config/wire_py/wg_py'
class Message(tk.Tk):
@ -23,7 +27,8 @@ class Message(tk.Tk):
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,
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)
@ -160,11 +165,10 @@ class ListTunnels:
return tl
class ImportTunnel:
def __init__(self):
self.enp = None
self.DNS = None
self.add = None
class ImportTunnel(tk.Tk):
def __init__(self, container, **kwargs):
super().__init__(container, **kwargs)
self.select_tunnel = None
self.wg_switch = None
self.btn_stst = None
@ -175,7 +179,7 @@ class ImportTunnel:
def wg_import_select(self):
try:
filepath = filedialog.askopenfilename(initialdir=os.environ['HOME'], title='Select Wireguard config File',
filepath = filedialog.askopenfilename(initialdir=home, title='Select Wireguard config File',
filetypes=[('WG config files', '*.conf')])
file = open(filepath, 'r')
read = file.read()
@ -184,18 +188,18 @@ class ImportTunnel:
path_split1 = path_split[-1]
if 'PrivateKey = ' in read and 'PublicKey = ' in read:
if len(path_split1) > 17:
p1 = shutil.copy(filepath, os.environ['HOME'] + '/tester/')
p1 = shutil.copy(filepath, home / 'tester/')
path_split = path_split1[len(path_split1) - 17:]
os.rename(p1, os.environ['HOME'] + '/tester/' + str(path_split))
os.rename(p1, home / 'tester/' / str(path_split))
if self.a != '':
os.system('nmcli connection down ' + str(TunnelActiv.active()))
os.system('nmcli connection import type wireguard file ' + os.environ['HOME'] + '/tester/' +
str(path_split))
check_call(['nmcli', 'connection', 'down', TunnelActiv.active()])
check_call(['nmcli', 'connection', 'import', 'type', 'wireguard', 'file', home / 'tester' /
path_split])
else:
shutil.copy(filepath, os.environ['HOME'] + '/tester/')
shutil.copy(filepath, home / 'tester/')
if self.a != '':
os.system('nmcli connection down ' + str(TunnelActiv.active()))
os.system('nmcli connection import type wireguard file ' + str(filepath))
check_call(['nmcli', 'connection', 'down', TunnelActiv.active()])
check_call(['nmcli', 'connection', 'import', 'type', 'wireguard', 'file', filepath])
self.StrVar.set('')
self.a = TunnelActiv.active()
@ -205,14 +209,14 @@ class ImportTunnel:
self.StrVar.set(self.a)
GreenLabel.green_show_label(self)
StartStopBTN.button_stop(self)
wg_read = os.environ['HOME'] + '/tester/' + str(self.a) + '.conf'
file = open(wg_read, 'r')
data = ConToDict.covert_to_dict(file)
wg_read = home / 'tester/' / 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)
file.close()
os.system('nmcli con mod ' + str(self.a) + ' connection.autoconnect no')
check_call(['nmcli', 'con', 'mod', self.a, 'connection.autoconnect', 'no'])
if 'PrivateKey = ' not in read:
Message()
except EOFError:
@ -241,11 +245,12 @@ class FileHandle:
self.select_tunnel = self.l_box.curselection()
select_tl = self.l_box.get(self.select_tunnel[0])
if self.selected_option.get() == 0:
off = open('wg_py', 'w')
off = open(path_to_file, 'w')
off.write('false ' + '\n')
off.close()
if self.selected_option.get() >= 1:
set_on = open('wg_py', 'w')
set_on.write(select_tl)
set_on = open(path_to_file, 'w')
set_on.write('true ' + select_tl)
set_on.close()
except IndexError:
@ -260,20 +265,17 @@ class OnOff(tk.Tk, FileHandle):
self.lb_frame_buttons = None
def on_off(self):
a_connect = {}
on_or_off = open('wg_py', 'r')
for line in on_or_off.readlines():
line_splitted = line.split()
a_connect[line_splitted[0]] = line_splitted[1]
if 'true' in a_connect:
self.selected_option.set(1)
self.autoconnect_var.set('')
self.auto_con = a_connect['true']
else:
self.wg_autostart.configure(state='disabled')
self.auto_con = 'no Autoconnect'
on_or_off.close()
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()
self.autoconnect_var = tk.StringVar()
self.autoconnect_var.set(self.auto_con)
@ -286,12 +288,12 @@ class ExportTunnels:
@staticmethod
def wg_export():
now_time = datetime.now()
now_datetime = now_time.strftime('/wg-exp-' + '%m-%d-%Y' + '-' + '%H:%M')
now_datetime = now_time.strftime('wg-exp-' + '%m-%d-%Y' + '-' + '%H:%M')
tl = ListTunnels.tl_list()
try:
if len(tl) != 0:
wg_tar = os.environ['HOME'] + now_datetime
p_to_conf = os.environ['HOME'] + '/tester'
wg_tar = home / now_datetime
p_to_conf = home / 'tester/'
shutil.make_archive(wg_tar, 'zip', p_to_conf)
#if zip_full != 0:
#print('Export erfolgraeich')