- add: utils library
- add: packet-service to handle game client packets - fix: health check for database-service - fix: health check for auth-service
This commit is contained in:
90
packet-service/src/handlers/auth.rs
Normal file
90
packet-service/src/handlers/auth.rs
Normal file
@@ -0,0 +1,90 @@
|
||||
use std::collections::HashMap;
|
||||
use tonic::{Code, Status};
|
||||
use std::error::Error;
|
||||
use std::sync::Arc;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio::sync::Mutex;
|
||||
use tracing::{debug, error, info, warn};
|
||||
use crate::auth_client::AuthClient;
|
||||
use crate::packet::{send_packet, Packet, PacketPayload};
|
||||
use crate::packet_type::PacketType;
|
||||
use crate::packets::cli_accept_req::CliAcceptReq;
|
||||
use crate::packets::cli_join_server_req::CliJoinServerReq;
|
||||
use crate::packets::cli_login_req::CliLoginReq;
|
||||
use crate::packets::{srv_accept_reply, srv_login_reply};
|
||||
use crate::packets::srv_accept_reply::SrvAcceptReply;
|
||||
use crate::packets::srv_login_reply::SrvLoginReply;
|
||||
|
||||
pub(crate) async fn handle_accept_req(stream: &mut TcpStream, packet: Packet) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let data = CliAcceptReq::decode(packet.payload.as_slice());
|
||||
debug!("{:?}", data);
|
||||
|
||||
// We need to do reply to this packet
|
||||
let data = SrvAcceptReply { result: srv_accept_reply::Result::Accepted, rand_value: 0 };
|
||||
let response_packet = Packet::new(PacketType::PakssAcceptReply, &data)?;
|
||||
|
||||
debug!("{:?}", response_packet);
|
||||
send_packet(stream, &response_packet).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn handle_join_server_req(stream: &mut TcpStream, packet: Packet) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let data = CliJoinServerReq::decode(packet.payload.as_slice());
|
||||
debug!("{:?}", data);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn handle_login_req(stream: &mut TcpStream, packet: Packet, auth_client: Arc<Mutex<AuthClient>>) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
debug!("decoding packet payload of size {}", packet.payload.as_slice().len());
|
||||
let data = CliLoginReq::decode(packet.payload.as_slice())?;
|
||||
debug!("{:?}", data);
|
||||
|
||||
let mut auth_client = auth_client.lock().await;
|
||||
match auth_client.login(&data.username.0, &data.password.password).await {
|
||||
Ok(response) => {
|
||||
debug!("successfully logged in");
|
||||
let data = SrvLoginReply { result: srv_login_reply::Result::Ok, right: 0, type_: 0, servers_info: Vec::new() };
|
||||
let response_packet = Packet::new(PacketType::PaklcLoginReply, &data)?;
|
||||
send_packet(stream, &response_packet).await?;
|
||||
}
|
||||
Err(status) => {
|
||||
if let Some(tonic_status) = status.downcast_ref::<Status>() {
|
||||
match tonic_status.code() {
|
||||
Code::Unauthenticated => {
|
||||
info!("Login failed: Invalid credentials");
|
||||
|
||||
let data = SrvLoginReply { result: srv_login_reply::Result::UnknownAccount, right: 0, type_: 0, servers_info: Vec::new() };
|
||||
let response_packet = Packet::new(PacketType::PaklcLoginReply, &data)?;
|
||||
send_packet(stream, &response_packet).await?;
|
||||
}
|
||||
Code::Unavailable => {
|
||||
warn!("Login failed: Service is unavailable");
|
||||
let data = SrvLoginReply { result: srv_login_reply::Result::Failed, right: 0, type_: 0, servers_info: Vec::new() };
|
||||
let response_packet = Packet::new(PacketType::PaklcLoginReply, &data)?;
|
||||
send_packet(stream, &response_packet).await?;
|
||||
}
|
||||
_ => {
|
||||
error!("Unexpected error: {}", tonic_status.message());
|
||||
let data = SrvLoginReply { result: srv_login_reply::Result::Failed, right: 0, type_: 0, servers_info: Vec::new() };
|
||||
let response_packet = Packet::new(PacketType::PaklcLoginReply, &data)?;
|
||||
send_packet(stream, &response_packet).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn handle_server_select_req(stream: &mut TcpStream, packet: Packet) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let data = CliJoinServerReq::decode(packet.payload.as_slice());
|
||||
debug!("{:?}", data);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn handle_channel_list_req(stream: &mut TcpStream, packet: Packet) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let data = CliJoinServerReq::decode(packet.payload.as_slice());
|
||||
debug!("{:?}", data);
|
||||
Ok(())
|
||||
}
|
||||
2
packet-service/src/handlers/mod.rs
Normal file
2
packet-service/src/handlers/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod auth;
|
||||
pub mod null_string;
|
||||
43
packet-service/src/handlers/null_string.rs
Normal file
43
packet-service/src/handlers/null_string.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use bincode::{Decode, Encode, de::Decoder, enc::Encoder, error::{DecodeError, EncodeError}};
|
||||
use std::str;
|
||||
use bincode::de::read::Reader;
|
||||
use bincode::enc::write::Writer;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NullTerminatedString(pub String);
|
||||
|
||||
impl NullTerminatedString {
|
||||
pub fn new(string: &str) -> Self {
|
||||
NullTerminatedString(string.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl Encode for NullTerminatedString {
|
||||
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
|
||||
let bytes = self.0.as_bytes();
|
||||
encoder.writer().write(bytes)?; // Write the string bytes
|
||||
encoder.writer().write(&[0])?; // Add the null terminator
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode for NullTerminatedString {
|
||||
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
|
||||
let mut buffer = Vec::new();
|
||||
let mut byte = [0u8; 1];
|
||||
|
||||
// Read until the null terminator
|
||||
while decoder.reader().read(&mut byte).is_ok() {
|
||||
if byte[0] == 0 {
|
||||
break; // Null terminator found
|
||||
}
|
||||
buffer.push(byte[0]);
|
||||
}
|
||||
|
||||
let string = str::from_utf8(&buffer)
|
||||
.map_err(|e| DecodeError::OtherString(format!("Invalid UTF-8: {}", e)))?
|
||||
.to_string();
|
||||
|
||||
Ok(NullTerminatedString(string))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user