Compare commits

..

No commits in common. "main" and "wire-py-reformat-10-08-2024" have entirely different histories.

5 changed files with 111 additions and 115 deletions

View File

@ -4,10 +4,7 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="940e1630-c825-4d4c-be80-bc11f543c122" name="Changes" comment="little fixes a labels when stop and start, installer first functions works"> <list default="true" id="940e1630-c825-4d4c-be80-bc11f543c122" name="Changes" comment="little fixes replace os.system with check_call&#10;first steps in install Script add wg_start.service file">
<change afterPath="$PROJECT_DIR$/message.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/test.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/test2.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$/install.py" beforeDir="false" afterPath="$PROJECT_DIR$/install.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/install.py" beforeDir="false" afterPath="$PROJECT_DIR$/install.py" 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" />
@ -50,10 +47,8 @@
<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.install.executor&quot;: &quot;Run&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.messagebox.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;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;, &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
@ -353,23 +348,10 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1725820337669</updated> <updated>1725820337669</updated>
</task> </task>
<task id="LOCAL-00029" summary="little fixes a labels when stop and start, installer first functions works"> <option name="localTasksCounter" value="29" />
<option name="closed" value="true" />
<created>1725991610908</created>
<option name="number" value="00029" />
<option name="presentableId" value="LOCAL-00029" />
<option name="project" value="LOCAL" />
<updated>1725991610908</updated>
</task>
<option name="localTasksCounter" value="30" />
<servers /> <servers />
</component> </component>
<component name="Vcs.Log.Tabs.Properties"> <component name="Vcs.Log.Tabs.Properties">
<option name="OPEN_GENERIC_TABS">
<map>
<entry key="f9756e7d-0c5c-4b84-84e4-660314c46ea4" value="TOOL_WINDOW" />
</map>
</option>
<option name="TAB_STATES"> <option name="TAB_STATES">
<map> <map>
<entry key="MAIN"> <entry key="MAIN">
@ -389,16 +371,12 @@
</State> </State>
</value> </value>
</entry> </entry>
<entry key="f9756e7d-0c5c-4b84-84e4-660314c46ea4">
<value>
<State />
</value>
</entry>
</map> </map>
</option> </option>
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" /> <option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" />
<MESSAGE value="wg_import_select in wg_func.py extended" />
<MESSAGE value="add class Frame and class Massage, delete funktion go 100%, add resize window" /> <MESSAGE value="add class Frame and class Massage, delete funktion go 100%, add resize window" />
<MESSAGE value="add class Frame and class Massage, delete funktion go 100%, add resize window, add warning pic" /> <MESSAGE value="add class Frame and class Massage, delete funktion go 100%, add resize window, add warning pic" />
<MESSAGE value="add class ListTunnels and class ImportTunnel, and add class ExportTunnels (ExportTunnels no finish)" /> <MESSAGE value="add class ListTunnels and class ImportTunnel, and add class ExportTunnels (ExportTunnels no finish)" />
@ -423,7 +401,6 @@
<MESSAGE value="add Frame widget 3&#10;for Buttons and Listbox with Scrollbar.&#10;all Widgets new format&#10;delete works now of disable checkbox when Listbox empty (part two)" /> <MESSAGE value="add Frame widget 3&#10;for Buttons and Listbox with Scrollbar.&#10;all Widgets new format&#10;delete works now of disable checkbox when Listbox empty (part two)" />
<MESSAGE value="little fixes" /> <MESSAGE value="little fixes" />
<MESSAGE value="little fixes replace os.system with check_call&#10;first steps in install Script add wg_start.service file" /> <MESSAGE value="little fixes replace os.system with check_call&#10;first steps in install Script add wg_start.service file" />
<MESSAGE value="little fixes a labels when stop and start, installer first functions works" /> <option name="LAST_COMMIT_MESSAGE" value="little fixes replace os.system with check_call&#10;first steps in install Script add wg_start.service file" />
<option name="LAST_COMMIT_MESSAGE" value="little fixes a labels when stop and start, installer first functions works" />
</component> </component>
</project> </project>

View File

