gistfile1.txt
· 3.0 KiB · Text
Surowy
function cmd::list::_collect_all_rows() {
local dir
dir="$(ctx::clients)"
local _verbose_status="${LIST_VERBOSE_STATUS:-true}"
for conf in "${dir}"/*.conf; do
[[ -f "$conf" ]] || continue
local client_name
client_name=$(basename "$conf" .conf)
[[ -z "$client_name" ]] && continue
if [[ ${#p_identity_filter[@]} -gt 0 && \
-z "${p_identity_filter[$client_name]:-}" ]]; then
continue
fi
local ip="${p_ips[$client_name]:-}"
[[ -z "$ip" ]] && ip=$(grep "^Address" "$conf" | awk '{print $3}' | cut -d'/' -f1)
[[ -z "$ip" ]] && continue
local type="${p_types[$client_name]:-unknown}"
[[ -n "$filter_type" && "$type" != "$filter_type" ]] && continue
local pubkey="${p_pubkeys[$client_name]:-}"
local handshake_ts="${wg_handshakes[$pubkey]:-0}"
local is_blocked="${p_blocked[$client_name]:-false}"
local is_restricted="${p_restricted[$client_name]:-false}"
local last_ts="${p_last_ts[$client_name]:-}"
local rule="${p_rules[$client_name]:-}"
local group="${p_main_groups[$client_name]:-}"
if $online_only; then peers::is_online "$client_name" "$handshake_ts" "$last_ts" || continue; fi
if $offline_only; then peers::is_offline "$client_name" "$handshake_ts" "$last_ts" || continue; fi
if $restricted_only && [[ "$is_restricted" != "true" ]]; then continue; fi
if $blocked_only && [[ "$is_blocked" != "true" ]]; then continue; fi
if $allowed_only && { [[ "$is_blocked" == "true" ]] || \
[[ "$is_restricted" == "true" ]]; }; then continue; fi
if [[ -n "$filter_rule" && "$rule" != "$filter_rule" ]]; then continue; fi
if [[ -n "$filter_group" ]]; then
local all_groups="${peer_group_map[$client_name]:-}"
[[ "$all_groups" != *"$filter_group"* ]] && continue
fi
# Resolve status — verbose or simple
local status
if [[ "$_verbose_status" == "true" ]]; then
status=$(peers::format_status_verbose "$client_name" "$pubkey" \
"$is_blocked" "$is_restricted" "$handshake_ts" "$last_ts" | \
sed 's/\x1b\[[0-9;]*m//g')
else
local state
state=$(peers::connection_state "$is_blocked" "$is_restricted" "$handshake_ts" "$last_ts")
status="${state%%|*}"
fi
# Resolve last seen
local last_seen="-"
if [[ "$is_blocked" == "true" && -n "$last_ts" && "$last_ts" != "0" ]]; then
local attempt_ts
attempt_ts=$(json::iso_to_ts "$last_ts")
last_seen="$(fmt::datetime_short "$attempt_ts") (dropped)"
elif [[ -n "$handshake_ts" && "$handshake_ts" != "0" ]]; then
local ts_display
ts_display=$(fmt::datetime_short "$handshake_ts")
if [[ "$status" == "online"* ]]; then
last_seen="${ts_display} (handshake)"
else
last_seen="$ts_display"
fi
fi
printf "%s|%s|%s|%s|%s|%s|%s|%s|%s\n" \
"$client_name" "$ip" "$type" \
"${rule:--}" "${group:--}" \
"$status" "$last_seen" \
"$is_blocked" "$is_restricted"
done
}
| 1 | function cmd::list::_collect_all_rows() { |
| 2 | local dir |
| 3 | dir="$(ctx::clients)" |
| 4 | local _verbose_status="${LIST_VERBOSE_STATUS:-true}" |
| 5 | |
| 6 | for conf in "${dir}"/*.conf; do |
| 7 | [[ -f "$conf" ]] || continue |
| 8 | local client_name |
| 9 | client_name=$(basename "$conf" .conf) |
| 10 | [[ -z "$client_name" ]] && continue |
| 11 | |
| 12 | if [[ ${#p_identity_filter[@]} -gt 0 && \ |
| 13 | -z "${p_identity_filter[$client_name]:-}" ]]; then |
| 14 | continue |
| 15 | fi |
| 16 | |
| 17 | local ip="${p_ips[$client_name]:-}" |
| 18 | [[ -z "$ip" ]] && ip=$(grep "^Address" "$conf" | awk '{print $3}' | cut -d'/' -f1) |
| 19 | [[ -z "$ip" ]] && continue |
| 20 | |
| 21 | local type="${p_types[$client_name]:-unknown}" |
| 22 | [[ -n "$filter_type" && "$type" != "$filter_type" ]] && continue |
| 23 | |
| 24 | local pubkey="${p_pubkeys[$client_name]:-}" |
| 25 | local handshake_ts="${wg_handshakes[$pubkey]:-0}" |
| 26 | local is_blocked="${p_blocked[$client_name]:-false}" |
| 27 | local is_restricted="${p_restricted[$client_name]:-false}" |
| 28 | local last_ts="${p_last_ts[$client_name]:-}" |
| 29 | local rule="${p_rules[$client_name]:-}" |
| 30 | local group="${p_main_groups[$client_name]:-}" |
| 31 | |
| 32 | if $online_only; then peers::is_online "$client_name" "$handshake_ts" "$last_ts" || continue; fi |
| 33 | if $offline_only; then peers::is_offline "$client_name" "$handshake_ts" "$last_ts" || continue; fi |
| 34 | if $restricted_only && [[ "$is_restricted" != "true" ]]; then continue; fi |
| 35 | if $blocked_only && [[ "$is_blocked" != "true" ]]; then continue; fi |
| 36 | if $allowed_only && { [[ "$is_blocked" == "true" ]] || \ |
| 37 | [[ "$is_restricted" == "true" ]]; }; then continue; fi |
| 38 | if [[ -n "$filter_rule" && "$rule" != "$filter_rule" ]]; then continue; fi |
| 39 | if [[ -n "$filter_group" ]]; then |
| 40 | local all_groups="${peer_group_map[$client_name]:-}" |
| 41 | [[ "$all_groups" != *"$filter_group"* ]] && continue |
| 42 | fi |
| 43 | |
| 44 | # Resolve status — verbose or simple |
| 45 | local status |
| 46 | if [[ "$_verbose_status" == "true" ]]; then |
| 47 | status=$(peers::format_status_verbose "$client_name" "$pubkey" \ |
| 48 | "$is_blocked" "$is_restricted" "$handshake_ts" "$last_ts" | \ |
| 49 | sed 's/\x1b\[[0-9;]*m//g') |
| 50 | else |
| 51 | local state |
| 52 | state=$(peers::connection_state "$is_blocked" "$is_restricted" "$handshake_ts" "$last_ts") |
| 53 | status="${state%%|*}" |
| 54 | fi |
| 55 | |
| 56 | # Resolve last seen |
| 57 | local last_seen="-" |
| 58 | if [[ "$is_blocked" == "true" && -n "$last_ts" && "$last_ts" != "0" ]]; then |
| 59 | local attempt_ts |
| 60 | attempt_ts=$(json::iso_to_ts "$last_ts") |
| 61 | last_seen="$(fmt::datetime_short "$attempt_ts") (dropped)" |
| 62 | elif [[ -n "$handshake_ts" && "$handshake_ts" != "0" ]]; then |
| 63 | local ts_display |
| 64 | ts_display=$(fmt::datetime_short "$handshake_ts") |
| 65 | if [[ "$status" == "online"* ]]; then |
| 66 | last_seen="${ts_display} (handshake)" |
| 67 | else |
| 68 | last_seen="$ts_display" |
| 69 | fi |
| 70 | fi |
| 71 | |
| 72 | printf "%s|%s|%s|%s|%s|%s|%s|%s|%s\n" \ |
| 73 | "$client_name" "$ip" "$type" \ |
| 74 | "${rule:--}" "${group:--}" \ |
| 75 | "$status" "$last_seen" \ |
| 76 | "$is_blocked" "$is_restricted" |
| 77 | done |
| 78 | } |
| 79 |