config.rs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. use fxhash::FxHashMap;
  2. use log::{info, warn};
  3. use serde_derive::Deserialize;
  4. use structopt::StructOpt;
  5. use structopt_toml::StructOptToml;
  6. use toml::Value;
  7. use url::Url;
  8. use darkfi::{cli_desc, Result};
  9. #[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
  10. #[serde(default)]
  11. #[structopt(name = "lilith", about = cli_desc!())]
  12. pub struct Args {
  13. #[structopt(short, long)]
  14. /// Configuration file to use
  15. pub config: Option<String>,
  16. #[structopt(long)]
  17. /// Daemon published urls, common for all enabled networks (repeatable flag)
  18. pub urls: Vec<Url>,
  19. #[structopt(short, parse(from_occurrences))]
  20. /// Increase verbosity (-vvv supported)
  21. pub verbose: u8,
  22. }
  23. /// Defines the network specific settings
  24. #[derive(Clone)]
  25. pub struct NetInfo {
  26. /// Specific port the network will use
  27. pub port: u16,
  28. /// Connect to seeds (repeatable flag)
  29. pub seeds: Vec<Url>,
  30. /// Connect to peers (repeatable flag)
  31. pub peers: Vec<Url>,
  32. }
  33. /// Parse a TOML string for any configured network and return
  34. /// a map containing said configurations.
  35. ///
  36. /// ```toml
  37. /// [network."darkfid_sync"]
  38. /// port = 33032
  39. /// seeds = []
  40. /// peers = []
  41. /// ```
  42. pub fn parse_configured_networks(data: &str) -> Result<FxHashMap<String, NetInfo>> {
  43. let mut ret = FxHashMap::default();
  44. if let Value::Table(map) = toml::from_str(data)? {
  45. if map.contains_key("network") && map["network"].is_table() {
  46. for net in map["network"].as_table().unwrap() {
  47. info!("Found configuration for network: {}", net.0);
  48. let table = net.1.as_table().unwrap();
  49. if !table.contains_key("port") {
  50. warn!("Network port is mandatory, skipping network.");
  51. continue
  52. }
  53. let name = net.0.to_string();
  54. let port = table["port"].as_integer().unwrap().try_into().unwrap();
  55. let mut seeds = vec![];
  56. if table.contains_key("seeds") {
  57. if let Some(s) = table["seeds"].as_array() {
  58. for seed in s {
  59. if let Some(u) = seed.as_str() {
  60. if let Ok(url) = Url::parse(u) {
  61. seeds.push(url);
  62. }
  63. }
  64. }
  65. }
  66. }
  67. let mut peers = vec![];
  68. if table.contains_key("peers") {
  69. if let Some(p) = table["peers"].as_array() {
  70. for peer in p {
  71. if let Some(u) = peer.as_str() {
  72. if let Ok(url) = Url::parse(u) {
  73. peers.push(url);
  74. }
  75. }
  76. }
  77. }
  78. }
  79. let net_info = NetInfo { port, seeds, peers };
  80. ret.insert(name, net_info);
  81. }
  82. }
  83. };
  84. Ok(ret)
  85. }