Handshake
parent: tbd commit: e3e5bf5
Showing 6 changed files with 64 insertions and 24 deletions
Cargo.lock
@@ -629,7 +629,7 @@ dependencies = [ | ||
629 | 629 | |
630 | 630 | [[package]] |
631 | 631 | name = "giterated-daemon" |
632 | version = "0.1.0" | |
632 | version = "0.0.1" | |
633 | 633 | dependencies = [ |
634 | 634 | "aes-gcm", |
635 | 635 | "anyhow", |
@@ -644,6 +644,7 @@ dependencies = [ | ||
644 | 644 | "rand", |
645 | 645 | "reqwest", |
646 | 646 | "rsa", |
647 | "semver", | |
647 | 648 | "serde", |
648 | 649 | "serde_json", |
649 | 650 | "sqlx", |
@@ -1589,6 +1590,15 @@ dependencies = [ | ||
1589 | 1590 | ] |
1590 | 1591 | |
1591 | 1592 | [[package]] |
1593 | name = "semver" | |
1594 | version = "1.0.18" | |
1595 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
1596 | checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918" | |
1597 | dependencies = [ | |
1598 | "serde", | |
1599 | ] | |
1600 | ||
1601 | [[package]] | |
1592 | 1602 | name = "serde" |
1593 | 1603 | version = "1.0.188" |
1594 | 1604 | source = "registry+https://github.com/rust-lang/crates.io-index" |
Cargo.toml
@@ -1,6 +1,6 @@ | ||
1 | 1 | [package] |
2 | 2 | name = "giterated-daemon" |
3 | version = "0.1.0" | |
3 | version = "0.0.1" | |
4 | 4 | edition = "2021" |
5 | 5 | |
6 | 6 | # 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"]} | ||
21 | 21 | reqwest = "*" |
22 | 22 | argon2 = "*" |
23 | 23 | aes-gcm = "0.10.2" |
24 | semver = {version = "*", features = ["serde"]} | |
24 | 25 | |
25 | 26 | toml = { version = "0.7" } |
26 | 27 |
src/connection.rs
@@ -1,7 +1,8 @@ | ||
1 | use std::{collections::HashMap, net::SocketAddr, sync::Arc}; | |
1 | use std::{collections::HashMap, net::SocketAddr, sync::Arc, str::FromStr}; | |
2 | 2 | |
3 | 3 | use anyhow::Error; |
4 | 4 | use futures_util::{stream::StreamExt, SinkExt}; |
5 | use semver::{Version, VersionReq}; | |
5 | 6 | use serde::Serialize; |
6 | 7 | use tokio::{ |
7 | 8 | net::TcpStream, |
@@ -105,26 +106,45 @@ pub async fn connection_worker( | ||
105 | 106 | |
106 | 107 | if let MessageKind::Handshake(handshake) = message { |
107 | 108 | match handshake { |
108 | HandshakeMessage::Initiate(_) => { | |
109 | HandshakeMessage::Initiate(request) => { | |
109 | 110 | // Send HandshakeMessage::Response |
110 | 111 | let message = HandshakeResponse { |
111 | identity: Instance { | |
112 | url: String::from("foo.com"), | |
113 | }, | |
114 | version: String::from("0.1.0"), | |
112 | identity: this_instance.clone(), | |
113 | version: Version::from_str(&env!("CARGO_PKG_VERSION")).unwrap(), | |
115 | 114 | }; |
116 | 115 | |
117 | let _result = send( | |
118 | &mut socket, | |
119 | MessageKind::Handshake(HandshakeMessage::Response(message)), | |
120 | ) | |
121 | .await; | |
116 | let version_check = VersionReq::from_str("=0.0.1").unwrap(); | |
117 | ||
118 | let result = if !version_check.matches( &request.version) { | |
119 | error!("Version compatibility failure! Our Version: {}, Their Version: {}", Version::from_str(&std::env::var("CARGO_PKG_VERSION").unwrap()).unwrap(), request.version); | |
120 | ||
121 | send( | |
122 | &mut socket, | |
123 | MessageKind::Handshake(HandshakeMessage::Finalize(HandshakeFinalize { success: false })), | |
124 | ).await | |
125 | } else { | |
126 | send( | |
127 | &mut socket, | |
128 | MessageKind::Handshake(HandshakeMessage::Response(message)), | |
129 | ) | |
130 | .await | |
131 | }; | |
122 | 132 | |
123 | 133 | continue; |
124 | 134 | } |
125 | HandshakeMessage::Response(_) => { | |
126 | // Send HandshakeMessage::Finalize | |
127 | let message = HandshakeFinalize { success: true }; | |
135 | HandshakeMessage::Response(response) => { | |
136 | // Check version | |
137 | let version_check = VersionReq::from_str("=0.0.1").unwrap(); | |
138 | ||
139 | let message = if !version_check.matches( &response.version) { | |
140 | error!("Version compatibility failure! Our Version: {}, Their Version: {}", Version::from_str(&std::env::var("CARGO_PKG_VERSION").unwrap()).unwrap(), response.version); | |
141 | ||
142 | HandshakeFinalize { success: false } | |
143 | } else { | |
144 | info!("Connected with a compatible version"); | |
145 | ||
146 | HandshakeFinalize { success: true } | |
147 | }; | |
128 | 148 | |
129 | 149 | let _result = send( |
130 | 150 | &mut socket, |
@@ -451,7 +471,9 @@ async fn send_and_get_listener( | ||
451 | 471 | let connections = connections.lock().await; |
452 | 472 | |
453 | 473 | if let Some(connection) = connections.instance_connections.get(&target) { |
454 | connection.sender.send(message); | |
474 | if let Err(_) = connection.sender.send(message) { | |
475 | error!("Error sending message."); | |
476 | } | |
455 | 477 | } else { |
456 | 478 | error!("Unable to message {}, this is a bug.", target.url); |
457 | 479 |
src/handshake.rs
@@ -1,3 +1,4 @@ | ||
1 | use semver::Version; | |
1 | 2 | use serde::{Deserialize, Serialize}; |
2 | 3 | |
3 | 4 | use crate::model::instance::Instance; |
@@ -6,14 +7,14 @@ use crate::model::instance::Instance; | ||
6 | 7 | #[derive(Clone, Serialize, Deserialize)] |
7 | 8 | pub struct InitiateHandshake { |
8 | 9 | pub identity: Instance, |
9 | pub version: String, | |
10 | pub version: Version, | |
10 | 11 | } |
11 | 12 | |
12 | 13 | /// Sent in response to [`InitiateHandshake`] |
13 | 14 | #[derive(Clone, Serialize, Deserialize)] |
14 | 15 | pub struct HandshakeResponse { |
15 | 16 | pub identity: Instance, |
16 | pub version: String, | |
17 | pub version: Version, | |
17 | 18 | } |
18 | 19 | |
19 | 20 | #[derive(Clone, Serialize, Deserialize)] |
src/lib.rs
@@ -1,3 +1,7 @@ | ||
1 | use std::str::FromStr; | |
2 | ||
3 | use semver::Version; | |
4 | ||
1 | 5 | pub mod authentication; |
2 | 6 | pub mod backend; |
3 | 7 | pub mod connection; |
@@ -8,3 +12,7 @@ pub mod model; | ||
8 | 12 | |
9 | 13 | #[macro_use] |
10 | 14 | extern crate tracing; |
15 | ||
16 | pub fn version() -> Version { | |
17 | Version::from_str(env!("CARGO_PKG_VERSION")).unwrap() | |
18 | } | |
18 | \ No newline at end of file |
src/main.rs
@@ -49,17 +49,15 @@ async fn main() -> Result<(), Error> { | ||
49 | 49 | sqlx::migrate!().run(&db_pool).await?; |
50 | 50 | info!("Connected"); |
51 | 51 | |
52 | let repository_backend: Arc<Mutex<dyn RepositoryBackend + Send>> = Arc::new(Mutex::new({ | |
53 | let foo: GitBackend = GitBackend { | |
52 | let repository_backend: Arc<Mutex<dyn RepositoryBackend + Send>> = | |
53 | Arc::new(Mutex::new(GitBackend { | |
54 | 54 | pg_pool: db_pool.clone(), |
55 | 55 | repository_folder: String::from( |
56 | 56 | config["giterated"]["backend"]["git"]["root"] |
57 | 57 | .as_str() |
58 | 58 | .unwrap(), |
59 | 59 | ), |
60 | }; | |
61 | foo | |
62 | })); | |
60 | })); | |
63 | 61 | |
64 | 62 | let token_granter = Arc::new(Mutex::new(AuthenticationTokenGranter { |
65 | 63 | config: config.clone(), |