Select Page
VPN Gateway With Dead Man Switch

VPN Gateway With Dead Man Switch

VPN Gateway Server with Dead Man Switch

Overview

Keep machines on your network safe with all internet traffic leaving using end to end encryption via AirVPN. A dead man switch ensures internet traffic can never be broadcast over your clear internet.

  • A VPN gateway that any client on your network can use.
  • Stop prying eyes seeing your internet activity.
  • Protect yourself on public networks.
  • Protect your net neutrality.

This guide is aimed at Raspberry Pis, but will work for any Debian based OS. E.g. Ubuntu 18.04 server, so just use what suits you. I’ve chosen a Raspberry Pi as it makes for a great low power client.

Technical Jargon

VPN

VPN stands for virtual private network. VPN secures your computer’s internet connection by ensuring all of the data being sent and recieved is encrypted and secure from prying eyes.

Click here for a full detailed description of VPN.

DNS

Domain Name System (DNS) translates easily rememberable names such as google.com into addresses that a machine understands.

Click here for a full detailed description of DNS.

DNS Server

A DNS server is like a telephone directory, you ask for the address of a computer and it will tell you what the address is.

Click here for a full detailed description of DNS server.

DNS Leak

DNS leaking is when your requests are being sent to DNS servers that are not your designated ones (usually your VPN server). This means that while no one can read your encrypted traffic, they can see which addresses you are requesting. To ensure you stay as safe online as possible making sure your DNS does not leak is critical.

Click here for a full detailed description of DNS leak.

VPN Gateway

A computer that routes internet traffic from other computers via its VPN connection.

IPTables

IPTables is a utility program that allows admins to define rules on how to treat packets of data.

Click here for a full detailed description of IPTables.

Assumptions

This guide assumes you have a fresh install of Raspbian on a headless server

This guide assumes you have a VPN client installed and configured on your device.

This guide assumes you have set a static IP for your device.

This guide assumes your network connection is called eth0.

Install Software

We only need to install two extra pieces of software (iptables-persistent & dnsmasq) to get the VPN Gateway working.

Type the following into the command line:

sudo apt install iptables-persistent dnsmasq -y

Select “<yes>” for both IPv4 and IPv6 rules and allow it to complete the installation.

Enable Forwarding

The Raspberry Pi is going to be setup to forward incoming requests from other clients to its VPN connection. By default this is not enabled or setup so we’re going to configure it now.

First ssh into your Raspberry Pi with a sudo enabled user.

Now we need to enable forwarding in the sysctl.conf file by removing the # at the start of line 28 “#net.ipv4.ip_forward=1”:

sudo nano /etc/sysctl.conf

Delete “#” infront of “net.ipv4.ip_forward=1” then press Ctrl+x to exit and you’ll be prompted to Save modified. Type Y and then return to save the file.

Enable the forwarding service:

sudo sysctl -p

Update IPTables

Inorder to forward the incoming traffic correctly we need to make some changes to the IPTables on our Raspberry Pi. These updates will create a dead man switch so traffic from any client using our VPN Gateway can only exit it via its VPN connection. This means, if the VPN connection is lost, the clients will lose their internet.

Make sure to update the network interface name to match the one being used on the device. My network interface is called “eth0” for this example. If you were using a Raspberry Pi Zero W and using the WiFi connection, the interface name is wlan0. Use the command “ip -c a” to show all network interface information and confirm the name, as described in the guide set a static IP.

Paste the following into the command line to update the IPTables:

