Tiny bit torrent client written in go. Originally inspired from build-your-own-x (this is perhaps one of the most iconic repository I have came across on github)
BitTorrent is the ideal way to transfer large files to thousands of locations in a short period of time. This doesn't only apply to movies and music that are downloaded by the average BitTorrent user, companies can benefit from it as well. With help from BitTorrent, Facebook can now push hundreds of megabytes of new code to all servers worldwide in just a minute.
This is a lightweight software application designed to download and upload files using the BitTorrent protocol. BitTorrent is a peer-to-peer (P2P) communication protocol widely used for distributing large amounts of data over the internet. The protocol enables efficient sharing of files by dividing them into smaller pieces, which are then distributed among multiple users (peers).
Many companies use the BitTorrent protocol, including Facebook, Twitter, Wikipedia, and Blizzard. Watch this to learn more about how Facebook uses BitTorrent protocol and why they love it.
An amazing blog post by veggiedefender
- Go: Make sure you have Go installed on your system.
go get -u github.com/adimail/torrent-client/internal/torrentfile
go get -u github.com/manifoldco/promptui
go build cmd/torrent-client/main.go
./main
Through a user interface, select the torrent file you would like to download and hot enter. The downloaded files will be saved in downloads
directory.
Use the arrow keys to navigate: ↓ ↑ → ←
Contains the information about the file we want to download. Each torrent is independent.
- Seeders: Seeders are seeding the data in the network. At least one seeder.
- P2P: No indicative for anyone to join a torrent and become a seeder.
- A user downloads the torrent file from the internet via a normal HTTP request. We can discard the torrent file upon download completion.
- We can either discard the file or we ourselves become the seeder for anyone who wants to download the file. We become a seeder from a leecher.
Holds meta information.
- Announce: Announce of the URL.
- Created by: Name and version of the program that created it.
- Creation date: Creating time of the torrent in UNIX epoch.
- Encoding: Encoding of strings (utf-8) as part of the info dictionary.
- Comment: Information.
- Info: Dictionary that describes files of the torrent.
- Single file format:
- Name: Filename
- Length: File size
- Mdsum: MD5 of the file
- Multifile format:
- Name: Name of the dictionary
- Files: List of dictionaries
- Length: Length
- Mdsum: MD5 sum of the title
- Path: List representing the path
- Format: /a/b/c.txt -> [a, b, c.txt]
- Single file format:
- Storing the path as a list of strings; they store it as an array because different operating systems have different separators.
The info dict also contains:
- Piece length: Number of bytes in each piece.
- Pieces: 20 bytes SHA1 hash value concatenated.
- piece1 -> SHA1 -> S1
- piece2 -> SHA2 -> S2
- piece3 -> SHA3 -> S3
- Then we concatenate S1 + S2 + S3.
Torrent files are "Bencoded," and to extract the above fields, we would need to parse the torrent file.
Supports: Strings, lists, integers, dictionaries.
- Strings:
- Format:
<length>: <string>
- Example:
"Aditya"
->6:Aditya
- Format:
- Integers:
- Format:
i<int>e
- Example:
10
->i10e
- Format:
- List:
- Format:
l<bencoded values>
- Example:
["a", "b", 1]
->l 1:a 1:b i1e e
- Format:
- Dictionary:
- Format:
d<bencoded string><bencoded value>...e
- Example:
{"a": 1, "b":2}
->d 1:a i1e 1:b i2e e
- Format:
- It is very fun to write your own Bencoding parser.
Pieces and piece size are very important.