NordVPN Random

How long does it take to “find the best server”? I just want to connect to a random server in the US…

Goal

Run openvpn_random to connect to a randomized NordVPN server.

$ openvpn_random
[sudo] password for michael:
using config: us9836.nordvpn.com.udp1194.ovpn
...
2022-12-17 17:25:05 [us9836.nordvpn.com] Peer Connection Initiated ...

Background

I’m not sure how it got designed, but the most annoying part of the nordvpn client is the waiting time for it to connect to a server. It’s inconsistent too, it’ll give you cryptic messages about not being able to connect sometimes and other times just fails silently.

Fortunately, NordVPN also provides OpenVPN servers that you can connect to (without searching) as long as you have an account. https://nordvpn.com/ovpn/

You can download a .ovpn from that page and use it to connect with OpenVPN

sudo openvpn --config us9300.nordvpn.com.udp1194.ovpn

By default, it will prompt you for a username and password. This can be annoying to type in each time so you’ll probably want to store it in a credentials file. You can then add an auth-user-pass line to the .ovpn file to tell it to use the credentials file instead of prompting for a password.

login.conf

james@example.com
very-secure-password

us9300.nordvpn.com.udp1194.ovpn

...
-----END OpenVPN Static key V1-----
</tls-auth>
auth-user-pass /etc/openvpn/client/login.conf

This works great if you’re just connecting to a single server. However, NordVPN provides many more than one server to connect to. I wanted to be able to connect to any server in the US at random so I wrote a script to make it possible.

Getting the .ovpn files

The ovpn page is pretty annoying to download from in bulk so I used some HTML/JS querySelector magic to get a list of server domain names that are in the US (as of October 26, 2022).

us-nordvpn-openvpn-domains.txt

With this file as a source, I ran the following script to download all of the .ovpn files for the servers in that list, add my login credentials line, and store them in a dedicated directory.

#!/bin/bash

# see https://nordvpn.com/ovpn
servers=$(cat ~/scripts/us-nordvpn-openvpn-domains.txt)
for server in $servers; do
    filename="$server.udp1194.ovpn"
    dl_url="https://downloads.nordcdn.com/configs/files/ovpn_legacy/servers/$filename"

    echo downloading $dl_url

    curl --output-dir ~/scripts -O $dl_url
    echo "auth-user-pass /etc/openvpn/client/login.conf" >> ~/scripts/$filename
    sudo mv ~/scripts/$filename /etc/openvpn/client/
    echo added $filename
    sleep 0.3
done

Once it was done running (took 30 mins or so), I had all the .ovpn files in my OpenVPN client directory.

$ sudo ls /etc/openvpn/client
login.conf
us10000.nordvpn.com.udp1194.ovpn
us10002.nordvpn.com.udp1194.ovpn
us10003.nordvpn.com.udp1194.ovpn
us10004.nordvpn.com.udp1194.ovpn
us10005.nordvpn.com.udp1194.ovpn
...

Conecting to a Random Server

Now, when I want to connect to a random server, I can use the alias that I added to my .zshrc:

openvpn_random () {
        configs=$(sudo ls /etc/openvpn/client)
        config=$(echo $configs | shuf -n 1)
        echo using config: $config
        sudo openvpn --config /etc/openvpn/client/$config
}

This command selects a random configuration file from /etc/openvpn/client to use to connect.

It can be used as follows to easily connect to a randomized NordVPN server.

$ openvpn_random
[sudo] password for michael:
using config: us9836.nordvpn.com.udp1194.ovpn
...
2022-12-17 17:25:05 [us9836.nordvpn.com] Peer Connection Initiated ...