net.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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;
  20. use simplelog::*;
  21. use smol::Executor;
  22. use darkfi::{
  23. net,
  24. util::cli::{get_log_config, get_log_level},
  25. Result,
  26. };
  27. async fn start(executor: Arc<Executor<'_>>, options: ProgramOptions) -> Result<()> {
  28. let p2p = net::P2p::new(options.network_settings).await;
  29. p2p.clone().start(executor.clone()).await?;
  30. p2p.run(executor).await?;
  31. Ok(())
  32. }
  33. struct ProgramOptions {
  34. network_settings: net::Settings,
  35. }
  36. #[derive(Parser)]
  37. #[clap(name = "dnode")]
  38. pub struct DarkCli {
  39. /// accept address
  40. #[clap(short, long)]
  41. pub accept: Option<String>,
  42. /// seed nodes
  43. #[clap(long, short)]
  44. pub seeds: Option<Vec<String>>,
  45. /// manual connections
  46. #[clap(short, short)]
  47. pub connect: Option<Vec<String>>,
  48. /// connections slots
  49. #[clap(long)]
  50. pub connect_slots: Option<usize>,
  51. /// RPC port
  52. #[clap(long)]
  53. pub rpc_port: Option<String>,
  54. }
  55. impl ProgramOptions {
  56. fn load() -> Result<ProgramOptions> {
  57. let programcli = DarkCli::parse();
  58. let accept_addr = if let Some(accept_addr) = programcli.accept {
  59. vec![accept_addr.parse()?]
  60. } else {
  61. vec![]
  62. };
  63. let mut seed_addrs: Vec<url::Url> = vec![];
  64. if let Some(seeds) = programcli.seeds {
  65. for seed in seeds {
  66. seed_addrs.push(seed.parse()?);
  67. }
  68. }
  69. let mut manual_connects: Vec<url::Url> = vec![];
  70. if let Some(connections) = programcli.connect {
  71. for connect in connections {
  72. manual_connects.push(connect.parse()?);
  73. }
  74. }
  75. let connection_slots = if let Some(connection_slots) = programcli.connect_slots {
  76. connection_slots
  77. } else {
  78. 0
  79. };
  80. Ok(ProgramOptions {
  81. network_settings: net::Settings {
  82. inbound_addrs: accept_addr.clone(),
  83. outbound_connections: connection_slots,
  84. external_addrs: accept_addr,
  85. peers: manual_connects,
  86. seeds: seed_addrs,
  87. ..Default::default()
  88. },
  89. })
  90. }
  91. }
  92. fn main() -> Result<()> {
  93. let options = ProgramOptions::load()?;
  94. let lvl = get_log_level(1);
  95. let conf = get_log_config(1);
  96. TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
  97. let ex = Arc::new(Executor::new());
  98. smol::block_on(ex.run(start(ex.clone(), options)))
  99. }