diff --git a/src/authentication.rs b/src/authentication.rs index 2ae8003..f0f682c 100644 --- a/src/authentication.rs +++ b/src/authentication.rs @@ -107,7 +107,9 @@ impl AuthenticationTokenGranter { ) .unwrap(); - Ok(AuthenticationTokenResponse { token: UserAuthenticationToken::from(token) }) + Ok(AuthenticationTokenResponse { + token: UserAuthenticationToken::from(token), + }) } pub async fn extension_request( diff --git a/src/backend/git.rs b/src/backend/git.rs index a95b312..c596d18 100644 --- a/src/backend/git.rs +++ b/src/backend/git.rs @@ -262,8 +262,7 @@ impl RepositoryBackend for GitBackend { error!("Failed creating repository on disk!? {:?}", err); // Delete repository from database - self - .delete_by_owner_user_name(&request.owner, request.name.as_str()) + self.delete_by_owner_user_name(&request.owner, request.name.as_str()) .await?; // ??? diff --git a/src/backend/mod.rs b/src/backend/mod.rs index a1f0199..6c644a2 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -6,6 +6,7 @@ pub mod user; use anyhow::Error; use async_trait::async_trait; +use crate::backend::git::GitBackendError; use crate::{ messages::{ authentication::{ @@ -28,7 +29,6 @@ use crate::{ user::User, }, }; -use crate::backend::git::GitBackendError; #[async_trait] pub trait RepositoryBackend: IssuesBackend { diff --git a/src/connection/authentication.rs b/src/connection/authentication.rs index 25cff28..e194703 100644 --- a/src/connection/authentication.rs +++ b/src/connection/authentication.rs @@ -11,29 +11,27 @@ use super::wrapper::ConnectionState; use super::HandlerUnhandled; pub async fn authentication_handle( + message_type: &str, message: &NetworkMessage, state: &ConnectionState, ) -> Result<(), Error> { - if register_account_request - .handle_message(&message, state) - .await - .is_ok() - { - Ok(()) - } else if authentication_token_request - .handle_message(&message, state) - .await - .is_ok() - { - Ok(()) - } else if token_extension_request - .handle_message(&message, state) - .await - .is_ok() - { - Ok(()) - } else { - Err(Error::from(HandlerUnhandled)) + match message_type { + "&giterated_daemon::messages::authentication::RegisterAccountRequest" => { + register_account_request + .handle_message(&message, state) + .await + } + "&giterated_daemon::messages::authentication::AuthenticationTokenRequest" => { + authentication_token_request + .handle_message(&message, state) + .await + } + "&giterated_daemon::messages::authentication::TokenExtensionRequest" => { + token_extension_request + .handle_message(&message, state) + .await + } + _ => Err(Error::from(HandlerUnhandled)), } } diff --git a/src/connection/handshake.rs b/src/connection/handshake.rs index 83ef0c4..1368ded 100644 --- a/src/connection/handshake.rs +++ b/src/connection/handshake.rs @@ -7,7 +7,7 @@ use crate::{ connection::ConnectionError, messages::handshake::{HandshakeFinalize, HandshakeResponse, InitiateHandshake}, model::authenticated::{AuthenticatedInstance, Message, MessageHandler, NetworkMessage, State}, - validate_version, + validate_version, version, }; use super::{wrapper::ConnectionState, HandlerUnhandled}; @@ -42,7 +42,6 @@ pub async fn handshake_handle( async fn initiate_handshake( Message(initiation): Message, State(connection_state): State, - _instance: AuthenticatedInstance, ) -> Result<(), HandshakeError> { if !validate_version(&initiation.version) { error!( @@ -59,7 +58,10 @@ async fn initiate_handshake( Ok(()) } else { connection_state - .send(HandshakeFinalize { success: true }) + .send(HandshakeResponse { + identity: connection_state.instance.clone(), + version: version(), + }) .await .map_err(|e| HandshakeError::SendError(e))?; @@ -70,7 +72,6 @@ async fn initiate_handshake( async fn handshake_response( Message(response): Message, State(connection_state): State, - _instance: AuthenticatedInstance, ) -> Result<(), HandshakeError> { if !validate_version(&response.version) { error!( @@ -98,7 +99,6 @@ async fn handshake_response( async fn handshake_finalize( Message(finalize): Message, State(connection_state): State, - _instance: AuthenticatedInstance, ) -> Result<(), HandshakeError> { if !finalize.success { error!("Error during handshake, aborting connection"); diff --git a/src/connection/repository.rs b/src/connection/repository.rs index 4cbe206..117d3ea 100644 --- a/src/connection/repository.rs +++ b/src/connection/repository.rs @@ -1,5 +1,6 @@ use anyhow::Error; +use crate::backend::git::GitBackendError; use crate::{ messages::repository::{ RepositoryCreateRequest, RepositoryFileInspectRequest, RepositoryInfoRequest, @@ -7,40 +8,36 @@ use crate::{ }, model::authenticated::{AuthenticatedUser, Message, MessageHandler, NetworkMessage, State}, }; -use crate::backend::git::GitBackendError; use super::{wrapper::ConnectionState, HandlerUnhandled}; pub async fn repository_handle( + message_type: &str, message: &NetworkMessage, state: &ConnectionState, ) -> Result<(), Error> { - if create_repository - .handle_message(&message, state) - .await - .is_ok() - { - Ok(()) - } else if repository_file_inspect - .handle_message(&message, state) - .await - .is_ok() - { - Ok(()) - } else if repository_info - .handle_message(&message, state) - .await - .is_ok() - { - Ok(()) - } else if issues_count.handle_message(&message, state).await.is_ok() { - Ok(()) - } else if issue_labels.handle_message(&message, state).await.is_ok() { - Ok(()) - } else if issues.handle_message(&message, state).await.is_ok() { - Ok(()) - } else { - Err(Error::from(HandlerUnhandled)) + match message_type { + "&giterated_daemon::messages::repository::RepositoryCreateRequest" => { + create_repository.handle_message(&message, state).await + } + "&giterated_daemon::messages::repository::RepositoryFileInspectRequest" => { + repository_file_inspect + .handle_message(&message, state) + .await + } + "&giterated_daemon::messages::repository::RepositoryInfoRequest" => { + repository_info.handle_message(&message, state).await + } + "&giterated_daemon::messages::repository::RepositoryIssuesCountRequest" => { + issues_count.handle_message(&message, state).await + } + "&giterated_daemon::messages::repository::RepositoryIssueLabelsRequest" => { + issue_labels.handle_message(&message, state).await + } + "&giterated_daemon::messages::repository::RepositoryIssuesRequest" => { + issues.handle_message(&message, state).await + } + _ => Err(Error::from(HandlerUnhandled)), } } diff --git a/src/connection/user.rs b/src/connection/user.rs index 7d8f91c..b9f5b15 100644 --- a/src/connection/user.rs +++ b/src/connection/user.rs @@ -1,4 +1,5 @@ use anyhow::Error; +use serde_json::Value; use crate::{ messages::user::{ @@ -10,17 +11,25 @@ use crate::{ use super::{wrapper::ConnectionState, HandlerUnhandled}; -pub async fn user_handle(message: &NetworkMessage, state: &ConnectionState) -> Result<(), Error> { - if display_name.handle_message(&message, state).await.is_ok() { - Ok(()) - } else if display_image.handle_message(&message, state).await.is_ok() { - Ok(()) - } else if bio.handle_message(&message, state).await.is_ok() { - Ok(()) - } else if repositories.handle_message(&message, state).await.is_ok() { - Ok(()) - } else { - Err(Error::from(HandlerUnhandled)) +pub async fn user_handle( + message_type: &str, + message: &NetworkMessage, + state: &ConnectionState, +) -> Result<(), Error> { + match message_type { + "&giterated_daemon::messages::user::UserDisplayNameRequest" => { + display_name.handle_message(&message, state).await + } + "&giterated_daemon::messages::user::UserDisplayImageRequest" => { + display_image.handle_message(&message, state).await + } + "&giterated_daemon::messages::user::UserBioRequest" => { + bio.handle_message(&message, state).await + } + "&giterated_daemon::messages::user::UserRepositoriesRequest" => { + repositories.handle_message(&message, state).await + } + _ => Err(Error::from(HandlerUnhandled)), } } diff --git a/src/connection/wrapper.rs b/src/connection/wrapper.rs index 8639f93..a6e5237 100644 --- a/src/connection/wrapper.rs +++ b/src/connection/wrapper.rs @@ -9,6 +9,7 @@ use std::{ use anyhow::Error; use futures_util::{SinkExt, StreamExt}; use serde::Serialize; +use serde_json::Value; use tokio::{net::TcpStream, sync::Mutex}; use tokio_tungstenite::{tungstenite::Message, WebSocketStream}; @@ -64,7 +65,7 @@ pub async fn connection_wrapper( _ => continue, }; - let message = NetworkMessage(payload); + let message = NetworkMessage(payload.clone()); if !handshaked { if handshake_handle(&message, &connection_state).await.is_ok() { @@ -73,25 +74,41 @@ pub async fn connection_wrapper( } } } else { - if authentication_handle(&message, &connection_state) + let raw = serde_json::from_slice::(&payload).unwrap(); + let message_type = raw.get("message_type").unwrap().as_str().unwrap(); + + if authentication_handle(message_type, &message, &connection_state) .await .is_ok() { continue; - } else if repository_handle(&message, &connection_state).await.is_ok() { + } else if repository_handle(message_type, &message, &connection_state) + .await + .is_ok() + { continue; - } else if user_handle(&message, &connection_state).await.is_ok() { + } else if user_handle(message_type, &message, &connection_state) + .await + .is_ok() + { continue; } else { - error!("Message completely unhandled"); + error!( + "Message completely unhandled: {}", + std::str::from_utf8(&payload).unwrap() + ); continue; } } } - _ => { - error!("Closing connection for {}", addr); + Some(Err(e)) => { + error!("Closing connection for {:?} for {}", e, addr); return; } + _ => { + info!("Unhandled"); + continue; + } } } } @@ -110,10 +127,12 @@ pub struct ConnectionState { impl ConnectionState { pub async fn send(&self, message: T) -> Result<(), Error> { + let payload = serde_json::to_string(&message)?; + info!("Sending payload: {}", &payload); self.socket .lock() .await - .send(Message::Binary(serde_json::to_vec(&message)?)) + .send(Message::Binary(payload.into_bytes())) .await?; Ok(()) diff --git a/src/model/authenticated.rs b/src/model/authenticated.rs index 3e0077f..f5f8b5f 100644 --- a/src/model/authenticated.rs +++ b/src/model/authenticated.rs @@ -243,7 +243,11 @@ impl FromMessage for AuthenticatedInstance { .source .iter() .filter_map(|auth| { - if let AuthenticationSource::Instance { instance, signature } = auth { + if let AuthenticationSource::Instance { + instance, + signature, + } = auth + { Some((instance, signature)) } else { None