myTorrent is a simple BitTorrent client that decodes bencoded torrent files and interacts with the BitTorrent network to download files. It’s a command-line utility written in shell script, making use of utilities like curl for HTTP requests and standard Linux tools for data manipulation.
myTorrent works by implementing the BitTorrent protocol which includes parsing .torrent files, communicating with trackers, and handling peer connections to download files.Bencode is the encoding format used by the BitTorrent protocol to store and transmit loosely structured data. It supports strings, integers, lists, and dictionaries. myTorrent can decode this data into JSON format.
Parses .torrent files to extract key information such as announce URLs, file length, and pieces info which is essential for downloading the file.
Communicates with trackers to get a list of peers, send periodic updates, and receive a list of potential peers to connect to.
Establishes TCP connections with peers, completes handshakes, and manages data transfer including requesting pieces of the file and assembling them.
Once all the pieces are downloaded, myTorrent verifies each piece’s integrity with SHA-1 hash checks before assembling them into the final file.
myTorrent is divided into stages, each responsible for a specific aspect of the BitTorrent protocol:
- Bencode Decoding
- Torrent File Parsing
- Tracker Communication
- Peer Handshake and Data Exchange
- File Download and Assembly
The below graph represents the flow of data and control through the application:
[ Torrent File ]
|
v
[ Bencode Decoder ] ---> [ Parser ] ---> [ Tracker Requester ]
| |
| v
| [ Peer Discovery ]
| |
v v
[ File Assembler ] <---+
Basic commands for interacting with the myTorrent client:
# To decode a bencoded string
./your_bittorrent.sh decode <bencoded_string>
# To parse a torrent file and print info
./your_bittorrent.sh info <path_to_torrent_file>
# To get the list of peers from the tracker
./your_bittorrent.sh peers <path_to_torrent_file>
# To perform a handshake with a peer
./your_bittorrent.sh handshake <path_to_torrent_file> <peer_ip>:<peer_port>
# To download a specific piece of the file
./your_bittorrent.sh download_piece -o <output_path> <path_to_torrent_file> <piece_index>
# To download the entire file
./your_bittorrent.sh download -o <output_path> <path_to_torrent_file>
To set up the myTorrent client on your machine, clone this repository and ensure you have the necessary dependencies installed. Instructions here…
Contributions to myTorrent are welcome! If you want to contribute to the project, please fork the repository, make your changes, and submit a pull request.
myTorrent is open-sourced software licensed under the MIT license.