diff --git a/Dockerfile b/Dockerfile index 75523241f..80943a655 100644 --- a/Dockerfile +++ b/Dockerfile @@ -48,6 +48,9 @@ ADD scripts /etc/scripts/ ENV OPENVPN_USERNAME=**None** \ OPENVPN_PASSWORD=**None** \ OPENVPN_PROVIDER=**None** \ + NORDVPN_COUNTRY= \ + NORDVPN_PROTOCOL=TCP \ + NORDVPN_CATEGORY=P2P \ GLOBAL_APPLY_PERMISSIONS=true \ TRANSMISSION_ALT_SPEED_DOWN=50 \ TRANSMISSION_ALT_SPEED_ENABLED=false \ diff --git a/openvpn/nordvpn/NordVPN_Server_Selector.sh b/openvpn/nordvpn/NordVPN_Server_Selector.sh new file mode 100755 index 000000000..41488722b --- /dev/null +++ b/openvpn/nordvpn/NordVPN_Server_Selector.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +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 + echo "Searching for country : ${country} (${country_id})" > /dev/stderr + 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 + echo "Searching for group: ${identifier}" > /dev/stderr + 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 + echo "Searching for technology: ${identifier}" > /dev/stderr + echo "filters\[servers_technologies\]\[identifier\]=${identifier}&" + fi +} +select_hostname() { #TODO return multiples + local nordvpn_api="https://api.nordvpn.com" \ + filters hostname + + echo "Selecting the best server..." > /dev/stderr + filters+="$(country_filter ${nordvpn_api})" + 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 + echo "Unable to find a server with the specified parameters, using any recommended server" > /dev/stderr + hostname=`curl -s "${nordvpn_api}/v1/servers/recommendations?limit=1" | jq --raw-output ".[].hostname"` + fi + + echo "Best server : ${hostname}" > /dev/stderr + echo ${hostname} +} + +# Select recommended VPN +echo "$(select_hostname).${NORDVPN_PROTOCOL,,}" \ No newline at end of file diff --git a/openvpn/nordvpn/updateConfigs.sh b/openvpn/nordvpn/updateConfigs.sh index 2f9a769fd..c3f2b8215 100755 --- a/openvpn/nordvpn/updateConfigs.sh +++ b/openvpn/nordvpn/updateConfigs.sh @@ -1,5 +1,4 @@ #!/bin/bash - set -e TIME_FORMAT=`date "+%Y-%m-%d %H:%M:%S"` @@ -21,8 +20,8 @@ script_init() { log "Checking curl installation" script_needs curl - log "Checking dos2unix installation" - script_needs dos2unix + # log "Checking dos2unix installation" + # script_needs dos2unix log "Checking unzip installation" script_needs unzip @@ -31,20 +30,23 @@ script_init() { script_init # If the script is called from elsewhere -cd "${0%/*}" +cd "${VPN_PROVIDER_CONFIGS}" # Delete everything (not this script though) log "Removing existing configs" -find . ! -name '*.sh' -delete +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 + # Ensure linux line endings log "Checking line endings" -dos2unix * $1 >/dev/null 2>&1 +# dos2unix * $1 >/dev/null 2>&1 +# find . -name '*.ovpn' -type f -print 0 | xargs -0 sed -i 's/^M$//' +find ${VPN_PROVIDER_CONFIGS} -name '*.ovpn' -type f -exec sed -i 's/^M$//' {} \; # Update configs with correct options log "Updating configs for docker-transmission-openvpn" @@ -57,4 +59,6 @@ sed -i 's/ping-timer-rem//g' *.ovpn # 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 -s $random_config default.ovpn \ No newline at end of file +ln -s $random_config default.ovpn + +cd "${0%/*}" \ No newline at end of file diff --git a/openvpn/start.sh b/openvpn/start.sh index ef31d3c9e..cda523033 100755 --- a/openvpn/start.sh +++ b/openvpn/start.sh @@ -1,6 +1,7 @@ #!/bin/bash VPN_PROVIDER="${OPENVPN_PROVIDER,,}" VPN_PROVIDER_CONFIGS="/etc/openvpn/${VPN_PROVIDER}" +export VPN_PROVIDER_CONFIGS # If create_tun_device is set, create /dev/net/tun if [[ "${CREATE_TUN_DEVICE,,}" == "true" ]]; then @@ -32,7 +33,9 @@ if [[ "$OPENVPN_PROVIDER" = "NORDVPN" ]] then if [[ -z "$OPENVPN_CONFIG" ]] then - export OPENVPN_CONFIG=$(curl -s 'https://nordvpn.com/wp-admin/admin-ajax.php?action=servers_recommendations' | jq -r '.[0].hostname').udp + # Update config files + . ${VPN_PROVIDER_CONFIGS}/updateConfigs.sh + export OPENVPN_CONFIG=$(${VPN_PROVIDER_CONFIGS}/NordVPN_Server_Selector.sh) echo "Setting best server ${OPENVPN_CONFIG}" fi fi