kubi/kubi-server/src/auth.rs

129 lines
4.1 KiB
Rust
Raw Normal View History

2023-03-12 20:24:37 -05:00
use shipyard::{UniqueView, NonSendSync, EntitiesViewMut, ViewMut, UniqueViewMut, AllStoragesView};
2023-03-08 14:10:48 -06:00
use uflow::{server::Event as ServerEvent, SendMode};
2023-03-08 20:30:37 -06:00
use kubi_shared::{
networking::{
messages::{
ClientToServerMessage,
ServerToClientMessage,
InitData,
C_CLIENT_HELLO
},
client::{Client, ClientId}, channels::CHANNEL_AUTH
},
2023-03-12 20:24:37 -05:00
player::{Player, PLAYER_HEALTH},
transform::Transform, entity::{Entity, Health}
2023-03-08 14:10:48 -06:00
};
use crate::{
server::{ServerEvents, UdpServer, IsMessageOfType},
2023-03-08 20:30:37 -06:00
config::ConfigTable,
client::{ClientAddress, ClientIdMap, ClientAddressMap}
2023-03-08 14:10:48 -06:00
};
2023-02-13 21:27:27 -06:00
pub fn authenticate_players(
2023-03-12 20:24:37 -05:00
storages: AllStoragesView,
2023-02-13 21:27:27 -06:00
) {
2023-03-12 20:24:37 -05:00
let mut client_entity_map = storages.borrow::<UniqueViewMut<ClientIdMap>>().unwrap();
let mut client_addr_map = storages.borrow::<UniqueViewMut<ClientAddressMap>>().unwrap();
let server = storages.borrow::<NonSendSync<UniqueView<UdpServer>>>().unwrap();
let events = storages.borrow::<UniqueView<ServerEvents>>().unwrap();
let config = storages.borrow::<UniqueView<ConfigTable>>().unwrap();
2023-02-13 21:27:27 -06:00
for event in &events.0 {
2023-03-08 14:10:48 -06:00
let ServerEvent::Receive(client_addr, data) = event else{
continue
};
2023-03-08 20:30:37 -06:00
if !event.is_message_of_type::<C_CLIENT_HELLO>() {
continue
}
2023-03-08 14:10:48 -06:00
let Some(client) = server.0.client(client_addr) else {
log::error!("Client doesn't exist");
continue
};
let Ok(parsed_message) = postcard::from_bytes(data) else {
log::error!("Malformed message");
2023-03-08 14:10:48 -06:00
continue
};
let ClientToServerMessage::ClientHello { username, password } = parsed_message else {
unreachable!()
};
log::info!("ClientHello; username={} password={:?}", username, password);
2023-03-06 18:51:19 -06:00
2023-03-08 14:10:48 -06:00
// Handle password auth
if let Some(server_password) = &config.server.password {
if let Some(user_password) = &password {
if server_password != user_password {
client.borrow_mut().send(
2023-03-08 20:30:37 -06:00
postcard::to_allocvec(&ServerToClientMessage::ServerFuckOff {
reason: "Incorrect password".into()
}).unwrap().into_boxed_slice(),
CHANNEL_AUTH,
SendMode::Reliable
2023-03-08 14:10:48 -06:00
);
2023-02-13 21:27:27 -06:00
continue
}
2023-03-08 14:10:48 -06:00
} else {
client.borrow_mut().send(
2023-03-08 20:30:37 -06:00
postcard::to_allocvec(&ServerToClientMessage::ServerFuckOff {
reason: "This server is password protected".into()
}).unwrap().into_boxed_slice(),
CHANNEL_AUTH,
SendMode::Reliable
2023-03-08 14:10:48 -06:00
);
continue
2023-02-13 21:27:27 -06:00
}
2023-03-08 14:10:48 -06:00
}
2023-02-13 21:31:17 -06:00
2023-03-08 20:30:37 -06:00
//Find the player ID
let max_clients = config.server.max_clients as ClientId;
let Some(client_id) = (0..max_clients).into_iter().find(|id| {
!client_entity_map.0.contains_key(id)
}) else {
client.borrow_mut().send(
postcard::to_allocvec(&ServerToClientMessage::ServerFuckOff {
reason: "Can't find a free spot for you!".into()
}).unwrap().into_boxed_slice(),
CHANNEL_AUTH,
SendMode::Reliable
);
continue
};
2023-03-08 14:10:48 -06:00
//Spawn the user
2023-03-12 20:24:37 -05:00
let entity_id = {
storages.borrow::<EntitiesViewMut>().unwrap().add_entity((
&mut storages.borrow::<ViewMut<Entity>>().unwrap(),
&mut storages.borrow::<ViewMut<Player>>().unwrap(),
&mut storages.borrow::<ViewMut<Health>>().unwrap(),
&mut storages.borrow::<ViewMut<Client>>().unwrap(),
&mut storages.borrow::<ViewMut<ClientAddress>>().unwrap(),
&mut storages.borrow::<ViewMut<Transform>>().unwrap(),
), (
Entity,
Player,
Health::new(PLAYER_HEALTH),
Client(client_id),
ClientAddress(*client_addr),
Transform::default(),
))
};
2023-03-08 20:30:37 -06:00
//Add the user to the ClientIdMap and ClientAddressMap
client_entity_map.0.insert(client_id, entity_id);
client_addr_map.0.insert(*client_addr, entity_id);
2023-02-13 21:52:11 -06:00
2023-03-08 14:10:48 -06:00
//Approve the user
client.borrow_mut().send(
2023-03-08 20:30:37 -06:00
postcard::to_allocvec(&ServerToClientMessage::ServerHello {
init: InitData {
users: vec![] //TODO create init data
}
}).unwrap().into_boxed_slice(),
CHANNEL_AUTH,
SendMode::Reliable
2023-03-08 14:10:48 -06:00
);
2023-03-08 14:13:50 -06:00
2023-03-08 20:30:37 -06:00
log::info!("{username}({client_id}) joined the game!")
2023-02-13 21:27:27 -06:00
}
}