main.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::sync::Arc;
  19. use async_std::prelude::StreamExt;
  20. use smol::Executor;
  21. use structopt_toml::{serde::Deserialize, structopt::StructOpt, StructOptToml};
  22. use tracing::info;
  23. use darkfi::{
  24. async_daemonize, cli_desc, net::settings::SettingsOpt, rpc::settings::RpcSettingsOpt, Error,
  25. Result,
  26. };
  27. use damd::Damd;
  28. const CONFIG_FILE: &str = "damd_config.toml";
  29. const CONFIG_FILE_CONTENTS: &str = include_str!("../damd_config.toml");
  30. #[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
  31. #[serde(default)]
  32. #[structopt(name = "damd", about = cli_desc!())]
  33. struct Args {
  34. #[structopt(short, long)]
  35. /// Configuration file to use
  36. config: Option<String>,
  37. #[structopt(flatten)]
  38. /// JSON-RPC settings
  39. rpc: RpcSettingsOpt,
  40. #[structopt(flatten)]
  41. /// P2P network settings
  42. net: SettingsOpt,
  43. #[structopt(short, long)]
  44. /// Set log file to ouput into
  45. log: Option<String>,
  46. #[structopt(short, parse(from_occurrences))]
  47. /// Increase verbosity (-vvv supported)
  48. verbose: u8,
  49. }
  50. async_daemonize!(realmain);
  51. async fn realmain(args: Args, ex: Arc<Executor<'static>>) -> Result<()> {
  52. info!(target: "damd", "Starting Denial-of-service Analysis Multitool daemon...");
  53. let net_settings: darkfi::net::Settings =
  54. (env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"), args.net).try_into()?;
  55. let daemon = Damd::init(&net_settings, &ex).await?;
  56. daemon.start(&ex, &args.rpc.into()).await?;
  57. // Signal handling for graceful termination.
  58. let (signals_handler, signals_task) = SignalHandler::new(ex)?;
  59. signals_handler.wait_termination(signals_task).await?;
  60. info!(target: "damd", "Caught termination signal, cleaning up and exiting");
  61. daemon.stop().await?;
  62. info!(target: "damd", "Shut down successfully");
  63. Ok(())
  64. }