From 8f40e954801229034da07e8ea89374a006f8e52c Mon Sep 17 00:00:00 2001 From: DJMrTV Date: Fri, 16 May 2025 13:08:13 +0200 Subject: [PATCH] feat: start implementing api --- src/main.rs | 5 ++- src/prudp/socket.rs | 2 - src/rmc/response.rs | 3 -- src/web/mod.rs | 92 +++++++++++++++++++++++++++++++++------------ 4 files changed, 71 insertions(+), 31 deletions(-) diff --git a/src/main.rs b/src/main.rs index e71b062..774adea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -312,6 +312,8 @@ async fn start_secure() -> JoinHandle<()> { rv_cid_counter: AtomicU32::new(1), }); + let web_server = web::start_web(mmm.clone()).await; + let (router_secure, _) = Router::new(SocketAddrV4::new(*OWN_IP_PRIVATE, *SECURE_SERVER_PORT)) .await @@ -383,7 +385,7 @@ async fn start_servers() { let auth_server = start_auth().await; #[cfg(feature = "secure")] let secure_server = start_secure().await; - //let web_server = web::start_web().await; + tokio::time::sleep(Duration::from_secs(1)).await; @@ -395,5 +397,4 @@ async fn start_servers() { auth_server.await.expect("auth server crashed"); #[cfg(feature = "secure")] secure_server.await.expect("auth server crashed"); - //web_server.await.expect("webserver crashed"); } diff --git a/src/prudp/socket.rs b/src/prudp/socket.rs index c9d7513..d9224fd 100644 --- a/src/prudp/socket.rs +++ b/src/prudp/socket.rs @@ -4,8 +4,6 @@ use crate::prudp::packet::PacketOption::{ConnectionSignature, FragmentId, Initia use crate::prudp::packet::{PRUDPHeader, PRUDPPacket, PacketOption, TypesFlags, VirtualPort}; use crate::prudp::router::{Error, Router}; use crate::prudp::sockaddr::PRUDPSockAddr; -use crate::web::DirectionalData::Outgoing; -use crate::web::WEB_DATA; use async_trait::async_trait; use hmac::digest::consts::U5; use log::info; diff --git a/src/rmc/response.rs b/src/rmc/response.rs index 26bc47b..f63c345 100644 --- a/src/rmc/response.rs +++ b/src/rmc/response.rs @@ -13,9 +13,6 @@ use crate::prudp::socket::{ExternalConnection, SendingConnection}; use crate::rmc::response::ErrorCode::Core_Exception; use crate::rmc::structures::qresult::ERROR_MASK; use crate::rmc::structures::RmcSerialize; -use crate::web::DirectionalData::{Incoming, Outgoing}; -use crate::web::WEB_DATA; - pub enum RMCResponseResult { Success { call_id: u32, diff --git a/src/web/mod.rs b/src/web/mod.rs index 3461257..e096f34 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -1,37 +1,81 @@ use std::net::SocketAddrV4; +use std::sync::Arc; +use async_trait::async_trait; use once_cell::sync::Lazy; -use rocket::{get, routes, Rocket}; +use rocket::{get, routes, Request, Rocket, State}; +use rocket::request::{FromRequest, Outcome}; use rocket::serde::json::Json; use tokio::task::JoinHandle; use serde::Serialize; use tokio::sync::Mutex; +use crate::nex::matchmake::MatchmakeManager; +use crate::rmc::protocols::notifications::NotificationEvent; -#[get("/")] -async fn server_data() -> Json { - Json(WEB_DATA.lock().await.clone()) +struct RnexApiAuth; + +#[async_trait] +impl<'r> FromRequest<'r> for RnexApiAuth{ + + type Error = (); + async fn from_request<'a>(request: &'r Request<'a>) -> Outcome { + Outcome::Success(RnexApiAuth) + } } -pub async fn start_web() -> JoinHandle<()>{ - tokio::spawn(async{ + +#[get("/gatherings")] +async fn gatherings(mmm: &State>) -> Json>{ + let matches = mmm.sessions.read().await; + + Json(matches.keys().map(|v| *v).collect()) +} + +#[get("/gathering//players")] +async fn players_in_match(mmm: &State>, gid: u32) -> Option>>{ + let mmm = mmm.sessions.read().await; + + let gathering = mmm.get(&gid)?; + + let gathering = gathering.clone(); + + drop(mmm); + + let gathering = gathering.lock().await; + + Some(Json(gathering.connected_players.iter().filter_map(|p| p.upgrade()).map(|p| p.pid).collect())) +} + +#[get("/gathering//close")] +async fn close_gathering(_auth: RnexApiAuth, mmm: &State>, gid: u32) -> Option<()>{ + // this doesnt work and is broken, there might be some other way to remotely close gatherings... + // also if anyone gets this working change it to POST cause the only reason its get is because + // that makes testing it easier + let mmm = mmm.sessions.read().await; + + let gathering = mmm.get(&gid)?; + + let gathering = gathering.clone(); + + drop(mmm); + + let gathering = gathering.lock().await; + + gathering.broadcast_notification(&NotificationEvent{ + pid_source: gathering.session.gathering.owner_pid, + notif_type: 109000, + param_1: gathering.session.gathering.self_gid, + ..Default::default() + }).await; + + Some(()) +} + +pub async fn start_web(mgr: Arc) -> JoinHandle<()> { + tokio::spawn(async move { rocket::build() - .mount("/",routes![server_data]) + .mount("/", routes![gatherings, players_in_match, close_gathering]) + .manage(mgr) .launch().await .expect("unable to start webserver"); }) -} -#[derive(Serialize, Clone)] -pub enum DirectionalData{ - Incoming(String), - Outgoing(String) -} - -#[derive(Serialize, Default, Clone)] -pub struct WebData{ - pub data: Vec<(SocketAddrV4, DirectionalData)> -} - -pub static WEB_DATA: Lazy> = Lazy::new(|| Mutex::new( - WebData{ - data: Vec::new(), - } -)); \ No newline at end of file +} \ No newline at end of file