NordVPN On Demand

After resolving an issue with the default.ovpn symlink, it was bugging me that each time the container ran it would download all of the NordVPN configs. After a bit of research I found a url where I could download just one ovpn file. A bit of rework and now only downloads the applicable config based off of NordVPNs api. No more 4000+ configs.

User can configure this using either NORDVPN_COUNTRY to connect to the best server in that country or a specific config using the OPENVPN_CONFIG parameter. If neither are specified config will be selected based off of NordVPN api and if both are specified it will connect to OPENVPN_CONFIG server first using the api to decide on a backup 'default.ovpn' config in case it fails.

In my rewriting of this I also realised there was no need for the updateConfigs.sh script anymore, but to keep everything in keeping I replaced the contents of the updateConfigs.sh script with the NordVPN_selector one and removed the latter.
This commit is contained in:
Tom Humphrey
2019-03-26 17:02:11 +00:00
parent aaab2f0155
commit ca99395250
9808 changed files with 139 additions and 832076 deletions

View File

@@ -1,9 +1,10 @@
#!/bin/bash
set -e
TIME_FORMAT=`date "+%Y-%m-%d %H:%M:%S"`
log() {
printf "${TIME_FORMAT} %b\n" "$*";
printf "${TIME_FORMAT} %b\n" "$*" > /dev/stderr;
}
fatal_error() {
@@ -19,47 +20,134 @@ script_needs() {
script_init() {
log "Checking curl installation"
script_needs curl
# log "Checking dos2unix installation"
# script_needs dos2unix
log "Checking unzip installation"
script_needs unzip
}
script_init
country_filter() { # curl -s "https://api.nordvpn.com/v1/servers/countries" | jq --raw-output '.[] | [.code, .name] | @tsv'
local nordvpn_api=$1 country=(${NORDVPN_COUNTRY//[;,]/ })
if [[ ${#country[@]} -ge 1 ]]; then
country=${country[0]//_/ }
local country_id=`curl -s "${nordvpn_api}/v1/servers/countries" | jq --raw-output ".[] |
select( (.name|test(\"^${country}$\";\"i\")) or
(.code|test(\"^${country}$\";\"i\")) ) |
.id" | head -n 1`
if [[ -n ${country_id} ]]; then
log "Searching for country : ${country} (${country_id})"
echo "filters\[country_id\]=${country_id}&"
fi
fi
}
group_filter() { # curl -s "https://api.nordvpn.com/v1/servers/groups" | jq --raw-output '.[] | [.identifier, .title] | @tsv'
local nordvpn_api=$1 category=(${NORDVPN_CATEGORY//[;,]/ })
if [[ ${#category[@]} -ge 1 ]]; then
category=${category[0]//_/ }
local identifier=`curl -s "${nordvpn_api}/v1/servers/groups" | jq --raw-output ".[] |
select( .title | test(\"${category}\";\"i\") ) |
.identifier" | head -n 1`
if [[ -n ${identifier} ]]; then
log "Searching for group: ${identifier}"
echo "filters\[servers_groups\]\[identifier\]=${identifier}&"
fi
fi
}
technology_filter() { # curl -s "https://api.nordvpn.com/v1/technologies" | jq --raw-output '.[] | [.identifier, .name ] | @tsv' | grep openvpn
local identifier
if [[ ${NORDVPN_PROTOCOL,,} =~ .*udp.* ]]; then
identifier="openvpn_udp"
elif [[ ${NORDVPN_PROTOCOL,,} =~ .*tcp.* ]];then
identifier="openvpn_tcp"
fi
if [[ -n ${identifier} ]]; then
log "Searching for technology: ${identifier}"
echo "filters\[servers_technologies\]\[identifier\]=${identifier}&"
fi
}
select_hostname() { #TODO return multiples
local nordvpn_api="https://api.nordvpn.com" \
filters hostname
log "Selecting the best server..."
if [[ "$1" != "-d" ]]; then
filters+="$(country_filter ${nordvpn_api})"
fi
filters+="$(group_filter ${nordvpn_api})"
filters+="$(technology_filter )"
hostname=`curl -s "${nordvpn_api}/v1/servers/recommendations?${filters}limit=1" | jq --raw-output ".[].hostname"`
if [[ -z ${hostname} ]]; then
log "Unable to find a server with the specified parameters, using any recommended server"
hostname=`curl -s "${nordvpn_api}/v1/servers/recommendations?limit=1" | jq --raw-output ".[].hostname"`
fi
log "Best server : ${hostname}"
echo ${hostname}
}
download_hostname() {
# "https://downloads.nordcdn.com/configs/files/ovpn_udp/servers/us3373.nordvpn.com.udp.ovpn"
local nordvpn_cdn="https://downloads.nordcdn.com/configs/files/"
if [[ ${NORDVPN_PROTOCOL,,} =~ udp ]]; then
nordvpn_cdn="${nordvpn_cdn}ovpn_udp/servers/"
elif [[ ${NORDVPN_PROTOCOL,,} =~ tcp ]];then
nordvpn_cdn="${nordvpn_cdn}ovpn_tcp/servers/"
fi
if [[ "$1" == "-d" ]]; then
nordvpn_cdn=${nordvpn_cdn}${2}.ovpn
ovpnName=default.ovpn
else
nordvpn_cdn=${nordvpn_cdn}${1}.ovpn
ovpnName=${1}.ovpn
fi
log "Downloading config: ${ovpnName}"
curl ${nordvpn_cdn} -o "${ovpnName}"
}
update_hostname() {
log "Checking line endings"
sed -i 's/^M$//' *.ovpn
# Update configs with correct options
log "Updating configs for docker-transmission-openvpn"
sed -i 's=auth-user-pass=auth-user-pass /config/openvpn-credentials.txt=g' *.ovpn
sed -i 's/ping 15/inactive 3600\
ping 10/g' *.ovpn
sed -i 's/ping-restart 0/ping-exit 60/g' *.ovpn
sed -i 's/ping-timer-rem//g' *.ovpn
}
# If the script is called from elsewhere
cd "${VPN_PROVIDER_CONFIGS}"
cd "${0%/*}"
script_init
# Delete everything (not this script though)
log "Removing existing configs"
find . ! -name '*.sh' -type f -delete
# Get updated configuration zip
log "Downloading latest configs"
curl -skL https://downloads.nordcdn.com/configs/archives/servers/ovpn.zip -o openvpn.zip \
&& unzip -j openvpn.zip $1 >/dev/null 2>&1 && rm openvpn.zip
if [[ ! -z $OPENVPN_CONFIG ]] && [[ ! -z $NORDVPN_COUNTRY ]]
then
default="$(select_hostname).${NORDVPN_PROTOCOL,,}"
else
default="$(select_hostname -d).${NORDVPN_PROTOCOL,,}"
fi
download_hostname -d ${default}
# Ensure linux line endings
log "Checking line endings"
# dos2unix * $1 >/dev/null 2>&1
# find . -name '*.ovpn' -type f -print 0 | xargs -0 sed -i 's/^M$//'
if [[ ${1} == "--get-recommended" ]] || [[ ${1} == "-r" ]]
then
selected="default"
elif [[ ${1} == "--openvpn-config" ]] || [[ ${1} == "-o" ]]
then
download_hostname ${OPENVPN_CONFIG,,}
elif [[ ! -z $NORDVPN_COUNTRY ]]
then
selected="$(select_hostname).${NORDVPN_PROTOCOL,,}"
download_hostname ${selected}
else
selected="default"
fi
find ${VPN_PROVIDER_CONFIGS} -name '*nordvpn*.ovpn' -type f -exec sed -i 's/^M$//' {} \;
update_hostname
# Update configs with correct options
log "Updating configs for docker-transmission-openvpn"
sed -i 's=auth-user-pass=auth-user-pass /config/openvpn-credentials.txt=g' *nordvpn*.ovpn
sed -i 's/ping 15/inactive 3600\
ping 10/g' *nordvpn*.ovpn
sed -i 's/ping-restart 0/ping-exit 60/g' *nordvpn*.ovpn
sed -i 's/ping-timer-rem//g' *nordvpn*.ovpn
if [[ ! -z $selected ]]
then
echo ${selected}
fi
# Pick a random file config for default.ovpn
random_config=$(ls uk*udp* | sort -R | head -n1)
log "Setting default.ovpn to $random_config"
ln -sf $random_config default.ovpn
cd "${0%/*}"
cd "${0%/*}"