sudo iptables --flush
sudo iptables --delete-chain
sudo iptables -t nat -F
sudo iptables -t nat -A POSTROUTING -o tun+ -j MASQUERADE
sudo iptables -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -i lo -m comment --comment "loopback" -j ACCEPT
sudo iptables -A OUTPUT -o lo -m comment --comment "loopback" -j ACCEPT
sudo iptables -I INPUT -i eth0 -m comment --comment "In from LAN" -j ACCEPT
sudo iptables -I OUTPUT -o tun+ -m comment --comment "Out to VPN" -j ACCEPT
sudo iptables -A OUTPUT -o eth0 -p udp --dport 443 -m comment --comment "openvpn" -j ACCEPT
sudo iptables -A OUTPUT -o eth0 -p udp --dport 123 -m comment --comment "ntp" -j ACCEPT
sudo iptables -A OUTPUT -p UDP --dport 67:68 -m comment --comment "dhcp" -j ACCEPT
sudo iptables -A OUTPUT -o eth0 -p udp --dport 53 -m comment --comment "dns" -j ACCEPT
sudo iptables -A FORWARD -i tun+ -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o tun+ -m comment --comment "LAN out to VPN" -j ACCEPT
sudo iptables -P FORWARD DROP

The change we’ve made is only temporary. To make it permanent type:

sudo netfilter-persistent save

To ensure these rules are applied every time the device turns on type:

sudo systemctl enable netfilter-persistent

Now that we’ve enabled forwarding, we need to make an edit to the route-up.sh and down.sh files in /etc/openvpn to ensure client traffic is routed correctly.

Navigate to the OpenVPN directory:

cd /etc/openvpn

Open route-up.sh:

sudo nano route-up.sh

At the bottom of the file add:

/etc/openvpn/update-resolv-conf

Press Ctrl+x to exit and you’ll be prompted to Save modified. Type Y and then return to save the file.

Now update down.sh:

sudo nano down.sh

At the bottom of the file add:

/etc/openvpn/update-resolv-conf

Press Ctrl+x to exit and you’ll be prompted to Save modified. Type Y and then return to save the file.

Finally reboot your Raspberry Pi to ensure the changes have been loaded

Client Configuration

Now the VPN Gateway is setup we need to configure a client to use it and do some final checks that everything is working as expected!

It’s as easy as changing two options to point at your VPN Gateway:

  • Default gateway
  • DNS server

Depending what operating system your client is using there are a number of different ways of achieving this. I’ll try to briefly outline the most common ones. I would recommend setting a static IP address for the clients and the below examples will assume that.

Raspberry Pi

The easiest option is to follow the static IP guide and use the VPN Gateway IP address for the static routes and static domain_name_servers.

Linux – Debian Based

If you are running a Debian based Linux distro, open the command line (or ssh into the client) and we’re going to update the static IP options to make sure it’s using our VPN Gateway.

sudo nano /etc/network/interfaces

Find the line “iface eth0 inet static” and just below that look for:

  • gateway
  • dns-nameservers

Now update both of them to be the VPN Gateway IP address.

Once updated save and exit and to be sure the change has stuck reboot the client.

Windows 10

Go to the search tool on the task bar and type “Network Settings” to open the network settings panel.

Once open find and click “Change Adaptor Options” to show all of your network adaptors. Locate the one you use to connect to the internet, right click it and select properties.

In the properties window double click “Internet Protocol Version 4 (TCP/IPv4)” to set a static IP address.

Fill in the boxes with the appropriate configuration, My VPN Gateway has an IP address of 10.8.60.185.

Click OK to close the panels and Windows will take care of updating your settings.

Final Checks

Now we have updated our client to use the VPN Gateway for all of its internet traffic we need up run some checks and make sure everything is working as expected.

There are 3 checks we’ll be carrying out:

  • Can it see the outside world?
  • Does it have the correct external IP?
  • Is the DNS leaking?

We’ll run through two methods of checking these for if you have a command line only client, or one with a full desktop.

Command line

If like me your client is a headless server and you only have a command line we’ll go about making these checks as follows, in the command line type:

ping google.com -c 4

You should see returns from google.

To check if you have the correct external IP type:

wget -qO- ifconfig.me/ip

The IP address shown should be the same as the VPN Gateway shows when you run the same command there.

Finally to check if the DNS is leaking we’ll use the same script we did when setting up the VPN Gateway.

