summaryrefslogtreecommitdiff
path: root/data.d/vpn-wireguard/hooks/post-up
blob: b200922a2934c3d651436bfbaf5c5a273ce4ef5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env bash

readonly COLOR_RESET="\033[0m"
readonly COLOR_OK="\033[32;1m"
readonly COLOR_NOK="\033[31;1m"

main() {
	printf "Verifying connectability...\n"

	# Ping all known hosts, as it seems that the wireguard interface comes up when
	# only after it gets used on the machine itself.
	while read -r addr;
	do
		check "$addr" &
	done < <(awk -F= '/vpn-wireguard.ipv(4|6)=/ { print $NF }' /etc/bashtard/hosts.d/*)

	wait
}

check() {
	local addr="$1"

	if ping -c 1 -q -w 1 "$addr" > /dev/null
	then
		log OK "$addr"
	else
		log NOK "$addr"
	fi
}

log() {
	local state="$1"
	local addr="$2"
	local color="$COLOR_NOK"

	if [[ $state == "OK" ]]
	then
		color="$COLOR_OK"
	fi

	printf "%b%3s%b: %s\n" "$color" "$state" "$COLOR_RESET" "$addr" >&2
}

main "$@"