settings.rs 9.9 KB

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