From a9172ed15714a6655807924e5f504821ea129de6 Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Thu, 11 Jun 2020 13:49:33 +0200 Subject: [PATCH] Moving password related commands to own thread --- gradle.properties | 2 +- .../org/samo_lego/simpleauth/SimpleAuth.java | 5 ++ .../simpleauth/commands/AccountCommand.java | 84 ++++++++++--------- .../simpleauth/commands/AuthCommand.java | 63 +++++++------- .../simpleauth/commands/LoginCommand.java | 55 ++++++------ .../simpleauth/commands/RegisterCommand.java | 17 ++-- .../simpleauth/storage/PlayerCache.java | 4 + .../simpleauth/utils/AuthHelper.java | 2 +- 8 files changed, 129 insertions(+), 103 deletions(-) diff --git a/gradle.properties b/gradle.properties index 838f2bc..ab344f0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -10,7 +10,7 @@ loader_version=0.8.7+build.201 fabric_version=0.11.8+build.357-1.16 # Mod Properties -mod_version = 1.4.3 +mod_version = 1.4.4 maven_group = org.samo_lego archives_base_name = simpleauth diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index 86079c1..63873d0 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -171,6 +171,10 @@ public class SimpleAuth implements DedicatedServerModInitializer { world.updateListeners(pos.up(), world.getBlockState(pos.up()), world.getBlockState(pos.up()), 3); } + // Setting last air to player + if(player.isSubmergedInWater()) + player.setAir(deauthenticatedUsers.get(convertUuid(player)).lastAir); + deauthenticatedUsers.remove(convertUuid(player)); // Player no longer needs to be invisible and invulnerable @@ -194,6 +198,7 @@ public class SimpleAuth implements DedicatedServerModInitializer { // Player is now not authenticated player.sendMessage(notAuthenticated(), false); + // Setting the player to be invisible to mobs and also invulnerable player.setInvulnerable(SimpleAuth.config.experimental.playerInvulnerable); player.setInvisible(SimpleAuth.config.experimental.playerInvisible); diff --git a/src/main/java/org/samo_lego/simpleauth/commands/AccountCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/AccountCommand.java index 6e9e7bd..96fef00 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/AccountCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/AccountCommand.java @@ -70,20 +70,23 @@ public class AccountCommand { ); return 0; } - - if (AuthHelper.checkPass(convertUuid(player), pass.toCharArray()) == 1) { - SimpleAuth.db.deleteUserData(convertUuid(player)); - player.sendMessage( - new LiteralText(config.lang.accountDeleted), - false - ); - SimpleAuth.deauthenticatePlayer(player); - return 1; - } - player.sendMessage( - new LiteralText(config.lang.wrongPassword), + + // New thread to avoid lag spikes + new Thread(() -> { + if (AuthHelper.checkPass(convertUuid(player), pass.toCharArray()) == 1) { + SimpleAuth.db.deleteUserData(convertUuid(player)); + player.sendMessage( + new LiteralText(config.lang.accountDeleted), false ); + SimpleAuth.deauthenticatePlayer(player); + return; + } + player.sendMessage( + new LiteralText(config.lang.wrongPassword), + false + ); + }).start(); return 0; } @@ -99,35 +102,38 @@ public class AccountCommand { ); return 0; } - else if (AuthHelper.checkPass(convertUuid(player), oldPass.toCharArray()) == 1) { - if(newPass.length() < config.main.minPasswordChars) { - player.sendMessage(new LiteralText( - String.format(config.lang.minPasswordChars, config.main.minPasswordChars) - ), false); - return 0; - } - else if(newPass.length() > config.main.maxPasswordChars && config.main.maxPasswordChars != -1) { - player.sendMessage(new LiteralText( - String.format(config.lang.maxPasswordChars, config.main.maxPasswordChars) - ), false); - return 0; - } - // JSON object holding password (may hold some other info in the future) - JsonObject playerdata = new JsonObject(); - String hash = AuthHelper.hashPass(newPass.toCharArray()); - playerdata.addProperty("password", hash); + // New thread to avoid lag spikes + new Thread(() -> { + if (AuthHelper.checkPass(convertUuid(player), oldPass.toCharArray()) == 1) { + if (newPass.length() < config.main.minPasswordChars) { + player.sendMessage(new LiteralText( + String.format(config.lang.minPasswordChars, config.main.minPasswordChars) + ), false); + return; + } + else if (newPass.length() > config.main.maxPasswordChars && config.main.maxPasswordChars != -1) { + player.sendMessage(new LiteralText( + String.format(config.lang.maxPasswordChars, config.main.maxPasswordChars) + ), false); + return; + } + // JSON object holding password (may hold some other info in the future) + JsonObject playerdata = new JsonObject(); + String hash = AuthHelper.hashPass(newPass.toCharArray()); + playerdata.addProperty("password", hash); - SimpleAuth.db.updateUserData(convertUuid(player), playerdata.toString()); - player.sendMessage( - new LiteralText(config.lang.passwordUpdated), + SimpleAuth.db.updateUserData(convertUuid(player), playerdata.toString()); + player.sendMessage( + new LiteralText(config.lang.passwordUpdated), + false + ); + } + else + player.sendMessage( + new LiteralText(config.lang.wrongPassword), false - ); - return 1; - } - player.sendMessage( - new LiteralText(config.lang.wrongPassword), - false - ); + ); + }).start(); return 0; } } diff --git a/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java index 265025d..bd88120 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java @@ -114,10 +114,13 @@ public class AuthCommand { private static int setGlobalPassword(ServerCommandSource source, String pass) { // Getting the player who send the command Entity sender = source.getEntity(); - // Writing the global pass to config - config.main.globalPassword = AuthHelper.hashPass(pass.toCharArray()); - config.main.enableGlobalPassword = true; - config.save(new File("./mods/SimpleAuth/config.json")); + // New thread to avoid lag spikes + new Thread(() -> { + // Writing the global pass to config + config.main.globalPassword = AuthHelper.hashPass(pass.toCharArray()); + config.main.enableGlobalPassword = true; + config.save(new File("./mods/SimpleAuth/config.json")); + }).start(); if(sender != null) ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.globalPasswordSet), false); @@ -147,8 +150,10 @@ public class AuthCommand { // Deleting (unregistering) user's account private static int removeAccount(ServerCommandSource source, String uuid) { Entity sender = source.getEntity(); - db.deleteUserData(uuid); - SimpleAuth.deauthenticatedUsers.put(uuid, new PlayerCache(uuid, null)); + new Thread(() -> { + db.deleteUserData(uuid); + SimpleAuth.deauthenticatedUsers.put(uuid, new PlayerCache(uuid, null)); + }).start(); if(sender != null) ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataDeleted), false); @@ -162,18 +167,19 @@ public class AuthCommand { // Getting the player who send the command Entity sender = source.getEntity(); - // JSON object holding password (may hold some other info in the future) - JsonObject playerdata = new JsonObject(); - String hash = AuthHelper.hashPass(password.toCharArray()); - playerdata.addProperty("password", hash); + new Thread(() -> { + // JSON object holding password (may hold some other info in the future) + JsonObject playerdata = new JsonObject(); + String hash = AuthHelper.hashPass(password.toCharArray()); + playerdata.addProperty("password", hash); - if(db.registerUser(uuid, playerdata.toString())) { - if(sender != null) - ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false); - else - LOGGER.info(config.lang.userdataUpdated); - return 1; - } + if (db.registerUser(uuid, playerdata.toString())) { + if (sender != null) + ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false); + else + LOGGER.info(config.lang.userdataUpdated); + } + }).start(); return 0; } @@ -182,17 +188,18 @@ public class AuthCommand { // Getting the player who send the command Entity sender = source.getEntity(); - // JSON object holding password (may hold some other info in the future) - JsonObject playerdata = new JsonObject(); - String hash = AuthHelper.hashPass(password.toCharArray()); - playerdata.addProperty("password", hash); + new Thread(() -> { + // JSON object holding password (may hold some other info in the future) + JsonObject playerdata = new JsonObject(); + String hash = AuthHelper.hashPass(password.toCharArray()); + playerdata.addProperty("password", hash); - db.updateUserData(uuid, playerdata.toString()); - if(sender != null) - ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false); - else - LOGGER.info(config.lang.userdataUpdated); - return 1; + db.updateUserData(uuid, playerdata.toString()); + if (sender != null) + ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false); + else + LOGGER.info(config.lang.userdataUpdated); + }).start(); + return 0; } - // todo PlayerEntity.getOfflinePlayerUuid("") } diff --git a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java index 8670fce..d784a75 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java @@ -33,37 +33,38 @@ public class LoginCommand { private static int login(ServerCommandSource source, String pass) throws CommandSyntaxException { // Getting the player who send the command ServerPlayerEntity player = source.getPlayer(); - - if(SimpleAuth.isAuthenticated(player)) { + String uuid = convertUuid(player); + if (SimpleAuth.isAuthenticated(player)) { player.sendMessage(new LiteralText(config.lang.alreadyAuthenticated), false); return 0; } + // Putting rest of the command in new thread to avoid lag spikes + new Thread(() -> { + int maxLoginTries = config.main.maxLoginTries; + int passwordResult = AuthHelper.checkPass(uuid, pass.toCharArray()); - String uuid = convertUuid(player); - int passwordResult = AuthHelper.checkPass(uuid, pass.toCharArray()); - int maxLoginTries = config.main.maxLoginTries; - - if(SimpleAuth.deauthenticatedUsers.get(uuid).loginTries >= maxLoginTries && maxLoginTries != -1) { - player.networkHandler.disconnect(new LiteralText(config.lang.loginTriesExceeded)); - return 0; - } - else if(passwordResult == 1) { - SimpleAuth.authenticatePlayer(player, new LiteralText(config.lang.successfullyAuthenticated)); - return 1; - } - else if(passwordResult == -1) { - player.sendMessage(new LiteralText(config.lang.notRegistered), false); - return 0; - } - // Kicking the player out - else if(maxLoginTries == 1) { - player.networkHandler.disconnect(new LiteralText(config.lang.wrongPassword)); - return 0; - } - // Sending wrong pass message - player.sendMessage(new LiteralText(config.lang.wrongPassword), false); - // ++ the login tries - SimpleAuth.deauthenticatedUsers.get(uuid).loginTries += 1; + if(SimpleAuth.deauthenticatedUsers.get(uuid).loginTries >= maxLoginTries && maxLoginTries != -1) { + player.networkHandler.disconnect(new LiteralText(config.lang.loginTriesExceeded)); + return; + } + else if(passwordResult == 1) { + SimpleAuth.authenticatePlayer(player, new LiteralText(config.lang.successfullyAuthenticated)); + return; + } + else if(passwordResult == -1) { + player.sendMessage(new LiteralText(config.lang.notRegistered), false); + return; + } + // Kicking the player out + else if(maxLoginTries == 1) { + player.networkHandler.disconnect(new LiteralText(config.lang.wrongPassword)); + return; + } + // Sending wrong pass message + player.sendMessage(new LiteralText(config.lang.wrongPassword), false); + // ++ the login tries + SimpleAuth.deauthenticatedUsers.get(uuid).loginTries += 1; + }).start(); return 0; } } diff --git a/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java index ba6c85d..3eec9c1 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java @@ -44,18 +44,23 @@ public class RegisterCommand { player.sendMessage(new LiteralText(config.lang.alreadyAuthenticated), false); return 0; } - else if(pass1.equals(pass2)) { + else if(!pass1.equals(pass2)) { + player.sendMessage(new LiteralText(config.lang.matchPassword), false); + return 0; + } + // New thread to avoid lag spikes + new Thread(() -> { if(pass1.length() < config.main.minPasswordChars) { player.sendMessage(new LiteralText( String.format(config.lang.minPasswordChars, config.main.minPasswordChars) ), false); - return 0; + return; } else if(pass1.length() > config.main.maxPasswordChars && config.main.maxPasswordChars != -1) { player.sendMessage(new LiteralText( String.format(config.lang.maxPasswordChars, config.main.maxPasswordChars) ), false); - return 0; + return; } String hash = AuthHelper.hashPass(pass1.toCharArray()); // JSON object holding password (may hold some other info in the future) @@ -64,12 +69,10 @@ public class RegisterCommand { if (SimpleAuth.db.registerUser(convertUuid(player), playerdata.toString())) { SimpleAuth.authenticatePlayer(player, new LiteralText(config.lang.registerSuccess)); - return 1; + return; } player.sendMessage(new LiteralText(config.lang.alreadyRegistered), false); - return 0; - } - player.sendMessage(new LiteralText(config.lang.matchPassword), false); + }).start(); return 0; } } diff --git a/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java b/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java index 47a2944..a3d8360 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java @@ -17,6 +17,8 @@ public class PlayerCache { public String lastIp; public long validUntil; + public int lastAir = 300; + public String lastDim; public double lastX; public double lastY; @@ -33,6 +35,8 @@ public class PlayerCache { if(player != null) { this.lastIp = player.getIp(); + this.lastAir = player.getAir(); + // Setting position cache this.lastDim = String.valueOf(player.getEntityWorld().getDimension()); this.lastX = player.getX(); diff --git a/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java b/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java index 4553369..f923776 100644 --- a/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java +++ b/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java @@ -50,7 +50,7 @@ public class AuthHelper { // Verify password return argon2.verify(hashed, pass) ? 1 : 0; } catch (Error e) { - LOGGER.error("[SimpleAuth] error: " + e); + LOGGER.error("[SimpleAuth] Argon2 error: " + e); return 0; } finally { // Wipe confidential data