settings.rs 12 KB

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