There is a commandline tool that will check if our DNS is leaking. For more information on the script we’re going to use see the authors GitHub page.

First make sure all dependencies are installed:

sudo apt install curl jq -y

We’re going to download it to the opt folder:

cd /opt

Download using:

sudo wget https://raw.githubusercontent.com/macvk/dnsleaktest/master/dnsleaktest.sh

Let’s make it executable:

sudo chmod +x dnsleaktest.sh

To run the script from /opt use:

./dnsleaktest.sh

Or outside this folder use:

/opt/dnsleaktest.sh

If everything is successful you should see something like the image below:

From a Web Browser

If you have a web browser on your client the checks are very quick and easy.

To see if you have access to the outside world let’s open up the browser and try navigating to your favourite site. e.g. https://philldavis.co.uk

If you can see the site, great news! You’re connected to the internet.

Now lets have a look at our IP address, go to https://whatismyipaddress.com/ and you’ll be shown your current external IP address. This should be the same one you see on your VPN Gateway.

To see if our DNS is leaking lets going to https://dnsleaktest.com and click “Standard Test”. Let it run and it should return the same DNS servers that your VPN Gateway returned when running the command line tool.

Check the Dead Man Switch

The final and possibly most immportant check is the dead man switch. Will the internet connection be terminated when the VPN connection drops?

This is easy to test, SSH into your VPN Gateway and stop the VPN client by typing:

sudo systemctl stop openvpn

Now back on your client machine try to access the internet. You shouldn’t be able to get any internet connection at all. If this is the case, hop back into the VPN Gateway and start the VPN client by typing:

sudo systemctl start openvpn

Finished

Congratulations! Assuming all of the checks passed with flying colours you have successfully created a VPN Gateway and are providing secure connection to the world to any clients that use it.

So what do we actually have in this setup?

  • A VPN Client that is not leaking its IP address or DNS.
  • A VPN Client that still has access to the internet when the VPN connection drops so regular maintenance and updates are simple.
  • A VPN Gateway with a dead man switch so no client traffic can accidentally go out via the regular internet connection.

Raspberry Pi Install Lidarr

Raspberry Pi Install Lidarr

Raspberry Pi – Install Lidarr

Overview

  • Install Lidarr.
  • Automatically manage your music.
  • Find new and missing albums.

Lidarr automates the finding, downloading, naming and organisation of Music libraries. It is designed to work in conjunction with a torrent client and media server. E.g. qBittorrent to download files and your choice of distribution software e.g. Plex Media Server to distribute the music to clients.

If you haven’t already, check out the guide to setup a secure torrent client before continuing with the install of Lidarr.

I do not in any way, shape or form condone or support the downloading of illegal or copyrighted material.

I use Lidarr to organise all of my legally purchased music, its tags, names etc and track which albums I’m missing. The Calendar view is great for seeing release dates of new albums from my favourite artists.

Technical Jargon

SSH

SSH stands for secure shell. SSH is an encrypted connection established between two computer programs. On the server side (the computer being connected to) a service is running that listens for another computer trying to contact it via SSH.

Click here for a full detailed description of SSH.

Assumptions

This guide assumes you have a fresh install of Raspbian on a headless server.

This guide assumes you either have a folder on the Raspberry Pi for your Music, or have setup a connection to your network share that contains all of your current Music.

If it is legal to download copyrighted music where you are and you wish to use Lidarr’s ability to tap into torrent RSS feeds, it’s assumed you have setup a secure torrent client.

Install Lidarr

Before we start we’re going to ensure the Raspberry Pi is up to date. Run the following commands to grab and install the latest packages:

sudo apt-get update

sudo apt-get upgrade -y

We want Lidarr to sit in the /opt directory so let’s cd into it:

cd: /opt

Now let’s grab the latest Lidar.tar.gz file. For me that is version 0.6.0.815, check https://github.com/lidarr/Lidarr/releases to find out which is the latest for you:

sudo wget https://github.com/lidarr/Lidarr/releases/download/v0.6.0.815/Lidarr.develop.0.6.0.815.linux.tar.gz

Once it’s downloaded extract the package:

sudo tar -xzvf Lidarr.develop.0.6.0.815.linux.tar.gz

Create a Service

It is recommended to run Lidarr as its own user for security purposes. We’re going to use qbtuser to own the Lidarr install and run the service. This is to tie in with the user setup in the secure torrent client guide. Feel free to use any user you like. E.g. pi:

sudo chown -R qbtuser:qbtuser /opt/Lidarr

sudo chmod -R a=,a+X,u+rw,g+r /opt/Lidarr

We’re going to create a file under /etc/systemd/system that will tell the Raspberry Pi how to handle Lidarr and ensure it runs as a service:

sudo nano /lib/systemd/system/lidarr.service

Now that we’ve created the file, paste the following into it:

[Unit]
Description=Lidarr Daemon
After=syslog.target network.target

[Service]
User=qbtuser
Group=media
Type=simple
ExecStart=/usr/bin/mono /opt/Lidarr/Lidarr.exe -nobrowser
TimeoutStopSec=20
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target

Press Ctrl+x to exit and you’ll be prompted to Save modified. Type Y and then return to save the file.

Start the Sonarr Service

If everything has gone to plan we can start the service.

Start the service for the first time with:

sudo systemctl start lidarr

Check it all Works

Now we’ve finished installing Lidarr and the service is running, lets check it all works by going to http://*Rasbperry Pi Ip Address*:8686 and we should see the default page.

Auto Start Lidarr – No Torrenting

If Lidarr is used to check the status of your collections we want to start Lidarr with the Raspberry Pi:

sudo systemctl enable lidarr

Now ensure everything works, reboot your Raspberry Pi:

sudo reboot

If you intend to use Sonarr to find torrents enable it by following the below section. Note I do not condone this.

Auto Start Lidarr – Linked to Torrent Client

If Lidarr is used to find torrents, we only want the Lidarr service to be active when there is a VPN connection available.

To do this we’re going to update some files in the OpenVPN directory so cd into /etc/openvpn:

cd /etc/openvpn

To auto start Lidarr when the VPN connection is established we need to edit route-up.sh.

sudo nano route-up.sh

And paste the following at the bottom of the file:

systemctl start lidarr

The file should look like:

#!/bin/sh
iptables -t nat -I POSTROUTING -o tun0 -j MASQUERADE
systemctl start qbittorrent

systemctl start lidarr

 Press Ctrl+x to exit and you’ll be prompted to Save modified. Type Y and then return to save the file.

Auto Stop Lidarr – Linked to Torrent Client

To make sure no peer 2 peer traffic is sent over your clear internet connection we’re going to ensure the Lidarr service is stopped before we lose our VPN connection.

To do this we’re going to add a line to down.sh:

sudo nano down.sh

Paste the following line above “iptables -t nat -D POSTROUTING -o tun0 -j MASQUERADE”:

systemctl stop lidarr

The file should look like:

#!/bin/sh
systemctl stop qbittorrent

systemctl stop lidarr
iptables -t nat -D POSTROUTING -o tun0 -j MASQUERADE

Press Ctrl+x to exit and you’ll be prompted to Save modified. Type Y and then return to save the file.

Reboot your Raspberry Pi to ensure all our changes are applied:

sudo reboot

Check Everything is Working

After the reboot lets check everything is working as we expect.

In your web browser navigate to the Lidarr Web UI as you did earlier and make sure it loads.

Import Your Current Library

Now that everything is working let’s import your current music library so Lidarr can start handling it – showing you what albums you are missing and the current track quality. This process can take a very long time, so once you’ve kicked it off it’s probably best to leave it over night. The time it takes will depend on your internet connection and device you’ve installed Lidarr on. The Raspberry Pi isn’t very powerful so will take a long time if you have a big library.