@ -1,30 +1,41 @@
#!/usr/bin/python3 #!/usr/bin/python3
import os
from pathlib import Path from pathlib import Path
import subprocess
from getpass import getpass from getpass import getpass
startfile = ['#!/usr/bin/python3',
'from subprocess import check_call', '\n',
'path_to_file = ', '\n',
'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()']
home = Path.home()
# for services file in /lib/systemd/system/ # for services file in /lib/systemd/system/
config_path = Path.home() / ".config" / "wire_py" / "wg_py" path_for_a_con = home / '.config/wire_py/wg_py'
# replace in startfile
startfile[3] = 'path_to_file = ' "'" + str(path_for_a_con) + "'"
start_file = f'''\ with open('start_wg.py', 'w') as file_for_a_con:
#!/usr/bin/python3 for line in startfile:
from subprocess import check_call file_for_a_con.write(line + '\n')
file_for_a_con.close()
path_to_file = {str(config_path)!r} # create dir in home
mkdir = home / '.config/wire_py'
os.makedirs(mkdir, exist_ok=True)
with open(path_to_file, 'r') as file: with open(mkdir / 'wg_py', 'w+') as file:
*_, line = file if 'true' not in file:
a_con = line[5:] file.write('false')
check_call(['nmcli', 'connection', 'up', a_con]) file.close()
'''
Path("start_wg.py").write_text(start_file)
config_path.parent.mkdir(exist_ok=True)
if not Path.exists(config_path):
config_path.write_text("false")
# Prompt the user for the sudo password # Prompt the user for the sudo password
#sudo_password = getpass('Enter sudo password: ') sudo_password = getpass('Enter sudo password: ')
#def cp_files(): #def cp_files():
# Define the command to run the Python script with sudo # Define the command to run the Python script with sudo
@ -32,3 +43,4 @@ if not Path.exists(config_path):
# Run the command as a subprocess, passing the sudo password # Run the command as a subprocess, passing the sudo password
#subprocess.run(command, input=sudo_password.encode(), check=True) #subprocess.run(command, input=sudo_password.encode(), check=True)

12
main.py
View File

@ -8,6 +8,7 @@ from wg_func import (TunnelActiv, ListTunnels, ImportTunnel, ConToDict, GreenLab
FileHandle, ExportTunnels, OnOff) FileHandle, ExportTunnels, OnOff)
font_color = '#4011a7' font_color = '#4011a7'
home = Path.home()
class MainWindow(tk.Tk): class MainWindow(tk.Tk):
@ -127,12 +128,13 @@ class FrameWidgets(ttk.Frame):
# Button Vpn # Button Vpn
if self.a != '': if self.a != '':
StartStopBTN.button_stop(self) StartStopBTN.button_stop(self)
wg_read = Path.home() / 'tester/' / str(self.a + '.conf') wg_read = home / 'tester/' / str(self.a + '.conf')
with open(wg_read, 'r') as file: 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()
else: else:
StartStopBTN.button_start(self) StartStopBTN.button_start(self)
# Address Label # Address Label
@ -153,9 +155,9 @@ class FrameWidgets(ttk.Frame):
select_tl = self.l_box.get(self.select_tunnel[0]) select_tl = self.l_box.get(self.select_tunnel[0])
check_call(['nmcli', 'connection', 'delete', 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(Path.home() / 'tester/' / str(select_tl + '.conf')) os.remove(home / 'tester/' / str(select_tl + '.conf'))
path_to_file = Path.home() / '.config/wire_py/wg_py' path_to_file = home / '.config/wire_py/wg_py'
with open(path_to_file, 'r') as file2: with open(path_to_file, 'r') as file2:
for line in file2.readlines(): for line in file2.readlines():
a_con = line[5:] a_con = line[5:]
@ -168,6 +170,7 @@ class FrameWidgets(ttk.Frame):
self.wg_autostart.configure(state='disabled') self.wg_autostart.configure(state='disabled')
with open(path_to_file, 'w') as file2: with open(path_to_file, 'w') as file2:
file2.write('false') file2.write('false')
file2.close()
if self.a != '' and self.a == select_tl: if self.a != '' and self.a == select_tl:
self.StrVar.set(value='') self.StrVar.set(value='')
@ -221,12 +224,13 @@ class FrameWidgets(ttk.Frame):
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])
check_call(['nmcli', 'connection', 'up', select_tl]) check_call(['nmcli', 'connection', 'up', select_tl])
wg_read = Path.home() / 'tester/' / str(select_tl + '.conf') wg_read = home / 'tester/' / str(select_tl + '.conf')
with open(wg_read, 'r') as file: 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()
# Button Start/Stop # Button Start/Stop
StartStopBTN.button_stop(self) StartStopBTN.button_stop(self)
self.a = TunnelActiv.active() self.a = TunnelActiv.active()

