From f3c0199e4c59e357dccbc289c687155905fc28e4 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Tue, 5 Dec 2017 23:18:19 -0800 Subject: basic centralized DNS discovery --- Cargo.lock | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/bin/geniza-net.rs | 17 +++++++++++++++++ src/discovery.rs | 39 ++++++++++++++++++++++++++++++++++++++ src/lib.rs | 5 +++++ 5 files changed, 115 insertions(+) create mode 100644 src/discovery.rs diff --git a/Cargo.lock b/Cargo.lock index 44d6b74..d859da1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,12 +5,14 @@ dependencies = [ "assert_cli 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)", "bit_field 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.27.0 (registry+https://github.com/rust-lang/crates.io-index)", + "data-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "integer-encoding 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "protobuf 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", + "resolve 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", "sodiumoxide 0.0.16 (git+https://github.com/bnewbold/sodiumoxide-patched?branch=stream_xor_ic)", "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -140,6 +142,11 @@ dependencies = [ "lazy_static 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "data-encoding" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "dbghelp-sys" version = "0.2.0" @@ -215,6 +222,16 @@ name = "glob" version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "idna" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-normalization 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "integer-encoding" version = "1.0.4" @@ -258,6 +275,11 @@ name = "log" version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "matches" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "memchr" version = "0.1.11" @@ -333,6 +355,17 @@ name = "regex-syntax" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "resolve" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "idna 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rust-crypto" version = "0.2.36" @@ -515,6 +548,19 @@ dependencies = [ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "unicode-bidi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-normalization" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unicode-width" version = "0.1.4" @@ -571,6 +617,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c819a1287eb618df47cc647173c5c4c66ba19d888a6e50d605672aed3140de" "checksum clap 2.27.0 (registry+https://github.com/rust-lang/crates.io-index)" = "260aa209e1e7165920b6925d508cd73674a4adeb4086197385968c70ecd14947" "checksum colored 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b0aa3473e85a3161b59845d6096b289bb577874cafeaf75ea1b1beaa6572c7fc" +"checksum data-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "099d2591f809713931cd770f2bdf4b8a4d2eb7314bc762da4c375ecaa74af80f" "checksum dbghelp-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "97590ba53bcb8ac28279161ca943a924d1fd4a8fb3fa63302591647c4fc5b850" "checksum difference 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b3304d19798a8e067e48d8e69b2c37f0b5e9b4e462504ad9e27e9f3fce02bba8" "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" @@ -582,6 +629,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum fuchsia-zircon-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "43f3795b4bae048dc6123a6b972cadde2e676f9ded08aef6bb77f5f157684a82" "checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb" "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" +"checksum idna 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "014b298351066f1512874135335d62a789ffe78a9974f94b43ed5621951eaf7d" "checksum integer-encoding 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "eb89832a57e4732a55a7753a4818f0d67c8a8822fa152475b169f57a264c557c" "checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" @@ -589,6 +637,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum libc 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)" = "56cce3130fd040c28df6f495c8492e5ec5808fb4c9093c310df02b0c8f030148" "checksum libsodium-sys 0.0.16 (git+https://github.com/bnewbold/sodiumoxide-patched?branch=stream_xor_ic)" = "" "checksum log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "880f77541efa6e5cc74e76910c9884d9859683118839d6a1dc3b11e63512565b" +"checksum matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "100aabe6b8ff4e4a7e32c1c13523379802df0772b82466207ac25b013f193376" "checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20" "checksum num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "99843c856d68d8b4313b03a17e33c4bb42ae8f6610ea81b28abe076ac721b9b0" "checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903" @@ -600,6 +649,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" "checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f" "checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957" +"checksum resolve 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a40df559bf6f525f5543572ca8c2a49c96c3ca0debfcb00c499dce9bf4ff548" "checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a" "checksum rustc-demangle 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "aee45432acc62f7b9a108cc054142dac51f979e69e71ddce7d6fc7adf29e817e" "checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" @@ -621,6 +671,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03" "checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5" "checksum time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)" = "d5d788d3aa77bc0ef3e9621256885555368b47bd495c13dd2e7413c89f845520" +"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" +"checksum unicode-normalization 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "51ccda9ef9efa3f7ef5d91e8f9b83bbe6955f9bf86aec89d5cce2c874625920f" "checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f" "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" "checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f" diff --git a/Cargo.toml b/Cargo.toml index 9b53408..904a794 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,8 @@ protobuf = "1.3" log = "0.3" sodiumoxide = "0.0.16" bit_field = "0.9" +resolve = "0.1.2" +data-encoding = "2.0" [dev-dependencies] tempdir = "0.3" diff --git a/src/bin/geniza-net.rs b/src/bin/geniza-net.rs index dd80c4f..e6fa448 100644 --- a/src/bin/geniza-net.rs +++ b/src/bin/geniza-net.rs @@ -55,6 +55,11 @@ fn run() -> Result<()> { .about("Prints the DNS name to query (mDNS or centrally) for peers") .arg_from_usage(" 'dat key (public key) to convert (in hex)'"), ) + .subcommand( + SubCommand::with_name("discover-dns") + .about("Does a centralized DNS lookup for peers with the given key") + .arg_from_usage(" 'dat key (public key) to lookup"), + ) .subcommand( SubCommand::with_name("naive-clone") .about("Pulls a drive from a single (known) peer, using a naive algorithm") @@ -126,6 +131,18 @@ fn run() -> Result<()> { } println!(".dat.local"); } + ("discover-dns", Some(subm)) => { + let dat_key = subm.value_of("dat_key").unwrap(); + let key_bytes = parse_dat_key(&dat_key)?; + let peers = discover_peers_dns(&key_bytes)?; + if peers.len() == 0 { + println!("No peers found!"); + } else { + for p in peers { + println!("{}", p); + } + } + } ("naive-clone", Some(subm)) => { let host_port = subm.value_of("host_port").unwrap(); let dat_key = subm.value_of("dat_key").unwrap(); diff --git a/src/discovery.rs b/src/discovery.rs new file mode 100644 index 0000000..aef2a60 --- /dev/null +++ b/src/discovery.rs @@ -0,0 +1,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> { + + 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 = resolve_host("discovery1.publicbits.org")?.collect(); + let dns2: Vec = 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 = resolver.resolve_record(&dk_name)?; + // target (IP addresses) are returned with a trailing period that must be stripped + let peers: Vec = peers.into_iter().map(|r| format!("{}:{}", &r.target[0..(r.target.len()-1)], r.port).parse().unwrap()).collect(); + info!("found peers: {:?}", peers); + Ok(peers) +} diff --git a/src/lib.rs b/src/lib.rs index 10139d3..bc62d78 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,8 @@ extern crate protobuf; extern crate rand; extern crate sodiumoxide; extern crate bit_field; +extern crate resolve; +extern crate data_encoding; #[cfg(test)] extern crate tempdir; @@ -35,6 +37,7 @@ mod errors { error_chain! { foreign_links { Fmt(::std::fmt::Error); Io(::std::io::Error) #[cfg(unix)]; + AddrParseError(::std::net::AddrParseError); Protobuf(::protobuf::ProtobufError); } } } @@ -55,6 +58,8 @@ pub mod network_msgs; pub mod metadata_msgs; mod node; pub use node::*; +mod discovery; +pub use discovery::*; // Shared functions use crypto::digest::Digest; -- cgit v1.2.3