gistfile1.txt
· 2.2 KiB · Text
Eredeti
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:-}" resolved="${9:-}"
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 with offline label only when gap > threshold * 2
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_threshold=$(( threshold * 2 ))
local offline_label=""
[[ "$gap_int" -gt "$offline_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
# Build endpoint display: raw_ip [→ resolved dim]
local endpoint_display="${endpoint:--}"
if [[ -n "$resolved" && -n "$endpoint" ]]; then
endpoint_display="${endpoint} \033[2m→ ${resolved}\033[0m"
fi
if [[ -n "$endpoint" ]]; then
local resolved
resolved=$(resolve::ip "$endpoint" 2>/dev/null || echo "")
if [[ -n "$resolved" && "$resolved" != "$endpoint" ]]; then
endpoint_display="${endpoint} \033[2m→ ${resolved}\033[0m"
else
endpoint_display="$endpoint"
fi
else
endpoint_display="-"
fi
local client_pad endpoint_pad_n
client_pad=$(printf "%-${w_client}s" "$client")
# Use raw endpoint length for padding (resolved part is dim/extra)
local ep_raw_len=${#endpoint}
[[ -z "$endpoint" ]] && ep_raw_len=1 # "-"
endpoint_pad_n=$(( w_endpoint - ep_raw_len ))
[[ $endpoint_pad_n -lt 0 ]] && endpoint_pad_n=0
local ts_pad
ts_pad=$(printf "%-11s" "$ts")
printf " %s %s %b%*s %b%s\033[0m%b%b\n" \
"$ts_pad" "$client_pad" \
"$endpoint_display" "$endpoint_pad_n" "" \
"$event_color" "$event" "$count_suffix" "$gap_suffix"
}
| 1 | function ui::logs::wg_row() { |
| 2 | local ts="${1:-}" client="${2:-}" endpoint="${3:-}" event="${4:-}" \ |
| 3 | count="${5:-1}" w_client="${6:-20}" w_endpoint="${7:-20}" \ |
| 4 | gap_seconds="${8:-}" resolved="${9:-}" |
| 5 | |
| 6 | local event_color |
| 7 | case "$event" in |
| 8 | handshake) event_color="\033[1;32m" ;; |
| 9 | attempt) event_color="\033[1;31m" ;; |
| 10 | *) event_color="\033[0;37m" ;; |
| 11 | esac |
| 12 | |
| 13 | local count_suffix="" |
| 14 | [[ "$count" -gt 1 ]] && count_suffix=" \033[2m(x${count})\033[0m" |
| 15 | |
| 16 | # Gap suffix with offline label only when gap > threshold * 2 |
| 17 | local gap_suffix="" |
| 18 | if [[ "$event" == "handshake" && -n "$gap_seconds" && "$gap_seconds" -gt 0 ]]; then |
| 19 | local gap_int="$gap_seconds" |
| 20 | local threshold="${WG_HANDSHAKE_CHECK_TIME_SEC:-300}" |
| 21 | local offline_threshold=$(( threshold * 2 )) |
| 22 | local offline_label="" |
| 23 | [[ "$gap_int" -gt "$offline_threshold" ]] && offline_label=" offline" |
| 24 | if (( gap_int >= 3600 )); then |
| 25 | gap_suffix=" \033[2m↑ $(( gap_int / 3600 ))h${offline_label}\033[0m" |
| 26 | elif (( gap_int >= 60 )); then |
| 27 | gap_suffix=" \033[2m↑ $(( gap_int / 60 ))m${offline_label}\033[0m" |
| 28 | fi |
| 29 | fi |
| 30 | |
| 31 | # Build endpoint display: raw_ip [→ resolved dim] |
| 32 | local endpoint_display="${endpoint:--}" |
| 33 | if [[ -n "$resolved" && -n "$endpoint" ]]; then |
| 34 | endpoint_display="${endpoint} \033[2m→ ${resolved}\033[0m" |
| 35 | fi |
| 36 | |
| 37 | if [[ -n "$endpoint" ]]; then |
| 38 | local resolved |
| 39 | resolved=$(resolve::ip "$endpoint" 2>/dev/null || echo "") |
| 40 | if [[ -n "$resolved" && "$resolved" != "$endpoint" ]]; then |
| 41 | endpoint_display="${endpoint} \033[2m→ ${resolved}\033[0m" |
| 42 | else |
| 43 | endpoint_display="$endpoint" |
| 44 | fi |
| 45 | else |
| 46 | endpoint_display="-" |
| 47 | fi |
| 48 | |
| 49 | local client_pad endpoint_pad_n |
| 50 | client_pad=$(printf "%-${w_client}s" "$client") |
| 51 | # Use raw endpoint length for padding (resolved part is dim/extra) |
| 52 | local ep_raw_len=${#endpoint} |
| 53 | [[ -z "$endpoint" ]] && ep_raw_len=1 # "-" |
| 54 | endpoint_pad_n=$(( w_endpoint - ep_raw_len )) |
| 55 | [[ $endpoint_pad_n -lt 0 ]] && endpoint_pad_n=0 |
| 56 | |
| 57 | local ts_pad |
| 58 | ts_pad=$(printf "%-11s" "$ts") |
| 59 | |
| 60 | printf " %s %s %b%*s %b%s\033[0m%b%b\n" \ |
| 61 | "$ts_pad" "$client_pad" \ |
| 62 | "$endpoint_display" "$endpoint_pad_n" "" \ |
| 63 | "$event_color" "$event" "$count_suffix" "$gap_suffix" |
| 64 | } |