main.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 async_std::sync::{Arc, Mutex};
  19. use log::{info, warn};
  20. use rand::rngs::OsRng;
  21. use structopt_toml::StructOptToml;
  22. use darkfi::{
  23. async_daemonize, net,
  24. rpc::server::listen_and_serve,
  25. system::Subscriber,
  26. util::{file::save_json_file, path::expand_path},
  27. Result,
  28. };
  29. pub mod crypto;
  30. pub mod events_queue;
  31. pub mod irc;
  32. pub mod model;
  33. pub mod privmsg;
  34. pub mod protocol_event;
  35. pub mod rpc;
  36. pub mod settings;
  37. pub mod view;
  38. use crate::{
  39. crypto::KeyPair,
  40. events_queue::EventsQueue,
  41. irc::IrcServer,
  42. model::Model,
  43. protocol_event::{ProtocolEvent, Seen, UnreadEvents},
  44. rpc::JsonRpcInterface,
  45. settings::{Args, ChannelInfo, CONFIG_FILE, CONFIG_FILE_CONTENTS},
  46. view::View,
  47. };
  48. async_daemonize!(realmain);
  49. async fn realmain(settings: Args, executor: Arc<smol::Executor<'_>>) -> Result<()> {
  50. ////////////////////
  51. // Generate new keypair and exit
  52. ////////////////////
  53. if settings.gen_keypair {
  54. let secret_key = crypto_box::SecretKey::generate(&mut OsRng);
  55. let pub_key = secret_key.public_key();
  56. let prv_encoded = bs58::encode(secret_key.as_bytes()).into_string();
  57. let pub_encoded = bs58::encode(pub_key.as_bytes()).into_string();
  58. let kp = KeyPair { private_key: prv_encoded, public_key: pub_encoded };
  59. if settings.output.is_some() {
  60. let datastore = expand_path(&settings.output.unwrap())?;
  61. save_json_file(&datastore, &kp)?;
  62. } else {
  63. println!("Generated KeyPair:\n{}", kp);
  64. }
  65. return Ok(())
  66. }
  67. ////////////////////
  68. // Initialize the base structures
  69. ////////////////////
  70. let events_queue = EventsQueue::new();
  71. let model = Arc::new(Mutex::new(Model::new(events_queue.clone())));
  72. let _view = Arc::new(Mutex::new(View::new(events_queue)));
  73. ////////////////////
  74. // P2p setup
  75. ////////////////////
  76. // Buffers
  77. let seen_event = Seen::new();
  78. let seen_inv = Seen::new();
  79. let unread_events = UnreadEvents::new();
  80. // Check the version
  81. let mut net_settings = settings.net.clone();
  82. net_settings.app_version = Some(option_env!("CARGO_PKG_VERSION").unwrap_or("").to_string());
  83. // New p2p
  84. let p2p = net::P2p::new(net_settings.into()).await;
  85. let p2p2 = p2p.clone();
  86. // Register the protocol_event
  87. let registry = p2p.protocol_registry();
  88. registry
  89. .register(net::SESSION_ALL, move |channel, p2p| {
  90. let seen_event = seen_event.clone();
  91. let seen_inv = seen_inv.clone();
  92. let model = model.clone();
  93. let unread_events = unread_events.clone();
  94. async move {
  95. ProtocolEvent::init(channel, p2p, model, seen_event, seen_inv, unread_events).await
  96. }
  97. })
  98. .await;
  99. // Start
  100. p2p.clone().start(executor.clone()).await?;
  101. // Run
  102. let executor_cloned = executor.clone();
  103. executor_cloned.spawn(p2p.clone().run(executor.clone())).detach();
  104. ////////////////////
  105. // RPC interface setup
  106. ////////////////////
  107. let rpc_listen_addr = settings.rpc_listen.clone();
  108. let rpc_interface =
  109. Arc::new(JsonRpcInterface { addr: rpc_listen_addr.clone(), p2p: p2p.clone() });
  110. let _ex = executor.clone();
  111. executor
  112. .spawn(async move { listen_and_serve(rpc_listen_addr, rpc_interface, _ex).await })
  113. .detach();
  114. ////////////////////
  115. // IRC server
  116. ////////////////////
  117. let clients_subscriptions = Subscriber::new();
  118. // New irc server
  119. let irc_server = IrcServer::new(settings.clone(), clients_subscriptions).await?;
  120. // Start the irc server and detach it
  121. let executor_cloned = executor.clone();
  122. executor_cloned.spawn(async move { irc_server.start(executor.clone()).await }).detach();
  123. ////////////////////
  124. // Wait for SIGINT
  125. ////////////////////
  126. let (signal, shutdown) = smol::channel::bounded::<()>(1);
  127. ctrlc::set_handler(move || {
  128. warn!(target: "ircd", "ircd start Exit Signal");
  129. // cleaning up tasks running in the background
  130. async_std::task::block_on(signal.send(())).unwrap();
  131. })
  132. .unwrap();
  133. shutdown.recv().await?;
  134. print!("\r");
  135. info!("Caught termination signal, cleaning up and exiting...");
  136. // stop p2p
  137. p2p2.stop().await;
  138. Ok(())
  139. }