How vxdxk/vdk-torrent-client works
Project type and purpose
• This is a standalone, command‐line BitTorrent client written in Rust from scratch.
• It consists of two crates:
– bencode: A library for encoding/decoding data in the Bencode format.
– torrent-client: The application crate with modules to parse CLI arguments, interact with trackers, manage peers, orchestrate downloads/uploads, and write data to disk.
File structure
Top‐level
• .github/workflows/* – CI workflows (not part of runtime)
• Cargo.toml / Cargo.lock – workspace and dependency configuration
• README.md – high‐level description
bencode/
• src/lib.rs – core Bencode encode/decode functionality (parsing dictionaries, lists, integers, byte strings)
torrent-client/
• src/main.rs – entry point: parse CLI, initialize the client
• src/cli/ – argument parsing (torrent file path, download directory, options)
• src/client/ – core orchestration logic (spawning workers, managing download state)
• src/tracker/ – HTTP/UDP tracker protocol client, contact tracker, parse peer list
• src/peer/ – peer connection management, handshake, piece message exchange
• src/file/ – piece/block storage, disk IO, reassembly, integrity checks
• src/util/ – shared helpers (bitfield handling, timing, logging)
Available documentation
• The README simply states “torrent client written from scratch.”
Core components
a. CLI Interface
– Parses user input (torrent .torrent file, destination path, options)
– Triggers the client orchestration
b. Torrent Client Orchestrator
– Coordinates between tracker module and peer module
– Maintains global download state, piece availability, retry logic
c. Tracker Module
– Opens HTTP/UDP connection to tracker URLs from the .torrent metadata
– Sends “announce” requests, parses Bencoded responses (list of peers)
– Uses the bencode library
d. Peer Module
– Manages TCP connections to peers returned by trackers
– Handles handshake, bitfield exchange, message pipeline (request, piece, keep-alive)
– May spawn worker threads/tasks for each peer
e. File Module
– Writes/reads raw blocks to disk (piece management)
– On piece completion, verifies SHA-1 hash from .torrent metadata
f. Bencode Library (bencode crate)
– Shared by tracker (response parsing) and possibly for reading .torrent files
g. Utility Module
– Common utilities (bitfield operations, timeouts, logging)
Relationships and interactions
• User ➔ CLI
• CLI ➔ Orchestrator
• Orchestrator ➔ Tracker Module ➔ Network (Tracker Servers)
• Tracker Module ➔ Bencode Library ➔ Parsed Peer List ➔ Orchestrator
• Orchestrator ➔ Peer Module ➔ Network (Peer TCP connections)
• Peer Module ⇄ File Module (read/write blocks)
• File Module ➔ Orchestrator (piece completion events)
Architectural patterns and design principles
• Modularization by responsibility (CLI, orchestration, tracker, peer, file, util)
• Separation of concerns: encoding/decoding in its own crate (bencode)
• Asynchronous or multi‑threaded worker model for peers (concurrency)
• Pluggable I/O and network abstractions (could replace TCP with any async transport)