feat(account): add grpc to communicate with account server

This commit is contained in:
DJMrTV 2025-02-02 00:46:04 +01:00
commit cb6a6f9028
15 changed files with 797 additions and 30 deletions

83
src/grpc/account.rs Normal file
View file

@ -0,0 +1,83 @@
use std::{env, result};
use std::net::{Ipv4Addr, SocketAddrV4};
use once_cell::sync::Lazy;
use thiserror::Error;
use tonic::codegen::http::uri::InvalidUri;
use tonic::metadata::{Ascii, MetadataValue};
use tonic::{Request, Status, transport};
use tonic::codegen::InterceptedService;
use tonic::service::Interceptor;
use tonic::transport::Channel;
use crate::grpc::{InterceptorFunc, protobufs};
use crate::grpc::protobufs::account::account_client::AccountClient;
use crate::grpc::protobufs::account::GetNexPasswordRequest;
static API_KEY: Lazy<MetadataValue<Ascii>> = Lazy::new(||{
let key = env::var("ACCOUNT_GRPC_API_KEY")
.expect("no public ip specified");
key.parse().expect("unable to parse metadata value")
});
static PORT: Lazy<u16> = Lazy::new(||{
env::var("ACCOUNT_GRPC_PORT")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(7071)
});
static IP: Lazy<Ipv4Addr> = Lazy::new(||{
env::var("ACCOUNT_GRPC_IP")
.ok()
.and_then(|s| s.parse().ok())
.expect("no public ip specified")
});
static CLIENT_URI: Lazy<String> = Lazy::new(||{
format!("http://{}:{}", *IP, *PORT)
});
#[derive(Error, Debug)]
pub enum Error{
#[error(transparent)]
Transport(#[from] transport::Error),
#[error(transparent)]
Status(#[from] tonic::Status)
}
pub type Result<T> = result::Result<T, Error>;
pub struct Client(AccountClient<InterceptedService<Channel, InterceptorFunc>>);
impl Client{
pub async fn new() -> Result<Self>{
let channel = Channel::from_static(&*CLIENT_URI).connect().await?;
let func = Box::new(&|mut req: Request<()>|{
req.metadata_mut().insert("x-api-key", API_KEY.clone());
Ok(req)
}) as InterceptorFunc;
let client = AccountClient::with_interceptor(channel, func);
Ok(Self(client))
}
pub async fn get_nex_password(&mut self , pid: u32) -> Result<Box<str>>{
let req = Request::new(GetNexPasswordRequest{
pid
});
let response = self.0.get_nex_password(req).await?.into_inner();
Ok(response.password.into_boxed_str())
}
}
#[cfg(test)]
mod test{
}

8
src/grpc/mod.rs Normal file
View file

@ -0,0 +1,8 @@
use std::env;
use std::net::Ipv4Addr;
use once_cell::sync::Lazy;
use tonic::{Request, Status};
type InterceptorFunc = Box<(dyn Fn(Request<()>) -> Result<Request<()>, Status> + Send)>;
mod protobufs;
pub mod account;

5
src/grpc/protobufs.rs Normal file
View file

@ -0,0 +1,5 @@
pub mod account {
tonic::include_proto!("account");
}

View file

@ -26,6 +26,7 @@ pub mod rmc;
mod protocols;
mod nex;
mod grpc;
static KERBEROS_SERVER_PASSWORD: Lazy<String> = Lazy::new(||{
env::var("AUTH_SERVER_PORT")
@ -96,11 +97,11 @@ async fn start_servers(){
async move {
let rc4: Rc4<U5> = Rc4::new_from_slice( "CD&ML".as_bytes()).unwrap();
let cypher = Box::new(rc4);
let server_cypher: Box<dyn StreamCipher + Send + Sync> = cypher;
let server_cypher: Box<dyn StreamCipher + Send> = cypher;
let rc4: Rc4<U5> = Rc4::new_from_slice( "CD&ML".as_bytes()).unwrap();
let cypher = Box::new(rc4);
let client_cypher: Box<dyn StreamCipher + Send + Sync> = cypher;
let client_cypher: Box<dyn StreamCipher + Send> = cypher;
(true, (server_cypher, client_cypher))
}

View file

@ -7,11 +7,11 @@ use crate::rmc::response::{ErrorCode, RMCResponseResult};
use crate::rmc::structures::any::Any;
use crate::rmc::structures::RmcSerialize;
pub fn login(rmcmessage: &RMCMessage, name: &str) -> RMCResponseResult{
pub async fn login(rmcmessage: &RMCMessage, name: &str) -> RMCResponseResult{
rmcmessage.error_result_with_code(ErrorCode::Core_NotImplemented)
}
pub fn login_raw_params(rmcmessage: &RMCMessage, account: &Account) -> RMCResponseResult{
pub async fn login_raw_params(rmcmessage: &RMCMessage, data: (&Account)) -> RMCResponseResult{
let mut reader = Cursor::new(&rmcmessage.rest_of_data);
let Ok(str) = String::deserialize(&mut reader) else {
@ -20,5 +20,5 @@ pub fn login_raw_params(rmcmessage: &RMCMessage, account: &Account) -> RMCRespon
};
login(rmcmessage, &str)
login(rmcmessage, &str).await
}

View file

@ -1,20 +1,29 @@
use std::io::Cursor;
use log::{error, info};
use crate::grpc::account;
use crate::nex::account::Account;
use crate::rmc::message::RMCMessage;
use crate::rmc::response::{ErrorCode, RMCResponse, RMCResponseResult};
use crate::rmc::structures::{string, any, RmcSerialize};
use crate::rmc::structures::any::Any;
pub fn login_ex(rmcmessage: &RMCMessage, name: &str) -> RMCResponseResult{
pub async fn login_ex(rmcmessage: &RMCMessage, secure_server_account: &Account , pid: u32) -> RMCResponseResult{
// todo: figure out how the AuthenticationInfo struct works, parse it and validate login info
let Ok(mut client) = account::Client::new().await else {
return rmcmessage.error_result_with_code(ErrorCode::Core_Exception);
};
let Ok(passwd) = client.get_nex_password(pid).await else{
return rmcmessage.error_result_with_code(ErrorCode::Core_Exception);
};
return rmcmessage.error_result_with_code(ErrorCode::Core_InvalidArgument);
}
pub fn login_ex_raw_params(rmcmessage: &RMCMessage, account: &Account) -> RMCResponseResult{
pub async fn login_ex_raw_params(rmcmessage: &RMCMessage, (secure_server_account): (&Account)) -> RMCResponseResult{
let mut reader = Cursor::new(&rmcmessage.rest_of_data);
let Ok(str) = String::deserialize(&mut reader) else {
@ -37,5 +46,9 @@ pub fn login_ex_raw_params(rmcmessage: &RMCMessage, account: &Account) -> RMCRes
}
}
login_ex(rmcmessage, &str)
let Ok(pid) = str.parse() else {
return rmcmessage.error_result_with_code(ErrorCode::Core_InvalidArgument);
};
login_ex(rmcmessage, secure_server_account, pid).await
}

View file

@ -11,7 +11,7 @@ use crate::rmc::response::{ErrorCode, RMCResponse, RMCResponseResult};
define_protocol!{
10<'a>(account: &'a Account) => {
10(secure_server_account: &'static Account) => {
0x01 => login_raw_params,
0x02 => login_ex_raw_params
}

View file

@ -2,15 +2,17 @@ pub mod auth;
pub mod server;
#[macro_export]
macro_rules! define_protocol {
($id:literal $( <$lifetime:lifetime> )?($($varname:ident : $ty:ty),*) => {$($func_id:literal => $func:path),*} ) => {
fn protocol $( <$lifetime> )? (rmcmessage: &RMCMessage, $($varname : $ty),*) -> Option<RMCResponse>{
($id:literal ($($varname:ident : $ty:ty),*) => {$($func_id:literal => $func:path),*} ) => {
async fn protocol (rmcmessage: &RMCMessage, $($varname : $ty),*) -> Option<RMCResponse>{
if rmcmessage.protocol_id != $id{
return None;
}
let response_function = match rmcmessage.method_id{
let self_data: ( $( $ty ),* ) = ($( $varname ),*);
let response_result = match rmcmessage.method_id{
$(
$func_id => $func,
$func_id => $func ( rmcmessage, self_data).await,
)*
_ => {
error!("invalid method id sent to protocol {}: {:?}", $id, rmcmessage.method_id);
@ -23,21 +25,21 @@ macro_rules! define_protocol {
}
};
let response_result = response_function(rmcmessage, $($varname),*);
Some(RMCResponse{
protocol_id: $id,
response_result
})
}
pub fn bound_protocol$(<$lifetime>)?($($varname : $ty,)*) -> Box<dyn Fn(&RMCMessage) -> Option<RMCResponse> + Send + Sync $( + $lifetime)?>{
pub fn bound_protocol($($varname : $ty,)*) -> Box<dyn for<'message_lifetime> Fn(&'message_lifetime RMCMessage) -> ::std::pin::Pin<Box<dyn ::std::future::Future<Output = Option<RMCResponse>> + Send + 'message_lifetime>> + Send + Sync>{
Box::new(
move |v| {
$(
let $varname = $varname.clone();
)*
protocol(v, $($varname,)*)
Box::pin(async move {
$(
let $varname = $varname.clone();
)*
protocol(v, $($varname,)*).await
})
}
)
}

View file

@ -1,4 +1,6 @@
use std::future::Future;
use std::io::Cursor;
use std::pin::Pin;
use std::sync::Arc;
use log::error;
use crate::prudp::packet::PRUDPPacket;
@ -7,7 +9,7 @@ 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>]>;
type ContainedProtocolList = Box<[Box<dyn for<'a> Fn(&'a RMCMessage) -> Pin<Box<dyn Future<Output = Option<RMCResponse>> + Send + 'a>> + Send + Sync>]>;
pub struct RMCProtocolServer(ContainedProtocolList);
@ -25,7 +27,7 @@ impl RMCProtocolServer{
println!("recieved rmc message: {{ protocol: {}, method: {}}}", rmc.protocol_id, rmc.method_id);
for proto in &self.0 {
if let Some(response) = proto(&rmc) {
if let Some(response) = proto(&rmc).await {
send_response(&packet, &socket, connection, response).await;
return;
}

View file

@ -37,8 +37,8 @@ pub struct Socket {
}
type OnConnectHandlerFn = Box<dyn Fn(PRUDPPacket) -> Pin<Box<dyn Future<Output=(bool, (Box<dyn StreamCipher + Send + Sync>, Box<dyn StreamCipher + Send + Sync>))> + Send + Sync>> + Send + Sync>;
type OnDataHandlerFn = Box<dyn for<'a> Fn(PRUDPPacket, Arc<SocketData>, &'a mut MutexGuard<'_, ConnectionData>) -> Pin<Box<dyn Future<Output=()> + 'a + Send + Sync>> + Send + Sync>;
type OnConnectHandlerFn = Box<dyn Fn(PRUDPPacket) -> Pin<Box<dyn Future<Output=(bool, (Box<dyn StreamCipher + Send>, Box<dyn StreamCipher + Send>))> + Send>> + Send + Sync>;
type OnDataHandlerFn = Box<dyn for<'a> Fn(PRUDPPacket, Arc<SocketData>, &'a mut MutexGuard<'_, ConnectionData>) -> Pin<Box<dyn Future<Output=()> + 'a + Send>> + Send + Sync>;
pub struct SocketData {
virtual_port: VirtualPort,
@ -54,8 +54,8 @@ pub struct ActiveConnectionData {
pub reliable_server_counter: u16,
pub reliable_client_queue: VecDeque<PRUDPPacket>,
pub connection_data_channel: Sender<Vec<u8>>,
server_encryption: Box<dyn StreamCipher + Send + Sync>,
client_decryption: Box<dyn StreamCipher + Send + Sync>,
server_encryption: Box<dyn StreamCipher + Send>,
client_decryption: Box<dyn StreamCipher + Send>,
pub server_session_id: u8,
}