View File

@ -1,9 +1,12 @@
#!/usr/bin/python3 #!/usr/bin/python3
from subprocess import check_call from subprocess import check_call
path_to_file = '/home/punix/.config/wire_py/wg_py' path_to_file = '/home/punix/.config/wire_py/wg_py'
with open(path_to_file, 'r') as file:
*_, line = file with open(path_to_file, ) as file:
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

@ -7,37 +7,39 @@ import tkinter as tk
from pathlib import Path from pathlib import Path
from subprocess import check_call from subprocess import check_call
home = Path.home()
font_color = '#4011a7' font_color = '#4011a7'
path_to_file = Path.home() / '.config/wire_py/wg_py' path_to_file = home / '.config/wire_py/wg_py'
def msg_window(): class Message(tk.Tk):
msg = tk.Toplevel() def __init__(self, *args, **kwargs):
msg.resizable(width=False, height=False) super().__init__(*args, **kwargs)
msg.x_width = 340
msg.y_height = 140 self.warning_pic = None
msg.title('Import error!') self.x_width = 300
msg.monitor_center_x = msg.winfo_screenwidth() / 2 - (msg.x_width / 2) self.y_height = 120
msg.monitor_center_y = msg.winfo_screenheight() / 2 - (msg.y_height / 2) self.monitor_center_x = self.winfo_screenwidth() / 2 - (self.x_width / 2)
msg.geometry('%dx%d+%d+%d' % (msg.x_width, msg.y_height, msg.monitor_center_x, msg.monitor_center_y)) self.monitor_center_y = self.winfo_screenheight() / 2 - (self.y_height / 2)
msg.columnconfigure(0, weight=1) self.resizable(width=False, height=False)
msg.configure(pady=20) self.title('Import error!')
msg.warning = tk.PhotoImage(file=r'icons/warning_64.png') self.configure()
msg.i_warning = tk.Label(msg, image=msg.warning) self.geometry('%dx%d+%d+%d' % (self.x_width, self.y_height, self.monitor_center_x, self.monitor_center_y))
msg.i_warning.grid(column=0, row=0) self.columnconfigure(0, weight=1)
label = tk.Label(msg, text='Oh... no valid Wireguard File!\nPlease select a valid Wireguard File')
label.config(font=('Ubuntu', 11), padx=15, pady=15) self.label = tk.Label(self,
label.grid(column=1, row=0) image=self.warning_pic,
button = tk.Button(msg, text='OK', command=msg.destroy) text='Oh... no valid Wireguard File!\nPlease select a valid Wireguard File')
button.config(padx=15, pady=5) self.label.config(font=('Ubuntu', 11), padx=15, pady=15)
button.grid(column=0, columnspan=2, row=1) self.label.grid(column=0, row=0)
wg_icon_2 = tk.PhotoImage(file=r'icons/wg-stop.png') self.button = tk.Button(self, text='OK', command=self.destroy)
msg.iconphoto(True, wg_icon_2) self.button.config(padx=15, pady=5)
msg.winfo_toplevel() self.button.grid(column=0, row=1)
class GreenLabel: class GreenLabel(tk.Tk):
def __init__(self): def __init__(self, container, **kwargs):
super().__init__(container, **kwargs)
self.StrVar = None self.StrVar = None
self.lb_tunnel = None self.lb_tunnel = None
@ -48,15 +50,10 @@ class GreenLabel:
self.columnconfigure(2, weight=1) self.columnconfigure(2, weight=1)
self.rowconfigure(0, weight=1) self.rowconfigure(0, weight=1)
def columnconfigure(self, param, weight):
pass
def rowconfigure(self, param, weight): class StartStopBTN(tk.Tk):
pass def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class StartStopBTN:
def __init__(self):
self.lb_frame_btn_lbox = None self.lb_frame_btn_lbox = None
self.wg_switch = None self.wg_switch = None
self.btn_stst = None self.btn_stst = None
@ -119,8 +116,9 @@ class TunnelActiv:
return active return active
class ShowAddress: class ShowAddress(tk.Tk):
def __init__(self): def __init__(self, container, **kwargs):
super().__init__(container, **kwargs)
self.lb_frame2 = None self.lb_frame2 = None
self.lb_frame = None self.lb_frame = None
self.endpoint = None self.endpoint = None
@ -167,9 +165,9 @@ class ListTunnels:
return tl return tl
class ImportTunnel: class ImportTunnel(tk.Tk):
def __init__(self, container, **kwargs):
def __init__(self): super().__init__(container, **kwargs)
self.select_tunnel = None self.select_tunnel = None
self.wg_switch = None self.wg_switch = None
@ -181,26 +179,27 @@ class ImportTunnel:
def wg_import_select(self): def wg_import_select(self):
try: try:
filepath = filedialog.askopenfilename(initialdir=Path.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')])
with open(filepath, 'r') as file: file = open(filepath, 'r')
read = file.read() read = file.read()
file.close()
path_split = filepath.split('/') path_split = filepath.split('/')
path_split1 = path_split[-1] path_split1 = path_split[-1]
self.a = TunnelActiv.active() self.a = TunnelActiv.active()
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, Path.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, Path.home() / 'tester/' / str(path_split)) os.rename(p1, home / 'tester/' / str(path_split))
if self.a != '': if self.a != '':
check_call(['nmcli', 'connection', 'down', TunnelActiv.active()]) check_call(['nmcli', 'connection', 'down', TunnelActiv.active()])
ShowAddress.label_empty(self) ShowAddress.label_empty(self)
check_call(['nmcli', 'connection', 'import', 'type', 'wireguard', 'file', Path.home() / 'tester' / check_call(['nmcli', 'connection', 'import', 'type', 'wireguard', 'file', home / 'tester' /
path_split]) path_split])
else: else:
shutil.copy(filepath, Path.home() / 'tester/') shutil.copy(filepath, home / 'tester/')
if self.a != '': if self.a != '':
check_call(['nmcli', 'connection', 'down', TunnelActiv.active()]) check_call(['nmcli', 'connection', 'down', TunnelActiv.active()])
ShowAddress.label_empty(self) ShowAddress.label_empty(self)
@ -214,15 +213,16 @@ 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 = Path.home() / 'tester/' / str(self.a + '.conf') wg_read = home / 'tester/' / str(self.a + '.conf')
with open(wg_read, 'r') as file: 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()
check_call(['nmcli', 'con', 'mod', 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:
msg_window() Message()
except EOFError: except EOFError:
pass pass
except TypeError: except TypeError:
@ -242,20 +242,23 @@ class FileHandle:
self.tl = None self.tl = None
self.selected_option = None self.selected_option = None
self.l_box = None self.l_box = None
self.select_tunnel = None
def box_set(self): def box_set(self):
try: try:
select_tunnel = self.l_box.curselection() self.select_tunnel = self.l_box.curselection()
select_tl = self.l_box.get(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:
with open(path_to_file, 'w') as off: off = open(path_to_file, 'w')
off.write('false ' + '\n') off.write('false ' + '\n')
off.close()
tl = ListTunnels.tl_list() tl = ListTunnels.tl_list()
if len(tl) == 0: if len(tl) == 0:
self.wg_autostart.configure(state='disabled') self.wg_autostart.configure(state='disabled')
if self.selected_option.get() >= 1: if self.selected_option.get() >= 1:
with open(path_to_file, 'w') as set_on: set_on = open(path_to_file, 'w')
set_on.write('true ' + select_tl) set_on.write('true ' + select_tl)
set_on.close()
except IndexError: except IndexError:
self.selected_option.set(1) self.selected_option.set(1)
@ -263,13 +266,9 @@ class FileHandle:
OnOff.on_off(self) OnOff.on_off(self)
class OnOff: class OnOff(tk.Tk, FileHandle):
def __init__(self): def __init__(self, container, **kwargs):
self.wg_autostart = None super().__init__(container, **kwargs)
self.selected_option = None
self.auto_con = None
self.autoconnect = None
self.autoconnect_var = None
self.lb_frame_buttons = None self.lb_frame_buttons = None
def on_off(self): def on_off(self):
@ -283,6 +282,7 @@ class OnOff:
else: else:
self.wg_autostart.configure(state='disabled') self.wg_autostart.configure(state='disabled')
self.auto_con = 'no Autoconnect' self.auto_con = 'no Autoconnect'
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)
@ -299,8 +299,8 @@ class ExportTunnels:
tl = ListTunnels.tl_list() tl = ListTunnels.tl_list()
try: try:
if len(tl) != 0: if len(tl) != 0:
wg_tar = Path.home() / now_datetime wg_tar = home / now_datetime
p_to_conf = Path.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')