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

ambee/giterated

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

Add repository settings

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨f8eaf38

⁨giterated-daemon/src/connection/handshake.rs⁩ - ⁨4312⁩ bytes
Raw
1 use std::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 message::{HandshakeMessage, MessageHandler, NetworkMessage, State},
11 version,
12 };
13
14 use super::{wrapper::ConnectionState, HandlerUnhandled};
15
16 pub async fn handshake_handle(
17 message: &NetworkMessage,
18 state: &ConnectionState,
19 ) -> Result<(), Error> {
20 if initiate_handshake
21 .handle_message(&message, state)
22 .await
23 .is_ok()
24 {
25 Ok(())
26 } else if handshake_response
27 .handle_message(&message, state)
28 .await
29 .is_ok()
30 {
31 Ok(())
32 } else if handshake_finalize
33 .handle_message(&message, state)
34 .await
35 .is_ok()
36 {
37 Ok(())
38 } else {
39 Err(Error::from(HandlerUnhandled))
40 }
41 }
42
43 async fn initiate_handshake(
44 HandshakeMessage(_initiation): HandshakeMessage<InitiateHandshake>,
45 State(connection_state): State<ConnectionState>,
46 ) -> Result<(), HandshakeError> {
47 info!("meow!");
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 HandshakeMessage(_initiation): HandshakeMessage<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 HandshakeMessage(_finalize): HandshakeMessage<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