torrentdht 0.1.1

DHT discovery with mTLS handshake handoff; optional BitTorrent when the host opts in
Documentation
use std::net::{SocketAddr, ToSocketAddrs};

use crate::config::BootstrapNode;

pub const BOOTSTRAP_NODES: &[(&str, u16)] = &[
    ("router.bittorrent.com", 6881),
    ("router.utorrent.com", 6881),
    ("dht.transmissionbt.com", 6881),
];

/// Resolve bootstrap hosts to socket addrs. Empty `nodes` uses the public Mainline defaults.
pub fn resolve_bootstrap(nodes: &[BootstrapNode]) -> Vec<SocketAddr> {
    let list: Vec<BootstrapNode> = if nodes.is_empty() {
        BOOTSTRAP_NODES
            .iter()
            .map(|(host, port)| BootstrapNode {
                host: (*host).to_string(),
                port: *port,
            })
            .collect()
    } else {
        nodes.to_vec()
    };
    let mut out = Vec::new();
    for node in list {
        if let Ok(addrs) = format!("{}:{}", node.host, node.port).to_socket_addrs() {
            out.extend(addrs);
        }
    }
    out
}

/// Resolve the built-in public Mainline routers.
pub fn bootstrap_addrs() -> Vec<SocketAddr> {
    resolve_bootstrap(&[])
}