main.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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 clap::{Parser, Subcommand};
  20. use darkfi::{cli_desc, rpc::util::JsonValue, util::logger::setup_logging, Result};
  21. use smol::Executor;
  22. use dam_cli::DamCli;
  23. #[derive(Parser)]
  24. #[command(about = cli_desc!())]
  25. struct Args {
  26. #[arg(short, long, default_value = "tcp://127.0.0.1:34780")]
  27. /// damd JSON-RPC endpoint
  28. endpoint: String,
  29. #[command(subcommand)]
  30. /// Sub command to execute
  31. command: Subcmd,
  32. }
  33. #[derive(Subcommand)]
  34. enum Subcmd {
  35. /// Send a ping request to the damd RPC endpoint
  36. Ping,
  37. /// This subscription will listen for incoming notifications from damd
  38. Subscribe {
  39. /// The method to subscribe to
  40. method: String,
  41. },
  42. /// Signal damd to execute a flooding attack against the network
  43. Flood {
  44. /// Optional flood messages count limit
  45. limit: Option<u32>,
  46. },
  47. /// Signal damd to stop an ongoing flooding attack
  48. StopFlood,
  49. }
  50. fn main() -> Result<()> {
  51. // Setup terminal logger. verbosity level 0 == Level::Info
  52. setup_logging(0, None)?;
  53. // Initialize an executor
  54. let executor = Arc::new(Executor::new());
  55. let ex = executor.clone();
  56. smol::block_on(executor.run(async {
  57. // Parse arguments
  58. let args = Args::parse();
  59. // Execute a subcommand
  60. let dam_cli = DamCli::new(&args.endpoint, &ex).await?;
  61. match args.command {
  62. Subcmd::Ping => {
  63. dam_cli.ping().await?;
  64. }
  65. Subcmd::Subscribe { method } => {
  66. dam_cli.subscribe(&args.endpoint, &method, &ex).await?;
  67. }
  68. Subcmd::Flood { limit } => {
  69. let limit = match limit {
  70. Some(l) => JsonValue::String(format!("{l}")),
  71. None => JsonValue::String(String::from("0")),
  72. };
  73. dam_cli
  74. .damd_daemon_request(
  75. "flood.switch",
  76. &JsonValue::Array(vec![JsonValue::Boolean(true), limit]),
  77. )
  78. .await?;
  79. }
  80. Subcmd::StopFlood => {
  81. dam_cli
  82. .damd_daemon_request(
  83. "flood.switch",
  84. &JsonValue::Array(vec![
  85. JsonValue::Boolean(false),
  86. JsonValue::String(String::from("0")),
  87. ]),
  88. )
  89. .await?;
  90. }
  91. }
  92. dam_cli.rpc_client.stop().await;
  93. Ok(())
  94. }))
  95. }