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.
This is an exploration-tutorial format experiment. I want to show the entire project process from idea to finished result (including all the mistakes and bad decisions along way). Published material usually skips right to a polished final project glosses over all the important learning experiences. This makes creators look a lot smarter, but I think that it can discourage people from tackling the unknown.
From a technical perspective, 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:
$ bt-get <torrent-file>
This client should be able to parse torrent files, connect to peers, upload data, and download data. 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.
The project will be written in node.js because I'm already comfortable with it, and it will allow me to think about the project instead of the programming language. It's also very popular right now, so I'm hoping that it lowers the learning curve for someone who wants to follow along.
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. That's all.
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.