Friday, May 27, 2011

Legal Uses of the BitTorrent Protocol: Part 2

The other day i posted a basic introduction to the bittorrent protocol.  Now i want to write down somewhere how bittorrent can be used in a legal situation.  As i mentioned in yesterday's blog, i have many servers that all need the same file every time there is an update, which could be as frequent as once a quarter for major updates.  The status quo is to remote into every server, log into the vendor website, and download the update.  This can be very time consuming, not to mention very boring. 
That's where bittorrent has helped me.  To continue with the analogy of the ants: each server is an anthill.  The update is the sandwich.  Now all i need are ants at each anthill (a bittorrent client at each server) and an ant with a radio (a bittorrent tracker).  As my torrent client, i chose to use uTorrent.  It's a fairly small torrent client with all the features i need.  Particularly, uTorrent can listen to an RSS feed and (as long as the feed is designed correctly) automatically get the torrents in the feed.  In other words, if i can create an RSS feed which contains the torrent file (the descriptor file that tells torrent clients about the sandwich) then i can have all the anthills subscribe to that feed.  But i get ahead of myself.
So, each anthill (server) has an army of ants (uTorrent client).  Now i need a seeder, that would be someone who has the original sandwich.  So, i install utorrent on my laptop (or i could use on of the servers, it doesn't really matter).  I also need a tracker.  Turns out the uTorrent client has a small basic tracker built in but disabled by default.  So, on the client on my laptop, i go and enable it.  Now all of the pieces are there to start torrenting.
For the first few torrents, i do things manually, just to make sure I've got everything setup properly.
  1. I open the uTorrent client on my laptop and start the 'new torrent' wizard.  I point the wizard to the update file i want to share (this could also be a directory with thousands of files, no need for zipping).  I also point the wizard to my laptop's IP address as the tracker.  When i hit finish, uTorrent outputs a *.torrent file (i saved that to my desktop).  If i look in my uTorrent client, I'll see that i'm now seeding the file and the fact that I'm seeding it has been reported to the tracker portion of the uTorrent client.  Now all i need to do is get that torrent file over to the other clients.  (Remember I'm doing this manually, this part will be much less complicated after i automate it.) 
  2. I go to each one of the servers and copy the *.torrent file (2-3KB max) to each server. 
  3. Then i open the *.torrent file using the uTorrent client on each server.
On a side note, i usually configure each torrent client to use the same port number.  This makes it easy to categorize the traffic in monitoring software.  It also makes it easy for security guys to recognize that it's a legal protocol.  Once i open the torrent file in the torrent client on each of the remote servers, that torrent client contacts the tracker on my laptop and asks for who else has the file.  The tracker says the file is being seeded (the whole file is shared) by my laptop.  uTorrent then starts to request very small pieces of the file from my laptop.  As it downloads the file, it updates the tracker to let it know which parts of the file it made available to other members of the torrent.  Eventually, between the whole copy on my laptop and the partial copies on the other servers, there are 2 copies of each piece of the entire file. At that point the torrent really speeds up and fairly quickly, the file is copied to each one of the servers.
Now, to automate the process.  I'm sure there is probably some perl code to automatically create a torrent from files in a certain directory, but for now, I'm still doing this manually.  It only happens once every time there's an update.  However, i can automate steps 2 and 3, by adding one piece of software to my laptop (obviously everything running on my laptop could actually be done on one of the servers, it's not crucial where it actually happens).  The software to be added is HFS (HTTP file server).  The reason i added this software is that it easily creates an RSS feed from the files in a directory.  So, by adding HFS, all i have to do is save the *.torrent files in a particular directory, and they automatically get added to the RSS feed.  There's some tweaking necessary for the RSS feed, so I'll detail how to do that.  Once the RSS feed is setup, i can go to each of the torrent clients on the remote servers and add it to the feeds list.  The client checks the RSS feed and downloads all torrents in it (there are obviously checks to make sure a torrent isn't downloaded twice).  By adding the RSS functionality, steps 2 and 3 are taken care of automatically.  All i have to do is save the *.torrent file in the HFS directory (saving the *.torrent file to a particular location is part of the torrent creation process, step 1).
So there you have it.  I'll post instructions tomorrow on how to customize HFS to make the RSS feed compatible with uTorrent's RSS feature.