BitTorrent VPN
Since doing this project I’ve discovered NetworkManger’s dispatcher.d scripts. I’m currently working on a follow-up article using them.
Backstory #
I recently spun up a new Ubuntu instance (VM) to serve as my dedicated Bit Torrent client. I have an older Dell server that runs Virtual Box, which is what I’m using as the host. The Dell server also hosts my Plex instance. The main motivator behind the dedicate BT client was the Plex instance. A few friends and I share our libraries and they were constantly telling me my Plex server was unreachable. This was b/c I use a VPN service, PrivateInternetAccess.com to mask my use of bit torrent from the world. The problem was that Plex isnt reliabley reachable behind the VPN. Sometimes I could tweak the port and get it to work, but the solution was always short lived.
Requirements #
A few requirements for my final product:
-The VM should auto launch when windows starts on the host
-The VM should automatically connect to PIA via OpenVPN
-The VM should constantly check the VPN connection and reconnect if it drops
-The Bit Torrent Client should have a web interface for ease of use
-The Bit Torrent Client should terminate in the event the VPN drops and then come back when the VPN connection returns.
Solution #
The VM #
I installed Virtual Box Go ahead and also download and install the Extension Pack
Setup the VM #
I’m not going to walk you through this one. There are plenty of howto guides out there.
I gave mine 2 cores and 4 GB of RAM. I also used Ubuntu 15.10
Auto Boot the VM #
This is for windows 10, but you should be able to easily adapt it to any version of windows
Launch the startup folder:
Win+R
type: shell:startup
Press Enter
Create a shortcut with the following command
"D:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm Ubuntu
Obviously you’ll need to update the path to VBoxManage to whatever it is on your system. Should look like this in the end:
You will have to wait until the windows session is logged in, this is not going to run as a service
Configure VPN #
Like I said, I use PrivateInternetAccess.com, which has a super simple script for configuring your vpn on ubuntu using OpenVPN. If you want to use PIA then just use the above link and follow the guide. Please not you’ll have to update the script to restart the network manager
Original Script
restart_network_manager( )
{
echo 'Restarting network manager..'
/usr/bin/nmcli nm enable false
/usr/bin/nmcli nm enable true
}
Modified for 15.10
restart_network_manager( )
{
echo 'Restarting network manager..'
service network-manager restart
}
Auto Connection #
The VPN needed to connect automagically on reboot and any time the connection was dropped. I created a simple shell script to achieve this:
nmcli con up id 'PIA - CA Toronto'
Then set this up under roots cronttab to run on reboot and every 5 minutes.
If the connection is already up it will fail out and then do nothing. If it is down it will connect to this named connection. You can also opt to use the UID, but, this works just as well.
So far we have a working ubuntu instance with a VPN connection that automagically starts up and will attempt to automagically reconnect every 5 min.
Setting up BitTorrent #
I ended up going with Transmission BT. It is a fully featured client that has a daemon with a web interface.
If you just want the daemon install it with:
sudo apt-get install tranmission-daemon
Or opt to install all the supporting packages
sudo apt-get install transmission*
There are tons of settings you can tweak. Follow this guide like I did to get up and going.
Monitor the VPN #
Finally I wanted to monitor the VPN connection. In the event it drops, I wanted to terminate the Bit Torrent Client. When it comes back up, I want to start the BitTorrent Client. Now you can get a bit more fancy than I did. Tranmission has a great “RPC” endpoint that allows you to control the torrents. So if you wanted too, you could stop each individual torrent. But, I like this shotgun approach better.
I created a script that runs in the background
#!/bin/bash
while [ "true" ]
do
VPNCON=$(nmcli c show --active | grep PIA)
LEN=${#VPNCON}
TSTATUS=$(ps aux | grep /usr/bin/transmission-daemon | grep -v grep)
TLEN=${#TSTATUS}
# echo $LEN
# echo $TLEN
if [[ $LEN == 0 ]]; then
if [[ $TLEN != 0 ]]; then
(service transmission-daemon stop)
logger VPN is down stopped bt
echo stopped bt
fi
else
if [[ $TLEN == 0 ]]; then
(service transmission-daemon start)
logger VPN is up started bt
echo started bt
fi
fi
sleep 3
done
And thats it! Now I can Torrent with a little more piece of mind.
Thanks for reading!