Skip to content

Commit

Permalink
feat: allow hostnames where we take addresses
Browse files Browse the repository at this point in the history
Now you can pass a hostname (i.e.: localhost, node, vps) for addresses
in configs like `rpc-address` and `electrum-address`. The format is
<hostname>[:port]
  • Loading branch information
Davidson-Souza committed Nov 21, 2024
1 parent 3a2ce7a commit aa260fe
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 35 deletions.
107 changes: 93 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions florestad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
76 changes: 55 additions & 21 deletions florestad/src/florestad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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::<LocalAddress>().unwrap());

// For now we only have compatible bridges on signet
let pow_fraud_proofs = match self.config.network {
Expand All @@ -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,
Expand Down Expand Up @@ -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,
);

Expand All @@ -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 {
Expand Down Expand Up @@ -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(),
Expand All @@ -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)
}),
);
Expand All @@ -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
Expand Down

0 comments on commit aa260fe

Please sign in to comment.