diff --git a/services/tunnelbroker/src/main.rs b/services/tunnelbroker/src/main.rs index e6606ae4a..8fc6a2455 100644 --- a/services/tunnelbroker/src/main.rs +++ b/services/tunnelbroker/src/main.rs @@ -1,17 +1,17 @@ pub mod constants; pub mod cxx_bridge; pub mod notifications; pub mod server; -mod websockets; +pub mod websockets; use std::io; use tracing; #[tokio::main] async fn main() -> Result<(), io::Error> { let subscriber = tracing_subscriber::FmtSubscriber::new(); tracing::subscriber::set_global_default(subscriber) .expect("Unable to configure tracing"); cxx_bridge::ffi::initialize(); websockets::create_server().await } diff --git a/services/tunnelbroker/src/websockets/messages.rs b/services/tunnelbroker/src/websockets/messages.rs new file mode 100644 index 000000000..8038af168 --- /dev/null +++ b/services/tunnelbroker/src/websockets/messages.rs @@ -0,0 +1,79 @@ +// This file intends to preserve the original structure of the tunnelbroker +// protobuf RPCs. However, this is converted to simple rust datastructures +// to allow for deserialization and serialization into JSON websocket messages + +// Original Tunnelbroker Service definition: +// +// service TunnelbrokerService { +// rpc SessionSignature(SessionSignatureRequest) returns (SessionSignatureResponse) {} +// rpc NewSession(NewSessionRequest) returns (NewSessionResponse) {} +// rpc MessagesStream(stream MessageToTunnelbroker) returns (stream MessageToClient) {} +// } + +// Session +pub struct SessionSignatureRequest { + pub device_id: String, +} +pub struct SessionSignatureResponse { + pub to_sign: String, +} + +pub enum DeviceTypes { + Mobile, + Web, + Keyserver, +} + +pub struct SessionRequest { + pub device_id: String, + pub public_key: String, + pub signature: String, + pub notify_token: Option, + pub device_type: DeviceTypes, + pub device_app_version: String, + pub device_os: String, +} + +pub struct SessionResponse { + pub session_id: String, +} + +// Common messages structures for the MessagesStream +pub struct ProcessedMessages { + pub message_id: Vec, +} + +// The messages from the Client to the Tunnelbroker +pub struct MessageToTunnelbrokerStruct { + pub to_device_id: String, + pub payload: String, + pub blob_hashes: Vec, +} + +pub struct MessagesToSend { + pub messages: Vec, +} + +pub enum MessageToTunnelbroker { + Messages(MessagesToSend), + ProcessedMessages(ProcessedMessages), + NewNotifyToken(String), +} + +// The messages from the Tunnelbroker to the Client +pub struct MessageToClientStruct { + pub message_id: String, + pub from_device_id: String, + pub payload: String, + pub blob_hashes: Vec, +} + +pub struct MessagesToDeliver { + pub messages: Vec, +} + +pub enum MessageToClient { + Messages(MessagesToDeliver), + ProcessedMessages(ProcessedMessages), + NewNotifyTokenRequired, +} diff --git a/services/tunnelbroker/src/websockets/mod.rs b/services/tunnelbroker/src/websockets/mod.rs index 9fd60135b..7cf30b56d 100644 --- a/services/tunnelbroker/src/websockets/mod.rs +++ b/services/tunnelbroker/src/websockets/mod.rs @@ -1,22 +1,24 @@ +pub mod messages; + use std::{env, io::Error}; use tokio::net::{TcpListener, TcpStream}; use tracing::info; pub async fn create_server() -> Result<(), Error> { let addr = env::var("COMM_TUNNELBROKER_WEBSOCKET_ADDR") .unwrap_or_else(|_| "127.0.0.1:51001".to_string()); let listener = TcpListener::bind(&addr).await.expect("Failed to bind"); info!("Listening on: {}", addr); while let Ok((stream, _)) = listener.accept().await { tokio::spawn(accept_connection(stream)); } Ok(()) } async fn accept_connection(_stream: TcpStream) { unimplemented!() }