settings.rs 10 KB

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