-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.sh
More file actions
270 lines (227 loc) · 10.2 KB
/
lib.sh
File metadata and controls
270 lines (227 loc) · 10.2 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/bin/bash
# shellcheck disable=SC1091,SC2009,SC2015,SC2034,SC2068
# Definitions for Closure
set -a
# Closure settings from env.sh
this_dir=$PWD
source "$this_dir/env.sh"
if [ -z "$CLS_TYPE_NODE" ]; then
echo "Node type is not set"
exit 1
fi
# WireGuard settings from compose.yml
SERVERURL="$(grep -oP '(?<=SERVERURL=).+' compose.yml)"
SERVERPORT="$(grep -oP '(?<=SERVERPORT=).+' compose.yml)"
PEERDNS="$(grep -oP '(?<=PEERDNS=).+' compose.yml)"
PEERS="$(grep -oP '(?<=PEERS=).+' compose.yml)"
ALLOWEDIPS="$(grep -oP '(?<=ALLOWEDIPS=).+' compose.yml)"
PERSISTENTKEEPALIVE_PEERS="$(grep -oP '(?<=PERSISTENTKEEPALIVE_PEERS=).+' compose.yml)"
INTERNAL_SUBNET="$(grep -oP '(?<=INTERNAL_SUBNET=).+' compose.yml)"
# get all "SERVER_ALLOWEDIPS_PEER_.*=.*" in compose.yml
while IFS= read -r line; do
var_name=$(cut -d= -f1 <<<"$line")
var_value=$(cut -d= -f2 <<<"$line")
declare "$var_name=$var_value"
done < <(grep -oP 'SERVER_ALLOWEDIPS_PEER_.*=.*' compose.yml)
CLS_TYPE_NODE=$(echo "$CLS_TYPE_NODE" | tr '[:upper:]' '[:lower:]')
CLS_WG_SERVER=$(echo "$INTERNAL_SUBNET" | awk 'BEGIN{FS=OFS="."} NF--').1
CLS_WG_SERVER_IP=""
CLS_LOCAL_IFACE=""
CLS_LOCAL_IP=""
CLS_GATEWAY=""
CLS_WG_ONLY=${CLS_WG_ONLY:-false}
user_exists() { id "$1" &>/dev/null; }
sudo() {
if command -v sudo >/dev/null; then
command sudo -E "${@:-:}" || "${@:-:}"
else
"${@:-:}"
fi
}
# shellcheck disable=SC2120
wg() {
sudo bash <<EOF
if [ "$CLS_DOCKER" = "true" ]; then
docker exec wireguard wg ${@@Q}
else
command wg ${@@Q}
fi
EOF
}
get_local_iface() {
comm -12 <(ifconfig | grep ': flags=' | grep -vP '(SLAVE|POINTOPOINT)' | grep -oP '.*(?=:)' | sort -u) <(route -n | awk '{$1=""; print substr($0,2)}' | grep -P '^\d' | grep -v '^0\.0\.0\.0' | awk '{print $NF}' | sort -u)
}
get_local_ip() {
CLS_LOCAL_IFACE=$(get_local_iface)
[ -n "$CLS_LOCAL_IFACE" ] || return 1
ip r | grep -q '^default via' || sudo ip r add default via "$(nmcli dev show "$CLS_LOCAL_IFACE" | grep -oP '((?<=GATEWAY:)[^-]*|/0.*?= [^,]+)' | grep -oE '[^ ]+$' | head -n1)" dev "$CLS_LOCAL_IFACE" &>/dev/null
CLS_GATEWAY=$(ip r | grep -oP '^default via \K\S+')
CLS_LOCAL_IP=$(ip a show "$CLS_LOCAL_IFACE" | grep -oP 'inet \K\S+' | cut -d/ -f1)
wg | grep -oE 'endpoint: [^:]+' | grep -oE '\S+$' | while read -r endpoint; do route -n | grep -q "$endpoint" || sudo route add -net "$endpoint" netmask 255.255.255.255 gw "$CLS_GATEWAY" &>/dev/null; done
}
restart_isc() {
source dhcp/isc-dhcp-server
if [ -n "$INTERFACESv4" ]; then
if ! diff -q dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf &>/dev/null || ! diff -q dhcp/isc-dhcp-server /etc/default/isc-dhcp-server &>/dev/null; then
sudo cp -f dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf
sudo cp -f dhcp/isc-dhcp-server /etc/default/isc-dhcp-server
fi
sudo systemctl restart isc-dhcp-server &
else
sudo systemctl stop isc-dhcp-server &
fi
}
stop_hostapd() {
local aps
aps=$(sudo find /var/run/hostapd -type s 2>&1 | grep -oP '(?<=/var/run/hostapd/).+')
ps -aux | grep -P "^[^-]+hostapd$2" | awk '{print $2}' | while read -r pid; do sudo kill -9 "$pid" &>/dev/null; done
for wiface in $1 $aps; do
[[ -z "$1" || "$1" == "$wiface" ]] || continue
sudo rm -rf /var/run/hostapd/"$wiface" &>/dev/null
[[ ! "$wiface" =~ @ ]] || sudo iw dev "$wiface" del &>/dev/null
done
}
start_hostapd() {
(
until [ -n "$CLS_LOCAL_IFACE" ]; do
get_local_ip
sleep 1
done
[ ! -f /etc/resolv.conf ] || sudo rm -f /etc/resolv.conf
(
cat resolv.conf
echo ""
(
nmcli dev show "$CLS_LOCAL_IFACE" | grep DNS | grep -oP '\S+$'
) | while read -r ip; do echo "nameserver $ip"; done
(
nmcli dev show "$CLS_LOCAL_IFACE" | grep DOMAIN | grep -oP '\S+$' || echo .
) | while read -r name; do echo "search $name"; done
) | sudo tee /etc/resolv.conf >/dev/null
sudo tc qdisc del dev "$CLS_LOCAL_IFACE" root &>/dev/null
sudo tc qdisc replace dev "$CLS_LOCAL_IFACE" root cake "$([ -z "$CLS_BANDWIDTH" ] && echo diffserv8 || echo "bandwidth $CLS_BANDWIDTH diffserv8")" nat docsis ack-filter
sudo busctl --system set-property org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager ConnectivityCheckEnabled "b" 0 2>/dev/null
(crontab -l 2>/dev/null | grep -Fv "/ddns.sh &") | crontab -
if [ -n "$CLS_DYN_DNS" ]; then
(
crontab -l 2>/dev/null
echo "0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/bin/sleep 10 ; /usr/bin/bash $this_dir/ddns.sh &"
) | crontab -
sudo bash ddns.sh
fi
) &
if [ -n "$CLS_WIFACE" ]; then
(
until iwconfig "$CLS_WIFACE" | grep -q 'Bit Rate='; do sleep 1; done
set_mac="$(jq ".[\"$(printf "%b" "$(iwconfig "$CLS_WIFACE" | grep -zoP '(?<=ESSID:").+(?=")' | tr -d '\0' | sed -r "s/\"/\\\\\"/g")")\"]" config/wifis.json 2>/dev/null | tr -d '"')"
if (("${#set_mac}" == 17)) && [ "$set_mac" != "$(ifconfig "$CLS_WIFACE" | grep -oP "(?<=ether )\S+")" ]; then
sudo ifconfig "$CLS_WIFACE" down
sudo macchanger -m "$set_mac" "$CLS_WIFACE"
sudo ifconfig "$CLS_WIFACE" up
fi
sudo iw dev "$CLS_WIFACE" set power_save off
ip a show "$CLS_INTERN_IFACE" | grep -q UP || exec sudo CLS_WG_ONLY=true bash restart.sh ${@@Q}
) &
fi
if ! $CLS_AP_HOSTAPD; then
restart_isc
else
declare -A wifaces_configs
IFS='/' read -r -a wifaces <<<"$CLS_AP_WIFACES"
IFS='/' read -r -a configs <<<"$CLS_AP_CONFIGS"
for i in "${!wifaces[@]}"; do wifaces_configs["${wifaces[$i]}"]="${configs[$i]}"; done
for wiface in "${!wifaces_configs[@]}"; do
config="${wifaces_configs[$wiface]}"
if [ "$config" = "." ]; then
if [[ "$wiface" =~ @ ]]; then
config="$(iwconfig "${wiface//*@/}" | grep -oP '(?<=Frequency:)\d+')@"
[ ! -f hostapd/"$config${wiface//*@/}".conf ] || config=$config${wiface//*@/}
else
config="$wiface"
fi
fi
[ -f hostapd/"$config".conf ] && ! yq '(.network.wifis | keys)[]' netplan.yml | grep -qFx "$wiface" && iw dev | grep -qzP "Interface ${wiface//*@/}\n" && ! iw dev "$wiface" info | grep -q ssid || continue
sudo sed -i "s/^\(interface\s*=\s*\).*/\1$wiface/" hostapd/"$config".conf
sudo chmod 644 hostapd/"$config".conf
(
while :; do
while iw dev "$wiface" info | grep -q ssid; do sleep 5; done
until iw dev "$wiface" info | grep -q ssid; do
stop_hostapd "$wiface" ".*$config"
if [[ "$wiface" =~ @ ]]; then
sudo iw dev "${wiface//*@/}" interface add "$wiface" type __ap
freq=$(iw list | grep "$(iwconfig "${wiface//*@/}" | grep -oP '(?<=Frequency:)\S+' | tr -d '.')." | head -n1 | grep -oP '(?<=\[)[^\]]+')
if [ -z "$freq" ]; then
grep -q '^hw_mode=a' hostapd/"$config".conf && freq=149 || freq=11
fi
sudo sed -i "s/^\(channel\s*=\s*\).*/\1$freq/" hostapd/"$config".conf
fi
sudo hostapd -i "$wiface" -P /run/hostapd.pid -B hostapd/"$config".conf
sudo iw dev "$wiface" set power_save off 2>/dev/null
local octet
octet=$(ifconfig "$wiface" | grep -zoP '(?<=inet 10\.42\.)\S+(?=\.1)')
if [ -z "$octet" ]; then
octet=$((1 + $( (
ifconfig | grep -zoP '(?<=10\.42\.)\S+(?=\.1)'
echo 1
) | grep -v 255 | sort -ru | head -n1))) 2>/dev/null
[ -n "$octet" ] || octet=1
sudo ifconfig "$wiface" 10.42."$octet".1 netmask 255.255.255.0
fi
grep -qF "subnet 10.42.$octet.0 netmask 255.255.255.0" dhcp/dhcpd.conf || echo -e "\nsubnet 10.42.$octet.0 netmask 255.255.255.0 {\n option routers 10.42.$octet.1;\n range 10.42.$octet.2 10.42.$octet.254;\n}" | sudo tee -a dhcp/dhcpd.conf
restart_isc
sleep 15
done
done
) &
done
fi
}
is_ip() {
[[ "$1" =~ ^[0-9.]+$ || "$1" =~ ^[0-9a-fA-F:]+$ ]]
}
curl() {
# if connection times out or max time is reached, wait increasing amounts of time before retrying
local i=2
local max_attempts=7
local wait_time=1
local result
while [ "$i" -lt "$max_attempts" ]; do
result=$(command curl -sSLNZ --connect-timeout 60 -m 120 --retry 5 --retry-delay 1 --retry-all-errors "$@" 2>/dev/null)
[ -n "$result" ] && echo "$result" && return 0
sleep "$wait_time"
((i++))
((wait_time *= i))
done
echo ""
return 1
}
dig() {
[ -z "$2" ] && command dig +short "$1" 2>/dev/null | tail -n1 || command dig "$1" +trace 2>/dev/null | grep -oP "(?<=^${1//\./\\\.}\.).+(AAA)?A.+" | grep -oP '\S+$'
}
direct_domain() {
local ichi
ichi=$(dig "$(grep -oP '((?<=http:\/\/)|(?<=https:\/\/)).+?[^/?]+' <<<"$1")")
for ip in $ichi; do ip r | grep -q "$ip" || sudo ip route add "$ip" via "$CLS_GATEWAY" dev "$CLS_LOCAL_IFACE" &>/dev/null; done
$1 2>/dev/null
for ip in $ichi; do ! ip r | grep -q "$ip" || sudo ip route del "$ip" &>/dev/null; done
}
get_server_ip() {
local server_ip="$SERVERURL"
if ! is_ip "$server_ip"; then
if [[ "$CLS_TYPE_NODE" =~ (spoke|saah) ]]; then
server_ip=$(dig "$SERVERURL" "$1" | tail -n1)
else
server_ip=$(direct_domain "curl https://icanhazip.com" | tail -n1)
fi
fi
echo "$server_ip"
}
cast() {
local hook="$1"
shift
pushd /home/"$CLS_ACTIVE_USER"/ || exit
sudo bash "$this_dir/hooks/$hook.sh" ${@@Q}
popd || exit
}
set +a