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

ambee/giterated

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

Implement Debug on all messages

Type: Fix

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨249c88e

⁨giterated-daemon/src/connection/handshake.rs⁩ - ⁨4385⁩ 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::{HandshakeMessage, 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 HandshakeMessage(initiation): HandshakeMessage<InitiateHandshake>,
46 State(connection_state): State<ConnectionState>,
47 ) -> Result<(), HandshakeError> {
48 info!("meow!");
49 connection_state
50 .send(HandshakeResponse {
51 identity: connection_state.instance.clone(),
52 version: version(),
53 })
54 .await
55 .map_err(|e| HandshakeError::SendError(e))?;
56
57 Ok(())
58 // if !validate_version(&initiation.version) {
59 // error!(
60 // "Version compatibility failure! Our Version: {}, Their Version: {}",
61 // Version::from_str(&std::env::var("CARGO_PKG_VERSION").unwrap()).unwrap(),
62 // initiation.version
63 // );
64
65 // connection_state
66 // .send(HandshakeFinalize { success: false })
67 // .await
68 // .map_err(|e| HandshakeError::SendError(e))?;
69
70 // Ok(())
71 // } else {
72 // connection_state
73 // .send(HandshakeResponse {
74 // identity: connection_state.instance.clone(),
75 // version: version(),
76 // })
77 // .await
78 // .map_err(|e| HandshakeError::SendError(e))?;
79
80 // Ok(())
81 // }
82 }
83
84 async fn handshake_response(
85 HandshakeMessage(initiation): HandshakeMessage<HandshakeResponse>,
86 State(connection_state): State<ConnectionState>,
87 ) -> Result<(), HandshakeError> {
88 connection_state
89 .send(HandshakeFinalize { success: true })
90 .await
91 .map_err(|e| HandshakeError::SendError(e))?;
92
93 Ok(())
94 // if !validate_version(&response.version) {
95 // error!(
96 // "Version compatibility failure! Our Version: {}, Their Version: {}",
97 // Version::from_str(&std::env::var("CARGO_PKG_VERSION").unwrap()).unwrap(),
98 // response.version
99 // );
100
101 // connection_state
102 // .send(HandshakeFinalize { success: false })
103 // .await
104 // .map_err(|e| HandshakeError::SendError(e))?;
105
106 // Ok(())
107 // } else {
108 // connection_state
109 // .send(HandshakeFinalize { success: true })
110 // .await
111 // .map_err(|e| HandshakeError::SendError(e))?;
112
113 // Ok(())
114 // }
115 }
116
117 async fn handshake_finalize(
118 HandshakeMessage(finalize): HandshakeMessage<HandshakeFinalize>,
119 State(connection_state): State<ConnectionState>,
120 ) -> Result<(), HandshakeError> {
121 connection_state.handshaked.store(true, Ordering::SeqCst);
122
123 connection_state
124 .send(HandshakeFinalize { success: true })
125 .await
126 .map_err(|e| HandshakeError::SendError(e))?;
127
128 Ok(())
129 // if !finalize.success {
130 // error!("Error during handshake, aborting connection");
131 // return Err(Error::from(ConnectionError::Shutdown).into());
132 // } else {
133 // connection_state.handshaked.store(true, Ordering::SeqCst);
134
135 // connection_state
136 // .send(HandshakeFinalize { success: true })
137 // .await
138 // .map_err(|e| HandshakeError::SendError(e))?;
139
140 // Ok(())
141 // }
142 }
143
144 #[derive(Debug, thiserror::Error)]
145 pub enum HandshakeError {
146 #[error("version mismatch during handshake, ours: {0}, theirs: {1}")]
147 VersionMismatch(Version, Version),
148 #[error("while sending message: {0}")]
149 SendError(Error),
150 #[error("{0}")]
151 Other(#[from] Error),
152 }
153