settings.rs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 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 crypto_box::SalsaBox;
  19. use log::error;
  20. use serde::{self, Deserialize, Serialize};
  21. use std::collections::HashMap;
  22. use structopt::StructOpt;
  23. use structopt_toml::StructOptToml;
  24. use url::Url;
  25. use darkfi::{net::settings::SettingsOpt, Result};
  26. // Location for config file
  27. pub const CONFIG_FILE: &str = "ircd_config.toml";
  28. pub const CONFIG_FILE_CONTENTS: &str = include_str!("../ircd_config.toml");
  29. // Msg config
  30. pub const MAXIMUM_LENGTH_OF_MESSAGE: usize = 1024;
  31. pub const MAXIMUM_LENGTH_OF_NICKNAME: usize = 32;
  32. // IRC Client
  33. pub enum RPL {
  34. NoTopic = 331,
  35. Topic = 332,
  36. NameReply = 353,
  37. EndOfNames = 366,
  38. }
  39. /// ircd cli
  40. #[derive(Clone, Deserialize, StructOpt, StructOptToml)]
  41. #[serde(default)]
  42. #[structopt(name = "ircd")]
  43. pub struct Args {
  44. /// Sets a custom config file
  45. #[structopt(long)]
  46. pub config: Option<String>,
  47. /// JSON-RPC listen URL
  48. #[structopt(long = "rpc", default_value = "tcp://127.0.0.1:25550")]
  49. pub rpc_listen: Url,
  50. /// IRC listen URL
  51. #[structopt(long = "irc", default_value = "tcp://127.0.0.1:6667")]
  52. pub irc_listen: Url,
  53. /// Optional TLS certificate file path if `irc_listen` uses TLS
  54. pub irc_tls_cert: Option<String>,
  55. /// Optional TLS certificate key file path if `irc_listen` uses TLS
  56. pub irc_tls_secret: Option<String>,
  57. /// Generate a new NaCl keypair and exit
  58. #[structopt(long)]
  59. pub gen_keypair: bool,
  60. /// Path to save keypair in
  61. #[structopt(short)]
  62. pub output: Option<String>,
  63. /// Autojoin channels
  64. #[structopt(long)]
  65. pub autojoin: Vec<String>,
  66. /// Password
  67. #[structopt(long)]
  68. pub password: Option<String>,
  69. /// Channels
  70. #[structopt(skip)]
  71. pub channels: HashMap<String, ChannelInfo>,
  72. /// Channels
  73. #[structopt(skip)]
  74. pub contacts: HashMap<String, ContactInfo>,
  75. /// Private key
  76. #[structopt(skip)]
  77. pub private_key: Option<String>,
  78. #[structopt(flatten)]
  79. pub net: SettingsOpt,
  80. /// Increase verbosity
  81. #[structopt(short, parse(from_occurrences))]
  82. pub verbose: u8,
  83. }
  84. /// This struct holds information about preconfigured contacts.
  85. /// In the TOML configuration file, we can configure contacts as such:
  86. ///
  87. /// ```toml
  88. /// [contact."nick"]
  89. /// pubkey = "7CkVuFgwTUpJn5Sv67Q3fyEDpa28yrSeL5Hg2GqQ4jfM"
  90. /// ```
  91. #[derive(Default, Clone, Debug, Deserialize, Serialize)]
  92. pub struct ContactInfo {
  93. pub pubkey: Option<String>,
  94. }
  95. impl ContactInfo {
  96. pub fn new() -> Self {
  97. Self { pubkey: None }
  98. }
  99. pub fn salt_box(&self, private_key: &str, contact_name: &str) -> Option<SalsaBox> {
  100. if let Ok(private) = parse_priv(private_key) {
  101. if let Some(p) = &self.pubkey {
  102. if let Ok(public) = parse_pub(p) {
  103. return Some(SalsaBox::new(&public, &private))
  104. } else {
  105. error!("Uncorrect public key in for contact {}", contact_name);
  106. }
  107. }
  108. } else {
  109. error!("Uncorrect Private key in config",);
  110. }
  111. None
  112. }
  113. }
  114. /// This struct holds information about preconfigured channels.
  115. /// In the TOML configuration file, we can configure channels as such:
  116. /// ```toml
  117. /// [channel."#dev"]
  118. /// secret = "GvH4kno3kUu6dqPrZ8zjMhqxTUDZ2ev16EdprZiZJgj1"
  119. /// topic = "DarkFi Development Channel"
  120. /// ```
  121. /// Having a secret will enable a NaCl box that is able to encrypt and
  122. /// decrypt messages in this channel using this set shared secret.
  123. /// The secret should be shared OOB, via a secure channel.
  124. /// Having a topic set is useful if one wants to have a topic in the
  125. /// configured channel. It is not shared with others, but it is useful
  126. /// for personal reference.
  127. #[derive(Default, Clone, Debug, Serialize, Deserialize)]
  128. pub struct ChannelInfo {
  129. /// Optional topic for the channel
  130. pub topic: Option<String>,
  131. /// Optional NaCl box for the channel, used for {en,de}cryption.
  132. pub secret: Option<String>,
  133. /// Flag indicates whether the user has joined the channel or not
  134. #[serde(default, skip_serializing)]
  135. pub joined: bool,
  136. /// All nicknames which are visible on the channel
  137. #[serde(default, skip_serializing)]
  138. pub names: Vec<String>,
  139. }
  140. impl ChannelInfo {
  141. pub fn new() -> Self {
  142. Self { topic: None, secret: None, joined: false, names: vec![] }
  143. }
  144. pub fn salt_box(&self, channel_name: &str) -> Option<SalsaBox> {
  145. if let Some(s) = &self.secret {
  146. let secret = parse_priv(s);
  147. if secret.is_err() {
  148. error!("Uncorrect secret key for the channel {}", channel_name);
  149. return None
  150. }
  151. let secret = secret.unwrap();
  152. let public = secret.public_key();
  153. return Some(SalsaBox::new(&public, &secret))
  154. }
  155. None
  156. }
  157. }
  158. pub fn get_current_time() -> u64 {
  159. let start = std::time::SystemTime::now();
  160. start
  161. .duration_since(std::time::UNIX_EPOCH)
  162. .expect("Time went backwards")
  163. .as_millis()
  164. .try_into()
  165. .unwrap()
  166. }
  167. fn parse_priv(key: &str) -> Result<crypto_box::SecretKey> {
  168. let bytes: [u8; 32] = bs58::decode(key).into_vec()?.try_into().unwrap();
  169. Ok(crypto_box::SecretKey::from(bytes))
  170. }
  171. fn parse_pub(key: &str) -> Result<crypto_box::PublicKey> {
  172. let bytes: [u8; 32] = bs58::decode(key).into_vec()?.try_into().unwrap();
  173. Ok(crypto_box::PublicKey::from(bytes))
  174. }