settings.rs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 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 advertising our
  51. /// own external addresses
  52. pub seeds: Vec<Url>,
  53. /// Magic bytes should be unique per P2P network.
  54. /// Avoid bleeding of networks.
  55. pub magic_bytes: MagicBytes,
  56. /// Application version, used for convenient protocol matching
  57. pub app_version: semver::Version,
  58. /// Whitelisted network transports for outbound connections
  59. pub allowed_transports: Vec<String>,
  60. /// Transports allowed to be mixed (tcp, tcp+tls, tor, tor+tls)
  61. /// When transport is added to this list the corresponding transport
  62. /// in allowed_transports is used to connect to the node.
  63. /// Supported mixing scenarios include
  64. /// allowed_transport | mixed_transport
  65. /// tor | tcp
  66. /// tor+tls | tcp+tls
  67. /// socks5 | tor
  68. /// socks5 | tcp
  69. /// socks5+tls | tor+tls
  70. /// socks5+tls | tcp+tls
  71. pub mixed_transports: Vec<String>,
  72. /// Tor socks5 proxy to connect to when socks5 or socks5+tls are added to allowed transports
  73. /// and transport mixing is enabled
  74. pub tor_socks5_proxy: Option<Url>,
  75. /// Nym socks5 proxy to connect to when socks5 or socks5+tls are added to allowed transports
  76. /// and transport mixing is enabled
  77. pub nym_socks5_proxy: Option<Url>,
  78. /// I2p Socks5 proxy to connect to i2p eepsite (hidden services)
  79. pub i2p_socks5_proxy: Url,
  80. /// Outbound connection slots number, this many connections will be
  81. /// attempted. (This does not include manual connections)
  82. pub outbound_connections: usize,
  83. /// Inbound connection slots number, this many active listening connections
  84. /// will be allowed. (This does not include manual connections)
  85. pub inbound_connections: usize,
  86. /// Outbound connection timeout (in seconds)
  87. pub outbound_connect_timeout: u64,
  88. /// Exchange versions (handshake) timeout (in seconds)
  89. pub channel_handshake_timeout: u64,
  90. /// Ping-pong exchange execution interval (in seconds)
  91. pub channel_heartbeat_interval: u64,
  92. /// Allow localnet hosts
  93. pub localnet: bool,
  94. /// Cooling off time for peer discovery when unsuccessful
  95. pub outbound_peer_discovery_cooloff_time: u64,
  96. /// Time between peer discovery attempts
  97. pub outbound_peer_discovery_attempt_time: u64,
  98. /// Maximum number of addresses (with preferred transports) to receive from
  99. /// seeds and peers.
  100. /// If undefined, `outbound_connections` will be used instead.
  101. pub getaddrs_max: Option<u32>,
  102. /// P2P datastore path
  103. pub p2p_datastore: Option<String>,
  104. /// Hostlist storage path
  105. pub hostlist: Option<String>,
  106. /// Pause interval within greylist refinery process
  107. pub greylist_refinery_interval: u64,
  108. /// Percent of connections to come from the whitelist
  109. pub white_connect_percent: usize,
  110. /// Number of goldlist connections
  111. pub gold_connect_count: usize,
  112. /// If this is true, strictly follow the gold_connect_count and
  113. /// white_connect_percent settings. Otherwise, connect to greylist
  114. /// entries if we have no white or gold connections.
  115. pub slot_preference_strict: bool,
  116. /// Number of seconds with no connections after which refinery
  117. /// process is paused.
  118. pub time_with_no_connections: u64,
  119. /// Nodes to avoid interacting with for the duration of the program,
  120. /// in the format ["host", ["scheme", "scheme"], [port, port]]
  121. /// If scheme is left empty it will default to "tcp+tls".
  122. /// If ports are left empty all ports from this peer will be blocked.
  123. pub blacklist: Vec<BlacklistEntry>,
  124. /// Do not ban nodes that send messages without dispatchers if set
  125. /// to `Relaxed`. For most uses, should be set to `Strict`.
  126. pub ban_policy: BanPolicy,
  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. Self {
  133. node_id: String::new(),
  134. inbound_addrs: vec![],
  135. external_addrs: vec![],
  136. magic_bytes: Default::default(),
  137. peers: vec![],
  138. seeds: vec![],
  139. app_version,
  140. allowed_transports: vec!["tcp+tls".to_string()],
  141. mixed_transports: vec![],
  142. tor_socks5_proxy: None,
  143. nym_socks5_proxy: None,
  144. i2p_socks5_proxy: Url::parse("socks5://127.0.0.1:4447").unwrap(),
  145. outbound_connections: 8,
  146. inbound_connections: 8,
  147. outbound_connect_timeout: 15,
  148. channel_handshake_timeout: 10,
  149. channel_heartbeat_interval: 30,
  150. localnet: false,
  151. outbound_peer_discovery_cooloff_time: 30,
  152. outbound_peer_discovery_attempt_time: 5,
  153. getaddrs_max: None,
  154. p2p_datastore: None,
  155. hostlist: None,
  156. greylist_refinery_interval: 15,
  157. white_connect_percent: 70,
  158. gold_connect_count: 2,
  159. slot_preference_strict: false,
  160. time_with_no_connections: 30,
  161. blacklist: vec![],
  162. ban_policy: BanPolicy::Strict,
  163. }
  164. }
  165. }
  166. // The following is used so we can have P2P settings configurable
  167. // from TOML files.
  168. /// Distinguishes distinct P2P networks
  169. #[derive(serde::Deserialize, Debug, Clone)]
  170. pub struct MagicBytes(pub [u8; 4]);
  171. impl Default for MagicBytes {
  172. fn default() -> Self {
  173. Self([0xd9, 0xef, 0xb6, 0x7d])
  174. }
  175. }
  176. /// Defines the network settings.
  177. #[derive(Clone, Debug, serde::Deserialize, structopt::StructOpt, structopt_toml::StructOptToml)]
  178. #[structopt()]
  179. pub struct SettingsOpt {
  180. /// P2P accept address node listens to for inbound connections
  181. #[serde(default)]
  182. #[structopt(long = "accept")]
  183. pub inbound: Vec<Url>,
  184. /// Outbound connection slots number
  185. #[structopt(long = "outbound-slots")]
  186. pub outbound_connections: Option<usize>,
  187. /// Inbound connection slots number
  188. #[structopt(long = "inbound-slots")]
  189. pub inbound_connections: Option<usize>,
  190. #[serde(default)]
  191. #[structopt(skip)]
  192. /// Magic bytes used to distinguish P2P distinct networks and
  193. /// avoid nodes bleeding due to user config error.
  194. pub magic_bytes: MagicBytes,
  195. /// P2P external addresses node advertises so other peers can
  196. /// reach us and connect to us, as long as inbound addresses
  197. /// are also configured
  198. #[serde(default)]
  199. #[structopt(long)]
  200. pub external_addrs: Vec<Url>,
  201. /// Peer nodes to manually connect to
  202. #[serde(default)]
  203. #[structopt(long)]
  204. pub peers: Vec<Url>,
  205. /// Seed nodes to connect to for peers retrieval and/or
  206. /// advertising our own external addresses
  207. #[serde(default)]
  208. #[structopt(long)]
  209. pub seeds: Vec<Url>,
  210. /// Connection establishment timeout in seconds
  211. #[structopt(skip)]
  212. pub outbound_connect_timeout: Option<u64>,
  213. /// Exchange versions (handshake) timeout in seconds
  214. #[structopt(skip)]
  215. pub channel_handshake_timeout: Option<u64>,
  216. /// Ping-pong exchange execution interval in seconds
  217. #[structopt(skip)]
  218. pub channel_heartbeat_interval: Option<u64>,
  219. /// Only used for debugging. Compromises privacy when set.
  220. #[serde(default)]
  221. #[structopt(skip)]
  222. pub node_id: String,
  223. /// Preferred transports for outbound connections
  224. #[serde(default)]
  225. #[structopt(long = "transports")]
  226. pub allowed_transports: Option<Vec<String>>,
  227. /// Transports allowed to be mixed (tcp, tcp+tls, tor, tor+tls)
  228. /// When transport is added to this list the corresponding transport
  229. /// in allowed_transports is used to connect to the node.
  230. /// Supported mixing scenarios include
  231. /// allowed_transport | mixed_transport
  232. /// tor | tcp
  233. /// tor+tls | tcp+tls
  234. /// socks5 | tor
  235. /// socks5 | tcp
  236. /// socks5+tls | tor+tls
  237. /// socks5+tls | tcp+tls
  238. #[serde(default)]
  239. #[structopt(long = "mixed-transports")]
  240. pub mixed_transports: Option<Vec<String>>,
  241. /// Tor socks5 proxy to connect to when socks5 or socks5+tls are added to allowed transports
  242. /// and transport mixing is enabled
  243. #[structopt(long)]
  244. pub tor_socks5_proxy: Option<Url>,
  245. /// Nym socks5 proxy to connect to when socks5 or socks5+tls are added to allowed transports
  246. /// and transport mixing is enabled
  247. #[structopt(long)]
  248. pub nym_socks5_proxy: Option<Url>,
  249. /// I2p Socks5 proxy to connect to i2p eepsite (hidden services)
  250. #[structopt(long)]
  251. pub i2p_socks5_proxy: Option<Url>,
  252. /// If this is true, strictly follow the gold_connect_count and
  253. /// white_connect_percent settings. Otherwise, connect to greylist
  254. /// entries if we have no white or gold connections.
  255. #[serde(default)]
  256. #[structopt(long)]
  257. pub localnet: bool,
  258. /// Cooling off time for peer discovery when unsuccessful
  259. #[structopt(skip)]
  260. pub outbound_peer_discovery_cooloff_time: Option<u64>,
  261. /// Time between peer discovery attempts
  262. #[structopt(skip)]
  263. pub outbound_peer_discovery_attempt_time: Option<u64>,
  264. /// Maximum number of addresses (with preferred transports) to receive from
  265. /// seeds and peers.
  266. /// If undefined, `outbound_connections` will be used instead.
  267. #[structopt(skip)]
  268. pub getaddrs_max: Option<u32>,
  269. /// P2P datastore path
  270. #[serde(default)]
  271. #[structopt(long)]
  272. pub p2p_datastore: Option<String>,
  273. /// Hosts .tsv file to use
  274. #[serde(default)]
  275. #[structopt(long)]
  276. pub hostlist: Option<String>,
  277. /// Pause interval within greylist refinery process
  278. #[structopt(skip)]
  279. pub greylist_refinery_interval: Option<u64>,
  280. /// Number of whitelist connections
  281. #[structopt(skip)]
  282. pub white_connect_percent: Option<usize>,
  283. /// Number of goldlist connections
  284. #[structopt(skip)]
  285. pub gold_connect_count: Option<usize>,
  286. /// Allow localnet hosts
  287. #[serde(default)]
  288. #[structopt(long)]
  289. pub slot_preference_strict: bool,
  290. /// Number of seconds with no connections after which refinery
  291. /// process is paused.
  292. #[structopt(skip)]
  293. pub time_with_no_connections: Option<u64>,
  294. /// Nodes to avoid interacting with for the duration of the program,
  295. /// in the format ["host", ["scheme", "scheme"], [port, port]]
  296. /// If scheme is left empty it will default to "tcp+tls".
  297. /// If ports are left empty all ports from this peer will be blocked.
  298. #[serde(default)]
  299. #[structopt(skip)]
  300. pub blacklist: Vec<BlacklistEntry>,
  301. /// Do not ban nodes that send messages without dispatchers if set
  302. /// to `Relaxed`. For most uses, should be set to `Strict`.
  303. #[serde(default)]
  304. #[structopt(skip)]
  305. pub ban_policy: BanPolicy,
  306. }
  307. impl From<SettingsOpt> for Settings {
  308. fn from(opt: SettingsOpt) -> Self {
  309. let def = Settings::default();
  310. Self {
  311. node_id: opt.node_id,
  312. inbound_addrs: opt.inbound,
  313. external_addrs: opt.external_addrs,
  314. magic_bytes: opt.magic_bytes,
  315. peers: opt.peers,
  316. seeds: opt.seeds,
  317. app_version: def.app_version,
  318. allowed_transports: opt.allowed_transports.unwrap_or(def.allowed_transports),
  319. mixed_transports: opt.mixed_transports.unwrap_or(def.mixed_transports),
  320. tor_socks5_proxy: opt.tor_socks5_proxy,
  321. nym_socks5_proxy: opt.nym_socks5_proxy,
  322. i2p_socks5_proxy: opt.i2p_socks5_proxy.unwrap_or(def.i2p_socks5_proxy),
  323. outbound_connections: opt.outbound_connections.unwrap_or(def.outbound_connections),
  324. inbound_connections: opt.inbound_connections.unwrap_or(def.inbound_connections),
  325. outbound_connect_timeout: opt
  326. .outbound_connect_timeout
  327. .unwrap_or(def.outbound_connect_timeout),
  328. channel_handshake_timeout: opt
  329. .channel_handshake_timeout
  330. .unwrap_or(def.channel_handshake_timeout),
  331. channel_heartbeat_interval: opt
  332. .channel_heartbeat_interval
  333. .unwrap_or(def.channel_heartbeat_interval),
  334. localnet: opt.localnet,
  335. outbound_peer_discovery_cooloff_time: opt
  336. .outbound_peer_discovery_cooloff_time
  337. .unwrap_or(def.outbound_peer_discovery_cooloff_time),
  338. outbound_peer_discovery_attempt_time: opt
  339. .outbound_peer_discovery_attempt_time
  340. .unwrap_or(def.outbound_peer_discovery_attempt_time),
  341. getaddrs_max: opt.getaddrs_max,
  342. p2p_datastore: opt.p2p_datastore,
  343. hostlist: opt.hostlist,
  344. greylist_refinery_interval: opt
  345. .greylist_refinery_interval
  346. .unwrap_or(def.greylist_refinery_interval),
  347. white_connect_percent: opt.white_connect_percent.unwrap_or(def.white_connect_percent),
  348. gold_connect_count: opt.gold_connect_count.unwrap_or(def.gold_connect_count),
  349. slot_preference_strict: opt.slot_preference_strict,
  350. time_with_no_connections: opt
  351. .time_with_no_connections
  352. .unwrap_or(def.time_with_no_connections),
  353. blacklist: opt.blacklist,
  354. ban_policy: opt.ban_policy,
  355. }
  356. }
  357. }