Simple, robust, BitTorrent's Mainline DHT implementation.
This is an iroh-flavored fork of nuhvi/dht: it is a support crate for iroh and integrates with the iroh ecosystem (e.g. iroh-base keys, async UDP via noq-udp). It does not depend on the iroh crate itself, so it can be used standalone. It does however require noq-udp for its socket layer.
The main purpose for which iroh uses n0-mainline is endpoint address lookup via BEP_0044.
Check the Examples.
Running as a client, means you can store and query for values on the DHT, but not accept any incoming requests.
use n0_mainline::{Dht, Mode};
let dht = Dht::client().await.unwrap();Supported BEPs:
- BEP_0005 DHT Protocol
- BEP_0042 DHT Security extension
- BEP_0043 Read-only DHT Nodes
- BEP_0044 Storing arbitrary data in the DHT
This implementation also includes measures against Vertical Sybil Attacks.
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 n0_mainline::{Dht, Mode};
let dht = Dht::builder().mode(Mode::Server).build()?; // or Dht::server()?Supported BEPs:
- BEP_0005 DHT Protocol
- BEP_0042 DHT Security extension
- BEP_0043 Read-only DHT Nodes
- BEP_0044 Storing arbitrary data in the DHT
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.
Nodes start in client mode and do not automatically switch to server mode.
If you know your node is publicly reachable and will be long running, enable server mode
with Dht::builder().mode(Mode::Server).build().
You can also provide a known public IPv4 address with .public_ip(ip) to derive the node ID
from that address.
Reported public addresses are observations from other nodes, not proof of inbound reachability.
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.