Skip to content

Commit 6bd4df6

Browse files
chore: sync 2026-11-15 22:57
Changes: M scripts/desktop/stoa-settings.sh M scripts/system/stoa-firewall.sh
1 parent 4218f73 commit 6bd4df6

2 files changed

Lines changed: 143 additions & 9 deletions

File tree

‎scripts/desktop/stoa-settings.sh‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,13 +1570,13 @@ _fw_toggle_port() {
15701570
if grep -qx "${port}/${proto}" "$WHITELIST" 2>/dev/null; then
15711571
# Currently allowed → block it
15721572
if _yad_confirm "Block port ${port}/${proto}?"; then
1573-
"${STOA_BIN}/stoa-firewall" deny "$port" "$proto" 2>/dev/null
1573+
STOA_FW_SOURCE=settings "${STOA_BIN}/stoa-firewall" deny "$port" "$proto" 2>/dev/null
15741574
_notify "Port ${port}/${proto} blocked"
15751575
fi
15761576
else
15771577
# Currently blocked → allow it
15781578
if _yad_confirm "Allow port ${port}/${proto}?"; then
1579-
"${STOA_BIN}/stoa-firewall" allow "$port" "$proto" 2>/dev/null
1579+
STOA_FW_SOURCE=settings "${STOA_BIN}/stoa-firewall" allow "$port" "$proto" 2>/dev/null
15801580
_notify "Port ${port}/${proto} allowed"
15811581
fi
15821582
fi
@@ -1616,7 +1616,7 @@ menu_firewall() {
16161616
local proto_choice
16171617
proto_choice=$(_yad_select " Protocol" "tcp" "udp")
16181618
[ -z "$proto_choice" ] && continue
1619-
"${STOA_BIN}/stoa-firewall" allow "$port_input" "$proto_choice" 2>/dev/null
1619+
STOA_FW_SOURCE=settings "${STOA_BIN}/stoa-firewall" allow "$port_input" "$proto_choice" 2>/dev/null
16201620
_notify "Port ${port_input}/${proto_choice} allowed"
16211621
;;
16221622
*"Block a port"*)
@@ -1630,7 +1630,7 @@ menu_firewall() {
16301630
local bport bproto
16311631
bport=$(echo "$block_choice" | cut -d/ -f1)
16321632
bproto=$(echo "$block_choice" | cut -d/ -f2)
1633-
"${STOA_BIN}/stoa-firewall" deny "$bport" "$bproto" 2>/dev/null
1633+
STOA_FW_SOURCE=settings "${STOA_BIN}/stoa-firewall" deny "$bport" "$bproto" 2>/dev/null
16341634
_notify "Port ${bport}/${bproto} blocked"
16351635
;;
16361636
*"Enable"*)

‎scripts/system/stoa-firewall.sh‎

Lines changed: 139 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ COMMANDS
4040
notification action
4141
enable enable nftables at boot
4242
disable disable it
43+
port PORT [tcp|udp] everything about one port: process, user, binary, who is
44+
connected, what got through/was dropped, allow/deny history
45+
log [HOURS] allow/deny history and traffic summary (default 24h)
4346
active exit 0 if the firewall is loaded (never asks for a password)
4447
monitor watch for new listening ports and notify, with allow/deny actions
4548
(run as a systemd user service)
@@ -71,6 +74,7 @@ STOA_FW_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/stoa/firewall"
7174
WHITELIST="${STOA_FW_DIR}/allowed-ports.conf"
7275
KNOWN_PORTS="${STOA_FW_DIR}/known-ports.list"
7376
NFT_RULES="/etc/nftables.d/stoa-firewall.conf"
77+
AUDIT_LOG="${STOA_FW_DIR}/audit.log"
7478

7579
_banner() {
7680
echo ""
@@ -137,6 +141,24 @@ _validate_port_proto() {
137141
return 0
138142
}
139143

144+
# ── Who is listening on PORT/PROTO: "name(pid)" or "-" ──
145+
_listener_of() {
146+
local port="$1" proto="${2:-tcp}" flag=-tlnpH
147+
[ "$proto" = udp ] && flag=-ulnpH
148+
ss "$flag" 2>/dev/null | awk -v p="$port" '
149+
{ n = split($4, a, ":"); if (a[n] == p) { print $NF; exit } }' |
150+
sed -nE 's/.*\(\("([^"]+)",pid=([0-9]+).*/\1(\2)/p' | head -1 | grep . || echo "-"
151+
}
152+
153+
# ── Audit: who allowed/blocked what, and what was listening then ──
154+
# $1 action (allow|deny|authorize) $2 port/proto. The source is STOA_FW_SOURCE
155+
# (the monitor and the settings panel set it), else the calling process.
156+
_audit() {
157+
local src="${STOA_FW_SOURCE:-$(ps -o comm= -p "$PPID" 2>/dev/null)}"
158+
printf '%s %-9s %-11s via=%s listener=%s\n' "$(date '+%F %T')" "$1" "$2" \
159+
"${src:-?}" "$(_listener_of "${2%/*}" "${2#*/}")" >> "$AUDIT_LOG" 2>/dev/null
160+
}
161+
140162
# ── Check if port is in whitelist ──
141163
_is_allowed() {
142164
local port="$1" proto="${2:-tcp}"
@@ -329,8 +351,8 @@ cmd_ports() {
329351
_banner
330352
_init_dirs
331353

332-
echo -e " ${F} Port Proto State PID Process${R}"
333-
echo -e " ${S} ───── ───── ──────── ──────── ───────────────${R}"
354+
echo -e " ${F} Port Proto State PID Process Reachable from${R}"
355+
echo -e " ${S} ───── ───── ──────── ──────── ─────────────── ──────────────${R}"
334356

335357
# Get all listening ports with process info
336358
local has_ports=false
@@ -363,8 +385,12 @@ cmd_ports() {
363385
status_icon="${T}BLOCKED${R}"
364386
fi
365387

366-
printf " ${F} %-5s %-5s ${R}%-18b ${S}%-8s ${F}%s${R}\n" \
367-
"$port" "$proto" "$status_icon" "$pid" "$process"
388+
# Loopback never goes through the firewall (iif lo accept).
389+
local reach="network"
390+
case "$local_addr" in 127.*|"[::1]"*|::1*) reach="this machine only" ;; esac
391+
392+
printf " ${F} %-5s %-5s ${R}%-18b ${S}%-8s ${F}%-15s ${S}%s${R}\n" \
393+
"$port" "$proto" "$status_icon" "$pid" "$process" "$reach"
368394
done < <(ss -tlnpH 2>/dev/null | sed 's/^/tcp /'; ss -ulnpH 2>/dev/null | sed 's/^/udp /')
369395

370396
if ! $has_ports; then
@@ -394,6 +420,7 @@ cmd_allow() {
394420
sed -i "/^${port}\/${proto}$/d" "$WHITELIST" # not applied: do not list it as allowed
395421
return 1
396422
fi
423+
_audit allow "${port}/${proto}"
397424
echo -e " ${O}[✓] Port ${port}/${proto} allowed.${R}"
398425
_notify "Port ${port}/${proto} allowed"
399426
}
@@ -419,6 +446,7 @@ cmd_deny() {
419446
echo "${port}/${proto}" >> "$WHITELIST" # not applied: it is still open
420447
return 1
421448
fi
449+
_audit deny "${port}/${proto}"
422450
echo -e " ${O}[✓] Port ${port}/${proto} blocked.${R}"
423451
_notify "Port ${port}/${proto} blocked"
424452
}
@@ -437,12 +465,115 @@ cmd_disable() {
437465
_notify "Firewall disabled — all ports open"
438466
}
439467

468+
# ══════════════════════════════════════════════════════════════
469+
# WHO / WHAT / WHEN
470+
# ══════════════════════════════════════════════════════════════
471+
472+
# journalctl line -> "SRC proto dport", from the kernel log prefixes.
473+
_fw_hits() { # <pass|drop> [since]
474+
journalctl -k --no-pager -o cat --since "${2:--24h}" --grep "stoa-fw-$1:" 2>/dev/null |
475+
awk '{ s=""; p=""; d=""
476+
for (i=1;i<=NF;i++) { split($i,k,"=")
477+
if (k[1]=="SRC") s=k[2]; else if (k[1]=="PROTO") p=tolower(k[2]); else if (k[1]=="DPT") d=k[2] }
478+
if (d != "") print s, p, d }'
479+
}
480+
481+
# stoa-firewall port PORT [tcp|udp] — everything known about one port.
482+
cmd_port() {
483+
_init_dirs
484+
local port="$1" want="${2:-}"
485+
[ -z "$port" ] && { echo -e " ${T}Usage: stoa-firewall port <port> [tcp|udp]${R}"; return 1; }
486+
_validate_port_proto "$port" "${want:-tcp}" || return 1
487+
_banner
488+
489+
local proto flag found=false
490+
for proto in tcp udp; do
491+
[ -n "$want" ] && [ "$proto" != "$want" ] && continue
492+
flag=-tlnpH; [ "$proto" = udp ] && flag=-ulnpH
493+
494+
echo -e " ${B}── ${port}/${proto} ──${R}"
495+
if _is_allowed "$port" "$proto"; then
496+
echo -e " ${F}Firewall: ${O}ALLOWED${R}"
497+
else
498+
echo -e " ${F}Firewall: ${T}BLOCKED${R}"
499+
fi
500+
501+
local line addr pid name
502+
while IFS= read -r line; do
503+
addr=$(echo "$line" | awk '{print $4}')
504+
[ "${addr##*:}" = "$port" ] || continue
505+
found=true
506+
name=""; pid=""
507+
if [[ "$line" =~ users:\(\(\"([^\"]+)\",pid=([0-9]+) ]]; then
508+
name="${BASH_REMATCH[1]}"; pid="${BASH_REMATCH[2]}"
509+
fi
510+
echo -e " ${F}Listening: ${R}${addr}"
511+
case "$addr" in
512+
127.*|"[::1]"*) echo -e " ${S} loopback: only this machine can reach it; the firewall is not involved${R}" ;;
513+
*) echo -e " ${S} reachable from the network (if allowed above)${R}" ;;
514+
esac
515+
if [ -n "$pid" ] && [ -d "/proc/$pid" ]; then
516+
echo -e " ${F}Process: ${R}${name} (pid ${pid}), user $(ps -o user= -p "$pid" 2>/dev/null | xargs)"
517+
echo -e " ${F}Started: ${R}$(ps -o lstart= -p "$pid" 2>/dev/null | xargs)"
518+
echo -e " ${F}Binary: ${R}$(readlink "/proc/$pid/exe" 2>/dev/null || echo '?')"
519+
echo -e " ${F}Command: ${R}$(tr '\0' ' ' < "/proc/$pid/cmdline" 2>/dev/null | cut -c1-200)"
520+
echo -e " ${F}Opened by: ${R}$(ps -o comm= -p "$(ps -o ppid= -p "$pid" | xargs)" 2>/dev/null) (parent of ${name})"
521+
else
522+
echo -e " ${F}Process: ${S}not visible (root/another user's process, or gone)${R}"
523+
fi
524+
done < <(ss "$flag" 2>/dev/null)
525+
526+
if [ "$proto" = tcp ]; then
527+
local conns
528+
conns=$(ss -tnpH state established "( sport = :${port} )" 2>/dev/null | awk '{print $4 " " $5}')
529+
if [ -n "$conns" ]; then
530+
echo -e " ${F}Connected now:${R}"; echo "$conns" | sed 's/^/ /'
531+
fi
532+
fi
533+
534+
local passed dropped
535+
passed=$(_fw_hits pass | awk -v p="$port" -v pr="$proto" '$3==p && $2==pr {print $1}' | sort | uniq -c | sort -rn | head -5)
536+
dropped=$(_fw_hits drop | awk -v p="$port" -v pr="$proto" '$3==p && $2==pr {print $1}' | sort | uniq -c | sort -rn | head -5)
537+
echo -e " ${F}Got through (24h, new connections):${R}"
538+
echo "${passed:- none logged}" | sed 's/^/ /'
539+
echo -e " ${F}Dropped (24h; the log is rate-limited, counts are a floor):${R}"
540+
echo "${dropped:- none logged}" | sed 's/^/ /'
541+
542+
echo -e " ${F}Allow/deny history:${R}"
543+
{ grep -F " ${port}/${proto} " "$AUDIT_LOG" 2>/dev/null | tail -5; } | sed 's/^/ /'
544+
echo ""
545+
done
546+
$found || echo -e " ${S}Nothing is listening on ${port} right now.${R}\n"
547+
}
548+
549+
# stoa-firewall log [HOURS] — recent allow/deny actions and traffic summary.
550+
cmd_log() {
551+
_init_dirs
552+
local hours="${1:-24}"
553+
[[ "$hours" =~ ^[0-9]+$ ]] || hours=24
554+
_banner
555+
echo -e " ${F}Allow/deny history (last 20):${R}"
556+
if [ -s "$AUDIT_LOG" ]; then tail -20 "$AUDIT_LOG" | sed 's/^/ /'
557+
else echo -e " ${S} none yet (recorded from now on)${R}"; fi
558+
echo ""
559+
local kind label
560+
for kind in pass drop; do
561+
[ "$kind" = pass ] && label="Got through" || label="Dropped"
562+
echo -e " ${F}${label}, last ${hours}h (count from proto/port):${R}"
563+
_fw_hits "$kind" "-${hours}h" | awk '{print $1 " " $2 "/" $3}' | sort | uniq -c | sort -rn | head -15 | sed 's/^/ /'
564+
echo ""
565+
done
566+
echo -e " ${S} Logs are rate-limited (30/min through, 5/min dropped): counts are a floor.${R}"
567+
echo ""
568+
}
569+
440570
# ══════════════════════════════════════════════════════════════
441571
# PORT MONITOR DAEMON
442572
# ══════════════════════════════════════════════════════════════
443573

444574
cmd_monitor() {
445575
_init_dirs
576+
export STOA_FW_SOURCE=monitor
446577

447578
# Snapshot current ports as known
448579
ss -tlnpH 2>/dev/null | awk '{print $4}' | rev | cut -d: -f1 | rev | sort -n > "$KNOWN_PORTS"
@@ -517,6 +648,7 @@ cmd_authorize() {
517648
sed -i "/^${port}\/${proto}$/d" "$WHITELIST"
518649
return 1
519650
fi
651+
_audit authorize "${port}/${proto}"
520652
_notify "Port ${port}/${proto} permanently allowed"
521653
}
522654

@@ -558,10 +690,12 @@ case "${1:-status}" in
558690
disable) cmd_disable ;;
559691
monitor) cmd_monitor ;;
560692
active) _fw_active ;;
693+
port) cmd_port "$2" "$3" ;;
694+
log) cmd_log "$2" ;;
561695
authorize) cmd_authorize "$2" "$3" ;;
562696
*)
563697
echo -e " ${T}Unknown command: $1${R}"
564-
echo -e " ${S}Usage: stoa-firewall {setup|status|ports|allow|deny|enable|disable|monitor}${R}"
698+
echo -e " ${S}Usage: stoa-firewall {setup|status|ports|port|log|allow|deny|enable|disable|monitor}${R}"
565699
exit 1
566700
;;
567701
esac

0 commit comments

Comments
 (0)