basic client auth

This commit is contained in:
griffi-gh 2023-02-14 04:27:27 +01:00
parent 1b1f6c6426
commit c284d5f7ed
8 changed files with 91 additions and 4 deletions

View file

@ -15,6 +15,7 @@ toml = "0.7"
glam = { version = "0.22", features = ["debug-glam-assert", "fast-math"] } glam = { version = "0.22", features = ["debug-glam-assert", "fast-math"] }
hashbrown = "0.13" hashbrown = "0.13"
nohash-hasher = "0.2.0" nohash-hasher = "0.2.0"
anyhow = "1.0"
[features] [features]
default = [] default = []

41
kubi-server/src/auth.rs Normal file
View file

@ -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<UdpServer>,
events: UniqueView<ServerEvents>,
config: UniqueView<ConfigTable>
) {
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();
}
}
}

13
kubi-server/src/chunk.rs Normal file
View file

@ -0,0 +1,13 @@
pub struct Chunk {
//TODO
}
pub struct ChunkManager {
//TODO
}
pub fn server_chunk_response(
) {
}

View file

@ -7,6 +7,7 @@ pub struct ConfigTableServer {
pub address: SocketAddr, pub address: SocketAddr,
pub max_clients: usize, pub max_clients: usize,
pub timeout_ms: u64, pub timeout_ms: u64,
pub password: Option<String>,
} }
#[derive(Unique, Serialize, Deserialize)] #[derive(Unique, Serialize, Deserialize)]

View file

@ -1,12 +1,17 @@
use shipyard::{World, Workload, IntoWorkload}; use shipyard::{World, Workload, IntoWorkload};
use std::{thread, time::Duration}; use std::{thread, time::Duration};
pub(crate) mod util;
pub(crate) mod config; pub(crate) mod config;
pub(crate) mod server; pub(crate) mod server;
pub(crate) mod client; pub(crate) mod client;
pub(crate) mod chunk;
pub(crate) mod auth;
use config::read_config; 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 { fn initialize() -> Workload {
( (
@ -18,6 +23,8 @@ fn initialize() -> Workload {
fn update() -> Workload { fn update() -> Workload {
( (
update_server, update_server,
update_server_events,
authenticate_players,
).into_workload() ).into_workload()
} }

View file

@ -1,12 +1,15 @@
use shipyard::{AllStoragesView, Unique, UniqueView, UniqueViewMut}; 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 kubi_shared::networking::messages::{ClientToServerMessage, ServerToClientMessage};
use std::time::Duration; use std::time::Duration;
use crate::config::ConfigTable; use crate::config::ConfigTable;
#[derive(Unique)] #[derive(Unique)]
#[repr(transparent)] #[repr(transparent)]
pub struct UdpServer(Server<ServerToClientMessage, ClientToServerMessage>); pub struct UdpServer(pub Server<ServerToClientMessage, ClientToServerMessage>);
#[derive(Unique, Default)]
pub struct ServerEvents(pub Vec<ServerEvent<ClientToServerMessage>>);
pub fn bind_server( pub fn bind_server(
storages: AllStoragesView, storages: AllStoragesView,
@ -22,6 +25,7 @@ pub fn bind_server(
} }
).unwrap(); ).unwrap();
storages.add_unique(UdpServer(server)); storages.add_unique(UdpServer(server));
storages.add_unique(ServerEvents::default());
} }
pub fn update_server( pub fn update_server(
@ -31,3 +35,13 @@ pub fn update_server(
log::error!("Server error: {error:?}") log::error!("Server error: {error:?}")
} }
} }
pub fn update_server_events(
mut server: UniqueViewMut<UdpServer>,
mut events: UniqueViewMut<ServerEvents>,
) {
//drop current events
events.0.clear();
//fetch new ones
events.0.extend(server.0.process_events().rev());
}

3
kubi-server/src/util.rs Normal file
View file

@ -0,0 +1,3 @@
pub fn log_error(error: anyhow::Error) {
log::error!("{}", error);
}

View file

@ -23,9 +23,16 @@ pub enum ClientToServerMessage {
}, },
} }
#[derive(Encode, Decode, Clone)]
pub struct InitData {
}
#[derive(Encode, Decode, Clone)] #[derive(Encode, Decode, Clone)]
pub enum ServerToClientMessage { pub enum ServerToClientMessage {
ServerHello, ServerHello {
init: InitData
},
ServerFuckOff { ServerFuckOff {
reason: String, reason: String,
}, },