In this post we are going to explore how to build a BitTorrent client. BitTorrent is a protocol that supports file distribution without official "clients" and "servers". It has a lot of cool possible uses beyond simply sharing files (the murder deployment system, for example).
In general, I think think BitTorrent is a Smashing Great Idea™ that doesn't get as much praise as it should. It's also been something that I've wanted to play with for a while, so I figured that this would be a perfect opportunity to learn more about it and possibly create a useful tool in the meantime.
I don't know how to build a BitTorrent client. I've never worked with BitTorrent tech before. I will be experimenting and learning along with you as I read the BitTorrent spec and try to make something work. I will eventually get something working, but I can't promise that it will be the best way to approach the problem or that it will work super well. The primary goal is to learn.
The finished product will probably not be production ready, and most likely won't act like a good peer until we get to version 1.0. If you need to torrent something, please use a real torrent client until our version is good enough to be a respectful peer. In general, please try to give as much as you take whenever you join any peer-to-peer network.
I want to focus on the core of the BitTorrent protocol (at least in the beginning). So we will be building a command line application which will be used like this:
$ snag fetch <torrent-file>
and possibly in a later version also support seeding files for others:
$ snag serve <file>
This client should be able to parse torrent files, connect to peers, download files, and seed files. If you kill the process and restart later, it should resume the download. The client should be respectful, so it should make an effort to contribute back to the network.
Maybe it the future we will try out a permanent background daemon that you can add downloads to asynchronously. It should also be possible to use the BitTorrent protocol to implement some sort of personal backup system. I'm not sure what the possible uses will be. We'll just have to build it and see.
Since we're already reinventing the wheel by implementing this protocol at all, I'm going to avoid using libraries wherever possible. I'll dip into the node.js standard library for some things that are totally unrelated to this project (like hash calculations). But for the most part I'm going to attempt to write everything from scratch.
You need a text editor and node.js installed. If you want to allow other peers to connect to you, you'll need to open ports on your router. There might be an automatic system to open ports, but I'm not sure about that yet.
We'll be referencing the official BitTorrent spec as well as the theory.org meta spec. You don't need to have any pre-existing knowledge of BitTorrent. (It would be a bit silly if you did, since I don't have any myself.)
To get the most learning, try following along and implementing your own version. If anything is unclear or you need help, contact me and I'll see what I can do.