cleaned up code, condensed code and fixed a couple of things

This commit is contained in:
DJMrTV 2025-01-26 23:21:35 +01:00
commit 38bcad37bd
10 changed files with 178 additions and 95 deletions

View file

@ -2,7 +2,8 @@ use std::io::Cursor;
use log::{error, info};
use crate::rmc::message::RMCMessage;
use crate::rmc::response::{ErrorCode, RMCResponse, RMCResponseResult};
use crate::rmc::structures::{string, any};
use crate::rmc::structures::{string, any, RmcSerialize};
use crate::rmc::structures::any::Any;
pub fn login_ex(name: &str) -> RMCResponseResult{
// todo: figure out how the AuthenticationInfo struct works, parse it and validate login info
@ -14,12 +15,12 @@ pub fn login_ex(name: &str) -> RMCResponseResult{
pub fn login_ex_raw_params(rmcmessage: &RMCMessage) -> RMCResponseResult{
let mut reader = Cursor::new(&rmcmessage.rest_of_data);
let Ok(str) = string::read(&mut reader) else {
let Ok(str) = String::deserialize(&mut reader) else {
error!("error reading packet");
return rmcmessage.error_result_with_code(ErrorCode::Core_InvalidArgument);
};
let Ok(any) = any::read(&mut reader) else {
let Ok(any) = Any::deserialize(&mut reader) else {
error!("error reading packet");
return rmcmessage.error_result_with_code(ErrorCode::Core_InvalidArgument);
};
@ -35,5 +36,5 @@ pub fn login_ex_raw_params(rmcmessage: &RMCMessage) -> RMCResponseResult{
}
//login_ex(&str)
rmcmessage.error_result_with_code(ErrorCode::Core_AccessDenied)
rmcmessage.error_result_with_code(ErrorCode::Authentication_UnderMaintenance)
}

View file

@ -1,25 +1,14 @@
mod method_login_ex;
use log::{error, info};
use crate::define_protocol;
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};
pub fn try_process_via_protocol(rmcmessage: &RMCMessage) -> Option<RMCResponse>{
if rmcmessage.protocol_id != 10{
return None;
define_protocol!{
10 => {
0x02 => login_ex_raw_params
}
let response_result = match rmcmessage.method_id{
0x02 => login_ex_raw_params(rmcmessage),
_ => {
error!("invalid method id sent to ticket-granting protocol: {:?}", rmcmessage.method_id);
rmcmessage.error_result_with_code(ErrorCode::Core_Exception)
}
};
Some(RMCResponse{
protocol_id: 10,
response_result
})
}

View file

@ -1 +1,27 @@
pub mod auth;
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<RMCResponse>{
if rmcmessage.protocol_id != $id{
return None;
}
let response_result = match rmcmessage.method_id{
$(
$func_id => $func(rmcmessage),
),*
_ => {
error!("invalid method id sent to protocol {}: {:?}", $id, rmcmessage.method_id);
rmcmessage.error_result_with_code(ErrorCode::Core_NotImplemented)
}
};
Some(RMCResponse{
protocol_id: $id,
response_result
})
}
};
}

42
src/protocols/server.rs Normal file
View file

@ -0,0 +1,42 @@
use std::io::Cursor;
use std::sync::Arc;
use log::error;
use crate::prudp::packet::PRUDPPacket;
use crate::prudp::socket::{ConnectionData, SocketData};
use crate::rmc::message::RMCMessage;
use crate::rmc::response::{RMCResponse, RMCResponseResult, send_response};
use crate::rmc::response::ErrorCode::Core_NotImplemented;
type ContainedProtocolList = Box<[Box<dyn Fn(&RMCMessage) -> Option<RMCResponse> + Send + Sync>]>;
pub struct RMCProtocolServer(ContainedProtocolList);
impl RMCProtocolServer{
pub fn new(protocols: ContainedProtocolList) -> Arc<Self>{
Arc::new(Self(protocols))
}
pub async fn process_message(&self, packet: PRUDPPacket, socket: &SocketData, connection: &mut ConnectionData){
let Ok(rmc) = RMCMessage::new(&mut Cursor::new(&packet.payload)) else {
error!("error reading rmc message");
return;
};
println!("recieved rmc message: {{ protocol: {}, method: {}}}", rmc.protocol_id, rmc.method_id);
for proto in &self.0 {
if let Some(response) = proto(&rmc) {
send_response(&packet, &socket, connection, response).await;
return;
}
}
send_response(&packet, &socket, connection, RMCResponse{
protocol_id: rmc.protocol_id as u8,
response_result: RMCResponseResult::Error {
call_id: rmc.call_id,
error_code: Core_NotImplemented
}
}).await;
}
}