main.rs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 log::info;
  21. use smol::Executor;
  22. use structopt_toml::{serde::Deserialize, structopt::StructOpt, StructOptToml};
  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 daemon = Damd::init(&args.net.into(), &ex).await?;
  54. daemon.start(&ex, &args.rpc.into()).await?;
  55. // Signal handling for graceful termination.
  56. let (signals_handler, signals_task) = SignalHandler::new(ex)?;
  57. signals_handler.wait_termination(signals_task).await?;
  58. info!(target: "damd", "Caught termination signal, cleaning up and exiting");
  59. daemon.stop().await?;
  60. info!(target: "damd", "Shut down successfully");
  61. Ok(())
  62. }