From 1679590bf3c426a26d09335a3a8ee22fada4704a Mon Sep 17 00:00:00 2001 From: DJMrTV Date: Sat, 1 Feb 2025 19:42:45 +0100 Subject: [PATCH] feat(protocols): extend functionality of protocol macro --- src/main.rs | 15 ++++++++++++++- src/nex/account.rs | 7 +++++-- src/nex/mod.rs | 2 +- src/protocols/auth/method_login_ex.rs | 8 +++----- src/protocols/auth/mod.rs | 3 ++- src/protocols/mod.rs | 15 +++++++++++++-- 6 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index c20cc5e..9e189b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ use once_cell::sync::Lazy; use rc4::{KeyInit, Rc4, StreamCipher}; use rc4::consts::U5; use simplelog::{ColorChoice, CombinedLogger, Config, LevelFilter, TerminalMode, TermLogger, WriteLogger}; +use crate::nex::account::Account; use crate::protocols::auth; use crate::protocols::server::RMCProtocolServer; use crate::prudp::socket::{Socket, SocketData}; @@ -24,6 +25,18 @@ mod prudp; pub mod rmc; mod protocols; +mod nex; + +static KERBEROS_SERVER_PASSWORD: Lazy = Lazy::new(||{ + env::var("AUTH_SERVER_PORT") + .ok() + .unwrap_or("password".to_owned()) +}); + + +static AUTH_SERVER_ACCOUNT: Lazy = Lazy::new(|| Account::new(1, "Quazal Authentication", &KERBEROS_SERVER_PASSWORD)); +static SECURE_SERVER_ACCOUNT: Lazy = Lazy::new(|| Account::new(2, "Quazal Rendez-Vous", &KERBEROS_SERVER_PASSWORD)); + static AUTH_SERVER_PORT: Lazy = Lazy::new(||{ env::var("AUTH_SERVER_PORT") .ok() @@ -70,7 +83,7 @@ async fn start_servers(){ // dont assign it to the name _ as that will make it drop right here and now let rmcserver = RMCProtocolServer::new(Box::new([ - Box::new(auth::protocol) + Box::new(auth::bound_protocol(&SECURE_SERVER_ACCOUNT)) ])); let mut _socket = diff --git a/src/nex/account.rs b/src/nex/account.rs index 2763aed..8dc94f7 100644 --- a/src/nex/account.rs +++ b/src/nex/account.rs @@ -1,14 +1,17 @@ -struct Account{ +pub struct Account{ pid: u32, + username: Box, kerbros_password: Box, + } impl Account{ - const fn new(pid: u32, passwd: &str) -> Self{ + pub fn new(pid: u32, username: &str, passwd: &str) -> Self{ Self{ kerbros_password: passwd.into(), + username: username.into(), pid } } diff --git a/src/nex/mod.rs b/src/nex/mod.rs index d1f6818..3ce1632 100644 --- a/src/nex/mod.rs +++ b/src/nex/mod.rs @@ -1 +1 @@ -mod account; \ No newline at end of file +pub mod account; \ No newline at end of file diff --git a/src/protocols/auth/method_login_ex.rs b/src/protocols/auth/method_login_ex.rs index 9f087fc..6805925 100644 --- a/src/protocols/auth/method_login_ex.rs +++ b/src/protocols/auth/method_login_ex.rs @@ -5,11 +5,10 @@ use crate::rmc::response::{ErrorCode, RMCResponse, RMCResponseResult}; use crate::rmc::structures::{string, any, RmcSerialize}; use crate::rmc::structures::any::Any; -pub fn login_ex(name: &str) -> RMCResponseResult{ +pub fn login_ex(rmcmessage: &RMCMessage, name: &str) -> RMCResponseResult{ // todo: figure out how the AuthenticationInfo struct works, parse it and validate login info - //return rmcmessage.error_result_with_code(ErrorCode::Core_InvalidArgument); - unreachable!() + return rmcmessage.error_result_with_code(ErrorCode::Core_InvalidArgument); } pub fn login_ex_raw_params(rmcmessage: &RMCMessage) -> RMCResponseResult{ @@ -35,6 +34,5 @@ pub fn login_ex_raw_params(rmcmessage: &RMCMessage) -> RMCResponseResult{ } } - //login_ex(&str) - rmcmessage.error_result_with_code(ErrorCode::Authentication_UnderMaintenance) + login_ex(rmcmessage, &str) } \ No newline at end of file diff --git a/src/protocols/auth/mod.rs b/src/protocols/auth/mod.rs index 3611d0c..e6243b7 100644 --- a/src/protocols/auth/mod.rs +++ b/src/protocols/auth/mod.rs @@ -2,13 +2,14 @@ mod method_login_ex; use log::{error, info}; use crate::define_protocol; +use crate::nex::account::Account; use crate::protocols::auth::method_login_ex::{login_ex, login_ex_raw_params}; use crate::rmc::message::RMCMessage; use crate::rmc::response::{ErrorCode, RMCResponse, RMCResponseResult}; define_protocol!{ - 10 => { + 10<'a>(account: &'a Account) => { 0x02 => login_ex_raw_params } } \ No newline at end of file diff --git a/src/protocols/mod.rs b/src/protocols/mod.rs index 36d749b..1dd1c98 100644 --- a/src/protocols/mod.rs +++ b/src/protocols/mod.rs @@ -2,8 +2,8 @@ pub mod auth; pub mod server; #[macro_export] macro_rules! define_protocol { - ($id:literal => {$($func_id:literal => $func:path),*} ) => { - pub fn protocol(rmcmessage: &RMCMessage) -> Option{ + ($id:literal $( <$lifetime:lifetime> )?($($varname:ident : $ty:ty),*) => {$($func_id:literal => $func:path),*} ) => { + fn protocol $( <$lifetime> )? (rmcmessage: &RMCMessage, $($varname : $ty,)*) -> Option{ if rmcmessage.protocol_id != $id{ return None; } @@ -23,5 +23,16 @@ macro_rules! define_protocol { response_result }) } + + pub fn bound_protocol$(<$lifetime>)?($($varname : $ty,)*) -> Box Option + Send + Sync $( + $lifetime)?>{ + Box::new( + move |v| { + $( + let $varname = $varname.clone(); + )* + protocol(v, $($varname,)*) + } + ) + } }; } \ No newline at end of file