I tried adding hang session cleanup as it works in Figura, but it doesn't work as it should.

Apparently authCheck (a variable from NetworkStuff.java), is updated a bit unexpectedly.
This commit is contained in:
Shiroyasha 2025-03-06 23:53:20 +03:00
parent a08985544f
commit 48b2b7eeab
Signed by: shiroyashik
GPG key ID: E4953D3940D7860A
3 changed files with 16 additions and 1 deletions

View file

@ -1,7 +1,9 @@
use std::time::Duration;
use anyhow::bail; use anyhow::bail;
use axum::{body::Bytes, extract::{ws::{Message, WebSocket}, State}}; use axum::{body::Bytes, extract::{ws::{Message, WebSocket}, State}};
use dashmap::DashMap; use dashmap::DashMap;
use tokio::sync::{broadcast, mpsc}; use tokio::{sync::{broadcast, mpsc}, time::{sleep_until, Instant}};
use tracing::instrument; use tracing::instrument;
use crate::{auth::Userinfo, AppState}; use crate::{auth::Userinfo, AppState};
@ -67,6 +69,7 @@ async fn handle_socket(mut ws: WebSocket, state: AppState) {
#[instrument(skip_all, fields(nickname = %session.user.nickname))] #[instrument(skip_all, fields(nickname = %session.user.nickname))]
async fn main_worker(session: &mut WSSession, ws: &mut WebSocket, state: &AppState) -> anyhow::Result<()> { async fn main_worker(session: &mut WSSession, ws: &mut WebSocket, state: &AppState) -> anyhow::Result<()> {
tracing::debug!("WebSocket control for {} is transferred to the main worker", session.user.nickname); tracing::debug!("WebSocket control for {} is transferred to the main worker", session.user.nickname);
let mut keep_alive = Instant::now() + Duration::from_secs(360); // 5 minutes and one more just in case + Don't forget about UpdateKeepAlive
loop { loop {
tokio::select! { tokio::select! {
external_msg = ws.recv_and_decode() => { external_msg = ws.recv_and_decode() => {
@ -137,8 +140,16 @@ async fn main_worker(session: &mut WSSession, ws: &mut WebSocket, state: &AppSta
); );
bail!("{} banned!", session.user.nickname) bail!("{} banned!", session.user.nickname)
}, },
SessionMessage::UpdateKeepAlive => {
tracing::trace!(keep_alive = ?keep_alive.saturating_duration_since(Instant::now()), "UpdateKeepAlive");
keep_alive = Instant::now() + Duration::from_secs(360);
}
} }
} }
_ = sleep_until(keep_alive) => {
let _ = ws.send(Message::Close(Some(axum::extract::ws::CloseFrame { code: 4000, reason: "Re-auth".into() }))).await;
bail!("session expired");
}
} }
} }
} }

View file

@ -12,4 +12,5 @@ pub struct WSSession {
pub enum SessionMessage { pub enum SessionMessage {
Ping(Vec<u8>), Ping(Vec<u8>),
Banned, Banned,
UpdateKeepAlive,
} }

View file

@ -295,6 +295,9 @@ pub async fn check_auth(
Err(ApiError::Unauthorized) Err(ApiError::Unauthorized)
} else { } else {
debug!(nickname = user.nickname, status = "ok", "Token verified successfully"); debug!(nickname = user.nickname, status = "ok", "Token verified successfully");
if let Some(session) = state.session.get(&user.uuid) {
let _ = session.send(crate::api::figura::SessionMessage::UpdateKeepAlive).await;
};
Ok("ok") Ok("ok")
} }
} }