settings.rs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 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::sync::Arc;
  19. use serde::Deserialize;
  20. use structopt::StructOpt;
  21. use structopt_toml::StructOptToml;
  22. use url::Url;
  23. use crate::net::transport::TransportName;
  24. /// Atomic pointer to network settings.
  25. pub type SettingsPtr = Arc<Settings>;
  26. /// Default settings for the network. Can be manually configured.
  27. #[derive(Clone, Debug)]
  28. pub struct Settings {
  29. /// P2P accept addresses node listens to for inbound connections
  30. pub inbound: Vec<Url>,
  31. /// Outbound connection slots number
  32. pub outbound_connections: u32,
  33. /// Manual connections retry limit, 0 for forever looping
  34. pub manual_attempt_limit: u32,
  35. /// Seed connection establishment timeout
  36. pub seed_query_timeout_seconds: u32,
  37. /// Connection establishment timeout
  38. pub connect_timeout_seconds: u32,
  39. /// Exchange versions (handshake) timeout
  40. pub channel_handshake_seconds: u32,
  41. /// Ping-pong exhange execution interval
  42. pub channel_heartbeat_seconds: u32,
  43. /// Try to fill an outbound slot interval
  44. pub outbound_retry_seconds: u64,
  45. /// P2P external addresses node advertises so other peers can reach us
  46. /// and connect to us, as long us inbound addresses are also configured
  47. pub external_addr: Vec<Url>,
  48. /// Peer nodes to manually connect to
  49. pub peers: Vec<Url>,
  50. /// Seed nodes to connect to for peers retrieval and/or advertising our own
  51. /// external address
  52. pub seeds: Vec<Url>,
  53. /// Only used for debugging. Compromises privacy when set.
  54. pub node_id: String,
  55. /// Application version, used for verification between peers
  56. pub app_version: Option<String>,
  57. /// Prefered transports for outbound connections
  58. pub outbound_transports: Vec<TransportName>,
  59. /// Allow localnet hosts
  60. pub localnet: bool,
  61. /// Enable peer discovery
  62. pub peer_discovery: bool,
  63. /// Enable channel logging
  64. pub channel_log: bool,
  65. }
  66. impl Default for Settings {
  67. fn default() -> Self {
  68. Self {
  69. inbound: Vec::new(),
  70. outbound_connections: 0,
  71. manual_attempt_limit: 0,
  72. seed_query_timeout_seconds: 8,
  73. connect_timeout_seconds: 10,
  74. channel_handshake_seconds: 4,
  75. channel_heartbeat_seconds: 10,
  76. outbound_retry_seconds: 20,
  77. external_addr: Vec::new(),
  78. peers: Vec::new(),
  79. seeds: Vec::new(),
  80. node_id: String::new(),
  81. app_version: Some(option_env!("CARGO_PKG_VERSION").unwrap_or("").to_string()),
  82. outbound_transports: get_outbound_transports(vec![]),
  83. localnet: false,
  84. peer_discovery: true,
  85. channel_log: false,
  86. }
  87. }
  88. }
  89. /// Defines the network settings.
  90. #[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
  91. #[structopt()]
  92. pub struct SettingsOpt {
  93. /// P2P accept addresses node listens to for inbound connections
  94. #[serde(default)]
  95. #[structopt(long = "accept")]
  96. pub inbound: Vec<Url>,
  97. /// Outbound connection slots number
  98. #[structopt(long = "slots")]
  99. pub outbound_connections: Option<u32>,
  100. /// P2P external addresses node advertises so other peers can reach us
  101. /// and connect to us, as long us inbound addresses are also configured
  102. #[serde(default)]
  103. #[structopt(long)]
  104. pub external_addr: Vec<Url>,
  105. /// Peer nodes to manually connect to
  106. #[serde(default)]
  107. #[structopt(long)]
  108. pub peers: Vec<Url>,
  109. /// Seed nodes to connect to for peers retrieval and/or advertising our own
  110. /// external address
  111. #[serde(default)]
  112. #[structopt(long)]
  113. pub seeds: Vec<Url>,
  114. /// Manual connections retry limit
  115. #[structopt(skip)]
  116. pub manual_attempt_limit: Option<u32>,
  117. /// Seed connection establishment timeout
  118. #[structopt(skip)]
  119. pub seed_query_timeout_seconds: Option<u32>,
  120. /// Connection establishment timeout
  121. #[structopt(skip)]
  122. pub connect_timeout_seconds: Option<u32>,
  123. /// Exchange versions (handshake) timeout
  124. #[structopt(skip)]
  125. pub channel_handshake_seconds: Option<u32>,
  126. /// Ping-pong exhange execution interval
  127. #[structopt(skip)]
  128. pub channel_heartbeat_seconds: Option<u32>,
  129. /// Try to fill an outbound slot interval
  130. #[structopt(skip)]
  131. pub outbound_retry_seconds: Option<u64>,
  132. /// Only used for debugging. Compromises privacy when set.
  133. #[serde(default)]
  134. #[structopt(skip)]
  135. pub node_id: String,
  136. /// Application version, used for verification between peers
  137. #[serde(default)]
  138. #[structopt(skip)]
  139. pub app_version: Option<String>,
  140. /// Prefered transports for outbound connections
  141. #[serde(default)]
  142. #[structopt(long = "transports")]
  143. pub outbound_transports: Vec<String>,
  144. /// Allow localnet hosts
  145. #[serde(default)]
  146. #[structopt(long)]
  147. pub localnet: bool,
  148. /// Enable peer discovery
  149. #[serde(default = "default_as_true")]
  150. #[structopt(long)]
  151. pub peer_discovery: bool,
  152. /// Enable channel logging
  153. #[serde(default)]
  154. #[structopt(long)]
  155. pub channel_log: bool,
  156. }
  157. impl From<SettingsOpt> for Settings {
  158. fn from(settings_opt: SettingsOpt) -> Self {
  159. Self {
  160. inbound: settings_opt.inbound,
  161. outbound_connections: settings_opt.outbound_connections.unwrap_or(0),
  162. manual_attempt_limit: settings_opt.manual_attempt_limit.unwrap_or(0),
  163. seed_query_timeout_seconds: settings_opt.seed_query_timeout_seconds.unwrap_or(8),
  164. connect_timeout_seconds: settings_opt.connect_timeout_seconds.unwrap_or(10),
  165. channel_handshake_seconds: settings_opt.channel_handshake_seconds.unwrap_or(4),
  166. channel_heartbeat_seconds: settings_opt.channel_heartbeat_seconds.unwrap_or(10),
  167. outbound_retry_seconds: settings_opt.outbound_retry_seconds.unwrap_or(1200),
  168. external_addr: settings_opt.external_addr,
  169. peers: settings_opt.peers,
  170. seeds: settings_opt.seeds,
  171. node_id: settings_opt.node_id,
  172. app_version: settings_opt.app_version,
  173. outbound_transports: get_outbound_transports(settings_opt.outbound_transports),
  174. localnet: settings_opt.localnet,
  175. peer_discovery: settings_opt.peer_discovery,
  176. channel_log: settings_opt.channel_log,
  177. }
  178. }
  179. }
  180. /// Auxiliary function to convert outbound transport Vec<String>
  181. /// to Vec<TransportName>, using defaults if empty.
  182. pub fn get_outbound_transports(opt_outbound_transports: Vec<String>) -> Vec<TransportName> {
  183. let mut outbound_transports = vec![];
  184. for transport in opt_outbound_transports {
  185. let transport_name = TransportName::try_from(transport.as_str()).unwrap();
  186. outbound_transports.push(transport_name);
  187. }
  188. if outbound_transports.is_empty() {
  189. let tls = TransportName::Tcp(Some("tls".into()));
  190. outbound_transports.push(tls);
  191. let tcp = TransportName::Tcp(None);
  192. outbound_transports.push(tcp);
  193. }
  194. outbound_transports
  195. }
  196. /// Auxiliary function to set serde bool value to true.
  197. fn default_as_true() -> bool {
  198. true
  199. }