main.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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, Result,
  25. };
  26. use damd::Damd;
  27. const CONFIG_FILE: &str = "damd_config.toml";
  28. const CONFIG_FILE_CONTENTS: &str = include_str!("../damd_config.toml");
  29. #[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
  30. #[serde(default)]
  31. #[structopt(name = "damd", about = cli_desc!())]
  32. struct Args {
  33. #[structopt(short, long)]
  34. /// Configuration file to use
  35. config: Option<String>,
  36. #[structopt(flatten)]
  37. /// JSON-RPC settings
  38. rpc: RpcSettingsOpt,
  39. #[structopt(flatten)]
  40. /// P2P network settings
  41. net: SettingsOpt,
  42. #[structopt(short, long)]
  43. /// Set log file to ouput into
  44. log: Option<String>,
  45. #[structopt(short, parse(from_occurrences))]
  46. /// Increase verbosity (-vvv supported)
  47. verbose: u8,
  48. }
  49. async_daemonize!(realmain);
  50. async fn realmain(args: Args, ex: Arc<Executor<'static>>) -> Result<()> {
  51. info!(target: "damd", "Starting Denial-of-service Analysis Multitool daemon...");
  52. let daemon = Damd::init(&args.net.into(), &ex).await?;
  53. daemon.start(&ex, &args.rpc.into()).await?;
  54. // Signal handling for graceful termination.
  55. let (signals_handler, signals_task) = SignalHandler::new(ex)?;
  56. signals_handler.wait_termination(signals_task).await?;
  57. info!(target: "damd", "Caught termination signal, cleaning up and exiting");
  58. daemon.stop().await?;
  59. info!(target: "damd", "Shut down successfully");
  60. Ok(())
  61. }