aboutsummaryrefslogtreecommitdiffstats
path: root/src/discovery.rs
blob: aef2a609bfa86a13bb3ebd0183c40b435306e699 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

use errors::*;
use std::net::{IpAddr, SocketAddr};
use make_discovery_key;
use data_encoding::HEXLOWER;
use resolve::{self, DnsConfig, DnsResolver, resolve_host};
use resolve::record::Srv;

pub fn discover_peers_dns(dat_key: &[u8]) -> Result<Vec<SocketAddr>> {

    let dk = make_discovery_key(dat_key);
    let dk_hex = HEXLOWER.encode(&dk);
    let dk_name = format!("{}.dat.local", &dk_hex[0..40]);
    info!("discovering peers using DNS: {}", dk_name);

    let dns1: Vec<IpAddr> = resolve_host("discovery1.publicbits.org")?.collect();
    let dns2: Vec<IpAddr> = resolve_host("discovery2.publicbits.org")?.collect();

    let default_config = resolve::default_config()?;
    let config = DnsConfig {
        name_servers: vec![
            SocketAddr::from((dns1[0], 53)),
            SocketAddr::from((dns2[0], 53))],
        search: vec!["dat.local".to_string()],
        n_dots: default_config.n_dots,
        timeout: default_config.timeout,
        attempts: default_config.attempts,
        rotate: true,
        use_inet6: false,
    };

    let resolver = DnsResolver::new(config)?;

    let peers: Vec<Srv> = resolver.resolve_record(&dk_name)?;
    // target (IP addresses) are returned with a trailing period that must be stripped
    let peers: Vec<SocketAddr> = peers.into_iter().map(|r| format!("{}:{}", &r.target[0..(r.target.len()-1)], r.port).parse().unwrap()).collect();
    info!("found peers: {:?}", peers);
    Ok(peers)
}