A high-performance BitTorrent client library built in Rust from scratch.
- Torrent File Parsing: Decode
.torrentfiles using built-in Bencode parsing. - Magnet Link Support: Resolve magnet links via Extended Metadata Exchange.
- HTTP API: RESTful API built with
actix-webfor managing downloads (start, pause, resume, stop). - Real-time Progress: WebSocket integration for streaming live download progress to clients.
- Peer Discovery: Fetch peer IPs from any standard HTTP tracker (it is not limited to CodeCrafters; it fully supports all standard public and private HTTP trackers). Note that UDP trackers are not supported yet.
- Swarm Management: Connect to multiple peers concurrently to download pieces and verify hashes.
- Rust toolchain (cargo, rustc)
-
Clone the repository:
git clone https://github.com/your-username/torrex.git cd torrex -
Build the project:
cargo build --release
Start the HTTP API server. By default, it runs on port 7878:
cargo run -p torrex-apiThe server will now be accessible at http://127.0.0.1:7878.
Why these links? Public torrent seeders often throttle or ban new peers. The CodeCrafters test tracker (
bittorrent-test-tracker.codecrafters.io) is purpose-built for BitTorrent client testing — it responds reliably and never chokes connections, making it the best starting point for verifying Torrex works correctly.
The sample.torrent file is included in the repo under torrex-lib/ and points to the same CodeCrafters tracker:
# Load the bundled sample.torrent (downloads sample.txt, ~90 KB)
GET http://127.0.0.1:7878/torrex/api/v1/initial_info_metafile?filepath="torrex-lib/sample.torrent"Or use one of these CodeCrafters magnet links directly:
| File | Magnet Link |
|---|---|
magnet1.gif |
magnet:?xt=urn:btih:ad42ce8109f54c99613ce38f9b4d87e70f24a165&dn=magnet1.gif&tr=http%3A%2F%2Fbittorrent-test-tracker.codecrafters.io%2Fannounce |
magnet2.gif |
magnet:?xt=urn:btih:3f994a835e090238873498636b98a3e78d1c34ca&dn=magnet2.gif&tr=http%3A%2F%2Fbittorrent-test-tracker.codecrafters.io%2Fannounce |
magnet3.gif |
magnet:?xt=urn:btih:c5fb9894bdaba464811b088d806bdd611ba490af&dn=magnet3.gif&tr=http%3A%2F%2Fbittorrent-test-tracker.codecrafters.io%2Fannounce |
Example using magnet1.gif:
# 1. Load the magnet link
GET http://127.0.0.1:7878/torrex/api/v1/initial_info_magnet?url=magnet:?xt=urn:btih:ad42ce8109f54c99613ce38f9b4d87e70f24a165&dn=magnet1.gif&tr=http%3A%2F%2Fbittorrent-test-tracker.codecrafters.io%2Fannounce
# 2. Start the download (use uuid from step 1's response)
POST http://127.0.0.1:7878/torrex/api/v1/start_download
Content-Type: application/json
{ "uuid": "<uuid-from-step-1>" }
# 3. Watch progress over WebSocket
ws://127.0.0.1:7878/torrex/api/v1/ws/download/<uuid-from-step-1>The API is exposed under the /torrex/api/v1 path prefix.
Check if the API server is up and running.
-
Endpoint:
GET / -
Response:
{ "success": "true", "message": "Torrex API is running" }
Parse and load a local .torrent file to prepare for downloading.
-
Endpoint:
GET /initial_info_metafile -
Query Parameters:
filepath: Path to the.torrentfile.
-
Example:
GET /torrex/api/v1/initial_info_metafile?filepath="sample.torrent" -
Response:
{ "success": "true", "uuid": "<download-uuid>", "name": "filename.iso", "length": 104857600 }
Parse and resolve a magnet link to prepare for downloading.
- Endpoint:
GET /initial_info_magnet - Query Parameters:
url: The full magnet URL.
- Example:
GET /torrex/api/v1/initial_info_magnet?url=magnet:?xt=urn:btih:...
Initiate the download process for a previously loaded torrent or magnet link.
-
Endpoint:
POST /start_download -
Content-Type:
application/json -
Body:
{ "uuid": "<download-uuid>", "destination": "/optional/custom/save/path" }(If
destinationis omitted, the downloaded file will be saved to the system's temporary directory).
Connect via WebSocket to stream real-time download progress events.
- Endpoint:
WS /ws/download/{uuid} - Example:
ws://127.0.0.1:7878/torrex/api/v1/ws/download/<download-uuid>
Pause an active download manager.
- Endpoint:
GET /pause - Query Parameters:
uuid: The UUID of the download.
- Example:
GET /torrex/api/v1/pause?uuid=<download-uuid>
Resume a paused download.
- Endpoint:
GET /resume - Query Parameters:
uuid: The UUID of the download.
- Example:
GET /torrex/api/v1/resume?uuid=<download-uuid>
Stop a download entirely and close connections.
- Endpoint:
GET /stop - Query Parameters:
uuid: The UUID of the download.
- Example:
GET /torrex/api/v1/stop?uuid=<download-uuid>
The following diagram illustrates the high-level architecture and data flow between the client, Torrex API, and the Torrex library.
- Extend support for various .torrent file metainfo.
- Expand metainfo struct for more options.
- Add UDP support for peer discovery (currently supports only HTTP).
- Add support for DHT (Distributed Hash Table).
- Heavy refactor needed for connection module.
- Complete Event announcement to tracker.
- Implement support for seeding.
- Fully implement download manager for pausing, resuming, and stopping downloads.
