settings.rs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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 async_std::sync::Arc;
  19. use structopt::StructOpt;
  20. use url::Url;
  21. /// Atomic pointer to network settings
  22. pub type SettingsPtr = Arc<Settings>;
  23. /// P2P network settings. The scope of this is a P2P network instance
  24. /// configured by the library user.
  25. #[derive(Debug, Clone)]
  26. pub struct Settings {
  27. /// Only used for debugging, compromises privacy when set
  28. pub node_id: String,
  29. /// P2P accept addresses the instance listens on for inbound connections
  30. pub inbound_addrs: Vec<Url>,
  31. /// P2P external addresses the instance advertises so other peers can
  32. /// reach us and connect to us, as long as inbound addrs are configured
  33. pub external_addrs: Vec<Url>,
  34. /// Peer nodes to manually connect to
  35. pub peers: Vec<Url>,
  36. /// Seed nodes to connect to for peer discovery and/or adversising our
  37. /// own external addresses
  38. pub seeds: Vec<Url>,
  39. /// Application version, used for convenient protocol matching
  40. pub app_version: semver::Version,
  41. /// Whitelisted network transports for outbound connections
  42. pub allowed_transports: Vec<String>,
  43. /// Allow transport mixing (e.g. Tor would be allowed to connect to `tcp://`)
  44. pub transport_mixing: bool,
  45. /// Outbound connection slots number, this many connections will be
  46. /// attempted. (This does not include manual connections)
  47. pub outbound_connections: usize,
  48. /// Manual connections retry limit, 0 for forever looping
  49. pub manual_attempt_limit: usize,
  50. /// Seed connection establishment timeout (in seconds)
  51. pub seed_query_timeout: u64,
  52. /// Outbound connection establishment timeout (in seconds)
  53. pub outbound_connect_timeout: u64,
  54. /// Exchange versions (handshake) timeout (in seconds)
  55. pub channel_handshake_timeout: u64,
  56. /// Ping-pong exchange execution interval (in seconds)
  57. pub channel_heartbeat_interval: u64,
  58. /// Allow localnet hosts
  59. pub localnet: bool,
  60. }
  61. impl Default for Settings {
  62. fn default() -> Self {
  63. let version = option_env!("CARGO_PKG_VERSION").unwrap_or("0.0.0");
  64. let app_version = semver::Version::parse(version).unwrap();
  65. Self {
  66. node_id: String::new(),
  67. inbound_addrs: vec![],
  68. external_addrs: vec![],
  69. peers: vec![],
  70. seeds: vec![],
  71. app_version,
  72. allowed_transports: vec![],
  73. transport_mixing: true,
  74. outbound_connections: 0,
  75. manual_attempt_limit: 0,
  76. seed_query_timeout: 30,
  77. outbound_connect_timeout: 15,
  78. channel_handshake_timeout: 4,
  79. channel_heartbeat_interval: 10,
  80. localnet: false,
  81. }
  82. }
  83. }
  84. // The following is used so we can have P2P settings configurable
  85. // from TOML files.
  86. /// Defines the network settings.
  87. #[derive(Clone, Debug, serde::Deserialize, structopt::StructOpt, structopt_toml::StructOptToml)]
  88. #[structopt()]
  89. pub struct SettingsOpt {
  90. /// P2P accept address node listens to for inbound connections
  91. #[serde(default)]
  92. #[structopt(long = "accept")]
  93. pub inbound: Vec<Url>,
  94. /// Outbound connection slots number
  95. #[structopt(long = "slots")]
  96. pub outbound_connections: Option<usize>,
  97. /// P2P external addresses node advertises so other peers can
  98. /// reach us and connect to us, as long as inbound addresses
  99. /// are also configured
  100. #[serde(default)]
  101. #[structopt(long)]
  102. pub external_addrs: Vec<Url>,
  103. /// Peer nodes to manually connect to
  104. #[serde(default)]
  105. #[structopt(long)]
  106. pub peers: Vec<Url>,
  107. /// Seed nodes to connect to for peers retrieval and/or
  108. /// advertising our own external addresses
  109. #[serde(default)]
  110. #[structopt(long)]
  111. pub seeds: Vec<Url>,
  112. /// Manual connections retry limit
  113. #[structopt(skip)]
  114. pub manual_attempt_limit: Option<usize>,
  115. /// Seed connection establishment timeout in seconds
  116. #[structopt(skip)]
  117. pub seed_query_timeout: Option<u64>,
  118. /// Connection establishment timeout in seconds
  119. #[structopt(skip)]
  120. pub outbound_connect_timeout: Option<u64>,
  121. /// Exchange versions (handshake) timeout in seconds
  122. #[structopt(skip)]
  123. pub channel_handshake_timeout: Option<u64>,
  124. /// Ping-pong exchange execution interval in seconds
  125. #[structopt(skip)]
  126. pub channel_heartbeat_interval: Option<u64>,
  127. /// Only used for debugging. Compromises privacy when set.
  128. #[serde(default)]
  129. #[structopt(skip)]
  130. pub node_id: String,
  131. /// Preferred transports for outbound connections
  132. #[serde(default)]
  133. #[structopt(long = "transports")]
  134. pub allowed_transports: Vec<String>,
  135. #[structopt(long)]
  136. pub transport_mixing: bool,
  137. /// Allow localnet hosts
  138. #[serde(default)]
  139. #[structopt(long)]
  140. pub localnet: bool,
  141. }
  142. impl From<SettingsOpt> for Settings {
  143. fn from(opt: SettingsOpt) -> Self {
  144. let version = option_env!("CARGO_PKG_VERSION").unwrap_or("0.0.0");
  145. let app_version = semver::Version::parse(version).unwrap();
  146. Self {
  147. node_id: opt.node_id,
  148. inbound_addrs: opt.inbound,
  149. external_addrs: opt.external_addrs,
  150. peers: opt.peers,
  151. seeds: opt.seeds,
  152. app_version,
  153. allowed_transports: opt.allowed_transports,
  154. transport_mixing: opt.transport_mixing,
  155. outbound_connections: opt.outbound_connections.unwrap_or(0),
  156. manual_attempt_limit: opt.manual_attempt_limit.unwrap_or(0),
  157. seed_query_timeout: opt.seed_query_timeout.unwrap_or(30),
  158. outbound_connect_timeout: opt.outbound_connect_timeout.unwrap_or(15),
  159. channel_handshake_timeout: opt.channel_handshake_timeout.unwrap_or(4),
  160. channel_heartbeat_interval: opt.channel_heartbeat_interval.unwrap_or(10),
  161. localnet: opt.localnet,
  162. }
  163. }
  164. }