Skip to content

Instantly share code, notes, and snippets.

@rnbguy
Created February 18, 2020 07:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnbguy/69d0db2dd13ed612792b169a11ebda39 to your computer and use it in GitHub Desktop.
Save rnbguy/69d0db2dd13ed612792b169a11ebda39 to your computer and use it in GitHub Desktop.
Toggle NordVPN using OpenVPN
# make sure you have - openvpn, jq, curl, sed
CURL_CMD='curl -s -H "accept-encoding: gzip, deflate" --compressed'
load() {
security="udp"
cd ~/.nordvpn
rm -f *.ovpn
link="https://downloads.nordcdn.com/configs/files/ovpn_${security}/servers/$1.${security}.ovpn"
filename=nordvpn.conf
$CURL_CMD $link > $filename
sudo cp $filename /etc/openvpn/client/$filename
sudo sed -i 's#auth-user-pass#auth-user-pass /etc/nordvpn.conf#g' /etc/openvpn/client/nordvpn.conf
}
ask() {
echo -n "Username: "
read username
echo -n "Password: "
read -s password
cred_file="/etc/nordvpn.conf"
sudo echo "$username\n$password" > ${cred_file}
sudo chown root:root ${cred_file}
sudo chmod 600 ${cred_file}
}
status() {
$CURL_CMD 'https://nordvpn.com/wp-admin/admin-ajax.php?action=get_user_info_data' | jq -r .status
}
on() {
if systemctl is-active --quiet openvpn-client@nordvpn; then
off
fi
echo on
if [ -z $1 ]; then
domain=`$CURL_CMD "https://nordvpn.com/wp-admin/admin-ajax.php?action=servers_recommendations" | jq -r .[0].hostname`
else
country=`grep -Irw --color=never $1 countries.txt | cut -d' ' -f1`
domain=`$CURL_CMD "https://nordvpn.com/wp-admin/admin-ajax.php?action=servers_recommendations&filters=\{%22country_id%22:$country\}" | jq -r .[0].hostname`
fi
echo $domain
load $domain
sudo systemctl start openvpn-client@nordvpn
}
off() {
echo off
sudo systemctl stop openvpn-client@nordvpn
}
toggle() {
if systemctl is-active --quiet openvpn-client@nordvpn; then
off
else
on
fi
}
countries() {
$CURL_CMD "https://nordvpn.com/wp-admin/admin-ajax.php?action=servers_countries" | jq -r '.[] | "\(.id) \(.code) \(.name)"' > countries.txt
}
case $1 in
"")
echo [default] toggling NordVPN.
toggle
;;
on)
echo turning on NordVPN.
on $2
;;
off)
echo turning off NordVPN.
off
;;
toggle)
echo toggling NordVPN.
toggle
;;
status)
echo checking NordVPN status.
status
;;
countries)
echo refreshing NordVPN counties.
countries
;;
*)
echo Unknown flag
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment