settings.rs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. use crypto_box::SalsaBox;
  2. use fxhash::FxHashMap;
  3. use log::info;
  4. use serde::Deserialize;
  5. use structopt::StructOpt;
  6. use structopt_toml::StructOptToml;
  7. use toml::Value;
  8. use url::Url;
  9. use darkfi::{net::settings::SettingsOpt, Result};
  10. pub const CONFIG_FILE: &str = "ircd_config.toml";
  11. pub const CONFIG_FILE_CONTENTS: &str = include_str!("../ircd_config.toml");
  12. /// ircd cli
  13. #[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
  14. #[serde(default)]
  15. #[structopt(name = "ircd")]
  16. pub struct Args {
  17. /// Sets a custom config file
  18. #[structopt(long)]
  19. pub config: Option<String>,
  20. /// JSON-RPC listen URL
  21. #[structopt(long = "rpc", default_value = "tcp://127.0.0.1:25550")]
  22. pub rpc_listen: Url,
  23. /// IRC listen URL
  24. #[structopt(long = "irc", default_value = "tcp://127.0.0.1:6667")]
  25. pub irc_listen: Url,
  26. /// Optional TLS certificate file path if `irc_listen` uses TLS
  27. pub irc_tls_cert: Option<String>,
  28. /// Optional TLS certificate key file path if `irc_listen` uses TLS
  29. pub irc_tls_secret: Option<String>,
  30. /// Generate a new NaCl secret and exit
  31. #[structopt(long)]
  32. pub gen_secret: bool,
  33. /// Generate a new NaCl keypair and exit
  34. #[structopt(long)]
  35. pub gen_keypair: bool,
  36. /// Path to save keypair in
  37. #[structopt(short)]
  38. pub output: Option<String>,
  39. /// Autojoin channels
  40. #[structopt(long)]
  41. pub autojoin: Vec<String>,
  42. /// Password
  43. #[structopt(long)]
  44. pub password: Option<String>,
  45. #[structopt(flatten)]
  46. pub net: SettingsOpt,
  47. /// Increase verbosity
  48. #[structopt(short, parse(from_occurrences))]
  49. pub verbose: u8,
  50. }
  51. #[derive(Clone)]
  52. pub struct ContactInfo {
  53. /// Optional NaCl box for the channel, used for {en,de}cryption.
  54. pub salt_box: Option<SalsaBox>,
  55. }
  56. impl ContactInfo {
  57. pub fn new() -> Result<Self> {
  58. Ok(Self { salt_box: None })
  59. }
  60. }
  61. /// This struct holds information about preconfigured channels.
  62. /// In the TOML configuration file, we can configure channels as such:
  63. /// ```toml
  64. /// [channel."#dev"]
  65. /// secret = "GvH4kno3kUu6dqPrZ8zjMhqxTUDZ2ev16EdprZiZJgj1"
  66. /// topic = "DarkFi Development Channel"
  67. /// ```
  68. /// Having a secret will enable a NaCl box that is able to encrypt and
  69. /// decrypt messages in this channel using this set shared secret.
  70. /// The secret should be shared OOB, via a secure channel.
  71. /// Having a topic set is useful if one wants to have a topic in the
  72. /// configured channel. It is not shared with others, but it is useful
  73. /// for personal reference.
  74. #[derive(Clone)]
  75. pub struct ChannelInfo {
  76. /// Optional topic for the channel
  77. pub topic: Option<String>,
  78. /// Optional NaCl box for the channel, used for {en,de}cryption.
  79. pub salt_box: Option<SalsaBox>,
  80. /// Flag indicates whether the user has joined the channel or not
  81. pub joined: bool,
  82. /// All nicknames which are visible on the channel
  83. pub names: Vec<String>,
  84. }
  85. impl ChannelInfo {
  86. pub fn new() -> Result<Self> {
  87. Ok(Self { topic: None, salt_box: None, joined: false, names: vec![] })
  88. }
  89. }
  90. fn salt_box_from_shared_secret(s: &str) -> Result<SalsaBox> {
  91. let bytes: [u8; 32] = bs58::decode(s).into_vec()?.try_into().unwrap();
  92. let secret = crypto_box::SecretKey::from(bytes);
  93. let public = secret.public_key();
  94. Ok(SalsaBox::new(&public, &secret))
  95. }
  96. fn parse_priv_key(data: &str) -> Result<String> {
  97. let mut pk = String::new();
  98. if let Value::Table(map) = toml::from_str(data)? {
  99. if map.contains_key("private_key") && map["private_key"].is_table() {
  100. for prv_key in map["private_key"].as_table().unwrap() {
  101. pk = prv_key.0.into();
  102. }
  103. }
  104. };
  105. Ok(pk)
  106. }
  107. /// Parse a TOML string for any configured contact list and return
  108. /// a map containing said configurations.
  109. ///
  110. /// ```toml
  111. /// [contact."nick"]
  112. /// contact_pubkey = "7CkVuFgwTUpJn5Sv67Q3fyEDpa28yrSeL5Hg2GqQ4jfM"
  113. /// ```
  114. pub fn parse_configured_contacts(data: &str) -> Result<FxHashMap<String, ContactInfo>> {
  115. let mut ret = FxHashMap::default();
  116. if let Value::Table(map) = toml::from_str(data)? {
  117. if map.contains_key("contact") && map["contact"].is_table() {
  118. for cnt in map["contact"].as_table().unwrap() {
  119. info!("Found configuration for contact {}", cnt.0);
  120. let mut contact_info = ContactInfo::new()?;
  121. if cnt.1.as_table().unwrap().contains_key("contact_pubkey") {
  122. // Build the NaCl box
  123. if let Some(p) = cnt.1["contact_pubkey"].as_str() {
  124. let bytes: [u8; 32] = bs58::decode(p).into_vec()?.try_into().unwrap();
  125. let public = crypto_box::PublicKey::from(bytes);
  126. let bytes: [u8; 32] =
  127. bs58::decode(parse_priv_key(data)?).into_vec()?.try_into().unwrap();
  128. let secret = crypto_box::SecretKey::from(bytes);
  129. contact_info.salt_box = Some(SalsaBox::new(&public, &secret));
  130. ret.insert(cnt.0.to_string(), contact_info);
  131. info!("Instantiated NaCl box for contact {}", cnt.0);
  132. }
  133. }
  134. }
  135. }
  136. };
  137. Ok(ret)
  138. }
  139. /// Parse a TOML string for any configured channels and return
  140. /// a map containing said configurations.
  141. ///
  142. /// ```toml
  143. /// [channel."#memes"]
  144. /// secret = "7CkVuFgwTUpJn5Sv67Q3fyEDpa28yrSeL5Hg2GqQ4jfM"
  145. /// topic = "Dank Memes"
  146. /// ```
  147. pub fn parse_configured_channels(data: &str) -> Result<FxHashMap<String, ChannelInfo>> {
  148. let mut ret = FxHashMap::default();
  149. if let Value::Table(map) = toml::from_str(data)? {
  150. if map.contains_key("channel") && map["channel"].is_table() {
  151. for chan in map["channel"].as_table().unwrap() {
  152. info!("Found configuration for channel {}", chan.0);
  153. let mut channel_info = ChannelInfo::new()?;
  154. if chan.1.as_table().unwrap().contains_key("topic") {
  155. let topic = chan.1["topic"].as_str().unwrap().to_string();
  156. info!("Found topic for channel {}: {}", chan.0, topic);
  157. channel_info.topic = Some(topic);
  158. }
  159. if chan.1.as_table().unwrap().contains_key("secret") {
  160. // Build the NaCl box
  161. if let Some(s) = chan.1["secret"].as_str() {
  162. let salt_box = salt_box_from_shared_secret(s)?;
  163. channel_info.salt_box = Some(salt_box);
  164. info!("Instantiated NaCl box for channel {}", chan.0);
  165. }
  166. }
  167. ret.insert(chan.0.to_string(), channel_info);
  168. }
  169. }
  170. };
  171. Ok(ret)
  172. }