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

ambee/giterated

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

Fixes

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨25c3410

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