diff options
| author | tv <tv@krebsco.de> | 2026-10-30 15:36:47 +0100 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-10-30 15:36:47 +0100 |
| commit | 54a8d7b3409f6a5857b6fc9a964a36b66c28ae25 (patch) | |
| tree | d9e8faca35c98b21b13030e845bbeaec3cd43298 /src/main.rs | |
| parent | bff42bf27f58bacb8bce58458dea7da74cda2c05 (diff) | |
refactor transports to start themselves
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 102 |
1 files changed, 4 insertions, 98 deletions
diff --git a/src/main.rs b/src/main.rs index 7481f38..1908775 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,6 @@ use api::auth::AuthState; use document::replica::Epoch; use swarm::bootstrap; use transport::Transport; -use transport::mycelium::{self, MyceliumSetup, MyceliumSpawnConfig}; #[tokio::main] async fn main() -> Result<()> { @@ -78,34 +77,11 @@ async fn main() -> Result<()> { }; // First thing: until a handler is installed, SIGINT and SIGTERM kill the process on - // the spot, and startup (spawning Mycelium, bootstrap) can take minutes. + // the spot, and startup (starting transports, bootstrap) can take minutes. let stop = watch_for_shutdown().context("Installing signal handlers")?; // A broken htpasswd will abort startup. let auth_state = AuthState::new(args.htpasswd.as_deref().context("htpasswd is required")?)?; - let uses = |name: &str| args.transports.iter().any(|transport| transport == name); - let mut mycelium_setup = if uses("mycelium") { - let spawn_config = MyceliumSpawnConfig { - rest_url_opt: args.mycelium_rest_url(), - key_file_opt: args.mycelium_key_file.clone(), - network_key_file: args.mycelium_network_key_file.clone(), - network_name: args.mycelium_network_name.clone(), - tcp_port: args.mycelium_tcp_listen_port, - peers: args.mycelium_peers.clone(), - }; - let setup = tokio::select! { - setup = MyceliumSetup::bootstrap(spawn_config) => setup?, - _ = stopped(stop.clone()) => return Ok(()), - }; - // Without a password it may carry (the option is secret). - let mut shown_url = setup.rest_url.clone(); - let _ = shown_url.set_password(None); - debug!(url = shown_url.to_string(), "Using Mycelium REST API"); - Some(setup) - } else { - None - }; - info!( bind_addr = args.bind_addr.to_string(), transports = ?args.transports, @@ -182,43 +158,8 @@ async fn main() -> Result<()> { // In the order of preference (`--transports`, checked when loading). let mut transports: Vec<(&'static str, Arc<dyn Transport>)> = Vec::new(); for name in &args.transports { - let transport: (&'static str, Arc<dyn Transport>) = match name.as_str() { - "mycelium" => { - let setup = mycelium_setup - .take() - .context("Mycelium named twice in transports")?; - ( - transport::mycelium::SCHEME, - Arc::new(mycelium::MyceliumTransport::connect(setup).await?), - ) - } - #[cfg(feature = "libp2p")] - "libp2p" => ( - transport::libp2p::SCHEME, - Arc::new(transport::libp2p::Libp2pTransport::start(libp2p_config( - args, false, - )?)?), - ), - #[cfg(feature = "tor")] - "tor" => { - let state_dir = args - .tor_state_dir - .clone() - .context("tor needs tor-state-dir")?; - let transport = tokio::select! { - transport = transport::tor::TorTransport::start(transport::tor::TorConfig { - key: Arc::clone(&node_key), - state_dir, - port: args.tor_port, - }) => transport?, - _ = stopped(stop.clone()) => return Ok(()), - }; - ( - transport::tor::SCHEME, - Arc::new(transport) as Arc<dyn Transport>, - ) - } - other => anyhow::bail!("transports: no {other} in this build"), + let Some(transport) = transport::start(name, args, &node_key, stop.clone()).await? else { + return Ok(()); }; info!( transport = transport.0, @@ -368,41 +309,6 @@ async fn main() -> Result<()> { Ok(()) } -/// The libp2p transport's configuration; `relay_only`: a relay that is no node. -#[cfg(feature = "libp2p")] -fn libp2p_config( - args: &config::Settings, - relay_only: bool, -) -> Result<transport::libp2p::Libp2pConfig> { - let parse = |option: &str, addresses: &[String]| { - addresses - .iter() - .map(|address| { - address - .parse::<libp2p::Multiaddr>() - .with_context(|| format!("{option} {address:?}")) - }) - .collect::<Result<Vec<_>>>() - }; - Ok(transport::libp2p::Libp2pConfig { - key: transport::libp2p::Libp2pConfig::load_key(args.libp2p_key_file.as_deref())?, - listen: parse("libp2p-listen", &args.libp2p_listen)?, - announce: parse("libp2p-announce", &args.libp2p_announce)?, - relays: parse("libp2p-relays", &args.libp2p_relays)?, - relay: args.libp2p_relay || relay_only, - continuum: !relay_only, - mdns: args.libp2p_mdns, - upnp: args.libp2p_upnp, - ignore: args - .libp2p_ignore - .iter() - .map(|range| range.parse().context("libp2p-ignore")) - .collect::<Result<Vec<_>>>()?, - public: args.libp2p_public, - memory: false, - }) -} - /// `--relay-only`: libp2p relaying for nodes of swarms, and the HTTP API's relay part /// (with `--htpasswd`), until stopped. #[cfg(feature = "libp2p")] @@ -410,7 +316,7 @@ async fn relay_only(args: &config::Settings) -> Result<()> { let stop = watch_for_shutdown().context("Installing signal handlers")?; let auth_state = args.htpasswd.as_deref().map(AuthState::new).transpose()?; let transport: Arc<dyn Transport> = Arc::new(transport::libp2p::Libp2pTransport::start( - libp2p_config(args, true)?, + transport::libp2p::Libp2pConfig::from_settings(args, true)?, )?); info!(peer_id = transport.local_id(), "Relaying only (no node)"); |
