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

ambee/giterated

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

Major post-refactor cleanup

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨f90d7fb

⁨src/connection/handshake.rs⁩ - ⁨3582⁩ bytes
Raw
1 use std::{str::FromStr, sync::atomic::Ordering};
2
3 use anyhow::Error;
4 use semver::Version;
5
6 use crate::model::authenticated::MessageHandler;
7 use crate::{
8 connection::ConnectionError,
9 handshake::{HandshakeFinalize, HandshakeResponse, InitiateHandshake},
10 model::authenticated::{AuthenticatedInstance, Message, NetworkMessage, State},
11 validate_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 Message(initiation): Message<InitiateHandshake>,
45 State(connection_state): State<ConnectionState>,
46 _instance: AuthenticatedInstance,
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(HandshakeFinalize { success: true })
64 .await
65 .map_err(|e| HandshakeError::SendError(e))?;
66
67 Ok(())
68 }
69 }
70
71 async fn handshake_response(
72 Message(response): Message<HandshakeResponse>,
73 State(connection_state): State<ConnectionState>,
74 _instance: AuthenticatedInstance,
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 _instance: AuthenticatedInstance,
103 ) -> Result<(), HandshakeError> {
104 if !finalize.success {
105 error!("Error during handshake, aborting connection");
106 return Err(Error::from(ConnectionError::Shutdown).into());
107 } else {
108 connection_state.handshaked.store(true, Ordering::SeqCst);
109
110 connection_state
111 .send(HandshakeFinalize { success: true })
112 .await
113 .map_err(|e| HandshakeError::SendError(e))?;
114
115 Ok(())
116 }
117 }
118
119 #[derive(Debug, thiserror::Error)]
120 pub enum HandshakeError {
121 #[error("version mismatch during handshake, ours: {0}, theirs: {1}")]
122 VersionMismatch(Version, Version),
123 #[error("while sending message: {0}")]
124 SendError(Error),
125 #[error("{0}")]
126 Other(#[from] Error),
127 }
128