Before we start the import we want to update some settings to tell Lidarr how to handle file naming and imported track metadata.

Media Management

Click on “Settings and then “Media Management”:

I only include the track number and title in my naming scheme so as can be seen above I’ve updated the Standard Track Format. Click the “?” if you want to customise the naming convention.

Once you’re happy click “Save Changes” at the top.

Metadata

Now click on “Metadata” to update how Lidarr will handle track Metadata:

Lidarr will handle all of your music metadata and you’ve got a few options on how and when it does this. From the dropdown box next to “Tag Audio Files with Metadata” select the right options for you.

I’ve already got my music tagged so I only want Lidarr to handle new files.

If you are unsure click “More Info” to find out which option is right for you.

Once you’re happy click “Save Changes” at the top.

Import Music

To perform a bulk import of our existing library click “Artist”, “Import” and then the big green “Choose Folder” button.

Use the menu to navigate to your music folder and click “Ok”: 

Once the folder is selected Lidarr will scrape through all of the sub folders matching the artists. If any are miss matches or not found use the drop down to search for the correct artist.

It will only try to import artists that aren’t already imported:

After clicking “Import” Lidarr will take you to the Artist page and start downloading all of the album artwork, artist/album information and scrapping your folders to see which albums you currently have.

Don’t worry if the images don’t show up straight away, this process can take a long time:

Finished

The installation and basic setup of Lidarr is complete and it’s up to you to play around with the rest of the settings to get everything sorted. Jump in and have a go.

Raspberry Pi Install Sonarr

Raspberry Pi Install Sonarr

Raspberry Pi – Install Sonarr

Overview

  • Install Sonarr.
  • Automatically manage your media.
  • Find missing episodes.

Sonarr automates the finding, downloading, naming and organisation of TV shows. It is designed to work in conjunction with a torrent client and media server. E.g. qBittorrent to download files and Plex Media Server to distribute them to clients.

If you haven’t already, check out the guide to setup a secure torrent client before continuing with the install of Sonarr.

I do not in any way, shape or form condone or support the downloading of illegal or copyrighted material.

Technical Jargon

SSH

SSH stands for secure shell. SSH is an encrypted connection established between two computer programs. On the server side (the computer being connected to) a service is running that listens for another computer trying to contact it via SSH.

Click here for a full detailed description of SSH.

Assumptions

This guide assumes you have a fresh install of Raspbian on a headless server.

This guide assumes you either have a folder on the Raspberry Pi for your TV shows, or have setup a connection to your network share that contains your TV media.

If it is legal to download TV box sets where you are and you wish to use Sonarr’s ability to tap into torrent RSS feeds, it’s assumed you have setup a secure torrent client.

Install Sonarr

Before we start we’re going to ensure the Raspberry Pi is up to date. Run the following commands to grab and install the latest packages:

sudo apt-get update
sudo apt-get upgrade -y

As Sonarr isn’t in the default Raspbian repository we’ll want to add Sonarr’s. To do this let’s install the directory manager to allow us to modify our repositories:

sudo apt-get install dirmngr

Now we can add new repositories let’s make sure we can use secure connections:

sudo apt-get install apt-transport-https -y --force-yes

With all that sorted we can add Sonarr’s repository:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 0xA236C58F409091A18ACA53CBEBFF6B99D9B78493
echo "deb http://apt.sonarr.tv/ master main" | sudo tee /etc/apt/sources.list.d/sonarr.list

With the repository available we’ll now be able to install Sonarr, but before we do we need to update our sources so the Pi knows where to look:

sudo apt update
sudo apt install nzbdrone -y

Create a Service

It is recommended to run Sonarr as its own user for security purposes. We’re going to use qbtuser to own the Sonarr install and run the service. This is to tie in with the user setup in the secure torrent client guide. Feel free to use any user you like. E.g. pi:

sudo chown -R qbtuser:qbtuser /opt/NzbDrone

