Skip to content

Commit

Permalink
ts-warp.sh: Log-file truncating moved here
Browse files Browse the repository at this point in the history
  • Loading branch information
mezantrop committed Nov 9, 2024
1 parent fa0c7e6 commit 03eb43a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# CHANGELOG

* **2024.11.08 Current**
* `ts-warp.sh`: Log-file truncating moved here
* `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]`
Expand Down
1 change: 0 additions & 1 deletion gui/ports/macOS/gui-warp.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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 =
33 changes: 10 additions & 23 deletions gui/ports/macOS/gui-warp.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ 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=''):

Expand Down Expand Up @@ -135,11 +134,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, logfile_size)
btn_pause['command'] = lambda: self.pauselog(btn_pause, log_txt, logfile)

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, logfile_size, refresh=True))
tab_log.bind("<Visibility>", self.readfile_log(log_txt, logfile, 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 @@ -176,7 +175,6 @@ def __init__(self, width=800, height=560,
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 @@ -334,10 +332,8 @@ 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, logfile_size, refresh=False):
def readfile_log(self, t_widget, filename, refresh=False):
"""
Read contents of the LOG-file
"""
Expand All @@ -346,19 +342,15 @@ def readfile_log(self, t_widget, filename, logfile_size, refresh=False):
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)
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, logfile_size, refresh)
self.root.after(500, self.readfile_log, t_widget, filename, refresh)

# ---------------------------------------------------------------------------------------------------------------- #
def saveini(self, t_widget, filename):
Expand All @@ -372,23 +364,21 @@ 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, logfile_size):
def pauselog(self, btn, txt, filename):
"""
Pause LOG
"""

if self.pause_log:
self.pause_log = False
btn['text'] = '■' # Pause log auto-refresh
self.readfile_log(txt, filename, logfile_size, refresh=True)
self.readfile_log(txt, filename, refresh=True)
else:
self.pause_log = True
btn['text'] = '↭' # Enable auto-refresh
Expand Down Expand Up @@ -539,7 +529,6 @@ 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 @@ -559,8 +548,6 @@ 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
6 changes: 6 additions & 0 deletions gui/ports/macOS/ts-warp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ tswarp_pidfile="$tswarp_prefix/var/run/ts-warp.pid"
tswarp_logfile="$tswarp_prefix/var/log/ts-warp.log"
tswarp_actfile="$tswarp_prefix/var/spool/ts-warp/ts-warp.act"
tswarp_loglevel="2"
tswarp_logfile_maxsize=3145728
tswarp_options="-c $tswarp_inifile -l $tswarp_logfile -p $tswarp_pidfile -t $tswarp_actfile -d -v $tswarp_loglevel"

# ---------------------------------------------------------------------------- #
Expand Down Expand Up @@ -88,6 +89,11 @@ _start() {
}
' /etc/pf.conf | /sbin/pfctl -q -f -

log_size=$(stat -f "%z" "$tswarp_logfile")

# Truncate logfile if it's too big
[ $tswarp_logfile_maxsize -lt $log_size ] && : > $tswarp_logfile

echo $tswarp_options $* | xargs $SCRIPTPATH/ts-warp > /dev/null
}

Expand Down
15 changes: 11 additions & 4 deletions ts-warp.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ tswarp_pidfile="$tswarp_prefix/var/run/ts-warp.pid"
tswarp_logfile="$tswarp_prefix/var/log/ts-warp.log"
tswarp_actfile="$tswarp_prefix/var/spool/ts-warp/ts-warp.act"
tswarp_loglevel="2"
tswarp_logfile_maxsize=3145728
tswarp_options="-c $tswarp_inifile -l $tswarp_logfile -p $tswarp_pidfile -t $tswarp_actfile -d -v $tswarp_loglevel"

# ---------------------------------------------------------------------------- #
Expand All @@ -62,7 +63,8 @@ _start() {
_check_root
printf "Starting ts-warp: "

case `uname -s` in
log_size=0
case $(uname -s) in
Linux)
if iptables -V ; then
printf "Using IPTABLES Firewall "
Expand All @@ -82,6 +84,7 @@ _start() {
printf "FATAL: Unable to find any Linux firewall\n"
exit 1
fi
log_size=$(stat -c "%s" "$tswarp_logfile")
;;

Darwin|FreeBSD|OpenBSD)
Expand All @@ -104,12 +107,16 @@ _start() {
printf("load anchor \"ts-warp\" from \"%s\"\n", pf_conf)
}
' /etc/pf.conf | /sbin/pfctl -q -f -
log_size=$(stat -f "%z" "$tswarp_logfile")
;;

*)
printf "FATAL: Unsupported OS "
esac

# Truncate logfile if it's too big
[ $tswarp_logfile_maxsize -lt $log_size ] && : > $tswarp_logfile

echo $tswarp_options $* | xargs "$tswarp_prefix"/bin/ts-warp > /dev/null
}

Expand All @@ -124,11 +131,11 @@ _status() {
pgrep -F "$tswarp_pidfile" > /dev/null 2>&1
case $? in
0)
[ "$1" ] && printf "ts-warp is running PID: %s: " `cat "$tswarp_pidfile"`
[ "$1" ] && printf "ts-warp is running PID: %s: " $(cat "$tswarp_pidfile")
return 0
;;
1)
[ "$1" ] && printf "ts-warp is NOT running PID: %s: " `cat "$tswarp_pidfile"`
[ "$1" ] && printf "ts-warp is NOT running PID: %s: " $(cat "$tswarp_pidfile")
return 2
;;
*)
Expand Down Expand Up @@ -178,7 +185,7 @@ _restart() {

# ---------------------------------------------------------------------------- #
_check_root() {
[ `id -u` -ne 0 ] && {
[ $(id -u) -ne 0 ] && {
printf "Fatal: You must be root to proceed\n"
exit 1
}
Expand Down

0 comments on commit 03eb43a

Please sign in to comment.