little fixes replace os.system with check_call

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

View File

@ -5,12 +5,14 @@
</component>
<component name="ChangeListManager">
<list default="true" id="940e1630-c825-4d4c-be80-bc11f543c122" name="Changes" comment="little fixes">
<change afterPath="$PROJECT_DIR$/start_wg.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/wg_py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/home/punix/.config/wire_py/wg_py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/install.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/punix/.config/wire_py/wg_py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/main.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/start_wg.py" beforeDir="false" afterPath="$PROJECT_DIR$/start_wg.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/wg_func.py" beforeDir="false" afterPath="$PROJECT_DIR$/wg_func.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/wg_py.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/wg_py" beforeDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -47,6 +49,7 @@
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;ASKED_ADD_EXTERNAL_FILES&quot;: &quot;true&quot;,
&quot;Python.install.executor&quot;: &quot;Run&quot;,
&quot;Python.main.executor&quot;: &quot;Run&quot;,
&quot;Python.start_wg.executor&quot;: &quot;Run&quot;,
&quot;Python.wg_func.executor&quot;: &quot;Run&quot;,
@ -331,7 +334,15 @@
<option name="project" value="LOCAL" />
<updated>1725639633873</updated>
</task>
<option name="localTasksCounter" value="27" />
<task id="LOCAL-00027" summary="little fixes">
<option name="closed" value="true" />
<created>1725746027132</created>
<option name="number" value="00027" />
<option name="presentableId" value="LOCAL-00027" />
<option name="project" value="LOCAL" />
<updated>1725746027132</updated>
</task>
<option name="localTasksCounter" value="28" />
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">

17
install.py Executable file
View File

@ -0,0 +1,17 @@
#!/usr/bin/python3
import os
from pathlib import Path
home = Path.home()
# for services file in /lib/systemd/system/
path_for_a_con = home / '.config/wire_py/wg_py'
# create dir in home
mkdir = home / '.config/wire_py'
os.makedirs(mkdir, exist_ok=True)
with open(mkdir / 'wg_py', 'w+') as file:
if 'true' not in file:
file.write('false')
file.close()

35
main.py
View File

@ -1,12 +1,14 @@
#!/usr/bin/python3
import os
import tkinter as tk
from subprocess import check_call
from tkinter import ttk
from pathlib import Path
from wg_func import (TunnelActiv, ListTunnels, ImportTunnel, ConToDict, GreenLabel, StartStopBTN, ShowAddress,
FileHandle, ExportTunnels, OnOff)
font_color = '#4011a7'
home = Path.home()
class MainWindow(tk.Tk):
@ -126,9 +128,9 @@ class FrameWidgets(ttk.Frame):
# Button Vpn
if self.a != '':
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)
@ -151,9 +153,20 @@ class FrameWidgets(ttk.Frame):
try:
self.select_tunnel = self.l_box.curselection()
select_tl = self.l_box.get(self.select_tunnel[0])
os.system('nmcli connection delete ' + str(select_tl))
check_call(['nmcli', 'connection', 'delete', select_tl])
self.l_box.delete(self.select_tunnel[0])
os.remove(os.environ['HOME'] + '/tester/' + str(select_tl) + '.conf')
os.remove(home / 'tester/' / str(select_tl + '.conf'))
path_to_file = home / '.config/wire_py/wg_py'
with open(path_to_file, 'r') as file2:
for line in file2.readlines():
a_con = line[5:]
if select_tl == a_con:
self.selected_option.set(0)
self.auto_con = 'no Autoconnect'
with open(path_to_file, 'w') as file2:
file2.write('false')
file2.close()
# for disable checkbox when Listbox empty
tl = ListTunnels.tl_list()
@ -209,10 +222,10 @@ class FrameWidgets(ttk.Frame):
StartStopBTN.button_start(self)
self.select_tunnel = self.l_box.curselection()
select_tl = self.l_box.get(self.select_tunnel[0])
os.system('nmcli connection up ' + str(select_tl))
wg_read = os.environ['HOME'] + '/tester/' + str(select_tl) + '.conf'
file = open(wg_read, 'r')
data = ConToDict.covert_to_dict(file)
check_call(['nmcli', 'connection', 'up', select_tl])
wg_read = home / 'tester/' / str(select_tl + '.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)
@ -226,7 +239,7 @@ class FrameWidgets(ttk.Frame):
elif self.a != '':
# Button Start/Stop
StartStopBTN.button_stop(self)
os.system('nmcli connection down ' + str(self.a))
check_call(['nmcli', 'connection', 'down', self.a])
# Button Start/Stop
StartStopBTN.button_start(self)
self.a = TunnelActiv.active()

View File

@ -3,10 +3,10 @@ from subprocess import check_call
from pathlib import Path
home = Path.home()
path_to_file = home / 'Pyapps/wire-py/wg_py'
path_to_file = home / '.config/wire_py/wg_py'
with open(path_to_file, 'r') as file:
for line in file.readlines():
a_con = line[5:]
check_call(['nmcli', 'connection', 'up', a_con])
file.close()

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')

10
wg_start.service Normal file
View File

@ -0,0 +1,10 @@
[Unit]
Description=Automatic Tunnel Start
After=network-online.target
[Service]
Type=oneshot
ExecStartPre=/bin/sleep 3
ExecStart=/bin/start_wg.py
[Install]
WantedBy=multi-user.target