diff --git a/Cargo.lock b/Cargo.lock index 3c1c86f..2810ca9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -629,7 +629,7 @@ dependencies = [ [[package]] name = "giterated-daemon" -version = "0.1.0" +version = "0.0.1" dependencies = [ "aes-gcm", "anyhow", @@ -644,6 +644,7 @@ dependencies = [ "rand", "reqwest", "rsa", + "semver", "serde", "serde_json", "sqlx", @@ -1589,6 +1590,15 @@ dependencies = [ ] [[package]] +name = "semver" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918" +dependencies = [ + "serde", +] + +[[package]] name = "serde" version = "1.0.188" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml index ac1acc0..9bbbda5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "giterated-daemon" -version = "0.1.0" +version = "0.0.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -21,6 +21,7 @@ rsa = {version = "0.9", features = ["sha2"]} reqwest = "*" argon2 = "*" aes-gcm = "0.10.2" +semver = {version = "*", features = ["serde"]} toml = { version = "0.7" } diff --git a/src/connection.rs b/src/connection.rs index df69dcf..30118ad 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -1,7 +1,8 @@ -use std::{collections::HashMap, net::SocketAddr, sync::Arc}; +use std::{collections::HashMap, net::SocketAddr, sync::Arc, str::FromStr}; use anyhow::Error; use futures_util::{stream::StreamExt, SinkExt}; +use semver::{Version, VersionReq}; use serde::Serialize; use tokio::{ net::TcpStream, @@ -105,26 +106,45 @@ pub async fn connection_worker( if let MessageKind::Handshake(handshake) = message { match handshake { - HandshakeMessage::Initiate(_) => { + HandshakeMessage::Initiate(request) => { // Send HandshakeMessage::Response let message = HandshakeResponse { - identity: Instance { - url: String::from("foo.com"), - }, - version: String::from("0.1.0"), + identity: this_instance.clone(), + version: Version::from_str(&env!("CARGO_PKG_VERSION")).unwrap(), }; - let _result = send( - &mut socket, - MessageKind::Handshake(HandshakeMessage::Response(message)), - ) - .await; + let version_check = VersionReq::from_str("=0.0.1").unwrap(); + + let result = if !version_check.matches( &request.version) { + error!("Version compatibility failure! Our Version: {}, Their Version: {}", Version::from_str(&std::env::var("CARGO_PKG_VERSION").unwrap()).unwrap(), request.version); + + send( + &mut socket, + MessageKind::Handshake(HandshakeMessage::Finalize(HandshakeFinalize { success: false })), + ).await + } else { + send( + &mut socket, + MessageKind::Handshake(HandshakeMessage::Response(message)), + ) + .await + }; continue; } - HandshakeMessage::Response(_) => { - // Send HandshakeMessage::Finalize - let message = HandshakeFinalize { success: true }; + HandshakeMessage::Response(response) => { + // Check version + let version_check = VersionReq::from_str("=0.0.1").unwrap(); + + let message = if !version_check.matches( &response.version) { + error!("Version compatibility failure! Our Version: {}, Their Version: {}", Version::from_str(&std::env::var("CARGO_PKG_VERSION").unwrap()).unwrap(), response.version); + + HandshakeFinalize { success: false } + } else { + info!("Connected with a compatible version"); + + HandshakeFinalize { success: true } + }; let _result = send( &mut socket, @@ -451,7 +471,9 @@ async fn send_and_get_listener( let connections = connections.lock().await; if let Some(connection) = connections.instance_connections.get(&target) { - connection.sender.send(message); + if let Err(_) = connection.sender.send(message) { + error!("Error sending message."); + } } else { error!("Unable to message {}, this is a bug.", target.url); diff --git a/src/handshake.rs b/src/handshake.rs index 357960f..0822f2c 100644 --- a/src/handshake.rs +++ b/src/handshake.rs @@ -1,3 +1,4 @@ +use semver::Version; use serde::{Deserialize, Serialize}; use crate::model::instance::Instance; @@ -6,14 +7,14 @@ use crate::model::instance::Instance; #[derive(Clone, Serialize, Deserialize)] pub struct InitiateHandshake { pub identity: Instance, - pub version: String, + pub version: Version, } /// Sent in response to [`InitiateHandshake`] #[derive(Clone, Serialize, Deserialize)] pub struct HandshakeResponse { pub identity: Instance, - pub version: String, + pub version: Version, } #[derive(Clone, Serialize, Deserialize)] diff --git a/src/lib.rs b/src/lib.rs index 39fb094..9b85651 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,7 @@ +use std::str::FromStr; + +use semver::Version; + pub mod authentication; pub mod backend; pub mod connection; @@ -8,3 +12,7 @@ pub mod model; #[macro_use] extern crate tracing; + +pub fn version() -> Version { + Version::from_str(env!("CARGO_PKG_VERSION")).unwrap() +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index d7750ef..00b5719 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,17 +49,15 @@ async fn main() -> Result<(), Error> { sqlx::migrate!().run(&db_pool).await?; info!("Connected"); - let repository_backend: Arc> = Arc::new(Mutex::new({ - let foo: GitBackend = GitBackend { + let repository_backend: Arc> = + Arc::new(Mutex::new(GitBackend { pg_pool: db_pool.clone(), repository_folder: String::from( config["giterated"]["backend"]["git"]["root"] .as_str() .unwrap(), ), - }; - foo - })); + })); let token_granter = Arc::new(Mutex::new(AuthenticationTokenGranter { config: config.clone(),