settings.rs 12 KB

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