forked from HarHarLinks/wireguard-rofi-waybar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wireguard.sh
executable file
·165 lines (150 loc) · 2.98 KB
/
wireguard.sh
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env bash
# nmcli WireGuard abstraction layer for use with my waybar module and rofi custom menu script
#
# requires nmcli on your path
# install to the same directory as wireguard-rofi.sh
#
# usage: ./wireguard.sh [short|menu|toggle NAME]
# no argument: print current connections
# short: print current connections in short format
# menu: print all connections
# toggle NAME: toggle connection NAME
if ! command -v nmcli >/dev/null 2>&1; then
echo "ERROR: 'nmcli' not found"
echo "This tool is a 'nmcli' frontend, hence you should install NetworkManager before trying to use it."
exit 1
fi
nargs=$#
get="no"
showmenu="no"
short="no"
dotoggle="no"
if [[ $nargs == 0 ]]
then
get="yes"
elif [[ $nargs == 1 ]]
then
if [[ $1 == "menu" ]]
then
showmenu="yes"
elif [[ $1 == "short" ]]
then
if ! command -v sed >/dev/null 2>&1; then
echo "ERROR: 'sed' not found"
echo "Please install 'sed' to enable the 'short' option."
exit 1
fi
get="yes"
short="short"
fi
elif [[ $nargs == 2 ]]
then
if [[ $1 == "toggle" ]]
then
dotoggle="yes"
conn="$2"
fi
fi
nmclicmd="nmcli connection"
wgconns="$nmclicmd show"
wgactive="$wgconns --active"
connected=()
available=()
function get_conns {
while read -r name uuid type device
do
if [[ $type != "wireguard" ]]
then
continue
fi
if [[ $device != "--" ]]
then
while read -r key value
do
if [[ $key != "ipv4.addresses:" ]]
then
continue
fi
connected+=("$name: $value")
done < <($wgconns $name)
else
available+=("$name")
fi
done < <($1)
}
function print_conns {
local first="yes"
local array_print="$1[@]"
local array_print=("${!array_print}")
local text=""
local tooltip=""
if [ "${#array_print[@]}" -le 0 ]
then
return
fi
if [[ "$2" == "list" ]]
then
for c in "${array_print[@]}"
do
echo "$1: $c"
done
else
for c in "${array_print[@]}"
do
if [[ "$first" != "yes" ]]
then
text="$text | "
tooltip="$tooltip\n"
fi
if [[ "$2" == "short" ]]
then
text="$text$(echo -n $c | sed -E 's/^(.+): ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]+)$/\1/')"
else
text="$text$c"
fi
tooltip="$tooltip$c"
first="no"
done
echo "{\"text\": \"$text\", \"tooltip\": \"$tooltip\"}"
fi
}
function array_contains {
local array_has="$1[@]"
local array_has=("${!array_has}")
local element="$2"
for e in "${array_has[@]}"
do
if [[ "$e" == *"$element"* ]]
then
echo "yes"
return
fi
done
echo "no"
}
if [[ $get == "yes" ]]
then
get_conns "$wgactive"
print_conns connected "$short"
elif [[ $showmenu == "yes" ]]
then
get_conns "$wgconns"
print_conns connected "list"
print_conns available "list"
elif [[ $dotoggle == "yes" ]]
then
get_conns "$wgconns"
if [[ "$(array_contains connected $conn)" == "yes" ]]
then
$nmclicmd down "$conn"
elif [[ "$(array_contains available $conn)" == "yes" ]]
then
$nmclicmd up "$conn"
else
echo "err: connection not found"
exit 1
fi
else
echo "err: wrong args"
exit 1
fi