- wg_events: sort_order param (desc default), --ascending/--descending flags - wg_events: endpoint cache fallback via _endpoint() helper - wg_events: gap computed ascending always, then sliced/reversed correctly - fw_events: sort_order param, descending default - ui::logs::wg_row: gap suffix with 'offline' label when gap > threshold - logs.command.sh: --ascending/--descending flags, pass sort_order to both functions - daemon: endpoint cache fallback in poll_handshakes - json.sh: json::wg_events passes ctx::endpoint_cache as arg
181 lines
No EOL
6.3 KiB
Bash
181 lines
No EOL
6.3 KiB
Bash
#!/usr/bin/env bash
|
|
# ui/logs.module.sh — rendering for logs and watch data
|
|
|
|
function ui::logs::build_dest() {
|
|
local dest_ip="${1:-}" dest_port="${2:-}" proto="${3:-}" svc="${4:-}"
|
|
if [[ -n "$svc" ]]; then
|
|
[[ -n "$dest_port" ]] && echo "${svc}/${proto}" || echo "${svc} (${proto})"
|
|
else
|
|
[[ -n "$dest_port" ]] && echo "${dest_ip}:${dest_port}/${proto}" || echo "${dest_ip} (${proto})"
|
|
fi
|
|
}
|
|
|
|
function ui::logs::fw_section_header() {
|
|
printf " Firewall Drops\n"
|
|
printf " %s\n" "$(printf '─%.0s' {1..42})"
|
|
}
|
|
|
|
function ui::logs::wg_section_header() {
|
|
printf " WireGuard Events\n"
|
|
printf " %s\n" "$(printf '─%.0s' {1..42})"
|
|
}
|
|
|
|
function ui::logs::fw_section_header_table() {
|
|
printf " Firewall Drops:\n"
|
|
printf " %-20s %-18s %-25s %s\n" "TIME" "CLIENT" "DESTINATION" "PROTOCOL"
|
|
printf " %s\n" "$(printf '─%.0s' {1..75})"
|
|
}
|
|
|
|
function ui::logs::wg_section_header_table() {
|
|
printf " WireGuard Events:\n"
|
|
printf " %-20s %-20s %-18s %s\n" "TIME" "CLIENT" "ENDPOINT" "EVENT"
|
|
printf " %s\n" "$(printf '─%.0s' {1..75})"
|
|
}
|
|
|
|
function ui::logs::fw_row() {
|
|
local ts="${1:-}" client="${2:-}" dest_ip="${3:-}" dest_port="${4:-}" \
|
|
proto="${5:-}" svc_name="${6:-}" count="${7:-1}" \
|
|
w_client="${8:-20}" w_dest="${9:-30}"
|
|
local dest_display
|
|
dest_display=$(ui::logs::build_dest "$dest_ip" "$dest_port" "$proto" "$svc_name")
|
|
local count_suffix=""
|
|
[[ "$count" -gt 1 ]] && count_suffix=" \033[2m(x${count})\033[0m"
|
|
local client_pad dest_pad_n
|
|
client_pad=$(printf "%-${w_client}s" "$client")
|
|
dest_pad_n=$(( w_dest - ${#dest_display} ))
|
|
[[ $dest_pad_n -lt 0 ]] && dest_pad_n=0
|
|
printf " %s %s \033[1;31m→\033[0m %s%*s%b\n" \
|
|
"$ts" "$client_pad" "$dest_display" "$dest_pad_n" "" "$count_suffix"
|
|
}
|
|
|
|
function ui::logs::fw_row_table() {
|
|
local ts="${1:-}" client="${2:-}" dst="${3:-}" proto="${4:-}" count="${5:-1}"
|
|
local count_str=""
|
|
[[ "$count" -gt 1 ]] && count_str=" (x${count})"
|
|
printf " %-20s %-18s %-25s %s%s\n" "$ts" "$client" "$dst" "$proto" "$count_str"
|
|
}
|
|
|
|
function ui::logs::wg_row() {
|
|
local ts="${1:-}" client="${2:-}" endpoint="${3:-}" event="${4:-}" \
|
|
count="${5:-1}" w_client="${6:-20}" w_endpoint="${7:-20}" \
|
|
gap_seconds="${8:-}"
|
|
|
|
local event_color
|
|
case "$event" in
|
|
handshake) event_color="\033[1;32m" ;;
|
|
attempt) event_color="\033[1;31m" ;;
|
|
*) event_color="\033[0;37m" ;;
|
|
esac
|
|
|
|
local count_suffix=""
|
|
[[ "$count" -gt 1 ]] && count_suffix=" \033[2m(x${count})\033[0m"
|
|
|
|
# Gap suffix — only for handshakes with a meaningful gap
|
|
local gap_suffix=""
|
|
if [[ "$event" == "handshake" && -n "$gap_seconds" && "$gap_seconds" -gt 0 ]]; then
|
|
local gap_int="$gap_seconds"
|
|
local threshold="${WG_HANDSHAKE_CHECK_TIME_SEC:-300}"
|
|
local offline_label=""
|
|
[[ "$gap_int" -gt "$threshold" ]] && offline_label=" offline"
|
|
if (( gap_int >= 3600 )); then
|
|
gap_suffix=" \033[2m↑ $(( gap_int / 3600 ))h${offline_label}\033[0m"
|
|
elif (( gap_int >= 60 )); then
|
|
gap_suffix=" \033[2m↑ $(( gap_int / 60 ))m${offline_label}\033[0m"
|
|
fi
|
|
fi
|
|
|
|
local client_pad endpoint_pad_n
|
|
client_pad=$(printf "%-${w_client}s" "$client")
|
|
endpoint_pad_n=$(( w_endpoint - ${#endpoint} ))
|
|
[[ $endpoint_pad_n -lt 0 ]] && endpoint_pad_n=0
|
|
|
|
printf " %s %s %s%*s %b%s\033[0m%b%b\n" \
|
|
"$ts" "$client_pad" "$endpoint" "$endpoint_pad_n" "" \
|
|
"$event_color" "$event" "$count_suffix" "$gap_suffix"
|
|
}
|
|
|
|
function ui::logs::wg_row_table() {
|
|
local ts="${1:-}" client="${2:-}" endpoint="${3:-}" event="${4:-}" count="${5:-1}"
|
|
local count_str=""
|
|
[[ "$count" -gt 1 ]] && count_str=" (x${count})"
|
|
local event_colored
|
|
case "$event" in
|
|
attempt*) event_colored="\033[1;31m${event}\033[0m" ;;
|
|
handshake*) event_colored="\033[1;32m${event}\033[0m" ;;
|
|
*) event_colored="$event" ;;
|
|
esac
|
|
printf " %-20s %-20s %-18s %b%s\n" "$ts" "$client" "$endpoint" "$event_colored" "$count_str"
|
|
}
|
|
|
|
_UI_WATCH_FW_COLOR="\033[1;31m"
|
|
_UI_WATCH_WG_COLOR="\033[1;32m"
|
|
|
|
function ui::watch::fw_row() {
|
|
local ts="${1:-}" client="${2:-}" dest_display="${3:-}" \
|
|
w_client="${4:-20}" w_dest="${5:-18}"
|
|
|
|
# "fw" is always 2 visible chars — no padding needed
|
|
local src="${_UI_WATCH_FW_COLOR}fw\033[0m"
|
|
|
|
local ts_pad client_pad dest_pad_n
|
|
ts_pad=$(printf "%-11s" "$ts")
|
|
client_pad=$(printf "%-${w_client}s" "$client")
|
|
dest_pad_n=$(( w_dest - ${#dest_display} ))
|
|
[[ $dest_pad_n -lt 0 ]] && dest_pad_n=0
|
|
|
|
printf " %s %b %s \033[1;31m→\033[0m %s%*s \033[1;31mdrop\033[0m\n" \
|
|
"$ts_pad" "$src" "$client_pad" "$dest_display" "$dest_pad_n" ""
|
|
}
|
|
|
|
function ui::watch::wg_row() {
|
|
local ts="${1:-}" client="${2:-}" endpoint="${3:-}" event="${4:-}" \
|
|
w_client="${5:-20}" w_endpoint="${6:-18}"
|
|
|
|
local event_color
|
|
case "$event" in
|
|
handshake) event_color="\033[1;32m" ;;
|
|
attempt) event_color="\033[1;31m" ;;
|
|
*) event_color="\033[0;37m" ;;
|
|
esac
|
|
|
|
# "wg" is always 2 visible chars — no padding needed
|
|
local src="${_UI_WATCH_WG_COLOR}wg\033[0m"
|
|
|
|
# Use "-" not "—" to avoid multi-byte padding issues
|
|
local endpoint_display="${endpoint:--}"
|
|
|
|
local ts_pad client_pad endpoint_pad_n
|
|
ts_pad=$(printf "%-11s" "$ts")
|
|
client_pad=$(printf "%-${w_client}s" "$client")
|
|
endpoint_pad_n=$(( w_endpoint - ${#endpoint_display} ))
|
|
[[ $endpoint_pad_n -lt 0 ]] && endpoint_pad_n=0
|
|
|
|
printf " %s %b %s %s%*s %b%s\033[0m\n" \
|
|
"$ts_pad" "$src" "$client_pad" "$endpoint_display" "$endpoint_pad_n" "" \
|
|
"$event_color" "$event"
|
|
}
|
|
|
|
|
|
function ui::watch::header_table() {
|
|
printf "\n %-20s %-8s %-22s %-28s %-14s %s\n" \
|
|
"TIME" "SOURCE" "CLIENT" "DESTINATION/ENDPOINT" "EVENT" "STATUS"
|
|
printf " %s\n\n" "$(printf '─%.0s' {1..105})"
|
|
}
|
|
|
|
function ui::watch::fw_row_table() {
|
|
local ts="${1:-}" client="${2:-}" dest="${3:-}" event="${4:-}" status="${5:-}"
|
|
printf " %-20s %-8s %-22s %-28s \033[1;31m%-14s\033[0m %s\n" \
|
|
"$ts" "firewall" "$client" "$dest" "$event" "$status"
|
|
}
|
|
|
|
function ui::watch::wg_row_table() {
|
|
local ts="${1:-}" client="${2:-}" endpoint="${3:-}" event="${4:-}" status="${5:-}"
|
|
local event_color
|
|
case "$event" in
|
|
handshake) event_color="\033[1;32m" ;;
|
|
attempt) event_color="\033[1;31m" ;;
|
|
*) event_color="\033[0;37m" ;;
|
|
esac
|
|
printf " %-20s %-8s %-22s %-28s %b%-14s\033[0m %s\n" \
|
|
"$ts" "wireguard" "$client" "$endpoint" "$event_color" "$event" "$status"
|
|
} |