feat(protocols): extend functionality of protocol macro

This commit is contained in:
DJMrTV 2025-02-01 19:42:45 +01:00
commit 1679590bf3
6 changed files with 38 additions and 12 deletions

View file

@ -10,6 +10,7 @@ use once_cell::sync::Lazy;
use rc4::{KeyInit, Rc4, StreamCipher}; use rc4::{KeyInit, Rc4, StreamCipher};
use rc4::consts::U5; use rc4::consts::U5;
use simplelog::{ColorChoice, CombinedLogger, Config, LevelFilter, TerminalMode, TermLogger, WriteLogger}; use simplelog::{ColorChoice, CombinedLogger, Config, LevelFilter, TerminalMode, TermLogger, WriteLogger};
use crate::nex::account::Account;
use crate::protocols::auth; use crate::protocols::auth;
use crate::protocols::server::RMCProtocolServer; use crate::protocols::server::RMCProtocolServer;
use crate::prudp::socket::{Socket, SocketData}; use crate::prudp::socket::{Socket, SocketData};
@ -24,6 +25,18 @@ mod prudp;
pub mod rmc; pub mod rmc;
mod protocols; mod protocols;
mod nex;
static KERBEROS_SERVER_PASSWORD: Lazy<String> = Lazy::new(||{
env::var("AUTH_SERVER_PORT")
.ok()
.unwrap_or("password".to_owned())
});
static AUTH_SERVER_ACCOUNT: Lazy<Account> = Lazy::new(|| Account::new(1, "Quazal Authentication", &KERBEROS_SERVER_PASSWORD));
static SECURE_SERVER_ACCOUNT: Lazy<Account> = Lazy::new(|| Account::new(2, "Quazal Rendez-Vous", &KERBEROS_SERVER_PASSWORD));
static AUTH_SERVER_PORT: Lazy<u16> = Lazy::new(||{ static AUTH_SERVER_PORT: Lazy<u16> = Lazy::new(||{
env::var("AUTH_SERVER_PORT") env::var("AUTH_SERVER_PORT")
.ok() .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 // dont assign it to the name _ as that will make it drop right here and now
let rmcserver = RMCProtocolServer::new(Box::new([ let rmcserver = RMCProtocolServer::new(Box::new([
Box::new(auth::protocol) Box::new(auth::bound_protocol(&SECURE_SERVER_ACCOUNT))
])); ]));
let mut _socket = let mut _socket =

View file

@ -1,14 +1,17 @@
struct Account{ pub struct Account{
pid: u32, pid: u32,
username: Box<str>,
kerbros_password: Box<str>, kerbros_password: Box<str>,
} }
impl Account{ impl Account{
const fn new(pid: u32, passwd: &str) -> Self{ pub fn new(pid: u32, username: &str, passwd: &str) -> Self{
Self{ Self{
kerbros_password: passwd.into(), kerbros_password: passwd.into(),
username: username.into(),
pid pid
} }
} }

View file

@ -1 +1 @@
mod account; pub mod account;

View file

@ -5,11 +5,10 @@ use crate::rmc::response::{ErrorCode, RMCResponse, RMCResponseResult};
use crate::rmc::structures::{string, any, RmcSerialize}; use crate::rmc::structures::{string, any, RmcSerialize};
use crate::rmc::structures::any::Any; 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 // todo: figure out how the AuthenticationInfo struct works, parse it and validate login info
//return rmcmessage.error_result_with_code(ErrorCode::Core_InvalidArgument); return rmcmessage.error_result_with_code(ErrorCode::Core_InvalidArgument);
unreachable!()
} }
pub fn login_ex_raw_params(rmcmessage: &RMCMessage) -> RMCResponseResult{ 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) login_ex(rmcmessage, &str)
rmcmessage.error_result_with_code(ErrorCode::Authentication_UnderMaintenance)
} }

View file

@ -2,13 +2,14 @@ mod method_login_ex;
use log::{error, info}; use log::{error, info};
use crate::define_protocol; use crate::define_protocol;
use crate::nex::account::Account;
use crate::protocols::auth::method_login_ex::{login_ex, login_ex_raw_params}; use crate::protocols::auth::method_login_ex::{login_ex, login_ex_raw_params};
use crate::rmc::message::RMCMessage; use crate::rmc::message::RMCMessage;
use crate::rmc::response::{ErrorCode, RMCResponse, RMCResponseResult}; use crate::rmc::response::{ErrorCode, RMCResponse, RMCResponseResult};
define_protocol!{ define_protocol!{
10 => { 10<'a>(account: &'a Account) => {
0x02 => login_ex_raw_params 0x02 => login_ex_raw_params
} }
} }

View file

@ -2,8 +2,8 @@ pub mod auth;
pub mod server; pub mod server;
#[macro_export] #[macro_export]
macro_rules! define_protocol { macro_rules! define_protocol {
($id:literal => {$($func_id:literal => $func:path),*} ) => { ($id:literal $( <$lifetime:lifetime> )?($($varname:ident : $ty:ty),*) => {$($func_id:literal => $func:path),*} ) => {
pub fn protocol(rmcmessage: &RMCMessage) -> Option<RMCResponse>{ fn protocol $( <$lifetime> )? (rmcmessage: &RMCMessage, $($varname : $ty,)*) -> Option<RMCResponse>{
if rmcmessage.protocol_id != $id{ if rmcmessage.protocol_id != $id{
return None; return None;
} }
@ -23,5 +23,16 @@ macro_rules! define_protocol {
response_result response_result
}) })
} }
pub fn bound_protocol$(<$lifetime>)?($($varname : $ty,)*) -> Box<dyn Fn(&RMCMessage) -> Option<RMCResponse> + Send + Sync $( + $lifetime)?>{
Box::new(
move |v| {
$(
let $varname = $varname.clone();
)*
protocol(v, $($varname,)*)
}
)
}
}; };
} }