JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Completely refactor project structure

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨ae8ff44

⁨giterated-daemon/src/connection/handshake.rs⁩ - ⁨3538⁩ bytes
Raw
1 use std::{str::FromStr, sync::atomic::Ordering};
2
3 use anyhow::Error;
4 use giterated_models::messages::handshake::{
5 HandshakeFinalize, HandshakeResponse, InitiateHandshake,
6 };
7 use semver::Version;
8
9 use crate::{
10 connection::ConnectionError,
11 message::{Message, MessageHandler, NetworkMessage, State},
12 validate_version, version,
13 };
14
15 use super::{wrapper::ConnectionState, HandlerUnhandled};
16
17 pub async fn handshake_handle(
18 message: &NetworkMessage,
19 state: &ConnectionState,
20 ) -> Result<(), Error> {
21 if initiate_handshake
22 .handle_message(&message, state)
23 .await
24 .is_ok()
25 {
26 Ok(())
27 } else if handshake_response
28 .handle_message(&message, state)
29 .await
30 .is_ok()
31 {
32 Ok(())
33 } else if handshake_finalize
34 .handle_message(&message, state)
35 .await
36 .is_ok()
37 {
38 Ok(())
39 } else {
40 Err(Error::from(HandlerUnhandled))
41 }
42 }
43
44 async fn initiate_handshake(
45 Message(initiation): Message<InitiateHandshake>,
46 State(connection_state): State<ConnectionState>,
47 ) -> Result<(), HandshakeError> {
48 if !validate_version(&initiation.version) {
49 error!(
50 "Version compatibility failure! Our Version: {}, Their Version: {}",
51 Version::from_str(&std::env::var("CARGO_PKG_VERSION").unwrap()).unwrap(),
52 initiation.version
53 );
54
55 connection_state
56 .send(HandshakeFinalize { success: false })
57 .await
58 .map_err(|e| HandshakeError::SendError(e))?;
59
60 Ok(())
61 } else {
62 connection_state
63 .send(HandshakeResponse {
64 identity: connection_state.instance.clone(),
65 version: version(),
66 })
67 .await
68 .map_err(|e| HandshakeError::SendError(e))?;
69
70 Ok(())
71 }
72 }
73
74 async fn handshake_response(
75 Message(response): Message<HandshakeResponse>,
76 State(connection_state): State<ConnectionState>,
77 ) -> Result<(), HandshakeError> {
78 if !validate_version(&response.version) {
79 error!(
80 "Version compatibility failure! Our Version: {}, Their Version: {}",
81 Version::from_str(&std::env::var("CARGO_PKG_VERSION").unwrap()).unwrap(),
82 response.version
83 );
84
85 connection_state
86 .send(HandshakeFinalize { success: false })
87 .await
88 .map_err(|e| HandshakeError::SendError(e))?;
89
90 Ok(())
91 } else {
92 connection_state
93 .send(HandshakeFinalize { success: true })
94 .await
95 .map_err(|e| HandshakeError::SendError(e))?;
96
97 Ok(())
98 }
99 }
100
101 async fn handshake_finalize(
102 Message(finalize): Message<HandshakeFinalize>,
103 State(connection_state): State<ConnectionState>,
104 ) -> Result<(), HandshakeError> {
105 if !finalize.success {
106 error!("Error during handshake, aborting connection");
107 return Err(Error::from(ConnectionError::Shutdown).into());
108 } else {
109 connection_state.handshaked.store(true, Ordering::SeqCst);
110
111 connection_state
112 .send(HandshakeFinalize { success: true })
113 .await
114 .map_err(|e| HandshakeError::SendError(e))?;
115
116 Ok(())
117 }
118 }
119
120 #[derive(Debug, thiserror::Error)]
121 pub enum HandshakeError {
122 #[error("version mismatch during handshake, ours: {0}, theirs: {1}")]
123 VersionMismatch(Version, Version),
124 #[error("while sending message: {0}")]
125 SendError(Error),
126 #[error("{0}")]
127 Other(#[from] Error),
128 }
129