From 6d94d430215b6e5f7979875b833f13fdc8da2e91 Mon Sep 17 00:00:00 2001 From: telastrus <4415544-telastrus@users.noreply.gitlab.com> Date: Wed, 7 Aug 2019 23:56:02 -0400 Subject: [PATCH] still compiles --- chat-cli/src/main.rs | 5 ++++- client/src/lib.rs | 11 ++++++++--- common/src/msg/client.rs | 1 + common/src/msg/server.rs | 1 + server/src/lib.rs | 19 +++++++++++++++++-- voxygen/src/menu/main/client_init.rs | 10 ++++++++-- voxygen/src/menu/main/mod.rs | 1 + voxygen/src/menu/main/start_singleplayer.rs | 1 + 8 files changed, 41 insertions(+), 8 deletions(-) diff --git a/chat-cli/src/main.rs b/chat-cli/src/main.rs index e6920e8dee..dea1044b75 100644 --- a/chat-cli/src/main.rs +++ b/chat-cli/src/main.rs @@ -30,6 +30,9 @@ fn main() { println!("Enter the server address"); let server_addr = read_input(); + println!("Enter your password"); + let password = read_input(); + // Create a client. let mut client = Client::new( server_addr @@ -45,7 +48,7 @@ fn main() { println!("Players online: {:?}", client.get_players()); - client.register(comp::Player::new(username, None)); + client.register(comp::Player::new(username, None), password); let (tx, rx) = mpsc::channel(); thread::spawn(move || loop { diff --git a/client/src/lib.rs b/client/src/lib.rs index a296cb567b..82e162d1d3 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -132,9 +132,13 @@ impl Client { } /// Request a state transition to `ClientState::Registered`. - pub fn register(&mut self, player: comp::Player) { - self.postbox.send_message(ClientMsg::Register { player }); - self.client_state = ClientState::Pending; + pub fn register(&mut self, player: comp::Player, password: String) -> Result<(), Error> { + self.postbox.send_message(ClientMsg::Register { player, password }); + /*match self.postbox.next_message() { + + } + self.client_state = ClientState::Pending;*/ + Err(Error::InvalidAuth) } /// Request a state transition to `ClientState::Character`. @@ -397,6 +401,7 @@ impl Client { match msg { ServerMsg::Error(e) => match e { ServerError::TooManyPlayers => return Err(Error::ServerWentMad), + ServerError::InvalidAuth => return Err(Error::InvalidAuth), //TODO: ServerError::InvalidAlias => return Err(Error::InvalidAlias), }, ServerMsg::Shutdown => return Err(Error::ServerShutdown), diff --git a/common/src/msg/client.rs b/common/src/msg/client.rs index cc325415d8..f5c7bf9f7b 100644 --- a/common/src/msg/client.rs +++ b/common/src/msg/client.rs @@ -7,6 +7,7 @@ use vek::*; pub enum ClientMsg { Register { player: comp::Player, + password: String, }, Character { name: String, diff --git a/common/src/msg/server.rs b/common/src/msg/server.rs index 8686e6f089..5d5b8a83a3 100644 --- a/common/src/msg/server.rs +++ b/common/src/msg/server.rs @@ -69,6 +69,7 @@ pub enum ServerMsg { #[derive(Debug, Clone, Serialize, Deserialize)] pub enum ServerError { TooManyPlayers, + InvalidAuth, //TODO: InvalidAlias, } diff --git a/server/src/lib.rs b/server/src/lib.rs index f0cbf76fd5..e4d2de309b 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -26,7 +26,7 @@ use log::debug; use rand::Rng; use specs::{join::Join, world::EntityBuilder as EcsEntityBuilder, Builder, Entity as EcsEntity}; use std::{ - collections::HashSet, + collections::{HashSet, HashMap}, i32, net::SocketAddr, sync::{mpsc, Arc}, @@ -68,6 +68,9 @@ pub struct Server { server_settings: ServerSettings, server_info: ServerInfo, + + // TODO: anything but this + accounts: HashMap, } impl Server { @@ -107,6 +110,7 @@ impl Server { description: settings.server_description.clone(), git_hash: common::util::GIT_HASH.to_string(), }, + accounts: HashMap::new(), server_settings: settings, }; @@ -468,6 +472,8 @@ impl Server { fn handle_new_messages(&mut self) -> Result, Error> { let mut frontend_events = Vec::new(); + let accounts = &mut self.accounts; + let state = &mut self.state; let mut new_chat_msgs = Vec::new(); let mut disconnected_clients = Vec::new(); @@ -522,7 +528,16 @@ impl Server { ClientState::Pending => {} }, // Valid player - ClientMsg::Register { player } if player.is_valid() => { + ClientMsg::Register { player, password } if player.is_valid() => { + if let Some(pass) = accounts.get(&player.alias) { + if pass != &password { + client.error_state(RequestStateError::Denied); + break; + } + } else { + accounts.insert(player.alias.clone(), password); + } + println!("{:?}", accounts); match client.client_state { ClientState::Connected => { Self::initialize_player(state, entity, client, player); diff --git a/voxygen/src/menu/main/client_init.rs b/voxygen/src/menu/main/client_init.rs index a4dc1ba7da..3b43d5a3d6 100644 --- a/voxygen/src/menu/main/client_init.rs +++ b/voxygen/src/menu/main/client_init.rs @@ -30,7 +30,7 @@ pub struct ClientInit { rx: Receiver>, } impl ClientInit { - pub fn new(connection_args: (String, u16, bool), player: comp::Player, wait: bool) -> Self { + pub fn new(connection_args: (String, u16, bool), player: comp::Player, password: String, wait: bool) -> Self { let (server_address, default_port, prefer_ipv6) = connection_args; let (tx, rx) = channel(); @@ -57,7 +57,13 @@ impl ClientInit { for socket_addr in first_addrs.into_iter().chain(second_addrs) { match Client::new(socket_addr, player.view_distance) { Ok(mut client) => { - client.register(player); + match client.register(player, password) { + Err(ClientError::InvalidAuth) => { + last_err = Some(Error::InvalidAuth); + break; + } + _ => {} + } let _ = tx.send(Ok(client)); #[cfg(feature = "discord")] diff --git a/voxygen/src/menu/main/mod.rs b/voxygen/src/menu/main/mod.rs index 33f7d22493..8ed9b98357 100644 --- a/voxygen/src/menu/main/mod.rs +++ b/voxygen/src/menu/main/mod.rs @@ -106,6 +106,7 @@ impl PlayState for MainMenuState { client_init = client_init.or(Some(ClientInit::new( (server_address, DEFAULT_PORT, false), player, + password, false, ))); } else { diff --git a/voxygen/src/menu/main/start_singleplayer.rs b/voxygen/src/menu/main/start_singleplayer.rs index f497767088..1f97cde4e2 100644 --- a/voxygen/src/menu/main/start_singleplayer.rs +++ b/voxygen/src/menu/main/start_singleplayer.rs @@ -38,6 +38,7 @@ impl PlayState for StartSingleplayerState { username.clone(), Some(global_state.settings.graphics.view_distance), ), + String::default(), true, );