-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvpn-killswitch
More file actions
executable file
·165 lines (146 loc) · 3.84 KB
/
vpn-killswitch
File metadata and controls
executable file
·165 lines (146 loc) · 3.84 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
#!/bin/bash
# SPDX-FileCopyrightText: 2020 - 2024 sudorook <daemon@nullcodon.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -euo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command drill ufw nmcli && exit 3
#
# Functions
#
function get_device() {
nmcli device status |
grep -v bridge |
sed -n "s/\(\S\+\)\s\+\S\+\s\+connected\s\+\(\S\+\)\s*$/\1/p"
}
function get_vpn_data() {
local device
local vpnid
local cmd
device="${1}"
vpnid="$(nmcli connection show |
sed -n "s/^\(.*\)\+\s\+\([0-9a-z\-]\+\)\s\+vpn\s\+${device}/\2/p")"
if [ -z "${vpnid}" ]; then
return
else
cmd="nmcli conn show ${vpnid}"
eval "${cmd}"
fi
}
function get_vpn_dns() {
local vpndata
vpndata="${1}"
echo "${vpndata}" | sed -n "s/^IP4.GATEWAY:\s\+\([0-9\.]\+\)/\1/p"
}
function get_vpn_ip() {
local vpnip=
local iplist=
local vpndata
vpndata="${1}"
for addr in $(echo "${vpndata}" | sed -n "s/^VPN.GATEWAY:\s\+\(.*\)/\1/p" | sed -e "s/,//g"); do
vpnip=$(drill "${addr%:*}" | grep "^${addr%:*}" | cut -d" " -f5)
if [[ -z "${iplist}" ]]; then
iplist="${vpnip}/32"
elif ! [[ "${iplist}" =~ ${vpnip} ]]; then
iplist="${iplist} ${vpnip}/32"
fi
done
echo "${iplist}"
}
function get_vpn_port() {
local vpnport=
local portlist=
local vpndata
vpndata="${1}"
for addr in $(echo "${vpndata}" | sed -n "s/^VPN.GATEWAY:\s\+\(.*\)/\1/p" | sed -e "s/,//g"); do
vpnport="${addr##*:}"
if [[ -z "${portlist}" ]]; then
portlist="${vpnport}"
elif ! [[ "${portlist}" =~ ${vpnport} ]]; then
portlist="${portlist},${vpnport}"
fi
done
echo "${portlist}"
}
function enable_killswitch() {
local device
local vpndata
local dns
local iplist
local portlist
device="$(get_device)"
vpndata="$(get_vpn_data "${device}")"
if [ -z "${vpndata}" ]; then
show_error "VPN info not found. Exiting."
exit 2
fi
dns="$(get_vpn_dns "${vpndata}")"
iplist="$(get_vpn_ip "${vpndata}")"
portlist="$(get_vpn_port "${vpndata}")"
sudo ufw --force reset
sudo ufw default deny incoming
sudo ufw default deny outgoing
sudo ufw allow out on tun0 to 0.0.0.0/0
for ip in ${iplist}; do
sudo ufw allow in on "${device}" from "${ip}" port "${portlist}"
sudo ufw allow out on "${device}" to "${ip}" port "${portlist}"
done
sudo ufw allow out on "${device}" to "${dns}" port 53
sudo ufw enable
}
function print_usage() {
show_header "Usage: vpn-killswitch"
show_listitem " -e|--enable enable firewall 'killswitch'"
show_listitem " -d|--disable disable 'killswitch' and reset firewall rules"
}
#
# Main
#
function disable_killswitch() {
sudo ufw --force reset
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw enable
}
OPTIONS=deh
LONGOPTIONS=disable,enable,help
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ ${#} -ge 1 ]; do
case "${1}" in
-d | --disable)
disable_killswitch
shift
;;
-e | --enable)
enable_killswitch
shift
;;
-h | --help)
print_usage
exit
;;
--)
shift
break
;;
*)
show_error "What was that?"
exit 3
;;
esac
done