Last active 1 month ago

gistfile1.txt Raw
1function 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}