wire-py-reformat-14-09-2024 #14

Merged
punix merged 5 commits from wire-py-reformat-14-09-2024 into main 2024-09-14 23:31:00 +02:00
6 changed files with 109 additions and 56 deletions
Showing only changes of commit 35406026f0 - Show all commits

View File

@ -5,12 +5,14 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="940e1630-c825-4d4c-be80-bc11f543c122" name="Changes" comment="little fixes"> <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$/home/punix/.config/wire_py/wg_py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/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$/.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$/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_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> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -47,6 +49,7 @@
<component name="PropertiesComponent">{ <component name="PropertiesComponent">{
&quot;keyToString&quot;: { &quot;keyToString&quot;: {
&quot;ASKED_ADD_EXTERNAL_FILES&quot;: &quot;true&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.main.executor&quot;: &quot;Run&quot;,
&quot;Python.start_wg.executor&quot;: &quot;Run&quot;, &quot;Python.start_wg.executor&quot;: &quot;Run&quot;,
&quot;Python.wg_func.executor&quot;: &quot;Run&quot;, &quot;Python.wg_func.executor&quot;: &quot;Run&quot;,
@ -331,7 +334,15 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1725639633873</updated> <updated>1725639633873</updated>
</task> </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 /> <servers />
</component> </component>
<component name="Vcs.Log.Tabs.Properties"> <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()

31
main.py
View File

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

View File

@ -3,10 +3,10 @@ from subprocess import check_call
from pathlib import Path from pathlib import Path
home = Path.home() 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: with open(path_to_file, 'r') as file:
for line in file.readlines(): for line in file.readlines():
a_con = line[5:] a_con = line[5:]
check_call(['nmcli', 'connection', 'up', a_con]) check_call(['nmcli', 'connection', 'up', a_con])
file.close()

View File

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