basic client auth

This commit is contained in:
griffi-gh 2023-02-14 04:27:27 +01:00
parent 71f89e8f28
commit e89747794b
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"] }
hashbrown = "0.13"
nohash-hasher = "0.2.0"
anyhow = "1.0"
[features]
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 max_clients: usize,
pub timeout_ms: u64,
pub password: Option<String>,
}
#[derive(Unique, Serialize, Deserialize)]

View file

@ -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()
}

View file

@ -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<ServerToClientMessage, ClientToServerMessage>);
pub struct UdpServer(pub Server<ServerToClientMessage, ClientToServerMessage>);
#[derive(Unique, Default)]
pub struct ServerEvents(pub Vec<ServerEvent<ClientToServerMessage>>);
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<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)]
pub enum ServerToClientMessage {
ServerHello,
ServerHello {
init: InitData
},
ServerFuckOff {
reason: String,
},