settings.rs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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::{
  19. collections::{HashMap, HashSet},
  20. sync::Arc,
  21. };
  22. use crypto_box::PublicKey;
  23. use darkfi::{Error::ParseFailed, Result};
  24. use log::info;
  25. use crate::irc::{IrcChannel, IrcContact};
  26. /// Parse configured autojoin channels from a TOML map.
  27. ///
  28. /// ```toml
  29. /// autojoin = ["#dev", "#memes"]
  30. /// ```
  31. pub fn parse_autojoin_channels(data: &toml::Value) -> Result<Vec<String>> {
  32. let mut ret = vec![];
  33. let Some(autojoin) = data.get("autojoin") else { return Ok(ret) };
  34. let Some(autojoin) = autojoin.as_array() else {
  35. return Err(ParseFailed("autojoin not an array"))
  36. };
  37. for item in autojoin {
  38. let Some(channel) = item.as_str() else {
  39. return Err(ParseFailed("autojoin channel not a string"))
  40. };
  41. if !channel.starts_with('#') {
  42. return Err(ParseFailed("autojoin channel not a valid channel"))
  43. }
  44. if ret.contains(&channel.to_string()) {
  45. return Err(ParseFailed("Duplicate autojoin channel found"))
  46. }
  47. ret.push(channel.to_string());
  48. }
  49. Ok(ret)
  50. }
  51. /// Parse a DM secret key from a TOML map.
  52. ///
  53. /// ```toml
  54. /// [crypto]
  55. /// dm_chacha_secret = "7CkVuFgwTUpJn5Sv67Q3fyEDpa28yrSeL5Hg2GqQ4jfM"
  56. /// ```
  57. fn parse_dm_chacha_secret(data: &toml::Value) -> Result<Option<crypto_box::SecretKey>> {
  58. let Some(table) = data.as_table() else { return Err(ParseFailed("TOML not a map")) };
  59. let Some(crypto) = table.get("crypto") else { return Ok(None) };
  60. let Some(crypto) = crypto.as_table() else { return Err(ParseFailed("`crypto` not a map")) };
  61. if !crypto.contains_key("dm_chacha_secret") {
  62. return Ok(None)
  63. }
  64. let Some(secret_str) = crypto["dm_chacha_secret"].as_str() else {
  65. return Err(ParseFailed("dm_chacha_secret not a string"))
  66. };
  67. let Ok(secret_bytes) = bs58::decode(secret_str).into_vec() else {
  68. return Err(ParseFailed("dm_chacha_secret not valid base58"))
  69. };
  70. if secret_bytes.len() != 32 {
  71. return Err(ParseFailed("dm_chacha_secret not 32 bytes long"))
  72. }
  73. let secret_bytes: [u8; 32] = secret_bytes.try_into().unwrap();
  74. Ok(Some(crypto_box::SecretKey::from(secret_bytes)))
  75. }
  76. pub fn list_configured_contacts(data: &toml::Value) -> Result<HashMap<String, PublicKey>> {
  77. let mut ret = HashMap::new();
  78. let Some(table) = data.as_table() else { return Err(ParseFailed("TOML not a map")) };
  79. let Some(contacts) = table.get("contact") else { return Ok(ret) };
  80. let Some(contacts) = contacts.as_table() else {
  81. return Err(ParseFailed("`contact` not a map"))
  82. };
  83. for (name, items) in contacts {
  84. let Some(public_str) = items.get("dm_chacha_public") else {
  85. return Err(ParseFailed("Invalid contact configuration"))
  86. };
  87. let Some(public_str) = public_str.as_str() else {
  88. return Err(ParseFailed("Invalid contact configuration"))
  89. };
  90. let Ok(public_bytes) = bs58::decode(public_str).into_vec() else {
  91. return Err(ParseFailed("Invalid base58 for contact pubkey"))
  92. };
  93. if public_bytes.len() != 32 {
  94. return Err(ParseFailed("Invalid contact pubkey (not 32 bytes)"))
  95. }
  96. let public_bytes: [u8; 32] = public_bytes.try_into().unwrap();
  97. let public = crypto_box::PublicKey::from(public_bytes);
  98. if ret.contains_key(name) {
  99. return Err(ParseFailed("Duplicate contact found"))
  100. }
  101. info!("Instantiated ChaChaBox for contact \"{}\"", name);
  102. ret.insert(name.to_string(), public);
  103. }
  104. Ok(ret)
  105. }
  106. /// Parse configured contacts from a TOML map.
  107. ///
  108. /// ```toml
  109. /// [contact."anon"]
  110. /// dm_chacha_public = "7CkVuFgwTUpJn5Sv67Q3fyEDpa28yrSeL5Hg2GqQ4jfM"
  111. /// ```
  112. pub fn parse_configured_contacts(data: &toml::Value) -> Result<HashMap<String, IrcContact>> {
  113. let mut ret = HashMap::new();
  114. let contacts = list_configured_contacts(data)?;
  115. let Some(secret) = parse_dm_chacha_secret(data)? else {
  116. return Err(ParseFailed("You have specified some contacts but you did not set up a valid chacha secret for yourself. You can generate a keypair with: 'darkirc --gen-chacha-keypair' and then add that keypair to your config toml file."))
  117. };
  118. for (name, public) in contacts {
  119. let saltbox = Some(Arc::new(crypto_box::ChaChaBox::new(&public, &secret)));
  120. if ret.contains_key(&name) {
  121. return Err(ParseFailed("Duplicate contact found"))
  122. }
  123. info!("Instantiated ChaChaBox for contact \"{}\"", name);
  124. ret.insert(name.to_string(), IrcContact { saltbox });
  125. }
  126. Ok(ret)
  127. }
  128. /// Parse a TOML string for any configured channels and return
  129. /// a map containing said configurations.
  130. ///
  131. /// ```toml
  132. /// [channel."#memes"]
  133. /// secret = "7CkVuFgwTUpJn5Sv67Q3fyEDpa28yrSeL5Hg2GqQ4jfM"
  134. /// topic = "Dank Memes"
  135. /// ```
  136. pub fn parse_configured_channels(data: &toml::Value) -> Result<HashMap<String, IrcChannel>> {
  137. let mut ret = HashMap::new();
  138. let Some(table) = data.as_table() else { return Err(ParseFailed("TOML not a map")) };
  139. let Some(chans) = table.get("channel") else { return Ok(ret) };
  140. let Some(chans) = chans.as_table() else { return Err(ParseFailed("`channel` not a map")) };
  141. for (name, items) in chans {
  142. let mut chan = IrcChannel { topic: String::new(), nicks: HashSet::new(), saltbox: None };
  143. if let Some(topic) = items.get("topic") {
  144. if let Some(topic) = topic.as_str() {
  145. info!("Found configured topic for {}: {}", name, topic);
  146. chan.topic = topic.to_string();
  147. } else {
  148. return Err(ParseFailed("Channel topic not a string"))
  149. }
  150. }
  151. if let Some(secret) = items.get("secret") {
  152. if let Some(secret) = secret.as_str() {
  153. let Ok(secret_bytes) = bs58::decode(secret).into_vec() else {
  154. return Err(ParseFailed("Channel secret not valid base58"))
  155. };
  156. if secret_bytes.len() != 32 {
  157. return Err(ParseFailed("Channel secret not 32 bytes long"))
  158. }
  159. let secret_bytes: [u8; 32] = secret_bytes.try_into().unwrap();
  160. let secret = crypto_box::SecretKey::from(secret_bytes);
  161. let public = secret.public_key();
  162. chan.saltbox = Some(Arc::new(crypto_box::ChaChaBox::new(&public, &secret)));
  163. info!("Configured NaCl box for channel {}", name);
  164. } else {
  165. return Err(ParseFailed("Channel secret not a string"))
  166. }
  167. }
  168. info!("Configured channel {}", name);
  169. ret.insert(name.to_string(), chan);
  170. }
  171. Ok(ret)
  172. }