A lightweight BitTorrent client implemented in JavaScript.
This project demonstrates the core functionalities of the BitTorrent protocol, enabling peer-to-peer file sharing in a decentralized network.
The BitTorrent protocol is a peer-to-peer (P2P) file-sharing system that allows users to download and distribute files efficiently without relying on a central server.
This client implements the fundamental features of the protocol, including:
- Tracker Communication: Finding peers with the desired file.
- Peer-to-Peer Networking: Exchanging pieces of the file with other peers.
- Piece Management: Downloading and assembling file pieces efficiently.
- File Writing: Saving the downloaded file to the local filesystem.
This project is a practical example of networking, concurrency, and protocol implementation in JavaScript.
- Connects to both HTTP and UDP trackers.
- Maintains a queue of pieces to download from multiple peers concurrently.
- Handles downloading, assembling, and writing file pieces.
- Supports basic message handling between peers.
- Modular design with separate modules for pieces, queue management, downloading, torrent parsing, tracker communication, and utility functions.
BitTorrent-Client/
├── src/
│ ├── pieces.js # Piece management and bitfield handling
│ ├── Queue.js # Queue system for pieces to download
│ ├── download.js # Handles piece downloading from peers
│ ├── message.js # Peer message creation and parsing
│ ├── torrent-parser.js # Parses .torrent files
│ ├── tracker.js # Tracker communication (HTTP/UDP)
│ └── utils.js # Utility functions (hashing, conversions, etc.)
├── index.js # Main entry point for the client
├── add-udp-tracker.js # Adds UDP tracker support
├── print-trackers.js # Prints tracker info for a torrent
├── package.json # Project metadata and dependencies
├── .gitignore # Git ignore file
- Clone the repository:
git clone https://github.com/Akrishna4/BitTorrent-Client.git
cd BitTorrent-Client- Install dependencies:
npm install- Run the client:
node index.js <path-to-torrent-file>-
Obtain a
.torrentfile from a trusted source. -
Place the
.torrentfile in the project directory. -
Run the client using the above command.
-
The client will:
- Parse the torrent file
- Connect to the tracker
- Find peers and start downloading pieces
- Reassemble pieces into the full file
add-udp-tracker.js: Adds support for discovering peers via UDP trackers.print-trackers.js: Prints all tracker URLs from a given.torrentfile.
- Reads metadata from
.torrentfiles - Extracts file info, piece hashes, and tracker URLs
- Connects to trackers via HTTP/UDP
- Retrieves a list of peers
- Keeps track of downloaded and missing pieces
- Validates piece integrity using SHA-1 hash
- Manages download order for pieces
- Optimizes concurrent downloading
- Requests pieces from multiple peers simultaneously
- Handles reassembly into the original file
- Implements peer messaging protocol
- Handles handshakes, requests, and piece transfers
- Common helper functions like hashing, buffer conversions, etc.
[Torrent Parser] --> [Tracker] --> [Peers]
|
v
[Queue System]
|
v
[Piece Downloader]
|
v
[Piece Management]
|
v
[File Writer]
- BitTorrent Protocol Specification
- Understanding the BitTorrent Protocol
- Implementing a BitTorrent Client in JavaScript