main.rs 3.3 KB

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