settings.rs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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::collections::HashMap;
  19. use structopt::StructOpt;
  20. use url::Url;
  21. use crate::error::{Error, Result};
  22. type BlacklistEntry = (String, Vec<String>, Vec<u16>);
  23. /// Ban policies definitions.
  24. ///
  25. /// If the ban policy is set to `Relaxed` will not ban peers in case
  26. /// they send a message without a corresponding MessageDispatcher.
  27. /// This is useful for nodes that may not be subscribed to protocols,
  28. /// such as Lilith. For most uses this should be set to `Strict`.
  29. ///
  30. /// TODO: this will be deprecated when we introduce the p2p resource
  31. /// mananger.
  32. #[derive(Clone, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
  33. #[serde(rename_all = "lowercase")]
  34. pub enum BanPolicy {
  35. #[default]
  36. Strict,
  37. Relaxed,
  38. }
  39. /// P2P network settings. The scope of this is a P2P network instance
  40. /// configured by the library user.
  41. #[derive(Debug, Clone)]
  42. pub struct Settings {
  43. /// Only used for debugging, compromises privacy when set
  44. pub node_id: String,
  45. /// P2P accept addresses the instance listens on for inbound connections
  46. pub inbound_addrs: Vec<Url>,
  47. /// P2P external addresses the instance advertises so other peers can
  48. /// reach us and connect to us, as long as inbound addrs are configured
  49. pub external_addrs: Vec<Url>,
  50. /// Peer nodes to manually connect to
  51. pub peers: Vec<Url>,
  52. /// Seed nodes to connect to for peer discovery and/or advertising our
  53. /// own external addresses
  54. pub seeds: Vec<Url>,
  55. /// Magic bytes should be unique per P2P network.
  56. /// Avoid bleeding of networks.
  57. pub magic_bytes: MagicBytes,
  58. /// Application version, used for convenient protocol matching
  59. pub app_version: semver::Version,
  60. /// Application Identifier
  61. pub app_name: String,
  62. /// Whitelisted network transports for outbound connections
  63. pub active_profiles: Vec<String>,
  64. /// Transports allowed to be mixed (tcp, tcp+tls, tor, tor+tls)
  65. /// When transport is added to this list the corresponding transport
  66. /// in active_profiles is used to connect to the node.
  67. /// Supported mixing scenarios include
  68. /// active_profile | mixed_profile
  69. /// tor | tcp
  70. /// tor+tls | tcp+tls
  71. /// socks5 | tor
  72. /// socks5 | tcp
  73. /// socks5+tls | tor+tls
  74. /// socks5+tls | tcp+tls
  75. pub mixed_profiles: Vec<String>,
  76. /// Tor socks5 proxy to connect to when socks5 or socks5+tls are added to active profiles
  77. /// and transport mixing is enabled
  78. pub tor_socks5_proxy: Option<Url>,
  79. /// Nym socks5 proxy to connect to when socks5 or socks5+tls are added to active profiles
  80. /// and transport mixing is enabled
  81. pub nym_socks5_proxy: Option<Url>,
  82. /// I2p Socks5 proxy to connect to i2p eepsite (hidden services)
  83. pub i2p_socks5_proxy: Url,
  84. /// Outbound connection slots number, this many connections will be
  85. /// attempted. (This does not include manual connections)
  86. pub outbound_connections: usize,
  87. /// Inbound connection slots number, this many active listening connections
  88. /// will be allowed. (This does not include manual connections)
  89. pub inbound_connections: usize,
  90. /// Allow localnet hosts
  91. pub localnet: bool,
  92. /// Cooling off time for peer discovery when unsuccessful
  93. pub outbound_peer_discovery_cooloff_time: u64,
  94. /// Time between peer discovery attempts
  95. pub outbound_peer_discovery_attempt_time: u64,
  96. /// Maximum number of addresses (with preferred transports) to receive from
  97. /// seeds and peers.
  98. /// If undefined, `outbound_connections` will be used instead.
  99. pub getaddrs_max: Option<u32>,
  100. /// P2P datastore path
  101. pub p2p_datastore: Option<String>,
  102. /// Hostlist storage path
  103. pub hostlist: Option<String>,
  104. /// Pause interval within greylist refinery process
  105. pub greylist_refinery_interval: u64,
  106. /// Percent of connections to come from the whitelist
  107. pub white_connect_percent: usize,
  108. /// Number of goldlist connections
  109. pub gold_connect_count: usize,
  110. /// If this is true, strictly follow the gold_connect_count and
  111. /// white_connect_percent settings. Otherwise, connect to greylist
  112. /// entries if we have no white or gold connections.
  113. pub slot_preference_strict: bool,
  114. /// Number of seconds with no connections after which refinery
  115. /// process is paused.
  116. pub time_with_no_connections: u64,
  117. /// Nodes to avoid interacting with for the duration of the program,
  118. /// in the format ["host", ["scheme", "scheme"], [port, port]]
  119. /// If scheme is left empty it will default to "tcp+tls".
  120. /// If ports are left empty all ports from this peer will be blocked.
  121. pub blacklist: Vec<BlacklistEntry>,
  122. /// Do not ban nodes that send messages without dispatchers if set
  123. /// to `Relaxed`. For most uses, should be set to `Strict`.
  124. pub ban_policy: BanPolicy,
  125. /// Mapping of transport/scheme to Network Profile
  126. pub profiles: HashMap<String, NetworkProfile>,
  127. }
  128. impl Default for Settings {
  129. fn default() -> Self {
  130. let version = option_env!("CARGO_PKG_VERSION").unwrap_or("0.0.0");
  131. let app_version = semver::Version::parse(version).unwrap();
  132. let app_name = option_env!("CARGO_PKG_NAME").unwrap_or("").to_string();
  133. Self {
  134. node_id: String::new(),
  135. inbound_addrs: vec![],
  136. external_addrs: vec![],
  137. magic_bytes: Default::default(),
  138. peers: vec![],
  139. seeds: vec![],
  140. app_version,
  141. app_name,
  142. active_profiles: vec![],
  143. mixed_profiles: vec![],
  144. tor_socks5_proxy: None,
  145. nym_socks5_proxy: None,
  146. i2p_socks5_proxy: Url::parse("socks5://127.0.0.1:4447").unwrap(),
  147. outbound_connections: 8,
  148. inbound_connections: 8,
  149. localnet: false,
  150. outbound_peer_discovery_cooloff_time: 30,
  151. outbound_peer_discovery_attempt_time: 5,
  152. getaddrs_max: None,
  153. p2p_datastore: None,
  154. hostlist: None,
  155. greylist_refinery_interval: 15,
  156. white_connect_percent: 70,
  157. gold_connect_count: 2,
  158. slot_preference_strict: false,
  159. time_with_no_connections: 30,
  160. blacklist: vec![],
  161. ban_policy: BanPolicy::Strict,
  162. profiles: HashMap::new(),
  163. }
  164. }
  165. }
  166. impl Settings {
  167. /// Returns `outbound_connect_timeout` for a specific profile.
  168. pub fn outbound_connect_timeout(&self, profile: &str) -> u64 {
  169. self.profiles.get(profile).unwrap_or(&NetworkProfile::default()).outbound_connect_timeout
  170. }
  171. /// Returns the maximum `outbound_connect_timeout` across all profiles,
  172. /// selecting a conservative value suitable for the slowest network profile.
  173. pub fn outbound_connect_timeout_max(&self) -> u64 {
  174. self.profiles
  175. .values()
  176. .map(|p| p.outbound_connect_timeout)
  177. .max()
  178. .unwrap_or(NetworkProfile::default().outbound_connect_timeout)
  179. }
  180. pub fn channel_heartbeat_interval(&self, profile: &str) -> u64 {
  181. self.profiles.get(profile).unwrap_or(&NetworkProfile::default()).channel_heartbeat_interval
  182. }
  183. pub fn channel_handshake_timeout(&self, profile: &str) -> u64 {
  184. self.profiles.get(profile).unwrap_or(&NetworkProfile::default()).channel_handshake_timeout
  185. }
  186. }
  187. /// Distinguishes distinct P2P networks
  188. #[derive(serde::Deserialize, Debug, Clone)]
  189. pub struct MagicBytes(pub [u8; 4]);
  190. impl Default for MagicBytes {
  191. fn default() -> Self {
  192. Self([0xd9, 0xef, 0xb6, 0x7d])
  193. }
  194. }
  195. /// Defines the network settings so we can have P2P configurations in
  196. /// TOML files.
  197. #[derive(Clone, Debug, serde::Deserialize, structopt::StructOpt, structopt_toml::StructOptToml)]
  198. #[structopt()]
  199. pub struct SettingsOpt {
  200. /// P2P accept address node listens to for inbound connections
  201. #[serde(default)]
  202. #[structopt(long = "accept")]
  203. pub inbound: Vec<Url>,
  204. /// Outbound connection slots number
  205. #[structopt(long = "outbound-slots")]
  206. pub outbound_connections: Option<usize>,
  207. /// Inbound connection slots number
  208. #[structopt(long = "inbound-slots")]
  209. pub inbound_connections: Option<usize>,
  210. #[serde(default)]
  211. #[structopt(skip)]
  212. /// Magic bytes used to distinguish P2P distinct networks and
  213. /// avoid nodes bleeding due to user config error.
  214. pub magic_bytes: MagicBytes,
  215. /// P2P external addresses node advertises so other peers can
  216. /// reach us and connect to us, as long as inbound addresses
  217. /// are also configured
  218. #[serde(default)]
  219. #[structopt(long)]
  220. pub external_addrs: Vec<Url>,
  221. /// Peer nodes to manually connect to
  222. #[serde(default)]
  223. #[structopt(long)]
  224. pub peers: Vec<Url>,
  225. /// Seed nodes to connect to for peers retrieval and/or
  226. /// advertising our own external addresses
  227. #[serde(default)]
  228. #[structopt(long)]
  229. pub seeds: Vec<Url>,
  230. /// Connection establishment timeout in seconds
  231. #[structopt(skip)]
  232. pub outbound_connect_timeout: Option<u64>,
  233. /// Exchange versions (handshake) timeout in seconds
  234. #[structopt(skip)]
  235. pub channel_handshake_timeout: Option<u64>,
  236. /// Ping-pong exchange execution interval in seconds
  237. #[structopt(skip)]
  238. pub channel_heartbeat_interval: Option<u64>,
  239. /// Only used for debugging. Compromises privacy when set.
  240. #[serde(default)]
  241. #[structopt(skip)]
  242. pub node_id: String,
  243. /// Preferred transports for outbound connections
  244. #[serde(default)]
  245. #[structopt(long = "network-profiles")]
  246. pub active_profiles: Option<Vec<String>>,
  247. /// Transports allowed to be mixed (tcp, tcp+tls, tor, tor+tls).
  248. /// When transport is added to this list the corresponding transport
  249. /// in active_profiles is used to connect to the node.
  250. /// Supported mixing scenarios include
  251. /// tor => tcp, tor+tls => tcp+tls,
  252. /// socks5 => tor, socks5 => tcp,
  253. /// socks5+tls => tor+tls, socks5+tls => tcp+tls
  254. /// where the first one overrides the second.
  255. #[serde(default)]
  256. #[structopt(long = "mixed-profiles")]
  257. pub mixed_profiles: Option<Vec<String>>,
  258. /// Tor socks5 proxy to connect to when socks5 or socks5+tls are added to active profiles
  259. /// and transport mixing is enabled
  260. #[structopt(long)]
  261. pub tor_socks5_proxy: Option<Url>,
  262. /// Nym socks5 proxy to connect to when socks5 or socks5+tls are added to active profiles
  263. /// and transport mixing is enabled
  264. #[structopt(long)]
  265. pub nym_socks5_proxy: Option<Url>,
  266. /// I2p Socks5 proxy to connect to i2p eepsite (hidden services)
  267. #[structopt(long)]
  268. pub i2p_socks5_proxy: Option<Url>,
  269. /// If this is true, strictly follow the gold_connect_count and
  270. /// white_connect_percent settings. Otherwise, connect to greylist
  271. /// entries if we have no white or gold connections.
  272. #[serde(default)]
  273. #[structopt(long)]
  274. pub localnet: bool,
  275. /// Cooling off time for peer discovery when unsuccessful
  276. #[structopt(skip)]
  277. pub outbound_peer_discovery_cooloff_time: Option<u64>,
  278. /// Time between peer discovery attempts
  279. #[structopt(skip)]
  280. pub outbound_peer_discovery_attempt_time: Option<u64>,
  281. /// Maximum number of addresses (with preferred transports) to receive from
  282. /// seeds and peers.
  283. /// If undefined, `outbound_connections` will be used instead.
  284. #[structopt(skip)]
  285. pub getaddrs_max: Option<u32>,
  286. /// P2P datastore path
  287. #[serde(default)]
  288. #[structopt(long)]
  289. pub p2p_datastore: Option<String>,
  290. /// Hosts .tsv file to use
  291. #[serde(default)]
  292. #[structopt(long)]
  293. pub hostlist: Option<String>,
  294. /// Pause interval within greylist refinery process
  295. #[structopt(skip)]
  296. pub greylist_refinery_interval: Option<u64>,
  297. /// Number of whitelist connections
  298. #[structopt(skip)]
  299. pub white_connect_percent: Option<usize>,
  300. /// Number of goldlist connections
  301. #[structopt(skip)]
  302. pub gold_connect_count: Option<usize>,
  303. /// Allow localnet hosts
  304. #[serde(default)]
  305. #[structopt(long)]
  306. pub slot_preference_strict: bool,
  307. /// Number of seconds with no connections after which refinery
  308. /// process is paused.
  309. #[structopt(skip)]
  310. pub time_with_no_connections: Option<u64>,
  311. /// Nodes to avoid interacting with for the duration of the program,
  312. /// in the format ["host", ["scheme", "scheme"], [port, port]]
  313. /// If scheme is left empty it will default to "tcp+tls".
  314. /// If ports are left empty all ports from this peer will be blocked.
  315. #[serde(default)]
  316. #[structopt(skip)]
  317. pub blacklist: Vec<BlacklistEntry>,
  318. /// Do not ban nodes that send messages without dispatchers if set
  319. /// to `Relaxed`. For most uses, should be set to `Strict`.
  320. #[serde(default)]
  321. #[structopt(skip)]
  322. pub ban_policy: BanPolicy,
  323. /// Network Profile for each transport
  324. #[serde(default)]
  325. #[structopt(skip)]
  326. pub profiles: HashMap<String, NetworkProfileOpt>,
  327. }
  328. impl TryFrom<(&str, &str, SettingsOpt)> for Settings {
  329. type Error = Error;
  330. fn try_from(st: (&str, &str, SettingsOpt)) -> Result<Self> {
  331. let app_name = st.0.to_string();
  332. let app_version = semver::Version::parse(st.1)?;
  333. let opt = st.2;
  334. let def = Settings::default();
  335. let mut inbound_addrs = opt.inbound;
  336. let mut external_addrs = opt.external_addrs;
  337. let mut peers = opt.peers;
  338. let mut seeds = opt.seeds;
  339. let active_profiles = opt.active_profiles.unwrap_or(def.active_profiles);
  340. let mixed_profiles = opt.mixed_profiles.unwrap_or(def.mixed_profiles);
  341. // check all the active profiles that are not mixed are found in net.profiles
  342. for name in &active_profiles {
  343. if !mixed_profiles.contains(name) && !opt.profiles.contains_key(name) {
  344. return Err(Error::ConfigError(format!(
  345. "Active profile '{name}' not defined in net.profiles"
  346. )));
  347. }
  348. }
  349. let profiles: HashMap<String, NetworkProfile> = opt
  350. .profiles
  351. .into_iter()
  352. .filter(|(k, _)| active_profiles.contains(k) && !mixed_profiles.contains(k))
  353. .map(|(k, v)| {
  354. inbound_addrs.extend_from_slice(&v.inbound);
  355. external_addrs.extend_from_slice(&v.external_addrs);
  356. peers.extend_from_slice(&v.peers);
  357. seeds.extend_from_slice(&v.seeds);
  358. (k.clone(), NetworkProfile::from_with_profile(v, &k))
  359. })
  360. .collect();
  361. Ok(Self {
  362. node_id: opt.node_id,
  363. inbound_addrs,
  364. external_addrs,
  365. magic_bytes: opt.magic_bytes,
  366. peers,
  367. seeds,
  368. app_version,
  369. app_name,
  370. active_profiles,
  371. mixed_profiles,
  372. tor_socks5_proxy: opt.tor_socks5_proxy,
  373. nym_socks5_proxy: opt.nym_socks5_proxy,
  374. i2p_socks5_proxy: opt.i2p_socks5_proxy.unwrap_or(def.i2p_socks5_proxy),
  375. outbound_connections: opt.outbound_connections.unwrap_or(def.outbound_connections),
  376. inbound_connections: opt.inbound_connections.unwrap_or(def.inbound_connections),
  377. localnet: opt.localnet,
  378. outbound_peer_discovery_cooloff_time: opt
  379. .outbound_peer_discovery_cooloff_time
  380. .unwrap_or(def.outbound_peer_discovery_cooloff_time),
  381. outbound_peer_discovery_attempt_time: opt
  382. .outbound_peer_discovery_attempt_time
  383. .unwrap_or(def.outbound_peer_discovery_attempt_time),
  384. getaddrs_max: opt.getaddrs_max,
  385. p2p_datastore: opt.p2p_datastore,
  386. hostlist: opt.hostlist,
  387. greylist_refinery_interval: opt
  388. .greylist_refinery_interval
  389. .unwrap_or(def.greylist_refinery_interval),
  390. white_connect_percent: opt.white_connect_percent.unwrap_or(def.white_connect_percent),
  391. gold_connect_count: opt.gold_connect_count.unwrap_or(def.gold_connect_count),
  392. slot_preference_strict: opt.slot_preference_strict,
  393. time_with_no_connections: opt
  394. .time_with_no_connections
  395. .unwrap_or(def.time_with_no_connections),
  396. blacklist: opt.blacklist,
  397. ban_policy: opt.ban_policy,
  398. profiles,
  399. })
  400. }
  401. }
  402. #[derive(Clone, Debug, serde::Deserialize, structopt::StructOpt, structopt_toml::StructOptToml)]
  403. #[structopt()]
  404. pub struct NetworkProfileOpt {
  405. /// P2P accept address node listens to for inbound connections
  406. #[serde(default)]
  407. #[structopt(long = "accept")]
  408. pub inbound: Vec<Url>,
  409. /// P2P external addresses node advertises so other peers can
  410. /// reach us and connect to us, as long as inbound addresses
  411. /// are also configured
  412. #[serde(default)]
  413. #[structopt(long)]
  414. pub external_addrs: Vec<Url>,
  415. /// Peer nodes to manually connect to
  416. #[serde(default)]
  417. #[structopt(long)]
  418. pub peers: Vec<Url>,
  419. /// Seed nodes to connect to for peers retrieval and/or
  420. /// advertising our own external addresses
  421. #[serde(default)]
  422. #[structopt(long)]
  423. pub seeds: Vec<Url>,
  424. /// Connection establishment timeout in seconds
  425. #[structopt(skip)]
  426. pub outbound_connect_timeout: Option<u64>,
  427. /// Exchange versions (handshake) timeout in seconds
  428. #[structopt(skip)]
  429. pub channel_handshake_timeout: Option<u64>,
  430. /// Ping-pong exchange execution interval in seconds
  431. #[structopt(skip)]
  432. pub channel_heartbeat_interval: Option<u64>,
  433. }
  434. /// Network Profile info unique for each profile/transport
  435. #[derive(Debug, Clone)]
  436. pub struct NetworkProfile {
  437. /// Outbound connection timeout (in seconds)
  438. pub outbound_connect_timeout: u64,
  439. /// Exchange versions (handshake) timeout (in seconds)
  440. pub channel_handshake_timeout: u64,
  441. /// Ping-pong exchange execution interval (in seconds)
  442. pub channel_heartbeat_interval: u64,
  443. }
  444. impl Default for NetworkProfile {
  445. fn default() -> Self {
  446. Self {
  447. outbound_connect_timeout: 15,
  448. channel_handshake_timeout: 10,
  449. channel_heartbeat_interval: 30,
  450. }
  451. }
  452. }
  453. impl NetworkProfile {
  454. /// Creates default [`NetworkProfile`] for non-clearnet profiles
  455. pub fn tor_default() -> Self {
  456. Self {
  457. outbound_connect_timeout: 65,
  458. channel_handshake_timeout: 55,
  459. channel_heartbeat_interval: 90,
  460. }
  461. }
  462. /// Creates [`NetworkProfile`] from [`NetworkProfileOpt`] based on the profile
  463. fn from_with_profile(opt: NetworkProfileOpt, profile: &str) -> Self {
  464. let def = if ["tcp", "tcp+tls", "quic"].contains(&profile) {
  465. NetworkProfile::default()
  466. } else {
  467. NetworkProfile::tor_default()
  468. };
  469. Self {
  470. outbound_connect_timeout: opt
  471. .outbound_connect_timeout
  472. .unwrap_or(def.outbound_connect_timeout),
  473. channel_handshake_timeout: opt
  474. .channel_handshake_timeout
  475. .unwrap_or(def.channel_handshake_timeout),
  476. channel_heartbeat_interval: opt
  477. .channel_heartbeat_interval
  478. .unwrap_or(def.channel_heartbeat_interval),
  479. }
  480. }
  481. }