jasonwryan.com

Miscellaneous ephemera…

RPi Headless Torrent Box

I was fortunate enough to be given a Raspberry Pi earlier this year and, naturally enough, have been running Arch ARM on it over the last several months. Initially, I was at somewhat of a loss as to what to do with it, so was just (under)using it1 for a persistent IRC connection. I have now, with very little effort, set it up as a headless torrent box and it is working nicely.

As is generally the case, this involved little more than stitching together a few lightweight tools to accomplish the task. In this case, it is a combination of rtorrent (with the canvas-colour patch applied), surfraw and w3m for discovering torrents,2 bittorrent sync and, optionally, a VPN.

I use a 16GB USB drive to act as storage for the device, and mount it at ~/Downloads via /etc/fstab; depending on the size of your SD card, this may be superfluous.

First a note on building packages; building anything on the Pi—with only 512MB RAM—is a protracted and thankless task. So if you intend to do more of this, or do it regularly, set up cross-compiling; you won’t regret the initial investment.

rtorrent is in the ARM repos, but I wanted to patch in colour, so I wrote a PKGBUILD, based on the package in [community] and ashren’s AUR package. Once you have rtorrent installed and working, the rest is straightforward.

The default config for w3m is perfectly functional, but can be easily customized further. The key to this setup is being able to find a torrent and then easily add it to rtorrent’s queue. To do this, I modified the hack that I wrote up a couple years back. Essentially, you use w3m’s external link function to trigger a script to grab the magnet link. Like so:

~/.w3m/config
1
extbrowser /home/jason/Scripts/magnets %s

and the script from the rtorrent wiki:

~/Scripts/magnets
1
2
3
4
#!/bin/bash
cd $HOME/Downloads/Queue/watch/    # set your watch directory here
[[ "$1" =~ xt=urn:btih:([^&/]+) ]] || exit;
echo "d10:magnet-uri${#1}:${1}e" > "meta-${BASH_REMATCH[1]}.torrent"

Remapping the w3m key to trigger the script to make it more Vim-like:

~/.w3m/keymap
1
keymap Y EXTERN_LINK

And now hitting Shifty over a magnet link adds it to your queue.

The final piece was to ameliorate the step of transferring the completed files from the Pi, or for allowing me to add torrents remotely. To do this, I used a bittorrent sync shared directory as ~/Downloads. All files are synched to my desktop as they are downloaded to the Pi. Simple.

As I mentioned above, you could also use a VPN and, with a service file, have it connect on boot if you wished to further protect your privacy.

Notes

  1. This is not to say that I am now using it to it’s full potential; quite the opposite. Recommendations or suggestions gratefully received.
  2. Discovery is your business; what you choose to download and share may be the business of copyright holders…

Comments