mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Prevented invalid username usage
This commit is contained in:
parent
dd82fc24a8
commit
0c918c835f
@ -1,5 +1,7 @@
|
||||
use specs::{Component, FlaggedStorage, NullStorage, VecStorage};
|
||||
|
||||
const MAX_ALIAS_LEN: usize = 32;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Player {
|
||||
pub alias: String,
|
||||
@ -13,6 +15,11 @@ impl Player {
|
||||
view_distance,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_valid(&self) -> bool {
|
||||
self.alias.chars().all(|c| c.is_alphanumeric() || c == '_') && self.alias.len() <= MAX_ALIAS_LEN
|
||||
// TODO: Check view distance here based on server config too
|
||||
}
|
||||
}
|
||||
|
||||
impl Component for Player {
|
||||
|
@ -1,4 +1,4 @@
|
||||
#![feature(drain_filter)]
|
||||
#![feature(drain_filter, bind_by_move_pattern_guards)]
|
||||
|
||||
pub mod client;
|
||||
pub mod cmd;
|
||||
@ -454,7 +454,8 @@ impl Server {
|
||||
ClientState::Dead => client.error_state(RequestStateError::Impossible),
|
||||
ClientState::Pending => {}
|
||||
},
|
||||
ClientMsg::Register { player } => match client.client_state {
|
||||
// Valid player
|
||||
ClientMsg::Register { player } if player.is_valid() => match client.client_state {
|
||||
ClientState::Connected => {
|
||||
Self::initialize_player(state, entity, client, player);
|
||||
if let Some(player) =
|
||||
@ -467,6 +468,8 @@ impl Server {
|
||||
// Use RequestState instead (No need to send `player` again).
|
||||
_ => client.error_state(RequestStateError::Impossible),
|
||||
},
|
||||
// Invalid player
|
||||
ClientMsg::Register { player } => client.error_state(RequestStateError::Impossible),
|
||||
ClientMsg::SetViewDistance(view_distance) => match client.client_state {
|
||||
ClientState::Character { .. } => {
|
||||
state
|
||||
|
@ -100,15 +100,22 @@ impl PlayState for MainMenuState {
|
||||
if let Err(err) = global_state.settings.save_to_file() {
|
||||
warn!("Failed to save settings: {:?}", err);
|
||||
}
|
||||
// Don't try to connect if there is already a connection in progress.
|
||||
client_init = client_init.or(Some(ClientInit::new(
|
||||
(server_address, DEFAULT_PORT, false),
|
||||
comp::Player::new(
|
||||
username.clone(),
|
||||
Some(global_state.settings.graphics.view_distance),
|
||||
),
|
||||
false,
|
||||
)));
|
||||
|
||||
let player = comp::Player::new(
|
||||
username.clone(),
|
||||
Some(global_state.settings.graphics.view_distance),
|
||||
);
|
||||
|
||||
if player.is_valid() {
|
||||
// Don't try to connect if there is already a connection in progress.
|
||||
client_init = client_init.or(Some(ClientInit::new(
|
||||
(server_address, DEFAULT_PORT, false),
|
||||
player,
|
||||
false,
|
||||
)));
|
||||
} else {
|
||||
self.main_menu_ui.login_error("Invalid username".to_string());
|
||||
}
|
||||
}
|
||||
MainMenuEvent::StartSingleplayer => {
|
||||
return PlayStateResult::Push(Box::new(StartSingleplayerState::new()));
|
||||
|
Loading…
Reference in New Issue
Block a user