From 4bd9a2ab2ee7738c25d4539202ca4897881a6ad7 Mon Sep 17 00:00:00 2001 From: shiroyashik Date: Tue, 18 Jun 2024 02:07:59 +0300 Subject: [PATCH] Added a feature to send pings to subscribers via HTTP requests --- src/ws/http.rs | 46 ++++++++++++++++++++++++++++++++++++++++----- src/ws/types/c2s.rs | 9 +++++++++ src/ws/types/s2c.rs | 6 +++--- src/ws/websocket.rs | 2 +- 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/src/ws/http.rs b/src/ws/http.rs index 6ce0518..4770781 100644 --- a/src/ws/http.rs +++ b/src/ws/http.rs @@ -14,12 +14,8 @@ use crate::{auth::Token, AppState}; pub fn router() -> Router { Router::new() .route("/verify", get(verify)) - // .route("/ping", post(ping)) - // .route("/event", post(event)) - // .route("/toast", post(toast)) - // .route("/chat", post(chat)) - // .route("/notice", post(notice)) .route("/raw", post(raw)) + .route("/sub/raw", post(sub_raw)) } #[derive(Deserialize)] @@ -80,4 +76,44 @@ async fn raw( return (StatusCode::NOT_FOUND, "uuid doesnt defined".to_string()).into_response(); }, } +} + +async fn sub_raw( + Token(token): Token, + Query(query): Query, + State(state): State, + body: String, +) -> Response { + debug!(body = body); + match token { + Some(t) => { + if !state.config.lock().await.verify_token(&t) { + return (StatusCode::UNAUTHORIZED, "wrong token".to_string()).into_response() + } + }, + None => return (StatusCode::UNAUTHORIZED, "unauthorized".to_string()).into_response(), + } + let payload = match hex::decode(body) { + Ok(v) => v, + Err(_) => return (StatusCode::NOT_ACCEPTABLE, "not raw data".to_string()).into_response(), + }; + debug!("{:?}", payload); + + + match query.uuid { + Some(uuid) => { + // for only one + let tx = match state.broadcasts.get(&uuid) { + Some(d) => d, + None => return (StatusCode::NOT_FOUND, "unknown uuid".to_string()).into_response(), + }; + match tx.value().send(payload) { + Ok(_) => return (StatusCode::OK, "ok".to_string()).into_response(), + Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, "cant send".to_string()).into_response(), + }; + }, + None => { + return (StatusCode::NOT_FOUND, "uuid doesnt defined".to_string()).into_response(); + }, + } } \ No newline at end of file diff --git a/src/ws/types/c2s.rs b/src/ws/types/c2s.rs index 607ba64..db2d4e7 100644 --- a/src/ws/types/c2s.rs +++ b/src/ws/types/c2s.rs @@ -89,3 +89,12 @@ impl<'a> From> for Box<[u8]> { a } } + +impl<'a> C2SMessage<'a> { + pub fn to_array(&self) -> Box<[u8]> { + >>::into(self.clone()) + } + pub fn to_vec(&self) -> Vec { + self.to_array().to_vec() + } +} \ No newline at end of file diff --git a/src/ws/types/s2c.rs b/src/ws/types/s2c.rs index e08d1cc..d62cdc3 100644 --- a/src/ws/types/s2c.rs +++ b/src/ws/types/s2c.rs @@ -84,10 +84,10 @@ impl<'a> From> for Box<[u8]> { } impl<'a> S2CMessage<'a> { - pub fn to_array(self) -> Box<[u8]> { - >>::into(self) + pub fn to_array(&self) -> Box<[u8]> { + >>::into(self.clone()) } - pub fn to_vec(self) -> Vec { + pub fn to_vec(&self) -> Vec { self.to_array().to_vec() } } diff --git a/src/ws/websocket.rs b/src/ws/websocket.rs index c5c55aa..72fd068 100644 --- a/src/ws/websocket.rs +++ b/src/ws/websocket.rs @@ -78,7 +78,7 @@ async fn handle_socket(mut socket: WebSocket, state: AppState) { }, }; - debug!("[WebSocket{}] Raw: {newmsg:?}", owner.name()); + debug!("[WebSocket{}] MSG: {:?}, HEX: {}", owner.name(), newmsg, hex::encode(newmsg.to_vec())); match newmsg { C2SMessage::Token(token) => {