We’re going to create a file under /etc/systemd/system that will tell the Raspberry Pi how to handle Sonarr and ensure it runs as a service:

sudo nano /lib/systemd/system/sonarr.service

Now that we’ve created the file, paste the following into it:

[Unit]
Description=Sonarr Daemon
After=syslog.target network.target

[Service]
User=qbtuser
Group=qbtuser

Type=simple
ExecStart=/usr/bin/mono --debug /opt/NzbDrone/NzbDrone.exe -nobrowser
TimeoutStopSec=20
KillMode=process
Restart=on-failure

[Install]
WantedBy=multi-user.target

Press Ctrl+x to exit and you’ll be prompted to Save modified. Type Y and then return to save the file.

Start the Sonarr Service

If everything has gone to plan we can start the service.

Start the service for the first time with:

sudo systemctl start sonarr

Check it all Works

Now we’ve finished installing Sonarr and the service is running, lets check it all works by going to http://*Rasbperry Pi Ip Address*:8989 and we should see the default page.

Auto Start Sonarr – No Torrenting

If Sonarr is just used to check the status of your collections we want to start Sonarr with the Raspberry Pi:

sudo systemctl enable sonarr

Now ensure everything works, reboot your Raspberry Pi:

sudo reboot

Once the Raspberry Pi has rebooted check the status of the sonarr service to ensure it is working:

sudo systemctl status sonarr

If you intend to use Sonarr to find torrents enable it by following the below section. Note I do not condone this.

Auto Start Sonarr – Linked to Torrent Client

If Sonarr is used to find torrents, we only want the Sonarr service to be active when there is a VPN connection available.

To do this we’re going to update some files in the OpenVPN directory so cd into /etc/openvpn:

cd /etc/openvpn

To auto start Sonarr when the VPN connection is established we need to edit route-up.sh.

sudo nano route-up.sh

And paste the following at the bottom of the file:

systemctl start sonarr

The file should look like:

#!/bin/sh
/etc/openvpn/update-systemd-resolved
# replace /etc/resolv.conf with special version for AirVPN
rm /etc/resolv.conf
cp /etc/resolv.conf.airvpn /etc/resolv.conf
systemctl start qbittorrent
systemctl start sonarr

Press Ctrl+x to exit and you’ll be prompted to Save modified. Type Y and then return to save the file.

Auto Stop Sonarr – Linked to Torrent Client

To make sure no peer 2 peer traffic is sent over your clear internet connection we’re going to ensure the Sonarr service is stopped before we lose our VPN connection.

To do this we’re going to add a line to down.sh:

sudo nano down.sh

Paste the following line below systemctl stop qbittorrent:

systemctl stop sonarr

The file should look like:

#!/bin/sh
systemctl stop qbittorrent
systemctl stop sonarr
/etc/openvpn/update-systemd-resolved
# replace /etc/resolv.conf with special version for AirVPN
rm /etc/resolv.conf
cp /etc/resolv.conf.airvpn /etc/resolv.conf

Press Ctrl+x to exit and you’ll be prompted to Save modified. Type Y and then return to save the file.

Reboot your Raspberry Pi to ensure all our changes are applied:

sudo reboot

Check Everything is Working

After the reboot lets check everything is working as we expect.

In your web browser navigate to the Sonarr Web UI as you did earlier and make sure it loads.

Add A TV Show

Now that everything is working let’s add a TV show. Click Add Series and search for the series you want to monitor. I’ve searched for one of my favourite shows Black Books.

Point the path to the parent folder containing your TV Shows and click the green plus button to add it to your list.

You can either add a single series that you don’t currently have by searching for it and clicking add, or you can import your entire folder by clicking “Import Existing Series On Disk” and selecting the parent folder of your TV shows. This will bulk import everything you have already.

Finished

The installation of Sonarr is complete and it’s up to you to play around with the settings to get everything sorted. Jump in and have a go.