forked from VTChevalier/killswitch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkillswitch.sh
More file actions
executable file
·431 lines (369 loc) · 10.1 KB
/
Copy pathkillswitch.sh
File metadata and controls
executable file
·431 lines (369 loc) · 10.1 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/bin/sh
#
# /usr/sbin/killswitch.sh
#
# Copyright (C) 2018-2026 Victor T. Chevalier
# SPDX-License-Identifier: GPL-3.0-only
#
# Written by Victor T. Chevalier
#
# Designed for use with OpenVPN on Debian-family Linux distributions.
#
# Network configuration
SETUP="no" # Change to yes after updating script
NET_DEV="eth0" # Physical network interface
LOCAL_NET="192.168.0.0/24" # Local network subnet
NET_TUN="tun0" # VPN tunnel interface
PORT=443 # Port used by the VPN connection
VPN_ENDPOINT="" # Optional VPN server IP/host
# Service and health configuration
SERVICE="deluged" # Protected service stopped on VPN loss
OPENVPN_SERVICE="openvpn-client@ipvanish.service"
CHECK_HOST="google.com" # Connectivity check host
CHECK_INTERVAL=60 # Seconds between checks
WAIT_INTERVAL=5 # Seconds between readiness checks
READINESS_TIMEOUT=180 # Seconds to wait for VPN readiness
OPENVPN_RESTART_ATTEMPTS=2 # OpenVPN restarts before reboot
REQUIRE_UFW_IPV6="yes" # Require UFW to manage IPv6 rules
LOGFILE="/var/log/killswitch/vpn.log" # Log file location
# Installation targets
INSTALL_PATH="/usr/sbin/killswitch.sh"
SERVICE_FILE="/etc/systemd/system/killswitch.service"
UFW_DEFAULT="/etc/default/ufw"
# Command paths
UFW="/usr/sbin/ufw"
IP="/usr/sbin/ip"
PING="/usr/bin/ping"
GETENT="/usr/bin/getent"
SYSTEMCTL="/usr/bin/systemctl"
REBOOT="/usr/sbin/reboot"
MKDIR="/usr/bin/mkdir"
GREP="/usr/bin/grep"
HOSTNAME="/usr/bin/hostname"
DATE="/usr/bin/date"
SLEEP="/usr/bin/sleep"
CP="/usr/bin/cp"
CHMOD="/usr/bin/chmod"
CAT="/usr/bin/cat"
ID="/usr/bin/id"
info()
{
printf '%s\n\n' "Usage: killswitch.sh up/down/check/health/guard/install"
printf '%s\n\n' "killswitch.sh guards a protected service behind OpenVPN and UFW."
printf '%s\n' "Options:"
printf '%s\n' " up configure UFW, verify VPN, then start the protected service"
printf '%s\n' " down stop the protected service and keep UFW deny-by-default"
printf '%s\n' " check start safely, monitor VPN, recover or reboot on failure"
printf '%s\n' " health checks OpenVPN, tunnel interface, DNS, and tunnel ping once"
printf '%s\n' " guard waits until OpenVPN, tunnel interface, DNS, and tunnel ping are ready"
printf '%s\n' " install install the script and systemd monitor service"
printf '\n%s\n' "An argument must be provided or you will receive this message."
}
fail()
{
printf '%s\n' "$1" >&2
exit 1;
}
require_setup()
{
if [ "$SETUP" != "yes" ]; then
fail "Please update variables in /usr/sbin/killswitch.sh"
fi
if [ -z "$OPENVPN_SERVICE" ]; then
fail "Please set OPENVPN_SERVICE to the systemd OpenVPN client service name"
fi
}
require_paths()
{
MISSING_PATHS=""
MISSING_PACKAGES=""
while [ "$#" -gt 0 ]; do
COMMAND_PATH=$1
PACKAGE=$2
if [ ! -x "$COMMAND_PATH" ]; then
MISSING_PATHS="${MISSING_PATHS} ${COMMAND_PATH}"
case " $MISSING_PACKAGES " in
*" $PACKAGE "*)
;;
*)
MISSING_PACKAGES="${MISSING_PACKAGES} ${PACKAGE}"
;;
esac
fi
shift 2
done
if [ -n "$MISSING_PATHS" ]; then
printf 'Missing required command path(s):%s\n' "$MISSING_PATHS" >&2
printf 'Install the related Debian packages, for example:\n' >&2
printf ' sudo apt install%s\n' "$MISSING_PACKAGES" >&2
printf 'If your system uses different paths, update the command path variables near the top of this script.\n' >&2
exit 1
fi
}
require_guard_paths()
{
require_paths \
"$UFW" ufw \
"$IP" iproute2 \
"$PING" iputils-ping \
"$GETENT" libc-bin \
"$GREP" grep \
"$SYSTEMCTL" systemd \
"$SLEEP" coreutils
}
require_monitor_paths()
{
require_guard_paths
require_paths \
"$REBOOT" systemd \
"$MKDIR" coreutils \
"$HOSTNAME" hostname \
"$DATE" coreutils
}
require_install_paths()
{
require_monitor_paths
require_paths "$CP" coreutils "$CHMOD" coreutils "$CAT" coreutils
}
require_root()
{
if [ "$("$ID" -u)" != "0" ]; then
fail "This command must be run as root"
fi
}
ensure_log_dir()
{
LOGDIR=${LOGFILE%/*}
if [ "$LOGDIR" != "$LOGFILE" ]; then
"$MKDIR" -p "$LOGDIR" || fail "Could not create log directory: $LOGDIR"
fi
}
tunnel_is_up()
{
"$PING" -c 1 -I "$NET_TUN" "$CHECK_HOST" >/dev/null 2>&1
}
dns_is_ready()
{
"$GETENT" hosts "$CHECK_HOST" >/dev/null 2>&1
}
tunnel_interface_exists()
{
"$IP" link show dev "$NET_TUN" >/dev/null 2>&1
}
openvpn_is_active()
{
"$SYSTEMCTL" is-active --quiet "$OPENVPN_SERVICE"
}
verify_ufw_ipv6()
{
if [ "$REQUIRE_UFW_IPV6" = "yes" ]; then
if [ ! -r "$UFW_DEFAULT" ] || ! "$GREP" -Eq '^IPV6=yes$' "$UFW_DEFAULT"; then
fail "UFW IPv6 handling is not enabled in $UFW_DEFAULT; set IPV6=yes or disable IPv6 before using killswitch"
fi
fi
}
health_check()
{
openvpn_is_active || fail "OpenVPN service is not active: $OPENVPN_SERVICE"
tunnel_interface_exists || fail "Tunnel interface is not present: $NET_TUN"
dns_is_ready || fail "DNS lookup failed: $CHECK_HOST"
tunnel_is_up || fail "Tunnel ping failed: $CHECK_HOST via $NET_TUN"
}
guard_vpn()
{
ELAPSED=0
while [ "$ELAPSED" -lt "$READINESS_TIMEOUT" ]; do
if openvpn_is_active && tunnel_interface_exists && dns_is_ready && tunnel_is_up; then
return 0
fi
"$SLEEP" "$WAIT_INTERVAL"
ELAPSED=$((ELAPSED + WAIT_INTERVAL))
done
return 1
}
wait_for_vpn_or_fail()
{
guard_vpn || fail "VPN readiness failed after ${READINESS_TIMEOUT}s: $OPENVPN_SERVICE, $NET_TUN, DNS, or tunnel ping is not ready"
}
try_openvpn_recovery()
{
ATTEMPT=1
while [ "$ATTEMPT" -le "$OPENVPN_RESTART_ATTEMPTS" ]; do
printf '*** [Restarting OpenVPN attempt %s/%s: %s @ %s] ***\n' "$ATTEMPT" "$OPENVPN_RESTART_ATTEMPTS" "$("$HOSTNAME")" "$("$DATE")" >> "$LOGFILE"
"$SYSTEMCTL" restart "$OPENVPN_SERVICE" || return 1
if guard_vpn; then
return 0
fi
ATTEMPT=$((ATTEMPT + 1))
done
return 1
}
apply_ufw_defaults()
{
"$UFW" default deny outgoing
"$UFW" default deny incoming
"$UFW" --force enable
"$UFW" reload
}
allow_vpn_endpoint()
{
if [ -n "$VPN_ENDPOINT" ]; then
"$UFW" allow out on "$NET_DEV" to "$VPN_ENDPOINT" port "$PORT"
"$UFW" allow in on "$NET_DEV" from "$VPN_ENDPOINT" port "$PORT"
else
"$UFW" allow out on "$NET_DEV" to any port "$PORT"
"$UFW" allow in on "$NET_DEV" from any port "$PORT"
fi
}
configure_firewall()
{
apply_ufw_defaults
"$UFW" allow out on "$NET_TUN"
"$UFW" allow in on "$NET_TUN"
allow_vpn_endpoint
"$UFW" allow out on "$NET_TUN" to any port 53
"$UFW" allow in on "$NET_TUN" to any port 53
"$UFW" allow out on "$NET_DEV" from any to "$LOCAL_NET"
"$UFW" allow in on "$NET_DEV" from "$LOCAL_NET" to any
"$UFW" reload
}
stop_protected_service()
{
if [ -n "$SERVICE" ]; then
"$SYSTEMCTL" stop "$SERVICE" || fail "Could not stop protected service: $SERVICE"
fi
}
start_protected_service()
{
if [ -n "$SERVICE" ]; then
"$SYSTEMCTL" start "$SERVICE" || fail "Could not start protected service: $SERVICE"
fi
}
verify_protected_service_stopped()
{
if [ -n "$SERVICE" ] && "$SYSTEMCTL" is-active --quiet "$SERVICE"; then
fail "Protected service is still active: $SERVICE"
fi
}
prepare_protected_start()
{
stop_protected_service
verify_protected_service_stopped
verify_ufw_ipv6
}
start_protected_after_vpn()
{
wait_for_vpn_or_fail
start_protected_service
}
recover_vpn_or_reboot()
{
stop_protected_service
verify_protected_service_stopped
if try_openvpn_recovery; then
health_check
start_protected_service
printf '*** [OpenVPN recovery succeeded: %s @ %s] ***\n' "$("$HOSTNAME")" "$("$DATE")" >> "$LOGFILE"
printf '%s\n' "-----------------------------------------------------------------" >> "$LOGFILE"
return 0
fi
apply_ufw_defaults
printf '*** [VPN health failed, rebooting for OpenVPN recovery: %s @ %s] ***\n' "$("$HOSTNAME")" "$("$DATE")" >> "$LOGFILE"
printf '%s\n' "-----------------------------------------------------------------" >> "$LOGFILE"
"$REBOOT"
}
monitor_vpn()
{
while true; do
if ! tunnel_is_up; then
recover_vpn_or_reboot
fi
"$SLEEP" "$CHECK_INTERVAL"
done
}
write_service_file()
{
"$CAT" > "$SERVICE_FILE" <<EOF
[Unit]
Description=OpenVPN UFW killswitch monitor
After=network-online.target
Wants=network-online.target
After=$OPENVPN_SERVICE
[Service]
Type=simple
ExecStart=$INSTALL_PATH check
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
}
install_service()
{
"$CP" "$0" "$INSTALL_PATH" || fail "Could not install script to $INSTALL_PATH"
"$CHMOD" 755 "$INSTALL_PATH" || fail "Could not make $INSTALL_PATH executable"
write_service_file
if [ -n "$SERVICE" ]; then
"$SYSTEMCTL" disable "$SERVICE" >/dev/null 2>&1 || fail "Could not disable protected service autostart: $SERVICE"
fi
"$SYSTEMCTL" daemon-reload || fail "Could not reload systemd"
printf '%s\n' "Installed $INSTALL_PATH"
printf '%s\n' "Installed $SERVICE_FILE"
if [ -n "$SERVICE" ]; then
printf '%s\n' "Disabled independent autostart for $SERVICE"
fi
printf '%s\n' "Reloaded systemd"
printf '%s\n' "Run 'systemctl enable --now killswitch.service' when you are ready to start monitoring"
}
INPUT=$1
if [ -z "$INPUT" ]; then
info
exit 1
fi
case "$INPUT" in
up)
require_setup
require_guard_paths
prepare_protected_start
wait_for_vpn_or_fail
configure_firewall
health_check
start_protected_service
;;
check)
require_setup
require_monitor_paths
ensure_log_dir
prepare_protected_start
start_protected_after_vpn
monitor_vpn
;;
down)
require_setup
require_monitor_paths
stop_protected_service
verify_protected_service_stopped
apply_ufw_defaults
;;
health)
require_setup
require_guard_paths
health_check
;;
guard)
require_setup
require_guard_paths
wait_for_vpn_or_fail
;;
install)
require_setup
require_paths "$ID" coreutils
require_root
require_install_paths
install_service
;;
*)
info
;;
esac
exit 0;