gistfile1.txt
· 2.3 KiB · Text
Raw
function cmd::list::show_client() {
local name="$1"
local conf
conf="$(ctx::clients)/${name}.conf"
if [[ ! -f "$conf" ]]; then
log::error "Client not found: ${name}"
return 1
fi
local ip allowed_ips public_key
ip=$(grep "^Address" "$conf" | awk '{print $3}' | cut -d'/' -f1)
allowed_ips=$(grep "^AllowedIPs" "$conf" | cut -d'=' -f2- | xargs)
public_key=$(keys::public "$name" 2>/dev/null || echo "unknown")
local type
type=$(peers::get_type "$ip")
local endpoint="—"
if peers::is_blocked "$name"; then
local ep
ep=$(monitor::last_endpoint "$name")
[[ -n "$ep" ]] && endpoint="$ep"
else
local ep
ep=$(monitor::endpoint_for_key "$public_key")
[[ -n "$ep" ]] && endpoint="$ep"
fi
# Get handshake and last attempt for status/last_seen
local handshake_ts
handshake_ts=$(wg show "$(config::interface)" latest-handshakes 2>/dev/null \
| grep "^${public_key}" | awk '{print $2}')
handshake_ts="${handshake_ts:-0}"
local last_ts
last_ts=$(monitor::last_attempt "$name")
local is_blocked="false"
peers::is_blocked "$name" && is_blocked="true"
local status last_seen
status=$(cmd::list::_format_status "$name" "$public_key" \
"$is_blocked" "false" "$handshake_ts" "$last_ts")
last_seen=$(cmd::list::_format_last_seen "$name" "$public_key" \
"$is_blocked" "$last_ts" "" "$handshake_ts")
local block_file
block_file="$(ctx::block::path "${name}.block")"
local blocks=""
if [[ -f "$block_file" ]] && [[ -s "$block_file" ]]; then
while IFS=" " read -r client_ip target port proto; do
if [[ -z "$target" ]]; then
blocks+=" all traffic blocked\n"
else
local rule=" ${target}"
[[ -n "$port" ]] && rule+=":${port}/${proto}"
blocks+="${rule}\n"
fi
done < "$block_file"
fi
ui::section "Client: ${name}"
ui::row "IP" "$ip"
ui::row "Type" "$type"
ui::row "Status" "$(echo -e "$status")"
ui::row "Endpoint" "$endpoint"
ui::row "Last seen" "$last_seen"
ui::row "AllowedIPs" "$allowed_ips"
ui::row "Public key" "$public_key"
if [[ -z "$blocks" ]]; then
ui::row "Blocks" "none"
elif [[ "$blocks" == *"all traffic blocked"* ]]; then
ui::row "Blocks" "$(echo -e "\033[1;31mAll\033[0m")"
else
printf " %-20s\n" "Blocks:"
echo -e "$blocks"
fi
printf "\n"
}
| 1 | function cmd::list::show_client() { |
| 2 | local name="$1" |
| 3 | local conf |
| 4 | conf="$(ctx::clients)/${name}.conf" |
| 5 | |
| 6 | if [[ ! -f "$conf" ]]; then |
| 7 | log::error "Client not found: ${name}" |
| 8 | return 1 |
| 9 | fi |
| 10 | |
| 11 | local ip allowed_ips public_key |
| 12 | ip=$(grep "^Address" "$conf" | awk '{print $3}' | cut -d'/' -f1) |
| 13 | allowed_ips=$(grep "^AllowedIPs" "$conf" | cut -d'=' -f2- | xargs) |
| 14 | public_key=$(keys::public "$name" 2>/dev/null || echo "unknown") |
| 15 | |
| 16 | local type |
| 17 | type=$(peers::get_type "$ip") |
| 18 | |
| 19 | local endpoint="—" |
| 20 | if peers::is_blocked "$name"; then |
| 21 | local ep |
| 22 | ep=$(monitor::last_endpoint "$name") |
| 23 | [[ -n "$ep" ]] && endpoint="$ep" |
| 24 | else |
| 25 | local ep |
| 26 | ep=$(monitor::endpoint_for_key "$public_key") |
| 27 | [[ -n "$ep" ]] && endpoint="$ep" |
| 28 | fi |
| 29 | |
| 30 | # Get handshake and last attempt for status/last_seen |
| 31 | local handshake_ts |
| 32 | handshake_ts=$(wg show "$(config::interface)" latest-handshakes 2>/dev/null \ |
| 33 | | grep "^${public_key}" | awk '{print $2}') |
| 34 | handshake_ts="${handshake_ts:-0}" |
| 35 | |
| 36 | local last_ts |
| 37 | last_ts=$(monitor::last_attempt "$name") |
| 38 | |
| 39 | local is_blocked="false" |
| 40 | peers::is_blocked "$name" && is_blocked="true" |
| 41 | |
| 42 | local status last_seen |
| 43 | status=$(cmd::list::_format_status "$name" "$public_key" \ |
| 44 | "$is_blocked" "false" "$handshake_ts" "$last_ts") |
| 45 | last_seen=$(cmd::list::_format_last_seen "$name" "$public_key" \ |
| 46 | "$is_blocked" "$last_ts" "" "$handshake_ts") |
| 47 | |
| 48 | local block_file |
| 49 | block_file="$(ctx::block::path "${name}.block")" |
| 50 | local blocks="" |
| 51 | if [[ -f "$block_file" ]] && [[ -s "$block_file" ]]; then |
| 52 | while IFS=" " read -r client_ip target port proto; do |
| 53 | if [[ -z "$target" ]]; then |
| 54 | blocks+=" all traffic blocked\n" |
| 55 | else |
| 56 | local rule=" ${target}" |
| 57 | [[ -n "$port" ]] && rule+=":${port}/${proto}" |
| 58 | blocks+="${rule}\n" |
| 59 | fi |
| 60 | done < "$block_file" |
| 61 | fi |
| 62 | |
| 63 | ui::section "Client: ${name}" |
| 64 | ui::row "IP" "$ip" |
| 65 | ui::row "Type" "$type" |
| 66 | ui::row "Status" "$(echo -e "$status")" |
| 67 | ui::row "Endpoint" "$endpoint" |
| 68 | ui::row "Last seen" "$last_seen" |
| 69 | ui::row "AllowedIPs" "$allowed_ips" |
| 70 | ui::row "Public key" "$public_key" |
| 71 | |
| 72 | if [[ -z "$blocks" ]]; then |
| 73 | ui::row "Blocks" "none" |
| 74 | elif [[ "$blocks" == *"all traffic blocked"* ]]; then |
| 75 | ui::row "Blocks" "$(echo -e "\033[1;31mAll\033[0m")" |
| 76 | else |
| 77 | printf " %-20s\n" "Blocks:" |
| 78 | echo -e "$blocks" |
| 79 | fi |
| 80 | printf "\n" |
| 81 | } |