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

ambee/giterated

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

Expose errors

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨6b2125c

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