#bittorrent #dht #kademlia #server-client #torrent

mainline

Simple, robust, BitTorrent's Mainline DHT implementation

29 releases (18 stable)

new 5.1.0 Feb 11, 2025
4.2.1 Jan 19, 2025
4.2.0 Dec 13, 2024
4.1.0 Nov 29, 2024
0.6.1 Nov 27, 2023

#150 in Network programming

Download history 1528/week @ 2024-10-22 1969/week @ 2024-10-29 2317/week @ 2024-11-05 2481/week @ 2024-11-12 2141/week @ 2024-11-19 3180/week @ 2024-11-26 3054/week @ 2024-12-03 3340/week @ 2024-12-10 2203/week @ 2024-12-17 946/week @ 2024-12-24 2236/week @ 2024-12-31 2118/week @ 2025-01-07 2390/week @ 2025-01-14 1929/week @ 2025-01-21 1455/week @ 2025-01-28 1230/week @ 2025-02-04

7,540 downloads per month
Used in 6 crates (4 directly)

MIT license

265KB
5.5K SLoC

Mainline

Simple, robust, BitTorrent's Mainline DHT implementation.

This library is focused on being the best and simplest Rust client for Mainline, especially focused on reliable and fast time-to-first-response.

It should work as a routing / storing node (server mode) as well, and has been running in production for many months without an issue. However if you are concerned about spam or DoS, you should consider implementing rate limiting.

API Docs

Getting started

Check the Examples.

Features

Client

Running as a client, means you can store and query for values on the DHT, but not accept any incoming requests.

use mainline::Dht;

let dht = Dht::client().unwrap();

Supported BEPs:

This implementation also includes measures against Vertical Sybil Attacks.

Server

Running as a server is the same as a client, but you also respond to incoming requests and serve as a routing and storing node, supporting the general routing of the DHT, and contributing to the storage capacity of the DHT.

use mainline::Dht;

let dht = Dht::server().unwrap(); // or `Dht::builder::server_mode().build();` 

Supported BEPs:

Rate limiting

The server implementation has no rate-limiting, you can run your own request filter and apply your custom rate-limiting. However, that limit/block will only apply after parsing incoming messages, and it won't affect handling incoming responses.

Adaptive mode

The default Adaptive mode will start the node in client mode, and after 15 minutes of running with a publicly accessible address, it will switch to server mode. This way nodes that can serve as routing nodes (accessible and less likely to churn), serve as such.

If you want to explicitly start in Server mode, because you know you are not running behind firewall, you can call Dht::builder().server_mode().build(), and you can optionally add your known public ip so the node doesn't have to depend on, votes from responding nodes: Dht::builder().server_mode().public_ip().build().

Acknowledgment

This implementation was possible thanks to Webtorrent's Bittorrent-dht as a reference, and Rustydht-lib that saved me a lot of time, especially at the serialization and deserialization of Bencode messages.

Dependencies

~4–5.5MB
~105K SLoC