settings.rs 3.8 KB

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