Просмотр исходного кода

p2pnet: optional app_version implemented

aggstam 3 лет назад
Родитель
Сommit
d433dbd02e

+ 2 - 1
bin/darkwikid/src/main.rs

@@ -623,7 +623,6 @@ async fn realmain(settings: Args, executor: Arc<Executor<'_>>) -> 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<Executor<'_>>) -> 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::<NetMsg>();
 
     let p2p = net::P2p::new(net_settings.into()).await;

+ 1 - 1
bin/ircd/src/main.rs

@@ -130,7 +130,7 @@ async fn realmain(settings: Args, executor: Arc<Executor<'_>>) -> 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::<Privmsg>();
 
     let p2p = net::P2p::new(net_settings.into()).await;

+ 1 - 0
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()
     };
 

+ 2 - 1
bin/tau/taud/src/main.rs

@@ -184,7 +184,6 @@ async fn realmain(settings: Args, executor: Arc<Executor<'_>>) -> 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<Executor<'_>>) -> 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::<NetMsg>();
 
     let p2p = net::P2p::new(net_settings.into()).await;

+ 28 - 11
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<Self>, executor: Arc<Executor<'_>>) -> 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<Self>) -> 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<Self>) -> 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]");

+ 3 - 3
src/net/settings.rs

@@ -25,7 +25,7 @@ pub struct Settings {
     pub peers: Vec<Url>,
     pub seeds: Vec<Url>,
     pub node_id: String,
-    pub app_version: String,
+    pub app_version: Option<String>,
     pub outbound_transports: Vec<TransportName>,
     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<String>,
 
     /// Prefered transports for outbound connections
     #[serde(default)]