net.rs 3.2 KB

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