config.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. use fxhash::FxHashMap;
  2. use log::{info, warn};
  3. use structopt_toml::{serde::Deserialize, structopt::StructOpt, StructOptToml};
  4. use toml::Value;
  5. use url::Url;
  6. use darkfi::{cli_desc, Result};
  7. #[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
  8. #[serde(default)]
  9. #[structopt(name = "lilith", about = cli_desc!())]
  10. pub struct Args {
  11. #[structopt(long, default_value = "tcp://127.0.0.1:18927")]
  12. /// JSON-RPC listen URL
  13. pub rpc_listen: Url,
  14. #[structopt(short, long)]
  15. /// Configuration file to use
  16. pub config: Option<String>,
  17. #[structopt(long)]
  18. /// Daemon published urls, common for all enabled networks (repeatable flag)
  19. pub urls: Vec<Url>,
  20. #[structopt(long, default_value = "~/.config/darkfi/lilith_hosts.tsv")]
  21. /// Hosts .tsv file to use
  22. pub hosts_file: String,
  23. #[structopt(short, parse(from_occurrences))]
  24. /// Increase verbosity (-vvv supported)
  25. pub verbose: u8,
  26. }
  27. /// Defines the network specific settings
  28. #[derive(Clone)]
  29. pub struct NetInfo {
  30. /// Specific port the network will use
  31. pub port: u16,
  32. /// Connect to seeds (repeatable flag)
  33. pub seeds: Vec<Url>,
  34. /// Connect to peers (repeatable flag)
  35. pub peers: Vec<Url>,
  36. /// Enable localnet hosts
  37. pub localnet: bool,
  38. /// Enable channel log
  39. pub channel_log: 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 channel_log = if table.contains_key("channel_log") {
  93. table["channel_log"].as_bool().unwrap()
  94. } else {
  95. false
  96. };
  97. let net_info = NetInfo { port, seeds, peers, localnet, channel_log };
  98. ret.insert(name, net_info);
  99. }
  100. }
  101. };
  102. Ok(ret)
  103. }