config.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::collections::HashMap;
  19. use log::{info, warn};
  20. use structopt_toml::{serde::Deserialize, structopt::StructOpt, StructOptToml};
  21. use toml::Value;
  22. use url::Url;
  23. use darkfi::{cli_desc, Result};
  24. #[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
  25. #[serde(default)]
  26. #[structopt(name = "lilith", about = cli_desc!())]
  27. pub struct Args {
  28. #[structopt(long, default_value = "tcp://127.0.0.1:18927")]
  29. /// JSON-RPC listen URL
  30. pub rpc_listen: Url,
  31. #[structopt(short, long)]
  32. /// Configuration file to use
  33. pub config: Option<String>,
  34. #[structopt(long)]
  35. /// Daemon published urls, common for all enabled networks (repeatable flag)
  36. pub urls: Vec<Url>,
  37. #[structopt(long, default_value = "~/.config/darkfi/lilith_hosts.tsv")]
  38. /// Hosts .tsv file to use
  39. pub hosts_file: String,
  40. #[structopt(short, parse(from_occurrences))]
  41. /// Increase verbosity (-vvv supported)
  42. pub verbose: u8,
  43. }
  44. /// Defines the network specific settings
  45. #[derive(Clone)]
  46. pub struct NetInfo {
  47. /// Specific port the network will use
  48. pub port: u16,
  49. /// Connect to seeds (repeatable flag)
  50. pub seeds: Vec<Url>,
  51. /// Connect to peers (repeatable flag)
  52. pub peers: Vec<Url>,
  53. /// Enable localnet hosts
  54. pub localnet: bool,
  55. /// Enable channel log
  56. pub channel_log: bool,
  57. }
  58. /// Parse a TOML string for any configured network and return
  59. /// a map containing said configurations.
  60. ///
  61. /// ```toml
  62. /// [network."darkfid_sync"]
  63. /// port = 33032
  64. /// seeds = []
  65. /// peers = []
  66. /// ```
  67. pub fn parse_configured_networks(data: &str) -> Result<HashMap<String, NetInfo>> {
  68. let mut ret = HashMap::new();
  69. if let Value::Table(map) = toml::from_str(data)? {
  70. if map.contains_key("network") && map["network"].is_table() {
  71. for net in map["network"].as_table().unwrap() {
  72. info!("Found configuration for network: {}", net.0);
  73. let table = net.1.as_table().unwrap();
  74. if !table.contains_key("port") {
  75. warn!("Network port is mandatory, skipping network.");
  76. continue
  77. }
  78. let name = net.0.to_string();
  79. let port = table["port"].as_integer().unwrap().try_into().unwrap();
  80. let mut seeds = vec![];
  81. if table.contains_key("seeds") {
  82. if let Some(s) = table["seeds"].as_array() {
  83. for seed in s {
  84. if let Some(u) = seed.as_str() {
  85. if let Ok(url) = Url::parse(u) {
  86. seeds.push(url);
  87. }
  88. }
  89. }
  90. }
  91. }
  92. let mut peers = vec![];
  93. if table.contains_key("peers") {
  94. if let Some(p) = table["peers"].as_array() {
  95. for peer in p {
  96. if let Some(u) = peer.as_str() {
  97. if let Ok(url) = Url::parse(u) {
  98. peers.push(url);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. let localnet = if table.contains_key("localnet") {
  105. table["localnet"].as_bool().unwrap()
  106. } else {
  107. false
  108. };
  109. let channel_log = if table.contains_key("channel_log") {
  110. table["channel_log"].as_bool().unwrap()
  111. } else {
  112. false
  113. };
  114. let net_info = NetInfo { port, seeds, peers, localnet, channel_log };
  115. ret.insert(name, net_info);
  116. }
  117. }
  118. };
  119. Ok(ret)
  120. }