Skip to content

Commit

Permalink
gui-warp.py: Minor changes about log-file size and FW-file selectio…
Browse files Browse the repository at this point in the history
…n under `Linux`
  • Loading branch information
mezantrop committed Nov 8, 2024
1 parent 2d67b9d commit fa0c7e6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# CHANGELOG

* **2024.09.30 Current**
* **2024.11.08 Current**
* `gui-warp.py`: Minor changes about log-file size and FW-file selection under `Linux`
* `ts-warp.c`: Null-pointer dereference in `PIDS` table processing fixed
* `ts-warp.c`: `ACT` reporting `[SECTIONS]`
* `inifile.c`: Correct usage of multiple links of a proxy-chain
Expand Down
15 changes: 8 additions & 7 deletions gui/gui-warp.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class App:

self.password = ''

self.version = 'v1.0.25'
self.version = 'v1.0.26'
self.width = width
self.height = height

Expand Down Expand Up @@ -147,7 +147,8 @@ class App:
'tsw01:' + subprocess.Popen([prefix + '/bin/ts-pass', self.tswhash.get().encode()],
stdout=subprocess.PIPE).stdout.read().decode().strip('\n\r'))

ttk.Label(frm_tab_ini_top, text='Save changes:').grid(column=2, row=0, sticky=tk.E)
sch = ttk.Label(frm_tab_ini_top, text='Save configuration:')
sch.grid(column=2, row=0, sticky=tk.E)
btn_save_ini = ttk.Button(frm_tab_ini_top, width=self._btnw, text='▲')
btn_save_ini.grid(column=3, row=0, sticky=tk.W, padx=self._padx, pady=self._pady)
btn_save_ini['command'] = lambda: self.savefile(ini_txt, inifile)
Expand Down Expand Up @@ -358,11 +359,11 @@ It is a free and open-source software, but if you want to support it, please do'
"""

t_widget.config(state='normal')
with open(filename, 'r', encoding='utf-8') as f:
sz = os.path.getsize(filename)
if sz > self.log_size:
sz = os.path.getsize(filename)
if sz > self.log_size:
with open(filename, 'r', encoding='utf-8') as f:
f.seek(self.log_size)
self.log_size = sz
t_widget.delete(1.0, tk.END)
t_widget.insert(tk.END, ''.join(f.readlines()))
t_widget.see(tk.END)

Expand Down Expand Up @@ -536,7 +537,7 @@ if __name__ == "__main__":

runcmd = prefix + '/etc/ts-warp.sh'
inifile = prefix + '/etc/ts-warp.ini'
fwfile = prefix + ('/etc/ts-warp_pf.conf' if platform.system() != 'Linux' else '/etc/ts-warp_nftables.sh')
fwfile = prefix + ('/etc/ts-warp_pf.conf' if platform.system() != 'Linux' else '/etc/ts-warp_nftables.sh' if os.path.isfile('/etc/ts-warp_nftables.sh') else '/etc/ts-warp_iptables.sh')
logfile = prefix + '/var/log/ts-warp.log'
pidfile = prefix + '/var/run/ts-warp.pid'
actfile = prefix + '/var/spool/ts-warp/ts-warp.act'
Expand Down
1 change: 1 addition & 0 deletions gui/ports/macOS/gui-warp.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ prefix = ~/ts-warp
inifile = /etc/ts-warp.ini
# fwfile = /etc/ts-warp_pf.conf # This should be autoconfigured
logfile = /var/log/ts-warp.log
logfile_size = 3145728
pidfile = /var/run/ts-warp.pid
actfile = /var/spool/ts-warp/ts-warp.act
daemon_options =
44 changes: 29 additions & 15 deletions gui/ports/macOS/gui-warp.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ def __init__(self, width=800, height=560,
daemon_options="",
fwfile='/usr/local/etc/ts-warp_pf.conf',
logfile='/usr/local/var/log/ts-warp.log',
logfile_size=3145728,
pidfile='/usr/local/var/run/ts-warp.pid',
url_new_vesrsion=''):

self.password = ''

self.version = 'v1.0.32-mac'
self.version = 'v1.0.33-mac'
self.width = width
self.height = height

Expand Down Expand Up @@ -134,11 +135,11 @@ def __init__(self, width=800, height=560,
btn_pause = ttk.Button(tab_log, width=self._btnw, text='■')
btn_pause.grid(column=1, row=0, sticky=tk.W, padx=self._padx, pady=self._pady)
self.pause_log = False
btn_pause['command'] = lambda: self.pauselog(btn_pause, log_txt, logfile)
btn_pause['command'] = lambda: self.pauselog(btn_pause, log_txt, logfile, logfile_size)

log_txt = tk.Text(tab_log, highlightthickness=0, state='disabled')
log_txt.grid(column=0, row=1, columnspan=2, sticky=tk.NSEW)
tab_log.bind("<Visibility>", self.readfile_log(log_txt, logfile, refresh=True))
tab_log.bind("<Visibility>", self.readfile_log(log_txt, logfile, logfile_size, refresh=True))

scroll_log = ttk.Scrollbar(tab_log, orient=tk.VERTICAL)
scroll_log.grid(column=2, row=1, sticky=tk.NSEW)
Expand Down Expand Up @@ -166,14 +167,16 @@ def __init__(self, width=800, height=560,
'tsw01:' + subprocess.Popen(['./ts-pass', self.tswhash.get().encode()],
stdout=subprocess.PIPE).stdout.read().decode().strip('\n\r'))

ttk.Label(frm_tab_ini_top, text='Save changes:').grid(column=2, row=0, sticky=tk.E)
sch = ttk.Label(frm_tab_ini_top, text='Save configuration:', foreground='green')
sch.grid(column=2, row=0, sticky=tk.E)
btn_save_ini = ttk.Button(frm_tab_ini_top, width=self._btnw, text='▲')
btn_save_ini.grid(column=3, row=0, sticky=tk.W, padx=self._padx, pady=self._pady)
btn_save_ini['command'] = lambda: self.saveini(ini_txt, inifile)

ini_txt = tk.Text(tab_ini, highlightthickness=0)
ini_txt.grid(column=0, row=1, columnspan=2, sticky=tk.NSEW)
tab_ini.bind("<Visibility>", self.readfile_ini(ini_txt, inifile))
# ini_txt.bind('<<Modified>>', lambda: sch.config(foreground='red') if ini_txt.edit_modified() == 1 else sch.config(foreground='green'))

scroll_ini = ttk.Scrollbar(tab_ini, orient=tk.VERTICAL)
scroll_ini.grid(column=2, row=1, sticky=tk.NSEW)
Expand Down Expand Up @@ -331,25 +334,31 @@ def readfile_ini(self, t_widget, filename):
t_widget.insert(tk.END, ''.join(f.readlines()))
t_widget.see(tk.END)

# t_widget.edit_modified(False)

# ---------------------------------------------------------------------------------------------------------------- #
def readfile_log(self, t_widget, filename, refresh=False):
def readfile_log(self, t_widget, filename, logfile_size, refresh=False):
"""
Read contents of the LOG-file
"""

t_widget.config(state='normal')
with open(filename, 'r', encoding='utf-8') as f:
sz = os.path.getsize(filename)
if sz > self.log_size:
self.log_size = sz
t_widget.delete(1.0, tk.END)
t_widget.insert(tk.END, ''.join(f.readlines()))
t_widget.see(tk.END)
sz = os.path.getsize(filename)
if sz > self.log_size:
with open(filename, 'r', encoding='utf-8') as f:
if sz > logfile_size:
f.truncate(0)
t_widget.delete(1.0, tk.END)
else:
f.seek(self.log_size)
self.log_size = sz
t_widget.insert(tk.END, ''.join(f.readlines()))
t_widget.see(tk.END)

if refresh:
t_widget.config(state='disabled')
if not self.pause_log:
self.root.after(500, self.readfile_log, t_widget, filename, refresh)
self.root.after(500, self.readfile_log, t_widget, filename, logfile_size, refresh)

# ---------------------------------------------------------------------------------------------------------------- #
def saveini(self, t_widget, filename):
Expand All @@ -363,21 +372,23 @@ def saveini(self, t_widget, filename):
f.write(t_widget.get('1.0', tk.END)[:-1]) # Strip extra newline
os.chown(filename, uid, gid)

# t_widget.edit_modified(False)

# Rebuild ts-warp_pf.conf when saving the INI-file
with open(fwfile, 'w', encoding='utf8') as outfw:
subprocess.run(['./ts-warp_autofw.sh', prefix], stdout=outfw, check=False)
os.chown(fwfile, uid, gid)

# ---------------------------------------------------------------------------------------------------------------- #
def pauselog(self, btn, txt, filename):
def pauselog(self, btn, txt, filename, logfile_size):
"""
Pause LOG
"""

if self.pause_log:
self.pause_log = False
btn['text'] = '■' # Pause log auto-refresh
self.readfile_log(txt, filename, refresh=True)
self.readfile_log(txt, filename, logfile_size, refresh=True)
else:
self.pause_log = True
btn['text'] = '↭' # Enable auto-refresh
Expand Down Expand Up @@ -528,6 +539,7 @@ def dedupch(s, c='/'):
inifile = prefix + 'etc/ts-warp.ini'
fwfile = prefix + 'etc/ts-warp_pf.conf'
logfile = prefix + 'var/log/ts-warp.log'
logfile_size = 3145728
pidfile = prefix + 'var/run/ts-warp.pid'
actfile = prefix + 'var/spool/ts-warp/ts-warp.act'
daemon_options = ''
Expand All @@ -547,6 +559,8 @@ def dedupch(s, c='/'):
fwfile = '/' + dedupch(prefix + gui_ini['GUI-WARP']['fwfile'])
if 'logfile' in gui_ini['GUI-WARP'].keys():
logfile = '/' + dedupch(prefix + gui_ini['GUI-WARP']['logfile'])
if 'logfile_size' in gui_ini['GUI-WARP'].keys():
logfile_size = int(gui_ini['GUI-WARP']['logfile_size'])
if 'pidfile' in gui_ini['GUI-WARP'].keys():
pidfile = '/' + dedupch(prefix + gui_ini['GUI-WARP']['pidfile'])
if 'actfile' in gui_ini['GUI-WARP'].keys():
Expand Down

0 comments on commit fa0c7e6

Please sign in to comment.