From d433dbd02ee53d9a76f9b002ec4bc12b46df83c2 Mon Sep 17 00:00:00 2001 From: aggstam Date: Mon, 12 Sep 2022 22:04:57 +0300 Subject: [PATCH] p2pnet: optional app_version implemented --- bin/darkwikid/src/main.rs | 3 ++- bin/ircd/src/main.rs | 2 +- bin/lilith/src/main.rs | 1 + bin/tau/taud/src/main.rs | 3 ++- src/net/protocol/protocol_version.rs | 39 ++++++++++++++++++++-------- src/net/settings.rs | 6 ++--- 6 files changed, 37 insertions(+), 17 deletions(-) diff --git a/bin/darkwikid/src/main.rs b/bin/darkwikid/src/main.rs index bcea61f83..1eeafafdb 100644 --- a/bin/darkwikid/src/main.rs +++ b/bin/darkwikid/src/main.rs @@ -623,7 +623,6 @@ async fn realmain(settings: Args, executor: Arc>) -> Result<()> { // // Raft // - let net_settings = settings.net; let seen_net_msgs = Arc::new(Mutex::new(FxHashMap::default())); let datastore_raft = datastore_path.join("darkwiki.db"); @@ -634,6 +633,8 @@ async fn realmain(settings: Args, executor: Arc>) -> Result<()> { // // P2p setup // + let mut net_settings = settings.net.clone(); + net_settings.app_version = Some(option_env!("CARGO_PKG_VERSION").unwrap_or("").to_string()); let (p2p_send_channel, p2p_recv_channel) = async_channel::unbounded::(); let p2p = net::P2p::new(net_settings.into()).await; diff --git a/bin/ircd/src/main.rs b/bin/ircd/src/main.rs index 56fd641ca..228168a4d 100644 --- a/bin/ircd/src/main.rs +++ b/bin/ircd/src/main.rs @@ -130,7 +130,7 @@ async fn realmain(settings: Args, executor: Arc>) -> Result<()> { // P2p setup // let mut net_settings = settings.net.clone(); - net_settings.app_version = option_env!("CARGO_PKG_VERSION").unwrap_or("").to_string(); + net_settings.app_version = Some(option_env!("CARGO_PKG_VERSION").unwrap_or("").to_string()); let (p2p_send_channel, p2p_recv_channel) = async_channel::unbounded::(); let p2p = net::P2p::new(net_settings.into()).await; diff --git a/bin/lilith/src/main.rs b/bin/lilith/src/main.rs index 2ddc22407..eae1050fb 100644 --- a/bin/lilith/src/main.rs +++ b/bin/lilith/src/main.rs @@ -135,6 +135,7 @@ async fn spawn_network( peers: info.peers, outbound_connections: 0, localnet: info.localnet, + app_version: None, ..Default::default() }; diff --git a/bin/tau/taud/src/main.rs b/bin/tau/taud/src/main.rs index bfd02f636..678313732 100644 --- a/bin/tau/taud/src/main.rs +++ b/bin/tau/taud/src/main.rs @@ -184,7 +184,6 @@ async fn realmain(settings: Args, executor: Arc>) -> Result<()> { // // Raft // - let net_settings = settings.net; let seen_net_msgs = Arc::new(Mutex::new(FxHashMap::default())); let datastore_raft = datastore_path.join("tau.db"); @@ -200,6 +199,8 @@ async fn realmain(settings: Args, executor: Arc>) -> Result<()> { // // P2p setup // + let mut net_settings = settings.net.clone(); + net_settings.app_version = Some(option_env!("CARGO_PKG_VERSION").unwrap_or("").to_string()); let (p2p_send_channel, p2p_recv_channel) = async_channel::unbounded::(); let p2p = net::P2p::new(net_settings.into()).await; diff --git a/src/net/protocol/protocol_version.rs b/src/net/protocol/protocol_version.rs index 895107730..7d5911973 100644 --- a/src/net/protocol/protocol_version.rs +++ b/src/net/protocol/protocol_version.rs @@ -38,6 +38,7 @@ impl ProtocolVersion { Arc::new(Self { channel, version_sub, verack_sub, settings }) } + /// Start version information exchange. Start the timer. Send version info /// and wait for version acknowledgement. Wait for version info and send /// version acknowledgement. @@ -60,6 +61,7 @@ impl ProtocolVersion { debug!(target: "net", "ProtocolVersion::run() [END]"); Ok(()) } + /// Send and recieve version information. async fn exchange_versions(self: Arc, executor: Arc>) -> Result<()> { debug!(target: "net", "ProtocolVersion::exchange_versions() [START]"); @@ -73,8 +75,9 @@ impl ProtocolVersion { debug!(target: "net", "ProtocolVersion::exchange_versions() [END]"); Ok(()) } + /// Send version info and wait for version acknowledgement - /// and insures the app version is the same + /// and ensures the app version is the same, if configured. async fn send_version(self: Arc) -> Result<()> { debug!(target: "net", "ProtocolVersion::send_version() [START]"); @@ -85,20 +88,32 @@ impl ProtocolVersion { // Wait for version acknowledgement let verack_msg = self.verack_sub.receive().await?; - let app_version = self.settings.app_version.clone(); - - if app_version != verack_msg.app { - error!( - "Wrong app version from [{:?}]. Disconnecting from channel.", - self.channel.address() - ); - self.channel.stop().await; - return Err(Error::ChannelStopped) + // Validate peer received version against our version, if configured. + // Seeds version gets ignored. + if !self.settings.seeds.contains(&self.channel.address()) { + match &self.settings.app_version { + Some(app_version) => { + debug!(target: "net", "ProtocolVersion::send_version() [App version: {}]", app_version); + debug!(target: "net", "ProtocolVersion::send_version() [Recieved version: {}]", verack_msg.app); + if app_version != &verack_msg.app { + error!( + "Wrong app version from [{:?}]. Disconnecting from channel.", + self.channel.address() + ); + self.channel.stop().await; + return Err(Error::ChannelStopped) + } + } + None => { + debug!(target: "net", "ProtocolVersion::send_version() [App version not set, ignorring received]") + } + } } debug!(target: "net", "ProtocolVersion::send_version() [END]"); Ok(()) } + /// Recieve version info, check the message is okay and send version /// acknowledgement with app version attached. async fn recv_version(self: Arc) -> Result<()> { @@ -108,7 +123,9 @@ impl ProtocolVersion { self.channel.set_remote_node_id(version.node_id.clone()).await; // Send version acknowledgement - let verack = message::VerackMessage { app: self.settings.app_version.clone() }; + let verack = message::VerackMessage { + app: self.settings.app_version.clone().unwrap_or("".to_string()), + }; self.channel.clone().send(verack).await?; debug!(target: "net", "ProtocolVersion::recv_version() [END]"); diff --git a/src/net/settings.rs b/src/net/settings.rs index 341cc7a67..905d39bc4 100644 --- a/src/net/settings.rs +++ b/src/net/settings.rs @@ -25,7 +25,7 @@ pub struct Settings { pub peers: Vec, pub seeds: Vec, pub node_id: String, - pub app_version: String, + pub app_version: Option, pub outbound_transports: Vec, pub localnet: bool, } @@ -45,7 +45,7 @@ impl Default for Settings { peers: Vec::new(), seeds: Vec::new(), node_id: String::new(), - app_version: option_env!("CARGO_PKG_VERSION").unwrap_or("").to_string(), + app_version: Some(option_env!("CARGO_PKG_VERSION").unwrap_or("").to_string()), outbound_transports: get_outbound_transports(vec![]), localnet: false, } @@ -99,7 +99,7 @@ pub struct SettingsOpt { #[serde(default)] #[structopt(skip)] - pub app_version: String, + pub app_version: Option, /// Prefered transports for outbound connections #[serde(default)]