main.rs 5.4 KB

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