Skip to content

Commit

Permalink
feat: copy over more so downstream can compile
Browse files Browse the repository at this point in the history
  • Loading branch information
c-git committed Dec 11, 2024
1 parent 60953e0 commit b9db72f
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 21 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/ws-auth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ actix-web.workspace = true
actix-ws.workspace = true
anyhow.workspace = true
futures-util.workspace = true
rand.workspace = true
serde.workspace = true
thiserror.workspace = true
tokio.workspace = true
Expand Down
12 changes: 6 additions & 6 deletions crates/ws-auth/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::WsId;
use wykies_shared::host_branch::HostId;

#[derive(thiserror::Error, Debug)]
pub enum WebSocketError {
pub enum WebSocketAuthError {
/// Client was not expected to be trying to connect
#[error("Unexpected Client")]
UnexpectedClient {
Expand All @@ -25,13 +25,13 @@ pub mod conversions {
use super::*;
use actix_web::http::StatusCode;

impl actix_web::error::ResponseError for WebSocketError {
impl actix_web::error::ResponseError for WebSocketAuthError {
fn status_code(&self) -> StatusCode {
match self {
WebSocketError::UnexpectedClient { .. } => StatusCode::BAD_REQUEST,
WebSocketError::InvalidToken { .. } => StatusCode::BAD_REQUEST,
WebSocketError::FailedToStartSession(_) => StatusCode::INTERNAL_SERVER_ERROR,
WebSocketError::UnexpectedError(_) => StatusCode::INTERNAL_SERVER_ERROR,
WebSocketAuthError::UnexpectedClient { .. } => StatusCode::BAD_REQUEST,
WebSocketAuthError::InvalidToken { .. } => StatusCode::BAD_REQUEST,
WebSocketAuthError::FailedToStartSession(_) => StatusCode::INTERNAL_SERVER_ERROR,
WebSocketAuthError::UnexpectedError(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions crates/ws-auth/src/id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use rand::Rng as _;
use std::{hash::Hash, sync::Arc};
use wykies_shared::session::UserSessionInfo;

/// Distinguishes different types of Websocket services supported
#[derive(Debug, PartialEq, Eq, Clone, Copy, serde::Serialize, serde::Deserialize)]
pub struct WsId(u8);

impl WsId {
#[cfg(test)]
pub(crate) const TEST1: Self = Self::new(1);

pub const fn new(value: u8) -> Self {
Self(value)
}
}

#[derive(Debug, Clone)]
/// Websocket Connection ID
/// Includes user session info as this is not available in the websocket context
/// Only the id is used for hashing and equality checks
pub struct WsConnId {
id: usize,
pub user_info: Arc<UserSessionInfo>,
}

impl WsConnId {
pub fn new(user_info: Arc<UserSessionInfo>) -> Self {
Self {
id: rand::thread_rng().gen::<usize>(),
user_info,
}
}
}

impl PartialEq for WsConnId {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}

impl Eq for WsConnId {}

impl Hash for WsConnId {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
19 changes: 4 additions & 15 deletions crates/ws-auth/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
//! Provides authenticated access to web socket handlers
//! Provides authentication for web socket handlers
mod errors;
mod id;
mod manager;

pub use errors::WebSocketError;
pub use errors::WebSocketAuthError;
pub use id::{WsConnId, WsId};
pub use manager::{validate_ws_connection, AuthTokenManager};

/// Distinguishes different types of Websocket services supported
#[derive(Debug, PartialEq, Eq, Clone, Copy, serde::Serialize, serde::Deserialize)]
pub struct WsId(u8);

impl WsId {
#[cfg(test)]
const TEST1: Self = Self::new(1);

pub const fn new(value: u8) -> Self {
Self(value)
}
}
1 change: 1 addition & 0 deletions crates/wykies-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ secrecy.workspace = true
serde.workspace = true
serde-aux.workspace = true
sqlx.workspace = true
tokio.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
tracked-cancellations.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions crates/wykies-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ use wykies_shared::telemetry;
mod configuration;
mod macros;
pub mod plugin;
pub mod ws;

#[cfg_attr(feature = "mysql", path = "db_types_mysql.rs")]
pub mod db_types;

pub use configuration::{get_configuration, Configuration, DatabaseSettings, WebSocketSettings};

// TODO 1: Move server init into module
pub struct ServerInit<T: Clone> {
pub cancellation_token: TrackedCancellationToken,
pub cancellation_tracker: CancellationTracker,
Expand Down
3 changes: 3 additions & 0 deletions crates/wykies-server/src/ws.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod heartbeat;

pub use heartbeat::HeartbeatConfig;
37 changes: 37 additions & 0 deletions crates/wykies-server/src/ws/heartbeat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::{fmt::Display, time::Duration};

use crate::WebSocketSettings;
use tracing::instrument;
use wykies_time::Seconds;

#[derive(Debug, Clone, Copy)]
pub struct HeartbeatConfig {
interval_time: Seconds,
client_timeout: Seconds,
}

impl HeartbeatConfig {
#[instrument(ret)]
pub fn new(interval_time: Seconds, ws_config: &WebSocketSettings) -> Self {
let times_missed_allowance = ws_config.heartbeat_times_missed_allowance.into();
let additional_buffer_time = ws_config.heartbeat_additional_buffer_time_secs;
let client_timeout = interval_time * times_missed_allowance + additional_buffer_time;

Self {
interval_time,
client_timeout,
}
}

pub fn interval(&self) -> tokio::time::Interval {
tokio::time::interval(self.interval_time.into())
}

pub fn client_timeout(&self) -> Duration {
self.client_timeout.into()
}

pub fn client_timeout_display(&self) -> impl Display {
format!("{} sec", self.client_timeout)
}
}

0 comments on commit b9db72f

Please sign in to comment.