settings.rs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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::sync::Arc;
  19. use structopt::StructOpt;
  20. use url::Url;
  21. /// Atomic pointer to network settings
  22. pub type SettingsPtr = Arc<Settings>;
  23. /// P2P network settings. The scope of this is a P2P network instance
  24. /// configured by the library user.
  25. #[derive(Debug, Clone)]
  26. pub struct Settings {
  27. /// Only used for debugging, compromises privacy when set
  28. pub node_id: String,
  29. /// P2P accept addresses the instance listens on for inbound connections
  30. pub inbound_addrs: Vec<Url>,
  31. /// P2P external addresses the instance advertises so other peers can
  32. /// reach us and connect to us, as long as inbound addrs are configured
  33. pub external_addrs: Vec<Url>,
  34. /// Peer nodes to manually connect to
  35. pub peers: Vec<Url>,
  36. /// Seed nodes to connect to for peer discovery and/or adversising our
  37. /// own external addresses
  38. pub seeds: Vec<Url>,
  39. /// Application version, used for convenient protocol matching
  40. pub app_version: semver::Version,
  41. /// Whitelisted network transports for outbound connections
  42. pub allowed_transports: Vec<String>,
  43. /// Allow transport mixing (e.g. Tor would be allowed to connect to `tcp://`)
  44. pub transport_mixing: bool,
  45. /// Outbound connection slots number, this many connections will be
  46. /// attempted. (This does not include manual connections)
  47. pub outbound_connections: usize,
  48. /// Inbound connection slots number, this many active listening connections
  49. /// will be allowed. (This does not include manual connections)
  50. pub inbound_connections: usize,
  51. /// Manual connections retry limit, 0 for forever looping
  52. pub manual_attempt_limit: usize,
  53. /// Outbound connection timeout (in seconds)
  54. pub outbound_connect_timeout: u64,
  55. /// Exchange versions (handshake) timeout (in seconds)
  56. pub channel_handshake_timeout: u64,
  57. /// Ping-pong exchange execution interval (in seconds)
  58. pub channel_heartbeat_interval: u64,
  59. /// Allow localnet hosts
  60. pub localnet: bool,
  61. /// Delete a peer from hosts if they've been quarantined N times
  62. pub hosts_quarantine_limit: usize,
  63. /// Cooling off time for peer discovery when unsuccessful
  64. pub outbound_peer_discovery_cooloff_time: u64,
  65. /// Time between peer discovery attempts
  66. pub outbound_peer_discovery_attempt_time: u64,
  67. }
  68. impl Default for Settings {
  69. fn default() -> Self {
  70. let version = option_env!("CARGO_PKG_VERSION").unwrap_or("0.0.0");
  71. let app_version = semver::Version::parse(version).unwrap();
  72. Self {
  73. node_id: String::new(),
  74. inbound_addrs: vec![],
  75. external_addrs: vec![],
  76. peers: vec![],
  77. seeds: vec![],
  78. app_version,
  79. allowed_transports: vec![],
  80. transport_mixing: true,
  81. outbound_connections: 0,
  82. inbound_connections: 0,
  83. manual_attempt_limit: 0,
  84. outbound_connect_timeout: 15,
  85. channel_handshake_timeout: 10,
  86. channel_heartbeat_interval: 10,
  87. localnet: false,
  88. hosts_quarantine_limit: 50,
  89. outbound_peer_discovery_cooloff_time: 30,
  90. outbound_peer_discovery_attempt_time: 5,
  91. }
  92. }
  93. }
  94. // The following is used so we can have P2P settings configurable
  95. // from TOML files.
  96. /// Defines the network settings.
  97. #[derive(Clone, Debug, serde::Deserialize, structopt::StructOpt, structopt_toml::StructOptToml)]
  98. #[structopt()]
  99. pub struct SettingsOpt {
  100. /// P2P accept address node listens to for inbound connections
  101. #[serde(default)]
  102. #[structopt(long = "accept")]
  103. pub inbound: Vec<Url>,
  104. /// Outbound connection slots number
  105. #[structopt(long = "outbound-slots")]
  106. pub outbound_connections: Option<usize>,
  107. /// Inbound connection slots number
  108. #[structopt(long = "inbound-slots")]
  109. pub inbound_connections: Option<usize>,
  110. /// P2P external addresses node advertises so other peers can
  111. /// reach us and connect to us, as long as inbound addresses
  112. /// are also configured
  113. #[serde(default)]
  114. #[structopt(long)]
  115. pub external_addrs: Vec<Url>,
  116. /// Peer nodes to manually connect to
  117. #[serde(default)]
  118. #[structopt(long)]
  119. pub peers: Vec<Url>,
  120. /// Seed nodes to connect to for peers retrieval and/or
  121. /// advertising our own external addresses
  122. #[serde(default)]
  123. #[structopt(long)]
  124. pub seeds: Vec<Url>,
  125. /// Manual connections retry limit
  126. #[structopt(skip)]
  127. pub manual_attempt_limit: Option<usize>,
  128. /// Connection establishment timeout in seconds
  129. #[structopt(skip)]
  130. pub outbound_connect_timeout: Option<u64>,
  131. /// Exchange versions (handshake) timeout in seconds
  132. #[structopt(skip)]
  133. pub channel_handshake_timeout: Option<u64>,
  134. /// Ping-pong exchange execution interval in seconds
  135. #[structopt(skip)]
  136. pub channel_heartbeat_interval: Option<u64>,
  137. /// Only used for debugging. Compromises privacy when set.
  138. #[serde(default)]
  139. #[structopt(skip)]
  140. pub node_id: String,
  141. /// Preferred transports for outbound connections
  142. #[serde(default)]
  143. #[structopt(long = "transports")]
  144. pub allowed_transports: Vec<String>,
  145. #[structopt(long)]
  146. pub transport_mixing: Option<bool>,
  147. /// Allow localnet hosts
  148. #[serde(default)]
  149. #[structopt(long)]
  150. pub localnet: bool,
  151. #[structopt(skip)]
  152. pub hosts_quarantine_limit: Option<usize>,
  153. #[structopt(skip)]
  154. pub outbound_peer_discovery_cooloff_time: Option<u64>,
  155. #[structopt(skip)]
  156. pub outbound_peer_discovery_attempt_time: Option<u64>,
  157. }
  158. impl From<SettingsOpt> for Settings {
  159. fn from(opt: SettingsOpt) -> Self {
  160. let version = option_env!("CARGO_PKG_VERSION").unwrap_or("0.0.0");
  161. let app_version = semver::Version::parse(version).unwrap();
  162. Self {
  163. node_id: opt.node_id,
  164. inbound_addrs: opt.inbound,
  165. external_addrs: opt.external_addrs,
  166. peers: opt.peers,
  167. seeds: opt.seeds,
  168. app_version,
  169. allowed_transports: opt.allowed_transports,
  170. transport_mixing: opt.transport_mixing.unwrap_or(false),
  171. outbound_connections: opt.outbound_connections.unwrap_or(0),
  172. inbound_connections: opt.inbound_connections.unwrap_or(0),
  173. manual_attempt_limit: opt.manual_attempt_limit.unwrap_or(0),
  174. outbound_connect_timeout: opt.outbound_connect_timeout.unwrap_or(15),
  175. channel_handshake_timeout: opt.channel_handshake_timeout.unwrap_or(10),
  176. channel_heartbeat_interval: opt.channel_heartbeat_interval.unwrap_or(10),
  177. localnet: opt.localnet,
  178. hosts_quarantine_limit: opt.hosts_quarantine_limit.unwrap_or(15),
  179. outbound_peer_discovery_cooloff_time: opt
  180. .outbound_peer_discovery_cooloff_time
  181. .unwrap_or(30),
  182. outbound_peer_discovery_attempt_time: opt
  183. .outbound_peer_discovery_attempt_time
  184. .unwrap_or(5),
  185. }
  186. }
  187. }