main.rs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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::{collections::HashSet, io::Write, path::PathBuf, sync::Arc};
  19. use darkfi::{
  20. async_daemonize, cli_desc,
  21. event_graph::{proto::ProtocolEventGraph, EventGraph, EventGraphPtr},
  22. net::{session::SESSION_DEFAULT, settings::SettingsOpt, P2p, P2pPtr},
  23. rpc::{
  24. jsonrpc::JsonSubscriber,
  25. server::{listen_and_serve, RequestHandler},
  26. settings::{RpcSettings, RpcSettingsOpt},
  27. },
  28. system::{sleep, StoppableTask, StoppableTaskPtr, Subscription},
  29. util::path::{expand_path, get_config_path},
  30. Error, Result,
  31. };
  32. use darkfi_sdk::crypto::pasta_prelude::PrimeField;
  33. use rand::rngs::OsRng;
  34. use settings::list_configured_contacts;
  35. use sled_overlay::sled;
  36. use smol::{fs, lock::Mutex, stream::StreamExt, Executor};
  37. use structopt_toml::{serde::Deserialize, structopt::StructOpt, StructOptToml};
  38. use tracing::{debug, error, info};
  39. use url::Url;
  40. const CONFIG_FILE: &str = "darkirc_config.toml";
  41. const CONFIG_FILE_CONTENTS: &str = include_str!("../darkirc_config.toml");
  42. /// IRC server and client handler implementation
  43. mod irc;
  44. use irc::server::IrcServer;
  45. /// Cryptography utilities
  46. mod crypto;
  47. use crypto::{bcrypt::bcrypt_hash_password, rln::RlnIdentity};
  48. /// JSON-RPC methods
  49. mod rpc;
  50. /// Settings utilities
  51. mod settings;
  52. fn panic_hook(panic_info: &std::panic::PanicHookInfo) {
  53. error!("panic occurred: {panic_info}");
  54. error!("{}", std::backtrace::Backtrace::force_capture());
  55. std::process::abort()
  56. }
  57. #[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
  58. #[serde(default)]
  59. #[structopt(
  60. name = "darkirc",
  61. about = cli_desc!(),
  62. version = concat!(env!("CARGO_PKG_VERSION"), "-", env!("COMMITISH"))
  63. )]
  64. struct Args {
  65. #[structopt(short, parse(from_occurrences))]
  66. /// Increase verbosity (-vvv supported)
  67. verbose: u8,
  68. #[structopt(short, long)]
  69. /// Configuration file to use
  70. config: Option<String>,
  71. #[structopt(long)]
  72. /// Set log file output
  73. log: Option<String>,
  74. #[structopt(long, default_value = "tcp://127.0.0.1:6667")]
  75. /// IRC server listen address
  76. irc_listen: Url,
  77. /// Optional TLS certificate file path if `irc_listen` uses TLS
  78. irc_tls_cert: Option<String>,
  79. /// Optional TLS certificate key file path if `irc_listen` uses TLS
  80. irc_tls_secret: Option<String>,
  81. #[structopt(short, long, default_value = "~/.local/share/darkfi/darkirc_db")]
  82. /// Datastore (DB) path
  83. datastore: String,
  84. #[structopt(short, long, default_value = "~/.local/share/darkfi/replayed_darkirc_db")]
  85. /// Replay logs (DB) path
  86. replay_datastore: String,
  87. #[structopt(long)]
  88. /// Flag to store Sled DB instructions
  89. replay_mode: bool,
  90. #[structopt(long)]
  91. /// Generate a new NaCl keypair and exit
  92. gen_chacha_keypair: bool,
  93. #[structopt(long)]
  94. /// Generate a new encrypted channel NaCl secret and exit
  95. gen_channel_secret: bool,
  96. #[structopt(long = "get-chacha-pubkey")]
  97. /// Recover NaCl public key from a secret key
  98. chacha_secret: Option<String>,
  99. #[structopt(long)]
  100. /// Generate a new RLN identity
  101. gen_rln_identity: bool,
  102. #[structopt(long)]
  103. /// Flag to skip syncing the DAG (no history)
  104. skip_dag_sync: bool,
  105. #[structopt(long)]
  106. // Whether to sync headers only or full sync
  107. fast_mode: bool,
  108. #[structopt(long)]
  109. /// IRC Password (Encrypted with bcrypt-2b)
  110. password: Option<String>,
  111. #[structopt(long)]
  112. /// Encrypt a given password for the IRC server connection
  113. encrypt_password: bool,
  114. #[structopt(long)]
  115. /// List configured contacts.
  116. list_contacts: bool,
  117. #[structopt(flatten)]
  118. /// P2P network settings
  119. net: SettingsOpt,
  120. #[structopt(flatten)]
  121. /// JSON-RPC settings
  122. rpc: RpcSettingsOpt,
  123. }
  124. pub struct DarkIrc {
  125. /// P2P network pointer
  126. p2p: P2pPtr,
  127. /// Sled DB (also used in event_graph and for RLN)
  128. sled: sled::Db,
  129. /// Event Graph instance
  130. event_graph: EventGraphPtr,
  131. /// JSON-RPC connection tracker
  132. rpc_connections: Mutex<HashSet<StoppableTaskPtr>>,
  133. /// dnet JSON-RPC subscriber
  134. dnet_sub: JsonSubscriber,
  135. /// deg JSON-RPC subscriber
  136. deg_sub: JsonSubscriber,
  137. /// Replay logs (DB) path
  138. replay_datastore: PathBuf,
  139. }
  140. impl DarkIrc {
  141. fn new(
  142. p2p: P2pPtr,
  143. sled: sled::Db,
  144. event_graph: EventGraphPtr,
  145. dnet_sub: JsonSubscriber,
  146. deg_sub: JsonSubscriber,
  147. replay_datastore: PathBuf,
  148. ) -> Self {
  149. Self {
  150. p2p,
  151. sled,
  152. event_graph,
  153. rpc_connections: Mutex::new(HashSet::new()),
  154. dnet_sub,
  155. deg_sub,
  156. replay_datastore,
  157. }
  158. }
  159. }
  160. async_daemonize!(realmain);
  161. async fn realmain(args: Args, ex: Arc<Executor<'static>>) -> Result<()> {
  162. if args.fast_mode {
  163. info!("fast mode enabled");
  164. }
  165. // Abort the application on panic right away
  166. std::panic::set_hook(Box::new(panic_hook));
  167. if args.gen_chacha_keypair {
  168. let secret = crypto_box::SecretKey::generate(&mut OsRng);
  169. let public = secret.public_key();
  170. let secret = bs58::encode(secret.to_bytes()).into_string();
  171. let public = bs58::encode(public.to_bytes()).into_string();
  172. println!(
  173. "Place this in your config file under your contact, you can reuse this keypair for multiple contacts\n"
  174. );
  175. println!("[contact.\"satoshi\"]");
  176. println!("dm_chacha_public = \"YOUR_CONTACT_PUBLIC_KEY\"");
  177. println!("my_dm_chacha_secret = \"{secret}\"");
  178. println!("#my_dm_chacha_public = \"{public}\"");
  179. return Ok(());
  180. }
  181. if args.gen_channel_secret {
  182. let secret = crypto_box::SecretKey::generate(&mut OsRng);
  183. let secret = bs58::encode(secret.to_bytes()).into_string();
  184. println!("Place this in your config file:\n");
  185. println!("[channel.\"#yourchannelname\"]");
  186. println!("secret = \"{secret}\"");
  187. return Ok(());
  188. }
  189. if args.gen_rln_identity {
  190. let identity = RlnIdentity::new(&mut OsRng);
  191. let nullifier = bs58::encode(identity.nullifier.to_repr()).into_string();
  192. let trapdoor = bs58::encode(identity.trapdoor.to_repr()).into_string();
  193. println!("Place this in your config file:\n");
  194. println!("[rln]");
  195. println!("nullifier = \"{nullifier}\"");
  196. println!("trapdoor = \"{trapdoor}\"");
  197. return Ok(());
  198. }
  199. if let Some(chacha_secret) = args.chacha_secret {
  200. let bytes = match bs58::decode(chacha_secret).into_vec() {
  201. Ok(v) => v,
  202. Err(e) => {
  203. println!("Error: {e}");
  204. return Err(Error::ParseFailed("Secret key parsing failed"));
  205. }
  206. };
  207. if bytes.len() != 32 {
  208. return Err(Error::ParseFailed("Decoded base58 is not 32 bytes long"));
  209. }
  210. let secret: [u8; 32] = bytes.try_into().unwrap();
  211. let secret = crypto_box::SecretKey::from(secret);
  212. println!("{}", bs58::encode(secret.public_key().to_bytes()).into_string());
  213. return Ok(());
  214. }
  215. if args.list_contacts {
  216. let config_path = match get_config_path(args.config, CONFIG_FILE) {
  217. Ok(path) => path,
  218. Err(e) => {
  219. error!("Unable to get config path: {e}");
  220. return Err(e);
  221. }
  222. };
  223. let contents = match fs::read_to_string(&config_path).await {
  224. Ok(c) => c,
  225. Err(e) => {
  226. error!("Unable read path `{config_path:?}`: {e}");
  227. return Err(e.into());
  228. }
  229. };
  230. let contents = match toml::from_str(&contents) {
  231. Ok(v) => v,
  232. Err(e) => {
  233. error!("Failed parsing TOML config: {e}");
  234. return Err(Error::ParseFailed("Failed parsing TOML config"));
  235. }
  236. };
  237. // Parse configured contacts
  238. let contacts = match list_configured_contacts(&contents) {
  239. Ok(c) => c,
  240. Err(e) => {
  241. error!("List contacts failed `{config_path:?}`: {e}");
  242. return Err(e);
  243. }
  244. };
  245. for (name, (public_key, my_secret_key)) in contacts {
  246. let public_key = bs58::encode(public_key.to_bytes()).into_string();
  247. let my_public_key = my_secret_key.public_key();
  248. let my_secret_key = bs58::encode(my_secret_key.to_bytes()).into_string();
  249. let my_public_key = bs58::encode(my_public_key.to_bytes()).into_string();
  250. println!("{name}: {public_key} using key {my_secret_key}({my_public_key})")
  251. }
  252. return Ok(());
  253. }
  254. if args.encrypt_password {
  255. let mut pw = String::new();
  256. print!("Enter password: ");
  257. std::io::stdout().flush()?;
  258. std::io::stdin().read_line(&mut pw)?;
  259. if let Some('\n') = pw.chars().next_back() {
  260. pw.pop();
  261. }
  262. if let Some('\r') = pw.chars().next_back() {
  263. pw.pop();
  264. }
  265. println!("{}", bcrypt_hash_password(pw));
  266. std::io::stdout().flush()?;
  267. return Ok(());
  268. }
  269. info!("Initializing DarkIRC node");
  270. // Create datastore path if not there already.
  271. let datastore = match expand_path(&args.datastore) {
  272. Ok(v) => v,
  273. Err(e) => {
  274. error!("Bad datastore path `{}`: {e}", args.datastore);
  275. return Err(e);
  276. }
  277. };
  278. if let Err(e) = fs::create_dir_all(&datastore).await {
  279. error!("Failed to create data store path `{datastore:?}`: {e}");
  280. return Err(e.into());
  281. }
  282. let replay_datastore = match expand_path(&args.replay_datastore) {
  283. Ok(v) => v,
  284. Err(e) => {
  285. error!("Bad replay datastore path `{}`: {e}", args.replay_datastore);
  286. return Err(e);
  287. }
  288. };
  289. let replay_mode = args.replay_mode;
  290. let fast_mode = args.fast_mode;
  291. info!("Instantiating event DAG");
  292. let sled_db = match sled::open(datastore.clone()) {
  293. Ok(v) => v,
  294. Err(e) => {
  295. error!("Failed to open datastore database `{datastore:?}`: {e}");
  296. return Err(e.into());
  297. }
  298. };
  299. let p2p_settings: darkfi::net::Settings =
  300. (env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"), args.net).try_into()?;
  301. let p2p = match P2p::new(p2p_settings, ex.clone()).await {
  302. Ok(p2p) => p2p,
  303. Err(e) => {
  304. error!("Unable to create P2P network: {e}");
  305. return Err(e);
  306. }
  307. };
  308. let event_graph = match EventGraph::new(
  309. p2p.clone(),
  310. sled_db.clone(),
  311. replay_datastore.clone(),
  312. replay_mode,
  313. fast_mode,
  314. "darkirc_dag",
  315. 1,
  316. ex.clone(),
  317. )
  318. .await
  319. {
  320. Ok(v) => v,
  321. Err(e) => {
  322. error!("Event graph failed to start: {e}");
  323. return Err(e);
  324. }
  325. };
  326. let prune_task = event_graph.prune_task.get().unwrap();
  327. info!("Registering EventGraph P2P protocol");
  328. let event_graph_ = Arc::clone(&event_graph);
  329. let registry = p2p.protocol_registry();
  330. registry
  331. .register(SESSION_DEFAULT, move |channel, _| {
  332. let event_graph_ = event_graph_.clone();
  333. async move { ProtocolEventGraph::init(event_graph_, channel).await.unwrap() }
  334. })
  335. .await;
  336. info!("Starting dnet subs task");
  337. let dnet_sub = JsonSubscriber::new("dnet.subscribe_events");
  338. let dnet_sub_ = dnet_sub.clone();
  339. let p2p_ = p2p.clone();
  340. let dnet_task = StoppableTask::new();
  341. dnet_task.clone().start(
  342. async move {
  343. let dnet_sub = p2p_.dnet_subscribe().await;
  344. loop {
  345. let event = dnet_sub.receive().await;
  346. debug!("Got dnet event: {event:?}");
  347. dnet_sub_.notify(vec![event.into()].into()).await;
  348. }
  349. },
  350. |res| async {
  351. match res {
  352. Ok(()) | Err(Error::DetachedTaskStopped) => { /* Do nothing */ }
  353. Err(e) => panic!("{e}"),
  354. }
  355. },
  356. Error::DetachedTaskStopped,
  357. ex.clone(),
  358. );
  359. info!("Starting deg subs task");
  360. let deg_sub = JsonSubscriber::new("deg.subscribe_events");
  361. let deg_sub_ = deg_sub.clone();
  362. let event_graph_ = event_graph.clone();
  363. let deg_task = StoppableTask::new();
  364. deg_task.clone().start(
  365. async move {
  366. let deg_sub = event_graph_.deg_subscribe().await;
  367. loop {
  368. let event = deg_sub.receive().await;
  369. debug!("Got deg event: {event:?}");
  370. deg_sub_.notify(vec![event.into()].into()).await;
  371. }
  372. },
  373. |res| async {
  374. match res {
  375. Ok(()) | Err(Error::DetachedTaskStopped) => { /* Do nothing */ }
  376. Err(e) => panic!("{e}"),
  377. }
  378. },
  379. Error::DetachedTaskStopped,
  380. ex.clone(),
  381. );
  382. info!("Starting JSON-RPC server");
  383. let rpc_settings: RpcSettings = args.rpc.into();
  384. let darkirc = Arc::new(DarkIrc::new(
  385. p2p.clone(),
  386. sled_db.clone(),
  387. event_graph.clone(),
  388. dnet_sub,
  389. deg_sub,
  390. replay_datastore.clone(),
  391. ));
  392. let darkirc_ = Arc::clone(&darkirc);
  393. let rpc_task = StoppableTask::new();
  394. rpc_task.clone().start(
  395. listen_and_serve(rpc_settings, darkirc.clone(), None, ex.clone()),
  396. |res| async move {
  397. match res {
  398. Ok(()) | Err(Error::RpcServerStopped) => darkirc_.stop_connections().await,
  399. Err(e) => error!("Failed stopping JSON-RPC server: {e}"),
  400. }
  401. },
  402. Error::RpcServerStopped,
  403. ex.clone(),
  404. );
  405. info!("Starting IRC server");
  406. let password = args.password.unwrap_or_default();
  407. let config_path = match get_config_path(args.config.clone(), CONFIG_FILE) {
  408. Ok(v) => v,
  409. Err(e) => {
  410. error!("Cannot get config path `{:?}`: {e}", args.config);
  411. return Err(e);
  412. }
  413. };
  414. let irc_server = match IrcServer::new(
  415. darkirc.clone(),
  416. args.irc_listen,
  417. args.irc_tls_cert,
  418. args.irc_tls_secret,
  419. config_path,
  420. password,
  421. )
  422. .await
  423. {
  424. Ok(v) => v,
  425. Err(e) => {
  426. error!("Unable to create IRC server: {e}");
  427. return Err(e);
  428. }
  429. };
  430. let irc_task = StoppableTask::new();
  431. let ex_ = ex.clone();
  432. irc_task.clone().start(
  433. irc_server.clone().listen(ex_),
  434. |res| async move {
  435. match res {
  436. Ok(()) | Err(Error::DetachedTaskStopped) => { /* TODO: */ }
  437. Err(e) => error!("Failed stopping IRC server: {e}"),
  438. }
  439. },
  440. Error::DetachedTaskStopped,
  441. ex.clone(),
  442. );
  443. info!("Starting P2P network");
  444. if let Err(e) = p2p.clone().start().await {
  445. error!("P2P failed to start: {e}");
  446. return Err(e);
  447. }
  448. // Initial DAG sync
  449. if let Err(e) = sync_task(&p2p, &event_graph, args.skip_dag_sync, args.fast_mode).await {
  450. error!("DAG sync task failed to start: {e}");
  451. return Err(e);
  452. };
  453. // Stoppable task to monitor network and resync on disconnect.
  454. let sync_mon_task = StoppableTask::new();
  455. sync_mon_task.clone().start(
  456. sync_and_monitor(p2p.clone(), event_graph.clone(), args.skip_dag_sync, args.fast_mode),
  457. |res| async move {
  458. match res {
  459. Ok(()) | Err(Error::DetachedTaskStopped) => { /* TODO: */ }
  460. Err(e) => error!("Failed sync task: {e}"),
  461. }
  462. },
  463. Error::DetachedTaskStopped,
  464. ex.clone(),
  465. );
  466. // Signal handling for graceful termination.
  467. let (signals_handler, signals_task) = SignalHandler::new(ex)?;
  468. signals_handler.wait_termination(signals_task).await?;
  469. info!("Caught termination signal, cleaning up and exiting...");
  470. info!("Stopping P2P network");
  471. p2p.stop().await;
  472. info!("Stopping JSON-RPC server");
  473. rpc_task.stop().await;
  474. dnet_task.stop().await;
  475. deg_task.stop().await;
  476. info!("Stopping IRC server");
  477. irc_task.stop().await;
  478. prune_task.stop().await;
  479. info!("Flushing sled database...");
  480. let flushed_bytes = sled_db.flush_async().await?;
  481. info!("Flushed {flushed_bytes} bytes");
  482. info!("Shut down successfully");
  483. Ok(())
  484. }
  485. /// Async task to monitor network disconnections.
  486. async fn monitor_network(subscription: &Subscription<Error>) -> Result<()> {
  487. Err(subscription.receive().await)
  488. }
  489. /// Async task to endlessly try to sync DAG, returns Ok if done.
  490. async fn sync_task(
  491. p2p: &P2pPtr,
  492. event_graph: &EventGraphPtr,
  493. skip_dag_sync: bool,
  494. fast_mode: bool,
  495. ) -> Result<()> {
  496. let comms_timeout = p2p.settings().read_arc().await.outbound_connect_timeout_max();
  497. loop {
  498. if p2p.is_connected() {
  499. info!("Got peer connection");
  500. // We'll attempt to sync for ever
  501. if !skip_dag_sync {
  502. info!("Syncing event DAG");
  503. match event_graph.dag_sync(fast_mode).await {
  504. Ok(()) => break,
  505. Err(e) => {
  506. // TODO: Maybe at this point we should prune or something?
  507. // TODO: Or maybe just tell the user to delete the DAG from FS.
  508. error!("Failed syncing DAG ({e}), retrying in {comms_timeout}s...");
  509. sleep(comms_timeout).await;
  510. }
  511. }
  512. } else {
  513. *event_graph.synced.write().await = true;
  514. break;
  515. }
  516. } else {
  517. info!("Waiting for some P2P connections...");
  518. sleep(comms_timeout).await;
  519. }
  520. }
  521. Ok(())
  522. }
  523. /// Async task to monitor the network and force resync on disconnections
  524. async fn sync_and_monitor(
  525. p2p: P2pPtr,
  526. event_graph: EventGraphPtr,
  527. skip_dag_sync: bool,
  528. fast_mode: bool,
  529. ) -> Result<()> {
  530. loop {
  531. let net_subscription = p2p.hosts().subscribe_disconnect().await;
  532. let result = monitor_network(&net_subscription).await;
  533. net_subscription.unsubscribe().await;
  534. match result {
  535. Ok(_) => return Ok(()),
  536. Err(Error::NetworkNotConnected) => {
  537. // Sync node again
  538. info!("Network disconnection detected, resyncing...");
  539. *event_graph.synced.write().await = false;
  540. sync_task(&p2p, &event_graph, skip_dag_sync, fast_mode).await?;
  541. }
  542. Err(e) => return Err(e),
  543. }
  544. }
  545. }