settings.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 structopt::StructOpt;
  19. use url::Url;
  20. type BlacklistEntry = (String, Vec<String>, Vec<u16>);
  21. /// Ban policies definitions.
  22. ///
  23. /// If the ban policy is set to `Relaxed` will not ban peers in case
  24. /// they send a message without a corresponding MessageDispatcher.
  25. /// This is useful for nodes that may not be subscribed to protocols,
  26. /// such as Lilith. For most uses this should be set to `Strict`.
  27. ///
  28. /// TODO: this will be deprecated when we introduce the p2p resource
  29. /// mananger.
  30. #[derive(Clone, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
  31. #[serde(rename_all = "lowercase")]
  32. pub enum BanPolicy {
  33. #[default]
  34. Strict,
  35. Relaxed,
  36. }
  37. /// P2P network settings. The scope of this is a P2P network instance
  38. /// configured by the library user.
  39. #[derive(Debug, Clone)]
  40. pub struct Settings {
  41. /// Only used for debugging, compromises privacy when set
  42. pub node_id: String,
  43. /// P2P accept addresses the instance listens on for inbound connections
  44. pub inbound_addrs: Vec<Url>,
  45. /// P2P external addresses the instance advertises so other peers can
  46. /// reach us and connect to us, as long as inbound addrs are configured
  47. pub external_addrs: Vec<Url>,
  48. /// Peer nodes to manually connect to
  49. pub peers: Vec<Url>,
  50. /// Seed nodes to connect to for peer discovery and/or adversising our
  51. /// own external addresses
  52. pub seeds: Vec<Url>,
  53. /// Application version, used for convenient protocol matching
  54. pub app_version: semver::Version,
  55. /// Whitelisted network transports for outbound connections
  56. pub allowed_transports: Vec<String>,
  57. /// Allow transport mixing (e.g. Tor would be allowed to connect to `tcp://`)
  58. pub transport_mixing: bool,
  59. /// Outbound connection slots number, this many connections will be
  60. /// attempted. (This does not include manual connections)
  61. pub outbound_connections: usize,
  62. /// Inbound connection slots number, this many active listening connections
  63. /// will be allowed. (This does not include manual connections)
  64. pub inbound_connections: usize,
  65. /// Outbound connection timeout (in seconds)
  66. pub outbound_connect_timeout: u64,
  67. /// Exchange versions (handshake) timeout (in seconds)
  68. pub channel_handshake_timeout: u64,
  69. /// Ping-pong exchange execution interval (in seconds)
  70. pub channel_heartbeat_interval: u64,
  71. /// Allow localnet hosts
  72. pub localnet: bool,
  73. /// Cooling off time for peer discovery when unsuccessful
  74. pub outbound_peer_discovery_cooloff_time: u64,
  75. /// Time between peer discovery attempts
  76. pub outbound_peer_discovery_attempt_time: u64,
  77. /// P2P datastore path
  78. pub datastore: Option<String>,
  79. /// Hostlist storage path
  80. pub hostlist: Option<String>,
  81. /// Pause interval within greylist refinery process
  82. pub greylist_refinery_interval: u64,
  83. /// Percent of connections to come from the whitelist
  84. pub white_connect_percent: usize,
  85. /// Number of goldlist connections
  86. pub gold_connect_count: usize,
  87. /// If this is true, strictly follow the gold_connect_count and
  88. /// white_connect_percent settings. Otherwise, connect to greylist
  89. /// entries if we have no white or gold connections.
  90. pub slot_preference_strict: bool,
  91. /// Number of seconds with no connections after which refinery
  92. /// process is paused.
  93. pub time_with_no_connections: u64,
  94. /// Nodes to avoid interacting with for the duration of the program,
  95. /// in the format ["host", ["scheme", "scheme"], [port, port]]
  96. /// If scheme is left empty it will default to "tcp+tls".
  97. /// If ports are left empty all ports from this peer will be blocked.
  98. pub blacklist: Vec<BlacklistEntry>,
  99. /// Do not ban nodes that send messages without dispatchers if set
  100. /// to `Relaxed`. For most uses, should be set to `Strict`.
  101. pub ban_policy: BanPolicy,
  102. }
  103. impl Default for Settings {
  104. fn default() -> Self {
  105. let version = option_env!("CARGO_PKG_VERSION").unwrap_or("0.0.0");
  106. let app_version = semver::Version::parse(version).unwrap();
  107. Self {
  108. node_id: String::new(),
  109. inbound_addrs: vec![],
  110. external_addrs: vec![],
  111. peers: vec![],
  112. seeds: vec![],
  113. app_version,
  114. allowed_transports: vec!["tcp+tls".to_string()],
  115. transport_mixing: true,
  116. outbound_connections: 8,
  117. inbound_connections: 8,
  118. outbound_connect_timeout: 15,
  119. channel_handshake_timeout: 10,
  120. channel_heartbeat_interval: 30,
  121. localnet: false,
  122. outbound_peer_discovery_cooloff_time: 30,
  123. outbound_peer_discovery_attempt_time: 5,
  124. datastore: None,
  125. hostlist: None,
  126. greylist_refinery_interval: 15,
  127. white_connect_percent: 70,
  128. gold_connect_count: 2,
  129. slot_preference_strict: false,
  130. time_with_no_connections: 30,
  131. blacklist: vec![],
  132. ban_policy: BanPolicy::Strict,
  133. }
  134. }
  135. }
  136. // The following is used so we can have P2P settings configurable
  137. // from TOML files.
  138. /// Defines the network settings.
  139. #[derive(Clone, Debug, serde::Deserialize, structopt::StructOpt, structopt_toml::StructOptToml)]
  140. #[structopt()]
  141. pub struct SettingsOpt {
  142. /// P2P accept address node listens to for inbound connections
  143. #[serde(default)]
  144. #[structopt(long = "accept")]
  145. pub inbound: Vec<Url>,
  146. /// Outbound connection slots number
  147. #[structopt(long = "outbound-slots")]
  148. pub outbound_connections: Option<usize>,
  149. /// Inbound connection slots number
  150. #[structopt(long = "inbound-slots")]
  151. pub inbound_connections: Option<usize>,
  152. /// P2P external addresses node advertises so other peers can
  153. /// reach us and connect to us, as long as inbound addresses
  154. /// are also configured
  155. #[serde(default)]
  156. #[structopt(long)]
  157. pub external_addrs: Vec<Url>,
  158. /// Peer nodes to manually connect to
  159. #[serde(default)]
  160. #[structopt(long)]
  161. pub peers: Vec<Url>,
  162. /// Seed nodes to connect to for peers retrieval and/or
  163. /// advertising our own external addresses
  164. #[serde(default)]
  165. #[structopt(long)]
  166. pub seeds: Vec<Url>,
  167. /// Connection establishment timeout in seconds
  168. #[structopt(skip)]
  169. pub outbound_connect_timeout: Option<u64>,
  170. /// Exchange versions (handshake) timeout in seconds
  171. #[structopt(skip)]
  172. pub channel_handshake_timeout: Option<u64>,
  173. /// Ping-pong exchange execution interval in seconds
  174. #[structopt(skip)]
  175. pub channel_heartbeat_interval: Option<u64>,
  176. /// Only used for debugging. Compromises privacy when set.
  177. #[serde(default)]
  178. #[structopt(skip)]
  179. pub node_id: String,
  180. /// Preferred transports for outbound connections
  181. #[serde(default)]
  182. #[structopt(long = "transports")]
  183. pub allowed_transports: Option<Vec<String>>,
  184. /// Allow transport mixing (e.g. Tor would be allowed to connect to `tcp://`)
  185. #[structopt(long)]
  186. pub transport_mixing: Option<bool>,
  187. /// If this is true, strictly follow the gold_connect_count and
  188. /// white_connect_percent settings. Otherwise, connect to greylist
  189. /// entries if we have no white or gold connections.
  190. #[serde(default)]
  191. #[structopt(long)]
  192. pub localnet: bool,
  193. /// Cooling off time for peer discovery when unsuccessful
  194. #[structopt(skip)]
  195. pub outbound_peer_discovery_cooloff_time: Option<u64>,
  196. /// Time between peer discovery attempts
  197. #[structopt(skip)]
  198. pub outbound_peer_discovery_attempt_time: Option<u64>,
  199. /// P2P datastore path
  200. #[serde(default)]
  201. #[structopt(long)]
  202. pub datastore: Option<String>,
  203. /// Hosts .tsv file to use
  204. #[serde(default)]
  205. #[structopt(long)]
  206. pub hostlist: Option<String>,
  207. /// Pause interval within greylist refinery process
  208. #[structopt(skip)]
  209. pub greylist_refinery_interval: Option<u64>,
  210. /// Number of whitelist connections
  211. #[structopt(skip)]
  212. pub white_connect_percent: Option<usize>,
  213. /// Number of goldlist connections
  214. #[structopt(skip)]
  215. pub gold_connect_count: Option<usize>,
  216. /// Allow localnet hosts
  217. #[serde(default)]
  218. #[structopt(long)]
  219. pub slot_preference_strict: bool,
  220. /// Number of seconds with no connections after which refinery
  221. /// process is paused.
  222. #[structopt(skip)]
  223. pub time_with_no_connections: Option<u64>,
  224. /// Nodes to avoid interacting with for the duration of the program,
  225. /// in the format ["host", ["scheme", "scheme"], [port, port]]
  226. /// If scheme is left empty it will default to "tcp+tls".
  227. /// If ports are left empty all ports from this peer will be blocked.
  228. #[serde(default)]
  229. #[structopt(skip)]
  230. pub blacklist: Vec<BlacklistEntry>,
  231. /// Do not ban nodes that send messages without dispatchers if set
  232. /// to `Relaxed`. For most uses, should be set to `Strict`.
  233. #[serde(default)]
  234. #[structopt(skip)]
  235. pub ban_policy: BanPolicy,
  236. }
  237. impl From<SettingsOpt> for Settings {
  238. fn from(opt: SettingsOpt) -> Self {
  239. let def = Settings::default();
  240. Self {
  241. node_id: opt.node_id,
  242. inbound_addrs: opt.inbound,
  243. external_addrs: opt.external_addrs,
  244. peers: opt.peers,
  245. seeds: opt.seeds,
  246. app_version: def.app_version,
  247. allowed_transports: opt.allowed_transports.unwrap_or(def.allowed_transports),
  248. transport_mixing: opt.transport_mixing.unwrap_or(def.transport_mixing),
  249. outbound_connections: opt.outbound_connections.unwrap_or(def.outbound_connections),
  250. inbound_connections: opt.inbound_connections.unwrap_or(def.inbound_connections),
  251. outbound_connect_timeout: opt
  252. .outbound_connect_timeout
  253. .unwrap_or(def.outbound_connect_timeout),
  254. channel_handshake_timeout: opt
  255. .channel_handshake_timeout
  256. .unwrap_or(def.channel_handshake_timeout),
  257. channel_heartbeat_interval: opt
  258. .channel_heartbeat_interval
  259. .unwrap_or(def.channel_heartbeat_interval),
  260. localnet: opt.localnet,
  261. outbound_peer_discovery_cooloff_time: opt
  262. .outbound_peer_discovery_cooloff_time
  263. .unwrap_or(def.outbound_peer_discovery_cooloff_time),
  264. outbound_peer_discovery_attempt_time: opt
  265. .outbound_peer_discovery_attempt_time
  266. .unwrap_or(def.outbound_peer_discovery_attempt_time),
  267. datastore: opt.datastore,
  268. hostlist: opt.hostlist,
  269. greylist_refinery_interval: opt
  270. .greylist_refinery_interval
  271. .unwrap_or(def.greylist_refinery_interval),
  272. white_connect_percent: opt.white_connect_percent.unwrap_or(def.white_connect_percent),
  273. gold_connect_count: opt.gold_connect_count.unwrap_or(def.gold_connect_count),
  274. slot_preference_strict: opt.slot_preference_strict,
  275. time_with_no_connections: opt
  276. .time_with_no_connections
  277. .unwrap_or(def.time_with_no_connections),
  278. blacklist: opt.blacklist,
  279. ban_policy: opt.ban_policy,
  280. }
  281. }
  282. }