Tuesday, September 28, 2010

Automatically update Transmission's block list


My favourite BitTorrent client Transmission supports bluetack-formatted blocklists. In computing, a blacklist or block list is a basic access control mechanism that allows everyone access, except for the members of the black list (i.e. list of denied accesses). Transmission utilizes a blocklist of known anti-P2P IPs, to prevent them from connecting and possibly interfering with transfers.
When you press the "Update Blocklist" button in the Transmission GUI, a new copy of blocklist is downloaded from a Transmission mirror that's updated twice daily. Since transmission-daemon on my headless server does not have such button I have to download the blocklist by hand, uncompress it, and place the uncompressed file in the daemon's blocklists folder.
Here's how to do it manually:
  1. First, make sure you have enabled blocklist support. Stop transmission-daemon, open file /etc/transmission-daemon/settings.json, set "blocklist-enabled" property to true and re-start the daemon.
  2. Go to directory where transmission expects to find the blocklist, on my PPA install it's in /var/lib/transmission-daemon/info/blocklists/:
    # cd /var/lib/transmission-daemon/info/blocklists/
    Here, files ending in ".bin" are the binary representations of bluetack files that Transmission generates for faster lookups.
  3. Download gzip compressed blocklist file for Transmission:
    # wget http://update.transmissionbt.com/level1.gz
  4. Unzip the file:
    # gunzip level1.gz
    You should now have a plain text, bluetack-formatted blocklist file called level1.
  5. Reload transmissions-daemon:
    # /etc/init.d/transmission-daemon reload
    Now you should see a smaller file appearing called level1.bin. This a binary representation that Transmission generated for faster lookups by parsing our blocklist file. On start-up, Transmission will try to parse any non-".bin" file and generate a new blocklist from it, so you can have multiple blocklists just by copying new bluetack files into this directory.
Now to automate this procedure I created the following bash script called update-blocklist:

#!/bin/bash

# blocklist directory
BLOCKLISTDIR=/var/lib/transmission-daemon/info/blocklists

cd ${BLOCKLISTDIR}
if [ $? -eq 0 ]; then
  if [ -f level1 ]; then
    rm level1
  fi
  # if no blocklist file exist update blocklist
  if [ ! -f level1 ]; then
    wget -q -O level1.gz http://update.transmissionbt.com/level1.gz
    # if download successful unzip it
    if [ -f level1.gz ]; then
          gunzip level1.gz
          # if file extracted successfully reload transmission
          if [ $? -eq 0 ]; then
            chmod go+r level1
            /etc/init.d/transmission-daemon reload
          else
                rm -f level1*
          fi
        fi
  fi
  cd - 2>&1 >/dev/null
fi

Then I made script executable by all using chmod command:
# chmod a+x update-blocklist
Now depending on how frequently you want to update your blocklist you can place it either in /etc/cron.daily, /etc/cron.weekly or /etc/cron.monthly directory. I felt that weekly update was frequent enough:
mv update-blocklist /etc/cron.weekly/
You're done, now cron will automatically download the latest blocklist for you and reload Transmission with it.