config.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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(long, default_value = "tcp://127.0.0.1:18927")]
  14. /// JSON-RPC listen URL
  15. pub rpc_listen: Url,
  16. #[structopt(short, long)]
  17. /// Configuration file to use
  18. pub config: Option<String>,
  19. #[structopt(long)]
  20. /// Daemon published urls, common for all enabled networks (repeatable flag)
  21. pub urls: Vec<Url>,
  22. #[structopt(long, default_value = "~/.config/darkfi/lilith_hosts.tsv")]
  23. /// Hosts .tsv file to use
  24. pub hosts_file: String,
  25. #[structopt(short, parse(from_occurrences))]
  26. /// Increase verbosity (-vvv supported)
  27. pub verbose: u8,
  28. }
  29. /// Defines the network specific settings
  30. #[derive(Clone)]
  31. pub struct NetInfo {
  32. /// Specific port the network will use
  33. pub port: u16,
  34. /// Connect to seeds (repeatable flag)
  35. pub seeds: Vec<Url>,
  36. /// Connect to peers (repeatable flag)
  37. pub peers: Vec<Url>,
  38. /// Enable localnet hosts
  39. pub localnet: bool,
  40. }
  41. /// Parse a TOML string for any configured network and return
  42. /// a map containing said configurations.
  43. ///
  44. /// ```toml
  45. /// [network."darkfid_sync"]
  46. /// port = 33032
  47. /// seeds = []
  48. /// peers = []
  49. /// ```
  50. pub fn parse_configured_networks(data: &str) -> Result<FxHashMap<String, NetInfo>> {
  51. let mut ret = FxHashMap::default();
  52. if let Value::Table(map) = toml::from_str(data)? {
  53. if map.contains_key("network") && map["network"].is_table() {
  54. for net in map["network"].as_table().unwrap() {
  55. info!("Found configuration for network: {}", net.0);
  56. let table = net.1.as_table().unwrap();
  57. if !table.contains_key("port") {
  58. warn!("Network port is mandatory, skipping network.");
  59. continue
  60. }
  61. let name = net.0.to_string();
  62. let port = table["port"].as_integer().unwrap().try_into().unwrap();
  63. let mut seeds = vec![];
  64. if table.contains_key("seeds") {
  65. if let Some(s) = table["seeds"].as_array() {
  66. for seed in s {
  67. if let Some(u) = seed.as_str() {
  68. if let Ok(url) = Url::parse(u) {
  69. seeds.push(url);
  70. }
  71. }
  72. }
  73. }
  74. }
  75. let mut peers = vec![];
  76. if table.contains_key("peers") {
  77. if let Some(p) = table["peers"].as_array() {
  78. for peer in p {
  79. if let Some(u) = peer.as_str() {
  80. if let Ok(url) = Url::parse(u) {
  81. peers.push(url);
  82. }
  83. }
  84. }
  85. }
  86. }
  87. let localnet = if table.contains_key("localnet") {
  88. table["localnet"].as_bool().unwrap()
  89. } else {
  90. false
  91. };
  92. let net_info = NetInfo { port, seeds, peers, localnet };
  93. ret.insert(name, net_info);
  94. }
  95. }
  96. };
  97. Ok(ret)
  98. }