Riptide is a fully functional BitTorrent client built entirely from scratch in C#.
This project was developed as a deep dive into the technical intricacies of decentralized peer-to-peer networking. Rather than relying on external torrenting libraries, Riptide implements the core BitTorrent specification from the ground up. It handles everything from parsing custom data serializations to managing raw TCP sockets, cryptographic hashing, and concurrent file I/O.
- Custom BEncoding Engine: Includes a bespoke recursive descent parser that decodes the BitTorrent protocol's native serialization format into strictly typed C# data structures.
- Torrent File Management: Automatically extracts metadata, calculates the 20-byte SHA1 InfoHash, and slices piece hashes for data verification.
- Tracker Communication: Connects to HTTP/UDP trackers, safely URL-encodes binary hashes, and parses compact peer lists to discover available seeders and leechers in the swarm.
- The Peer Protocol: Manages active TCP connections and handshakes, handling the constant stream of binary state messages (Choke, Unchoke, Interested, Have, Request, Piece).
- Piece Assembly & File I/O: Safely downloads multiple file chunks concurrently, verifies their integrity against the original SHA1 hashes, and writes the completed blocks to disk.
- Azureus-Style Identification: Generates a compliant, randomized 20-byte Peer ID (using the
-RT0001-prefix) to uniquely identify the client to the network.
riptide/
Bencode.cs # Recursive descent parser for BEncoded data
Torrent.cs # File blueprint, metadata extraction, and SHA1 hashing
ClientConfig.cs # Static configuration and Azureus-style Peer ID generation
Tracker.cs # HTTP client for announcing to trackers and parsing peer IPs
Peer.cs # TCP socket management and peer-to-peer message protocol
ClientEngine.cs # Connection pooling, concurrent piece requests, and File I/O
- Initialization: The client reads a
.torrentfile and passes it toBencode.cs, which translates the raw bytes into a readable dictionary. - Metadata & Hashing:
Torrent.csextracts the file size, piece length, and the raw binary hashes. It then hashes the originalinfodictionary using SHA1 to create the torrent's unique ID. - Announcing: Riptide URL-encodes its InfoHash and Peer ID, sending an HTTP GET request to the tracker. The tracker responds with a list of IP addresses of other users downloading the same file.
- The Swarm: Riptide opens TCP sockets to those IP addresses, performs the BitTorrent handshake, and begins exchanging "Interested" and "Request" messages to download missing pieces.
- Verification: As raw bytes stream in, they are hashed and checked against the original torrent metadata. Valid pieces are saved to the hard drive.
Requires the .NET SDK to be installed.
- Clone the repository:
git clone [https://github.com/your-username/riptide.git](https://github.com/your-username/riptide.git)
- Navigate to the project directory:
cd riptide - Run the client, passing a
.torrentfile as an argument:dotnet run -- "path/to/debian-iso.torrent"
- C# (.NET)
- System.Security.Cryptography (SHA1 Hash Generation)
- System.Net.Sockets (TCP Peer Connections)
- System.Net.Http (Tracker Announce Requests)
- System.IO (Binary Stream Reading & Disk Writing)
