From 153c401cb16f9b0f62a8b057df0a6229864995b2 Mon Sep 17 00:00:00 2001 From: DJMrTV Date: Sat, 1 Feb 2025 17:18:52 +0100 Subject: [PATCH 1/6] feat(login): add account struct --- src/nex/account.rs | 15 +++++++++++++++ src/nex/mod.rs | 1 + 2 files changed, 16 insertions(+) create mode 100644 src/nex/account.rs create mode 100644 src/nex/mod.rs diff --git a/src/nex/account.rs b/src/nex/account.rs new file mode 100644 index 0000000..53728ed --- /dev/null +++ b/src/nex/account.rs @@ -0,0 +1,15 @@ + + +struct Account{ + pid: u32, + kerbros_password: Box, +} + +impl Account{ + fn new(pid: u32, passwd: &str) -> Self{ + Self{ + kerbros_password: passwd.into(), + pid + } + } +} \ No newline at end of file diff --git a/src/nex/mod.rs b/src/nex/mod.rs new file mode 100644 index 0000000..d1f6818 --- /dev/null +++ b/src/nex/mod.rs @@ -0,0 +1 @@ +mod account; \ No newline at end of file From ffe4084ba3efe7cde170dc93c130828394fd15ed Mon Sep 17 00:00:00 2001 From: DJMrTV Date: Sat, 1 Feb 2025 17:38:09 +0100 Subject: [PATCH 2/6] feat(account): make new const --- src/nex/account.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nex/account.rs b/src/nex/account.rs index 53728ed..2763aed 100644 --- a/src/nex/account.rs +++ b/src/nex/account.rs @@ -6,7 +6,7 @@ struct Account{ } impl Account{ - fn new(pid: u32, passwd: &str) -> Self{ + const fn new(pid: u32, passwd: &str) -> Self{ Self{ kerbros_password: passwd.into(), pid From 1679590bf3c426a26d09335a3a8ee22fada4704a Mon Sep 17 00:00:00 2001 From: DJMrTV Date: Sat, 1 Feb 2025 19:42:45 +0100 Subject: [PATCH 3/6] 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 From 44bcad7eda5a020ded984ff02b0c32f24a383f97 Mon Sep 17 00:00:00 2001 From: DJMrTV Date: Sat, 1 Feb 2025 20:59:21 +0100 Subject: [PATCH 4/6] fix(protocols): fix macro --- src/protocols/mod.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/protocols/mod.rs b/src/protocols/mod.rs index 1dd1c98..1b9e3dd 100644 --- a/src/protocols/mod.rs +++ b/src/protocols/mod.rs @@ -3,21 +3,28 @@ 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{ + fn protocol $( <$lifetime> )? (rmcmessage: &RMCMessage, $($varname : $ty),*) -> Option{ if rmcmessage.protocol_id != $id{ return None; } - let response_result = match rmcmessage.method_id{ + let response_function = match rmcmessage.method_id{ $( - $func_id => $func(rmcmessage), - ),* + $func_id => $func, + )* _ => { error!("invalid method id sent to protocol {}: {:?}", $id, rmcmessage.method_id); - rmcmessage.error_result_with_code(ErrorCode::Core_NotImplemented) + return Some( + RMCResponse{ + protocol_id: $id, + response_result: rmcmessage.error_result_with_code(ErrorCode::Core_NotImplemented) + } + ); } }; + let response_result = response_function(rmcmessage, $($varname),*); + Some(RMCResponse{ protocol_id: $id, response_result From 6984a20bddbdf7fdd22b2508de837f812f6634bb Mon Sep 17 00:00:00 2001 From: DJMrTV Date: Sat, 1 Feb 2025 20:59:53 +0100 Subject: [PATCH 5/6] feat(auth protocol): login(1) method --- src/protocols/auth/method_login.rs | 24 ++++++++++++++++++++++++ src/protocols/auth/method_login_ex.rs | 5 ++++- src/protocols/auth/mod.rs | 5 ++++- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/protocols/auth/method_login.rs diff --git a/src/protocols/auth/method_login.rs b/src/protocols/auth/method_login.rs new file mode 100644 index 0000000..641c1cd --- /dev/null +++ b/src/protocols/auth/method_login.rs @@ -0,0 +1,24 @@ +use std::io::Cursor; +use log::error; +use crate::nex::account::Account; +use crate::protocols::auth::method_login_ex::login_ex; +use crate::rmc::message::RMCMessage; +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{ + rmcmessage.error_result_with_code(ErrorCode::Core_NotImplemented) +} + +pub fn login_raw_params(rmcmessage: &RMCMessage, account: &Account) -> RMCResponseResult{ + let mut reader = Cursor::new(&rmcmessage.rest_of_data); + + let Ok(str) = String::deserialize(&mut reader) else { + error!("error reading packet"); + return rmcmessage.error_result_with_code(ErrorCode::Core_InvalidArgument); + }; + + + login(rmcmessage, &str) +} \ 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 6805925..2e57f55 100644 --- a/src/protocols/auth/method_login_ex.rs +++ b/src/protocols/auth/method_login_ex.rs @@ -1,5 +1,6 @@ use std::io::Cursor; use log::{error, info}; +use crate::nex::account::Account; use crate::rmc::message::RMCMessage; use crate::rmc::response::{ErrorCode, RMCResponse, RMCResponseResult}; use crate::rmc::structures::{string, any, RmcSerialize}; @@ -8,10 +9,12 @@ use crate::rmc::structures::any::Any; 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); } -pub fn login_ex_raw_params(rmcmessage: &RMCMessage) -> RMCResponseResult{ +pub fn login_ex_raw_params(rmcmessage: &RMCMessage, account: &Account) -> RMCResponseResult{ let mut reader = Cursor::new(&rmcmessage.rest_of_data); let Ok(str) = String::deserialize(&mut reader) else { diff --git a/src/protocols/auth/mod.rs b/src/protocols/auth/mod.rs index e6243b7..fa58519 100644 --- a/src/protocols/auth/mod.rs +++ b/src/protocols/auth/mod.rs @@ -1,8 +1,10 @@ mod method_login_ex; +mod method_login; use log::{error, info}; use crate::define_protocol; use crate::nex::account::Account; +use crate::protocols::auth::method_login::login_raw_params; 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}; @@ -10,6 +12,7 @@ use crate::rmc::response::{ErrorCode, RMCResponse, RMCResponseResult}; define_protocol!{ 10<'a>(account: &'a Account) => { + 0x01 => login_raw_params, 0x02 => login_ex_raw_params } -} \ No newline at end of file +} From cb6a6f90285c885e0d17e81f558a441acd8fa9c6 Mon Sep 17 00:00:00 2001 From: DJMrTV Date: Sun, 2 Feb 2025 00:46:04 +0100 Subject: [PATCH 6/6] feat(account): add grpc to communicate with account server --- .gitmodules | 3 + Cargo.lock | 642 +++++++++++++++++++++++++- Cargo.toml | 5 + build.rs | 10 + grpc-protobufs | 1 + src/grpc/account.rs | 83 ++++ src/grpc/mod.rs | 8 + src/grpc/protobufs.rs | 5 + src/main.rs | 5 +- src/protocols/auth/method_login.rs | 6 +- src/protocols/auth/method_login_ex.rs | 19 +- src/protocols/auth/mod.rs | 2 +- src/protocols/mod.rs | 24 +- src/protocols/server.rs | 6 +- src/prudp/socket.rs | 8 +- 15 files changed, 797 insertions(+), 30 deletions(-) create mode 100644 .gitmodules create mode 100644 build.rs create mode 160000 grpc-protobufs create mode 100644 src/grpc/account.rs create mode 100644 src/grpc/mod.rs create mode 100644 src/grpc/protobufs.rs diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..64cd88d --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "grpc-protobufs"] + path = grpc-protobufs + url = https://github.com/PretendoNetwork/grpc-protobufs.git diff --git a/Cargo.lock b/Cargo.lock index 0d180f7..7875aab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,6 +47,45 @@ version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04" +[[package]] +name = "async-stream" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b5a71a6f37880a80d1d7f19efd781e4b5de42c88f0722cc13bcb6cc2cfe8476" +dependencies = [ + "async-stream-impl", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-stream-impl" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7c24de15d275a1ecfd47a380fb4d5ec9bfe0933f309ed5e705b775596a3574d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.96", +] + +[[package]] +name = "async-trait" +version = "0.1.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "644dd749086bf3771a2fbc5f256fdb982d53f011c7d5d560304eafeecebce79d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.96", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + [[package]] name = "autocfg" version = "1.4.0" @@ -78,6 +117,53 @@ dependencies = [ "paste", ] +[[package]] +name = "axum" +version = "0.7.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edca88bc138befd0323b20752846e6587272d3b03b0343c8ea28a6f819e6e71f" +dependencies = [ + "async-trait", + "axum-core", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "sync_wrapper", + "tower 0.5.2", + "tower-layer", + "tower-service", +] + +[[package]] +name = "axum-core" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09f2bd6146b97ae3359fa0cc6d6b376d9539582c7b4220f041a33ec24c226199" +dependencies = [ + "async-trait", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "rustversion", + "sync_wrapper", + "tower-layer", + "tower-service", +] + [[package]] name = "backtrace" version = "0.3.74" @@ -93,6 +179,12 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + [[package]] name = "bindgen" version = "0.69.5" @@ -293,6 +385,12 @@ version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "errno" version = "0.3.10" @@ -303,18 +401,69 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + +[[package]] +name = "fixedbitset" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "fs_extra" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" +[[package]] +name = "futures-channel" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" +dependencies = [ + "futures-core", +] + [[package]] name = "futures-core" version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" +[[package]] +name = "futures-sink" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" + +[[package]] +name = "futures-task" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" + +[[package]] +name = "futures-util" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "pin-utils", +] + [[package]] name = "generic-array" version = "0.14.7" @@ -363,6 +512,43 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2" +[[package]] +name = "h2" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccae279728d634d083c00f6099cb58f01cc99c145b84b8be2f6c74618d79922e" +dependencies = [ + "atomic-waker", + "bytes", + "fnv", + "futures-core", + "futures-sink", + "http", + "indexmap 2.7.1", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "hashbrown" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hmac" version = "0.12.1" @@ -381,6 +567,105 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "http" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f16ca2af56261c99fba8bac40a10251ce8188205a4c448fbb745a2e4daa76fea" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793429d76616a256bcb62c2a2ec2bed781c8307e797e2598c50010f2bee2544f" +dependencies = [ + "bytes", + "futures-util", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2d708df4e7140240a16cd6ab0ab65c972d7433ab77819ea693fde9c43811e2a" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc2b571658e38e0c01b1fdca3bbbe93c00d3d71693ff2770043f8c29bc7d6f80" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-timeout" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b90d566bffbce6a75bd8b09a05aa8c2cb1fabb6cb348f8840c9e4c90a0d83b0" +dependencies = [ + "hyper", + "hyper-util", + "pin-project-lite", + "tokio", + "tower-service", +] + +[[package]] +name = "hyper-util" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df2dcfbe0677734ab2f3ffa7fa7bfd4706bfdc1ef393f2ee30184aed67e631b4" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "hyper", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", +] + [[package]] name = "iana-time-zone" version = "0.1.61" @@ -404,6 +689,26 @@ dependencies = [ "cc", ] +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown 0.12.3", +] + +[[package]] +name = "indexmap" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652" +dependencies = [ + "equivalent", + "hashbrown 0.15.2", +] + [[package]] name = "inout" version = "0.1.3" @@ -487,6 +792,12 @@ version = "0.4.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" +[[package]] +name = "matchit" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" + [[package]] name = "md-5" version = "0.10.6" @@ -503,6 +814,12 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -529,6 +846,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "multimap" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "defc4c55412d89136f966bbb339008b474350e5e6e78d2714439c386b3137a03" + [[package]] name = "nom" version = "7.1.3" @@ -584,12 +907,54 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + +[[package]] +name = "petgraph" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db" +dependencies = [ + "fixedbitset", + "indexmap 2.7.1", +] + +[[package]] +name = "pin-project" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e2ec53ad785f4d35dac0adea7f7dc6f1bb277ad84a680c7afefeae05d1f5916" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d56a66c0c55993aa927429d0f8a0abfd74f084e4d9c192cffed01e418d83eefb" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.96", +] + [[package]] name = "pin-project-lite" version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "powerfmt" version = "0.2.0" @@ -624,6 +989,58 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "prost" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c0fef6c4230e4ccf618a35c59d7ede15dea37de8427500f50aff708806e42ec" +dependencies = [ + "bytes", + "prost-derive", +] + +[[package]] +name = "prost-build" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0f3e5beed80eb580c68e2c600937ac2c4eedabdfd5ef1e5b7ea4f3fba84497b" +dependencies = [ + "heck", + "itertools", + "log", + "multimap", + "once_cell", + "petgraph", + "prettyplease", + "prost", + "prost-types", + "regex", + "syn 2.0.96", + "tempfile", +] + +[[package]] +name = "prost-derive" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "157c5a9d7ea5c2ed2d9fb8f495b64759f7816c7eaea54ba3978f0d63000162e3" +dependencies = [ + "anyhow", + "itertools", + "proc-macro2", + "quote", + "syn 2.0.96", +] + +[[package]] +name = "prost-types" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc2f1e56baa61e93533aebc21af4d2134b70f66275e0fcdf3cbe43d77ff7e8fc" +dependencies = [ + "prost", +] + [[package]] name = "quote" version = "1.0.38" @@ -633,17 +1050,38 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + [[package]] name = "rand" version = "0.9.0-beta.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fccbfebb3972a41a31c605a59207d9fba5489b9a87d9d87024cb6df73a32ec7" dependencies = [ - "rand_chacha", - "rand_core", + "rand_chacha 0.9.0-beta.1", + "rand_core 0.9.0-beta.1", "zerocopy 0.8.14", ] +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + [[package]] name = "rand_chacha" version = "0.9.0-beta.1" @@ -651,7 +1089,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f16da77124f4ee9fabd55ce6540866e9101431863b4876de58b68797f331adf2" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.9.0-beta.1", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.15", ] [[package]] @@ -818,6 +1265,21 @@ dependencies = [ "time", ] +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + [[package]] name = "socket2" version = "0.5.8" @@ -846,13 +1308,16 @@ dependencies = [ "log", "md-5", "once_cell", - "rand", + "prost", + "rand 0.9.0-beta.3", "rc4", "rustls", "simplelog", "thiserror", "tokio", "tokio-stream", + "tonic", + "tonic-build", "v_byte_macros", ] @@ -884,6 +1349,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" + +[[package]] +name = "tempfile" +version = "3.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8a559c81686f576e8cd0290cd2a24a2a9ad80c98b3478856500fcbd7acd704" +dependencies = [ + "cfg-if", + "fastrand", + "getrandom 0.2.15", + "once_cell", + "rustix", + "windows-sys 0.59.0", +] + [[package]] name = "termcolor" version = "1.4.1" @@ -984,6 +1469,146 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-util" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7fcaa8d55a2bdd6b83ace262b016eca0d79ee02818c5c1bcdf0305114081078" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tonic" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877c5b330756d856ffcc4553ab34a5684481ade925ecc54bcd1bf02b1d0d4d52" +dependencies = [ + "async-stream", + "async-trait", + "axum", + "base64", + "bytes", + "h2", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-timeout", + "hyper-util", + "percent-encoding", + "pin-project", + "prost", + "socket2", + "tokio", + "tokio-stream", + "tower 0.4.13", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tonic-build" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9557ce109ea773b399c9b9e5dca39294110b74f1f342cb347a80d1fce8c26a11" +dependencies = [ + "prettyplease", + "proc-macro2", + "prost-build", + "prost-types", + "quote", + "syn 2.0.96", +] + +[[package]] +name = "tower" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" +dependencies = [ + "futures-core", + "futures-util", + "indexmap 1.9.3", + "pin-project", + "pin-project-lite", + "rand 0.8.5", + "slab", + "tokio", + "tokio-util", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d039ad9159c98b70ecfd540b2573b97f7f52c3e8d9f8ad57a24b916a536975f9" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.96", +] + +[[package]] +name = "tracing-core" +version = "0.1.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" +dependencies = [ + "once_cell", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + [[package]] name = "typenum" version = "1.17.0" @@ -1018,6 +1643,15 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index cd7bd3c..e6d7e2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,8 @@ hmac = "0.12.1" md-5 = "^0.10.6" tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread", "net", "sync"] } tokio-stream = { version = "0.1.17", features = ["io-util"] } +tonic = "0.12.3" +prost = "0.13.4" + +[build-dependencies] +tonic-build = "0.12.3" \ No newline at end of file diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..d24b8ec --- /dev/null +++ b/build.rs @@ -0,0 +1,10 @@ + +fn main(){ + tonic_build::configure() + .build_server(false) + .compile_protos( + &["grpc-protobufs/account/account_service.proto"], + &["grpc-protobufs/account"] + ) + .unwrap(); +} \ No newline at end of file diff --git a/grpc-protobufs b/grpc-protobufs new file mode 160000 index 0000000..405fe9b --- /dev/null +++ b/grpc-protobufs @@ -0,0 +1 @@ +Subproject commit 405fe9b47b416e76b21d7087b2ed11606deccfcf diff --git a/src/grpc/account.rs b/src/grpc/account.rs new file mode 100644 index 0000000..5972cc9 --- /dev/null +++ b/src/grpc/account.rs @@ -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> = 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 = Lazy::new(||{ + env::var("ACCOUNT_GRPC_PORT") + .ok() + .and_then(|s| s.parse().ok()) + .unwrap_or(7071) +}); + +static IP: Lazy = Lazy::new(||{ + env::var("ACCOUNT_GRPC_IP") + .ok() + .and_then(|s| s.parse().ok()) + .expect("no public ip specified") +}); + +static CLIENT_URI: Lazy = 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 = result::Result; + + + +pub struct Client(AccountClient>); + +impl Client{ + pub async fn new() -> Result{ + 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>{ + 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{ + + + +} \ No newline at end of file diff --git a/src/grpc/mod.rs b/src/grpc/mod.rs new file mode 100644 index 0000000..30e893e --- /dev/null +++ b/src/grpc/mod.rs @@ -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, Status> + Send)>; +mod protobufs; +pub mod account; \ No newline at end of file diff --git a/src/grpc/protobufs.rs b/src/grpc/protobufs.rs new file mode 100644 index 0000000..bae7331 --- /dev/null +++ b/src/grpc/protobufs.rs @@ -0,0 +1,5 @@ + +pub mod account { + tonic::include_proto!("account"); +} + diff --git a/src/main.rs b/src/main.rs index 9e189b6..8acc9c2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,7 @@ pub mod rmc; mod protocols; mod nex; +mod grpc; static KERBEROS_SERVER_PASSWORD: Lazy = Lazy::new(||{ env::var("AUTH_SERVER_PORT") @@ -96,11 +97,11 @@ async fn start_servers(){ async move { let rc4: Rc4 = Rc4::new_from_slice( "CD&ML".as_bytes()).unwrap(); let cypher = Box::new(rc4); - let server_cypher: Box = cypher; + let server_cypher: Box = cypher; let rc4: Rc4 = Rc4::new_from_slice( "CD&ML".as_bytes()).unwrap(); let cypher = Box::new(rc4); - let client_cypher: Box = cypher; + let client_cypher: Box = cypher; (true, (server_cypher, client_cypher)) } diff --git a/src/protocols/auth/method_login.rs b/src/protocols/auth/method_login.rs index 641c1cd..37bb635 100644 --- a/src/protocols/auth/method_login.rs +++ b/src/protocols/auth/method_login.rs @@ -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 } \ 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 2e57f55..59268f6 100644 --- a/src/protocols/auth/method_login_ex.rs +++ b/src/protocols/auth/method_login_ex.rs @@ -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 } \ No newline at end of file diff --git a/src/protocols/auth/mod.rs b/src/protocols/auth/mod.rs index fa58519..ea03db7 100644 --- a/src/protocols/auth/mod.rs +++ b/src/protocols/auth/mod.rs @@ -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 } diff --git a/src/protocols/mod.rs b/src/protocols/mod.rs index 1b9e3dd..437eb7f 100644 --- a/src/protocols/mod.rs +++ b/src/protocols/mod.rs @@ -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{ + ($id:literal ($($varname:ident : $ty:ty),*) => {$($func_id:literal => $func:path),*} ) => { + async fn protocol (rmcmessage: &RMCMessage, $($varname : $ty),*) -> Option{ 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 Option + Send + Sync $( + $lifetime)?>{ + pub fn bound_protocol($($varname : $ty,)*) -> Box Fn(&'message_lifetime RMCMessage) -> ::std::pin::Pin> + 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 + }) } ) } diff --git a/src/protocols/server.rs b/src/protocols/server.rs index 15fadaa..13d69b0 100644 --- a/src/protocols/server.rs +++ b/src/protocols/server.rs @@ -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 Option + Send + Sync>]>; +type ContainedProtocolList = Box<[Box Fn(&'a RMCMessage) -> Pin> + 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; } diff --git a/src/prudp/socket.rs b/src/prudp/socket.rs index 2eeb7e7..3b7923d 100644 --- a/src/prudp/socket.rs +++ b/src/prudp/socket.rs @@ -37,8 +37,8 @@ pub struct Socket { } -type OnConnectHandlerFn = Box Pin, Box))> + Send + Sync>> + Send + Sync>; -type OnDataHandlerFn = Box Fn(PRUDPPacket, Arc, &'a mut MutexGuard<'_, ConnectionData>) -> Pin + 'a + Send + Sync>> + Send + Sync>; +type OnConnectHandlerFn = Box Pin, Box))> + Send>> + Send + Sync>; +type OnDataHandlerFn = Box Fn(PRUDPPacket, Arc, &'a mut MutexGuard<'_, ConnectionData>) -> Pin + '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, pub connection_data_channel: Sender>, - server_encryption: Box, - client_decryption: Box, + server_encryption: Box, + client_decryption: Box, pub server_session_id: u8, }