settings.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. /// Advertise our external address
  68. pub advertise: bool,
  69. /// Hostlist storage path
  70. pub hostlist: String,
  71. /// Pause interval within greylist refinery process
  72. pub greylist_refinery_interval: u64,
  73. /// Percent of connections to come from the whitelist
  74. pub white_connection_percent: usize,
  75. /// Number of anchorlist connections
  76. pub anchor_connection_count: usize,
  77. }
  78. impl Default for Settings {
  79. fn default() -> Self {
  80. let version = option_env!("CARGO_PKG_VERSION").unwrap_or("0.0.0");
  81. let app_version = semver::Version::parse(version).unwrap();
  82. // TODO: We don't have a cross-platform method for the app directory (.local/darkfi)
  83. // in util/path.rs currently.
  84. let hostlist =
  85. String::from(format!("~/.local/darkfi/{}/hostlist.tsv", env!("CARGO_PKG_NAME")));
  86. Self {
  87. node_id: String::new(),
  88. inbound_addrs: vec![],
  89. external_addrs: vec![],
  90. peers: vec![],
  91. seeds: vec![],
  92. app_version,
  93. allowed_transports: vec![],
  94. transport_mixing: true,
  95. outbound_connections: 0,
  96. inbound_connections: 0,
  97. manual_attempt_limit: 0,
  98. outbound_connect_timeout: 15,
  99. channel_handshake_timeout: 10,
  100. channel_heartbeat_interval: 30,
  101. localnet: false,
  102. hosts_quarantine_limit: 50,
  103. outbound_peer_discovery_cooloff_time: 30,
  104. outbound_peer_discovery_attempt_time: 5,
  105. advertise: true,
  106. hostlist,
  107. greylist_refinery_interval: 5,
  108. white_connection_percent: 90,
  109. anchor_connection_count: 2,
  110. }
  111. }
  112. }
  113. // The following is used so we can have P2P settings configurable
  114. // from TOML files.
  115. /// Defines the network settings.
  116. #[derive(Clone, Debug, serde::Deserialize, structopt::StructOpt, structopt_toml::StructOptToml)]
  117. #[structopt()]
  118. pub struct SettingsOpt {
  119. /// P2P accept address node listens to for inbound connections
  120. #[serde(default)]
  121. #[structopt(long = "accept")]
  122. pub inbound: Vec<Url>,
  123. /// Outbound connection slots number
  124. #[structopt(long = "outbound-slots")]
  125. pub outbound_connections: Option<usize>,
  126. /// Inbound connection slots number
  127. #[structopt(long = "inbound-slots")]
  128. pub inbound_connections: Option<usize>,
  129. /// P2P external addresses node advertises so other peers can
  130. /// reach us and connect to us, as long as inbound addresses
  131. /// are also configured
  132. #[serde(default)]
  133. #[structopt(long)]
  134. pub external_addrs: Vec<Url>,
  135. /// Peer nodes to manually connect to
  136. #[serde(default)]
  137. #[structopt(long)]
  138. pub peers: Vec<Url>,
  139. /// Seed nodes to connect to for peers retrieval and/or
  140. /// advertising our own external addresses
  141. #[serde(default)]
  142. #[structopt(long)]
  143. pub seeds: Vec<Url>,
  144. /// Manual connections retry limit
  145. #[structopt(skip)]
  146. pub manual_attempt_limit: Option<usize>,
  147. /// Connection establishment timeout in seconds
  148. #[structopt(skip)]
  149. pub outbound_connect_timeout: Option<u64>,
  150. /// Exchange versions (handshake) timeout in seconds
  151. #[structopt(skip)]
  152. pub channel_handshake_timeout: Option<u64>,
  153. /// Ping-pong exchange execution interval in seconds
  154. #[structopt(skip)]
  155. pub channel_heartbeat_interval: Option<u64>,
  156. /// Only used for debugging. Compromises privacy when set.
  157. #[serde(default)]
  158. #[structopt(skip)]
  159. pub node_id: String,
  160. /// Preferred transports for outbound connections
  161. #[serde(default)]
  162. #[structopt(long = "transports")]
  163. pub allowed_transports: Vec<String>,
  164. /// Allow transport mixing (e.g. Tor would be allowed to connect to `tcp://`)
  165. #[structopt(long)]
  166. pub transport_mixing: Option<bool>,
  167. /// Allow localnet hosts
  168. #[serde(default)]
  169. #[structopt(long)]
  170. pub localnet: bool,
  171. /// Delete a peer from hosts if they've been quarantined N times
  172. #[structopt(skip)]
  173. pub hosts_quarantine_limit: Option<usize>,
  174. /// Cooling off time for peer discovery when unsuccessful
  175. #[structopt(skip)]
  176. pub outbound_peer_discovery_cooloff_time: Option<u64>,
  177. /// Time between peer discovery attempts
  178. #[structopt(skip)]
  179. pub outbound_peer_discovery_attempt_time: Option<u64>,
  180. /// Advertise our external address
  181. #[serde(default)]
  182. #[structopt(long)]
  183. pub advertise: bool,
  184. /// Hosts .tsv file to use
  185. #[serde(default)]
  186. #[structopt(long)]
  187. pub hostlist: String,
  188. /// Pause interval within greylist refinery process
  189. #[structopt(skip)]
  190. pub greylist_refinery_interval: Option<u64>,
  191. /// Percent of connections to come from the whitelist
  192. #[structopt(skip)]
  193. pub white_connection_percent: Option<usize>,
  194. /// Number of anchorlist connections
  195. #[structopt(skip)]
  196. pub anchor_connection_count: Option<usize>,
  197. }
  198. impl From<SettingsOpt> for Settings {
  199. fn from(opt: SettingsOpt) -> Self {
  200. let def = Settings::default();
  201. Self {
  202. node_id: opt.node_id,
  203. inbound_addrs: opt.inbound,
  204. external_addrs: opt.external_addrs,
  205. peers: opt.peers,
  206. seeds: opt.seeds,
  207. app_version: def.app_version,
  208. allowed_transports: opt.allowed_transports,
  209. transport_mixing: opt.transport_mixing.unwrap_or(def.transport_mixing),
  210. outbound_connections: opt.outbound_connections.unwrap_or(def.outbound_connections),
  211. inbound_connections: opt.inbound_connections.unwrap_or(def.inbound_connections),
  212. manual_attempt_limit: opt.manual_attempt_limit.unwrap_or(def.manual_attempt_limit),
  213. outbound_connect_timeout: opt
  214. .outbound_connect_timeout
  215. .unwrap_or(def.outbound_connect_timeout),
  216. channel_handshake_timeout: opt
  217. .channel_handshake_timeout
  218. .unwrap_or(def.channel_handshake_timeout),
  219. channel_heartbeat_interval: opt
  220. .channel_heartbeat_interval
  221. .unwrap_or(def.channel_heartbeat_interval),
  222. localnet: opt.localnet,
  223. hosts_quarantine_limit: opt
  224. .hosts_quarantine_limit
  225. .unwrap_or(def.hosts_quarantine_limit),
  226. outbound_peer_discovery_cooloff_time: opt
  227. .outbound_peer_discovery_cooloff_time
  228. .unwrap_or(def.outbound_peer_discovery_cooloff_time),
  229. outbound_peer_discovery_attempt_time: opt
  230. .outbound_peer_discovery_attempt_time
  231. .unwrap_or(def.outbound_peer_discovery_attempt_time),
  232. advertise: opt.advertise,
  233. hostlist: opt.hostlist,
  234. greylist_refinery_interval: opt
  235. .greylist_refinery_interval
  236. .unwrap_or(def.greylist_refinery_interval),
  237. white_connection_percent: opt
  238. .white_connection_percent
  239. .unwrap_or(def.white_connection_percent),
  240. anchor_connection_count: opt
  241. .anchor_connection_count
  242. .unwrap_or(def.anchor_connection_count),
  243. }
  244. }
  245. }