forked from nstinus/nordvpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnordvpn
executable file
·221 lines (192 loc) · 6.27 KB
/
nordvpn
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
#!/bin/bash
main() {
while [ $# -ne 0 ]
do
case "$1" in
-v|--verbose)
set -x
shift
;;
-h|--help)
print_usage
break
;;
status)
service=$(get_service)
test -z "$service" || systemctl status $service
break
;;
update)
check_root
update_files
break
;;
disconnect)
check_root
stop_service > /dev/null
reenable_ipv6
break
;;
connect)
shift
check_root
service=$(stop_service)
server=$(find_server "$@")
if [[ $? != 0
|| ! -f /etc/openvpn/client/nordvpn_${server}_udp.conf ]]; then
if [[ "$service" == "" ]]; then
exit_msg 1 "'No valid nordvpn server found. Exiting."
else
echo "No valid nordvpn server found."\
"Resuming previous connection."
systemctl start "$service"
break
fi
fi
disable_ipv6
systemctl start openvpn-client@nordvpn_${server}_udp
break
;;
*)
print_usage
exit 1
;;
esac
done
}
disable_ipv6() {
## Disable ipv6 on every interface during the connection
#sysctl -w net.ipv6.conf.all.disable_ipv6=1 > /dev/null 2>&1
#sysctl -w net.ipv6.conf.default.disable_ipv6=1 > /dev/null 2>&1
## Interfaces can be found by running `ip link`
#sysctl -w net.ipv6.conf.lo.disable_ipv6=1 > /dev/null 2>&1
#sysctl -w net.ipv6.conf.tun0.disable_ipv6=1 > /dev/null 2>&1
#sysctl -w net.ipv6.conf.YOUR_INTERFACE.disable_ipv6=1 > /dev/null 2>&1
return 0
}
reenable_ipv6() {
#sysctl -w net.ipv6.conf.all.disable_ipv6=0 > /dev/null 2>&1
#sysctl -w net.ipv6.conf.default.disable_ipv6=0 > /dev/null 2>&1
#sysctl -w net.ipv6.conf.lo.disable_ipv6=0 > /dev/null 2>&1
#sysctl -w net.ipv6.conf.tun0.disable_ipv6=0 > /dev/null 2>&1
#sysctl -w net.ipv6.conf.YOUR_INTERFACE.disable_ipv6=0 > /dev/null 2>&1
return 0
}
stop_service() {
service=$(get_service)
if [[ ! -z "$service" ]]; then
systemctl stop $service
systemctl restart iptables.service
fi
echo "$service"
}
exit_msg() {
(>&2 echo "$2")
exit $1
}
get_service() {
systemctl --type=service --plain | grep openvpn-client@nordvpn | awk '{print $1}'
}
check_root() {
if [ $(id -u) -ne 0 ]
then
exit_msg 1 "This command requires super-user privileges."
fi
}
update_files() {
local remote_url=https://nordvpn.com/api/files/zipv2
local targetdir=/etc/openvpn/client/nordvpn
mkdir -p $targetdir
local tmpdir=$(mktemp -d)
cd $tmpdir
updown=/etc/openvpn/client/nordvpn/updown
if [ -x /etc/openvpn/vpnfailsafe.sh ]; then
ln -fs /etc/openvpn/vpnfailsafe.sh $updown
else
msg_exit 2 "Install vpnfailsafe.sh to proceed."
fi
echo "Downloading nordvpn config files..."
curl -sSL -o nordvpn.zip $remote_url || exit 1
mkdir conf
if ! unzip -q nordvpn.zip -d conf
then
msg_exit 2 "unzip failed"
fi
echo "Patching and installing files..."
test -f $targetdir/credentials.conf && cp $targetdir/credentials.conf .
find /etc/openvpn/client -name 'nordvpn_*.conf' | xargs -r rm
rm -rf $targetdir/conf && mkdir -p $targetdir/conf
export -f patch_config_file
find conf -name '*.ovpn' | xargs -P $(($(nproc --all)-1)) -n1 -I{} bash -c "patch_config_file {} $targetdir"
local cred_file=$targetdir/credentials.conf
if [ -f credentials.conf ]; then
cp credentials.conf $cred_file
else
echo "login" > $cred_file
echo "password" >> $cred_file
chmod 400 $cred_file
echo "Please update $cred_file"
fi
rm -rf $tmpdir
}
find_server() {
var="$@"
if [[ "$var" =~ [0-9] ]]; then
echo "$var"
return 0
fi
url_countries="https://api.nordvpn.com/v1/servers/countries"
url_recommendations="https://api.nordvpn.com/v1/servers/recommendations"
if [[ -z $var ]]; then
res=$(curl --silent "$url_recommendations?limit=1" \
| jq -Mr '.[].hostname')
echo ${res%%\.*}
return 0
fi
country=$(curl --silent $url_countries | jq -Mr --arg var "$var" \
'.[] | select(.code | test($var;"i")) | .id')
if [[ -z $country ]]; then
country=$(curl --silent $url_countries | jq -Mr --arg var "$var" \
'.[] | select(.name | test($var;"i")) | .id')
fi
if [[ -z $country ]]; then
return 1
fi
res=$(curl --silent "$url_recommendations?filters\[country_id\]=$country&limit=1" \
| jq -Mr '.[].hostname')
echo ${res%%\.*}
}
patch_config_file() {
local -r f=$1
local -r targetdir=$2
sed 's/^auth-user-pass.*$/auth-user-pass \/etc\/openvpn\/client\/nordvpn\/credentials.conf/g' -i $f
echo "" >> $f
echo "script-security 2" >> $f
echo "up /etc/openvpn/client/nordvpn/updown" >> $f
echo "down /etc/openvpn/client/nordvpn/updown" >> $f
echo "route-noexec" >> $f
install -D -m 444 $f $targetdir/conf/$(basename $f)
local -a tok=($(basename $f | tr '.' ' '))
ln -s $targetdir/conf/$(basename $f) $targetdir/../nordvpn_${tok[0]}_${tok[3]:0:3}.conf
}
print_usage() {
cat <<EOF
usage: $(basename $0) [options] command [args]
Available options:
-h, --help
Print this help and exit.
Available commands:
status
Show current systemd service status, if any.
connect [arg]
If no 'arg' is supplied connects to a recommended server. If 'arg' is
a server code connects to it. Otherwise 'arg' is assumed to be a
country code ("us", "de", etc.) or name. The command then connects to
a recommended server in this country.
disconnect
Disconnect from VPN service if it is running.
update
Download and install nordvpn config files.
EOF
}
main "$@"