diff --git a/Cargo.lock b/Cargo.lock index aeff184c..d905d9aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -150,7 +150,7 @@ dependencies = [ "miniz_oxide", "object", "rustc-demangle", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -395,7 +395,7 @@ dependencies = [ "js-sys", "num-traits", "wasm-bindgen", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -729,6 +729,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "dns-lookup" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5766087c2235fec47fafa4cfecc81e494ee679d0fd4a59887ea0919bfb0e4fc" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "socket2 0.5.7", + "windows-sys 0.48.0", +] + [[package]] name = "either" version = "1.13.0" @@ -903,7 +915,7 @@ name = "floresta-wire" version = "0.2.0" dependencies = [ "bitcoin 0.32.4", - "dns-lookup", + "dns-lookup 1.0.8", "floresta-chain", "floresta-common", "floresta-compact-filters", @@ -934,6 +946,7 @@ dependencies = [ "ctrlc", "daemonize", "dirs", + "dns-lookup 2.0.4", "fern", "floresta-chain", "floresta-common", @@ -1744,7 +1757,7 @@ dependencies = [ "libc", "redox_syscall 0.5.7", "smallvec", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -2809,7 +2822,16 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", ] [[package]] @@ -2818,7 +2840,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -2827,7 +2849,22 @@ version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -2836,28 +2873,46 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_gnu" version = "0.52.6" @@ -2870,24 +2925,48 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.6" diff --git a/florestad/Cargo.toml b/florestad/Cargo.toml index 7cf36f97..5721442d 100644 --- a/florestad/Cargo.toml +++ b/florestad/Cargo.toml @@ -37,6 +37,7 @@ jsonrpc-core-client = { version = "18.0.0", features = [ "http", ], optional = true } zmq = { version = "0.10.0", optional = true } +dns-lookup = "2.0.4" [target.'cfg(unix)'.dependencies] daemonize = { version = "0.5.0" } diff --git a/florestad/src/florestad.rs b/florestad/src/florestad.rs index 7b3790e7..74d32053 100644 --- a/florestad/src/florestad.rs +++ b/florestad/src/florestad.rs @@ -3,9 +3,9 @@ use std::fmt::Arguments; use std::fs::File; use std::io; use std::io::BufReader; +use std::net::SocketAddr; use std::path::PathBuf; use std::process::exit; -use std::str::FromStr; use std::sync::Arc; use std::sync::Mutex; #[cfg(feature = "json-rpc")] @@ -209,6 +209,47 @@ impl Florestad { } } + fn get_ip_address(hostname: &str, default_port: u16) -> SocketAddr { + if !hostname.contains(':') { + let Ok(ip) = hostname.parse() else { + error!("Invalid IP address: {hostname}"); + exit(1); + }; + + return SocketAddr::new(ip, default_port); + } + + let ip = hostname.parse(); + match ip { + Ok(ip) => ip, + Err(_) => { + let mut split = hostname.split(':'); + let hostname = split.next().unwrap(); + + debug!("Resolving hostname: {hostname}"); + + let ips: Vec<_> = match dns_lookup::lookup_host(hostname) { + Ok(ips) => ips, + Err(e) => { + error!("Could not resolve hostname: {e}"); + exit(1); + } + }; + + if ips.is_empty() { + error!("No IP addresses found for hostname: {}", hostname); + exit(1); + } + + let port = split + .next() + .map(|x| x.parse().unwrap_or(default_port)) + .unwrap_or(default_port); + SocketAddr::new(ips[0], port) + } + } + } + /// Actually runs florestad, spawning all modules and waiting util /// someone asks to stop. pub fn start(&self) { @@ -304,26 +345,16 @@ impl Florestad { } else { None }; + #[cfg(not(feature = "compact-filters"))] let cfilters = None; // Handle the `-connect` cli option - let connect = match self + let connect = self .config .clone() .connect - .map(|host| LocalAddress::from_str(&host)) - { - Some(Ok(host)) => { - debug!("Connecting to {:?}", host); - Some(host) - } - Some(Err(e)) => { - error!("Invalid host: {}", e); - exit(-1); - } - None => None, - }; + .map(|host| host.parse::().unwrap()); // For now we only have compatible bridges on signet let pow_fraud_proofs = match self.config.network { @@ -348,7 +379,7 @@ impl Florestad { .config .proxy .as_ref() - .map(|address| address.parse().expect("Invalid address")), + .map(|host| Self::get_ip_address(host, 9050)), datadir: data_dir.clone(), fixed_peer: connect, max_banscore: 50, @@ -405,7 +436,7 @@ impl Florestad { self.config .json_rpc_address .as_ref() - .map(|x| x.parse().expect("Invalid json rpc address")), + .map(|x| Self::get_ip_address(x, 8332)), runtime_handle, ); @@ -419,13 +450,15 @@ impl Florestad { .config .electrum_address .clone() - .unwrap_or("0.0.0.0:50001".into()); + .map(|addr| Self::get_ip_address(&addr, 50001)) + .unwrap_or("0.0.0.0:50001".parse().expect("hardcoded address")); let ssl_e_addr = self .config .ssl_electrum_address .clone() - .unwrap_or("0.0.0.0:50002".into()); + .map(|addr| Self::get_ip_address(&addr, 50002)) + .unwrap_or("0.0.0.0:50002".parse().expect("hardcoded address")); // Load TLS configuration if needed let tls_config = if !self.config.no_ssl { @@ -454,9 +487,10 @@ impl Florestad { // Non-TLS Electrum accept loop let non_tls_listener = Arc::new( - block_on(TcpListener::bind(e_addr.clone())) + block_on(TcpListener::bind(e_addr)) .unwrap_or_else(|e| panic!("Cannot bind to electrum address {}: {}", e_addr, e)), ); + task::spawn(client_accept_loop( non_tls_listener, electrum_server.message_transmitter.clone(), @@ -466,7 +500,7 @@ impl Florestad { // TLS Electrum accept loop if let Some(tls_acceptor) = tls_acceptor { let tls_listener = Arc::new( - block_on(TcpListener::bind(ssl_e_addr.clone())).unwrap_or_else(|e| { + block_on(TcpListener::bind(ssl_e_addr)).unwrap_or_else(|e| { panic!("Cannot bind to ssl electrum address {}: {}", ssl_e_addr, e) }), ); @@ -482,7 +516,7 @@ impl Florestad { info!("Server running on: {}", e_addr); if !self.config.no_ssl { - info!("TLS server running on: 0.0.0.0:50002"); + info!("TLS server running on: {ssl_e_addr}"); } // Chain provider