settings.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. use std::{
  2. path::PathBuf,
  3. sync::{atomic::AtomicBool, Arc},
  4. };
  5. use fxhash::FxHashMap;
  6. use log::info;
  7. use serde::Deserialize;
  8. use structopt::StructOpt;
  9. use structopt_toml::StructOptToml;
  10. use toml::Value;
  11. use url::Url;
  12. use darkfi::{net::settings::SettingsOpt, Result};
  13. pub const CONFIG_FILE: &str = "ircd_config.toml";
  14. pub const CONFIG_FILE_CONTENTS: &str = include_str!("../ircd_config.toml");
  15. /// ircd cli
  16. #[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
  17. #[serde(default)]
  18. #[structopt(name = "ircd")]
  19. pub struct Args {
  20. /// Sets a custom config file
  21. #[structopt(long)]
  22. pub config: Option<String>,
  23. /// JSON-RPC listen URL
  24. #[structopt(long = "rpc", default_value = "tcp://127.0.0.1:11055")]
  25. pub rpc_listen: Url,
  26. /// IRC listen URL
  27. #[structopt(long = "irc", default_value = "tcp://127.0.0.1:11066")]
  28. pub irc_listen: Url,
  29. /// Sets Datastore Path
  30. #[structopt(long, default_value = "~/.config/darkfi/ircd")]
  31. pub datastore: String,
  32. /// Generate a new NaCl secret and exit
  33. #[structopt(long)]
  34. pub gen_secret: bool,
  35. /// Autojoin channels
  36. #[structopt(long)]
  37. pub autojoin: Vec<String>,
  38. #[structopt(flatten)]
  39. pub net: SettingsOpt,
  40. /// Increase verbosity
  41. #[structopt(short, parse(from_occurrences))]
  42. pub verbose: u8,
  43. }
  44. /// This struct holds information about preconfigured channels.
  45. /// In the TOML configuration file, we can configure channels as such:
  46. /// ```toml
  47. /// [channel."#dev"]
  48. /// secret = "GvH4kno3kUu6dqPrZ8zjMhqxTUDZ2ev16EdprZiZJgj1"
  49. /// topic = "DarkFi Development Channel"
  50. /// ```
  51. /// Having a secret will enable a NaCl box that is able to encrypt and
  52. /// decrypt messages in this channel using this set shared secret.
  53. /// The secret should be shared OOB, via a secure channel.
  54. /// Having a topic set is useful if one wants to have a topic in the
  55. /// configured channel. It is not shared with others, but it is useful
  56. /// for personal reference.
  57. #[derive(Clone)]
  58. pub struct ChannelInfo {
  59. /// Optional topic for the channel
  60. pub topic: Option<String>,
  61. /// Optional NaCl box for the channel, used for {en,de}cryption.
  62. pub salt_box: Option<crypto_box::Box>,
  63. /// Flag indicates whether the user has joined the channel or not
  64. pub joined: Arc<AtomicBool>,
  65. }
  66. impl ChannelInfo {
  67. pub fn new() -> Result<Self> {
  68. Ok(Self { topic: None, salt_box: None, joined: Arc::new(AtomicBool::new(true)) })
  69. }
  70. }
  71. /// Parse the configuration file for any configured channels and return
  72. /// a map containing said configurations.
  73. pub fn parse_configured_channels(config_file: &PathBuf) -> Result<FxHashMap<String, ChannelInfo>> {
  74. let toml_contents = std::fs::read_to_string(config_file)?;
  75. let mut ret = FxHashMap::default();
  76. if let Value::Table(map) = toml::from_str(&toml_contents)? {
  77. if map.contains_key("channel") && map["channel"].is_table() {
  78. for chan in map["channel"].as_table().unwrap() {
  79. info!("Found configuration for channel {}", chan.0);
  80. let mut channel_info = ChannelInfo::new()?;
  81. if chan.1.as_table().unwrap().contains_key("topic") {
  82. let topic = chan.1["topic"].as_str().unwrap().to_string();
  83. info!("Found topic for channel {}: {}", chan.0, topic);
  84. channel_info.topic = Some(topic);
  85. }
  86. if chan.1.as_table().unwrap().contains_key("secret") {
  87. // Build the NaCl box
  88. let s = chan.1["secret"].as_str().unwrap();
  89. let bytes: [u8; 32] = bs58::decode(s).into_vec()?.try_into().unwrap();
  90. let secret = crypto_box::SecretKey::from(bytes);
  91. let public = secret.public_key();
  92. let msg_box = crypto_box::Box::new(&public, &secret);
  93. channel_info.salt_box = Some(msg_box);
  94. info!("Instantiated NaCl box for channel {}", chan.0);
  95. }
  96. ret.insert(chan.0.to_string(), channel_info);
  97. }
  98. }
  99. };
  100. Ok(ret)
  101. }