In this post we are going to explore how to build a BitTorrent client. BitTorrent is a protocol that supports data distribution directly between peers without requiring a centralized server to host anything. Typically it's used for file distribution, but the protocol has other uses. For example, you could use it to build your own CDN or a server deploy system.
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 let's build a fully working BitTorrent client together.
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 this version is good enough to be a respectful peer in a network. 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 haven't got that far yet.
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.