From c284d5f7eda7a3c66808083e4d3dcc495e1866ea Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Tue, 14 Feb 2023 04:27:27 +0100 Subject: [PATCH] basic client auth --- kubi-server/Cargo.toml | 1 + kubi-server/src/auth.rs | 41 ++++++++++++++++++++++++++ kubi-server/src/chunk.rs | 13 ++++++++ kubi-server/src/config.rs | 1 + kubi-server/src/main.rs | 9 +++++- kubi-server/src/server.rs | 18 +++++++++-- kubi-server/src/util.rs | 3 ++ kubi-shared/src/networking/messages.rs | 9 +++++- 8 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 kubi-server/src/auth.rs create mode 100644 kubi-server/src/chunk.rs create mode 100644 kubi-server/src/util.rs diff --git a/kubi-server/Cargo.toml b/kubi-server/Cargo.toml index f9e056e..13c0063 100644 --- a/kubi-server/Cargo.toml +++ b/kubi-server/Cargo.toml @@ -15,6 +15,7 @@ toml = "0.7" glam = { version = "0.22", features = ["debug-glam-assert", "fast-math"] } hashbrown = "0.13" nohash-hasher = "0.2.0" +anyhow = "1.0" [features] default = [] diff --git a/kubi-server/src/auth.rs b/kubi-server/src/auth.rs new file mode 100644 index 0000000..ad5098a --- /dev/null +++ b/kubi-server/src/auth.rs @@ -0,0 +1,41 @@ +use shipyard::{UniqueView, UniqueViewMut}; +use kubi_shared::networking::messages::{ClientToServerMessage, ServerToClientMessage, InitData}; +use kubi_udp::server::ServerEvent; +use crate::{server::{ServerEvents, UdpServer}, config::ConfigTable, util::log_error}; + +pub fn authenticate_players( + mut server: UniqueViewMut, + events: UniqueView, + config: UniqueView +) { + for event in &events.0 { + if let ServerEvent::MessageReceived { + from, + message: ClientToServerMessage::ClientHello { + username, + password + } + } = event { + // Handle password auth + if let Some(server_password) = &config.server.password { + if let Some(user_password) = &password { + if server_password != user_password { + server.0.send_message(*from, ServerToClientMessage::ServerFuckOff { + reason: "Passwords don't match".into() + }).map_err(log_error).ok(); + continue + } + } else { + server.0.send_message(*from, ServerToClientMessage::ServerFuckOff { + reason: "This server is password-protected".into() + }).map_err(log_error).ok(); + continue + } + } + //Approve the user + server.0.send_message(*from, ServerToClientMessage::ServerHello { + init: InitData {} + }).map_err(log_error).ok(); + } + } +} diff --git a/kubi-server/src/chunk.rs b/kubi-server/src/chunk.rs new file mode 100644 index 0000000..26210cd --- /dev/null +++ b/kubi-server/src/chunk.rs @@ -0,0 +1,13 @@ +pub struct Chunk { + //TODO +} + +pub struct ChunkManager { + //TODO +} + +pub fn server_chunk_response( + +) { + +} diff --git a/kubi-server/src/config.rs b/kubi-server/src/config.rs index 4593221..2ae114f 100644 --- a/kubi-server/src/config.rs +++ b/kubi-server/src/config.rs @@ -7,6 +7,7 @@ pub struct ConfigTableServer { pub address: SocketAddr, pub max_clients: usize, pub timeout_ms: u64, + pub password: Option, } #[derive(Unique, Serialize, Deserialize)] diff --git a/kubi-server/src/main.rs b/kubi-server/src/main.rs index bb6f60e..926c6b5 100644 --- a/kubi-server/src/main.rs +++ b/kubi-server/src/main.rs @@ -1,12 +1,17 @@ + use shipyard::{World, Workload, IntoWorkload}; use std::{thread, time::Duration}; +pub(crate) mod util; pub(crate) mod config; pub(crate) mod server; pub(crate) mod client; +pub(crate) mod chunk; +pub(crate) mod auth; use config::read_config; -use server::{bind_server, update_server}; +use server::{bind_server, update_server, update_server_events}; +use auth::authenticate_players; fn initialize() -> Workload { ( @@ -18,6 +23,8 @@ fn initialize() -> Workload { fn update() -> Workload { ( update_server, + update_server_events, + authenticate_players, ).into_workload() } diff --git a/kubi-server/src/server.rs b/kubi-server/src/server.rs index e69d710..c9bde00 100644 --- a/kubi-server/src/server.rs +++ b/kubi-server/src/server.rs @@ -1,12 +1,15 @@ use shipyard::{AllStoragesView, Unique, UniqueView, UniqueViewMut}; -use kubi_udp::server::{Server, ServerConfig}; +use kubi_udp::server::{Server, ServerConfig, ServerEvent}; use kubi_shared::networking::messages::{ClientToServerMessage, ServerToClientMessage}; use std::time::Duration; use crate::config::ConfigTable; #[derive(Unique)] #[repr(transparent)] -pub struct UdpServer(Server); +pub struct UdpServer(pub Server); + +#[derive(Unique, Default)] +pub struct ServerEvents(pub Vec>); pub fn bind_server( storages: AllStoragesView, @@ -22,6 +25,7 @@ pub fn bind_server( } ).unwrap(); storages.add_unique(UdpServer(server)); + storages.add_unique(ServerEvents::default()); } pub fn update_server( @@ -31,3 +35,13 @@ pub fn update_server( log::error!("Server error: {error:?}") } } + +pub fn update_server_events( + mut server: UniqueViewMut, + mut events: UniqueViewMut, +) { + //drop current events + events.0.clear(); + //fetch new ones + events.0.extend(server.0.process_events().rev()); +} diff --git a/kubi-server/src/util.rs b/kubi-server/src/util.rs new file mode 100644 index 0000000..2438d9e --- /dev/null +++ b/kubi-server/src/util.rs @@ -0,0 +1,3 @@ +pub fn log_error(error: anyhow::Error) { + log::error!("{}", error); +} diff --git a/kubi-shared/src/networking/messages.rs b/kubi-shared/src/networking/messages.rs index 5578781..95b7b16 100644 --- a/kubi-shared/src/networking/messages.rs +++ b/kubi-shared/src/networking/messages.rs @@ -23,9 +23,16 @@ pub enum ClientToServerMessage { }, } +#[derive(Encode, Decode, Clone)] +pub struct InitData { + +} + #[derive(Encode, Decode, Clone)] pub enum ServerToClientMessage { - ServerHello, + ServerHello { + init: InitData + }, ServerFuckOff { reason: String, },