Skip to content

Commit

Permalink
optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
mdxd44 committed Dec 17, 2023
1 parent b203992 commit 1dbca38
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 37 deletions.
8 changes: 6 additions & 2 deletions src/main/java/net/elytrium/limboauth/LimboAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,12 @@ public void cacheAuthUser(Player player) {
}

public void removePlayerFromCache(String username) {
this.cachedAuthChecks.remove(username.toLowerCase(Locale.ROOT));
this.premiumCache.remove(username.toLowerCase(Locale.ROOT));
this.removePlayerFromCacheLowercased(username.toLowerCase(Locale.ROOT));
}

public void removePlayerFromCacheLowercased(String username) {
this.cachedAuthChecks.remove(username);
this.premiumCache.remove(username);
}

public boolean needAuth(Player player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player;
import java.sql.SQLException;
import java.util.Locale;
import net.elytrium.commons.kyori.serialization.Serializer;
import net.elytrium.limboauth.LimboAuth;
import net.elytrium.limboauth.Settings;
Expand Down Expand Up @@ -62,8 +63,8 @@ public ChangePasswordCommand(LimboAuth plugin, Dao<RegisteredPlayer, String> pla
@Override
public void execute(CommandSource source, String[] args) {
if (source instanceof Player) {
String username = ((Player) source).getUsername();
RegisteredPlayer player = AuthSessionHandler.fetchInfo(this.playerDao, username);
String usernameLowercase = ((Player) source).getUsername().toLowerCase(Locale.ROOT);
RegisteredPlayer player = AuthSessionHandler.fetchInfoLowercased(this.playerDao, usernameLowercase);

if (player == null) {
source.sendMessage(this.notRegistered);
Expand Down Expand Up @@ -93,11 +94,11 @@ public void execute(CommandSource source, String[] args) {
final String newHash = RegisteredPlayer.genHash(newPassword);

UpdateBuilder<RegisteredPlayer, String> updateBuilder = this.playerDao.updateBuilder();
updateBuilder.where().eq(RegisteredPlayer.NICKNAME_FIELD, username);
updateBuilder.where().eq(RegisteredPlayer.LOWERCASE_NICKNAME_FIELD, usernameLowercase);
updateBuilder.updateColumnValue(RegisteredPlayer.HASH_FIELD, newHash);
updateBuilder.update();

this.plugin.removePlayerFromCache(username);
this.plugin.removePlayerFromCacheLowercased(usernameLowercase);

this.plugin.getServer().getEventManager().fireAndForget(
new ChangePasswordEvent(player, needOldPass ? args[0] : null, oldHash, newPassword, newHash));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ public List<String> suggest(SimpleCommand.Invocation invocation) {
public void execute(CommandSource source, String[] args) {
if (args.length == 2) {
String nickname = args[0];
String nicknameLowercased = args[0].toLowerCase(Locale.ROOT);
String newPassword = args[1];

Serializer serializer = LimboAuth.getSerializer();
try {
RegisteredPlayer registeredPlayer = AuthSessionHandler.fetchInfo(this.playerDao, nickname);
RegisteredPlayer registeredPlayer = AuthSessionHandler.fetchInfoLowercased(this.playerDao, nicknameLowercased);

if (registeredPlayer == null) {
source.sendMessage(serializer.deserialize(MessageFormat.format(this.notRegistered, nickname)));
Expand All @@ -84,11 +85,11 @@ public void execute(CommandSource source, String[] args) {
final String newHash = RegisteredPlayer.genHash(newPassword);

UpdateBuilder<RegisteredPlayer, String> updateBuilder = this.playerDao.updateBuilder();
updateBuilder.where().eq(RegisteredPlayer.LOWERCASE_NICKNAME_FIELD, nickname.toLowerCase(Locale.ROOT));
updateBuilder.where().eq(RegisteredPlayer.LOWERCASE_NICKNAME_FIELD, nicknameLowercased);
updateBuilder.updateColumnValue(RegisteredPlayer.HASH_FIELD, newHash);
updateBuilder.update();

this.plugin.removePlayerFromCache(nickname);
this.plugin.removePlayerFromCacheLowercased(nicknameLowercased);
this.server.getPlayer(nickname)
.ifPresent(player -> player.sendMessage(serializer.deserialize(MessageFormat.format(this.message, newPassword))));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ public List<String> suggest(SimpleCommand.Invocation invocation) {
public void execute(CommandSource source, String[] args) {
if (args.length == 1) {
String playerNick = args[0];
String usernameLowercased = playerNick.toLowerCase(Locale.ROOT);

Serializer serializer = LimboAuth.getSerializer();
try {
this.plugin.getServer().getEventManager().fireAndForget(new AuthUnregisterEvent(playerNick));
this.playerDao.deleteById(playerNick.toLowerCase(Locale.ROOT));
this.plugin.removePlayerFromCache(playerNick);
this.playerDao.deleteById(usernameLowercased);
this.plugin.removePlayerFromCacheLowercased(usernameLowercased);
this.server.getPlayer(playerNick).ifPresent(player -> player.disconnect(this.kick));
source.sendMessage(serializer.deserialize(MessageFormat.format(this.successful, playerNick)));
} catch (SQLException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@ public void execute(CommandSource source, String[] args) {
if (source instanceof Player) {
if (args.length == 2) {
if (this.confirmKeyword.equalsIgnoreCase(args[1])) {
String username = ((Player) source).getUsername();
RegisteredPlayer player = AuthSessionHandler.fetchInfo(this.playerDao, username);
String usernameLowercase = ((Player) source).getUsername().toLowerCase(Locale.ROOT);
RegisteredPlayer player = AuthSessionHandler.fetchInfoLowercased(this.playerDao, usernameLowercase);
if (player == null) {
source.sendMessage(this.notRegistered);
} else if (player.getHash().isEmpty()) {
source.sendMessage(this.alreadyPremium);
} else if (AuthSessionHandler.checkPassword(args[0], player, this.playerDao)) {
if (this.plugin.isPremiumExternal(username.toLowerCase(Locale.ROOT)).getState() == LimboAuth.PremiumState.PREMIUM_USERNAME) {
if (this.plugin.isPremiumExternal(usernameLowercase).getState() == LimboAuth.PremiumState.PREMIUM_USERNAME) {
try {
player.setHash("");
this.playerDao.update(player);
this.plugin.removePlayerFromCache(username);
this.plugin.removePlayerFromCacheLowercased(usernameLowercase);
((Player) source).disconnect(this.successful);
} catch (SQLException e) {
source.sendMessage(this.errorOccurred);
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/net/elytrium/limboauth/command/TotpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Locale;
import net.elytrium.commons.kyori.serialization.Serializer;
import net.elytrium.limboauth.LimboAuth;
import net.elytrium.limboauth.Settings;
Expand Down Expand Up @@ -94,12 +95,13 @@ public void execute(CommandSource source, String[] args) {
source.sendMessage(this.usage);
} else {
String username = ((Player) source).getUsername();
String usernameLowercase = username.toLowerCase(Locale.ROOT);

RegisteredPlayer playerInfo;
UpdateBuilder<RegisteredPlayer, String> updateBuilder;
if (args[0].equalsIgnoreCase("enable")) {
if (this.needPassword ? args.length == 2 : args.length == 1) {
playerInfo = AuthSessionHandler.fetchInfo(this.playerDao, username);
playerInfo = AuthSessionHandler.fetchInfoLowercased(this.playerDao, usernameLowercase);
if (playerInfo == null) {
source.sendMessage(this.notRegistered);
return;
Expand All @@ -119,7 +121,7 @@ public void execute(CommandSource source, String[] args) {
String secret = this.secretGenerator.generate();
try {
updateBuilder = this.playerDao.updateBuilder();
updateBuilder.where().eq(RegisteredPlayer.NICKNAME_FIELD, username);
updateBuilder.where().eq(RegisteredPlayer.LOWERCASE_NICKNAME_FIELD, usernameLowercase);
updateBuilder.updateColumnValue(RegisteredPlayer.TOTP_TOKEN_FIELD, secret);
updateBuilder.update();
} catch (SQLException e) {
Expand Down Expand Up @@ -147,7 +149,7 @@ public void execute(CommandSource source, String[] args) {
}
} else if (args[0].equalsIgnoreCase("disable")) {
if (args.length == 2) {
playerInfo = AuthSessionHandler.fetchInfo(this.playerDao, username);
playerInfo = AuthSessionHandler.fetchInfoLowercased(this.playerDao, usernameLowercase);

if (playerInfo == null) {
source.sendMessage(this.notRegistered);
Expand All @@ -157,7 +159,7 @@ public void execute(CommandSource source, String[] args) {
if (AuthSessionHandler.getTotpCodeVerifier().isValidCode(playerInfo.getTotpToken(), args[1])) {
try {
updateBuilder = this.playerDao.updateBuilder();
updateBuilder.where().eq(RegisteredPlayer.NICKNAME_FIELD, username);
updateBuilder.where().eq(RegisteredPlayer.LOWERCASE_NICKNAME_FIELD, usernameLowercase);
updateBuilder.updateColumnValue(RegisteredPlayer.TOTP_TOKEN_FIELD, "");
updateBuilder.update();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,17 @@ public void execute(CommandSource source, String[] args) {
if (args.length == 2) {
if (this.confirmKeyword.equalsIgnoreCase(args[1])) {
String username = ((Player) source).getUsername();
RegisteredPlayer player = AuthSessionHandler.fetchInfo(this.playerDao, username);
String usernameLowercase = username.toLowerCase(Locale.ROOT);
RegisteredPlayer player = AuthSessionHandler.fetchInfoLowercased(this.playerDao, usernameLowercase);
if (player == null) {
source.sendMessage(this.notRegistered);
} else if (player.getHash().isEmpty()) {
source.sendMessage(this.crackedCommand);
} else if (AuthSessionHandler.checkPassword(args[0], player, this.playerDao)) {
try {
this.plugin.getServer().getEventManager().fireAndForget(new AuthUnregisterEvent(username));
this.playerDao.deleteById(username.toLowerCase(Locale.ROOT));
this.plugin.removePlayerFromCache(username);
this.playerDao.deleteById(usernameLowercase);
this.plugin.removePlayerFromCacheLowercased(usernameLowercase);
((Player) source).disconnect(this.successful);
} catch (SQLException e) {
source.sendMessage(this.errorOccurred);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,12 @@ public static RegisteredPlayer fetchInfo(Dao<RegisteredPlayer, String> playerDao
}

public static RegisteredPlayer fetchInfo(Dao<RegisteredPlayer, String> playerDao, String nickname) {
return AuthSessionHandler.fetchInfoLowercased(playerDao, nickname.toLowerCase(Locale.ROOT));
}

public static RegisteredPlayer fetchInfoLowercased(Dao<RegisteredPlayer, String> playerDao, String nickname) {
try {
List<RegisteredPlayer> playerList = playerDao.queryForEq(RegisteredPlayer.LOWERCASE_NICKNAME_FIELD, nickname.toLowerCase(Locale.ROOT));
List<RegisteredPlayer> playerList = playerDao.queryForEq(RegisteredPlayer.LOWERCASE_NICKNAME_FIELD, nickname);
return (playerList != null ? playerList.size() : 0) == 0 ? null : playerList.get(0);
} catch (SQLException e) {
throw new SQLRuntimeException(e);
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/net/elytrium/limboauth/listener/AuthListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,11 @@
import com.velocitypowered.api.event.connection.PostLoginEvent;
import com.velocitypowered.api.event.connection.PreLoginEvent;
import com.velocitypowered.api.event.player.GameProfileRequestEvent;
import com.velocitypowered.api.proxy.InboundConnection;
import com.velocitypowered.api.util.UuidUtils;
import com.velocitypowered.proxy.connection.MinecraftConnection;
import com.velocitypowered.proxy.connection.client.InitialInboundConnection;
import com.velocitypowered.proxy.connection.client.InitialLoginSessionHandler;
import com.velocitypowered.proxy.connection.client.LoginInboundConnection;
import com.velocitypowered.proxy.protocol.packet.ServerLogin;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.sql.SQLException;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import net.elytrium.commons.utils.reflection.ReflectionException;
import net.elytrium.limboapi.api.event.LoginLimboRegisterEvent;
import net.elytrium.limboauth.LimboAuth;
import net.elytrium.limboauth.Settings;
Expand All @@ -49,8 +41,8 @@
// TODO: Customizable events priority
public class AuthListener {

private static final MethodHandle DELEGATE_FIELD;
private static final MethodHandle LOGIN_FIELD;
//private static final MethodHandle DELEGATE_FIELD;
//private static final MethodHandle LOGIN_FIELD;

private final LimboAuth plugin;
private final Dao<RegisteredPlayer, String> playerDao;
Expand All @@ -76,6 +68,7 @@ public void onPreLoginEvent(PreLoginEvent event) {
}

// Temporarily disabled because some clients send UUID version 4 (random UUID) even if the player is cracked
/*
private boolean isPremiumByIdentifiedKey(InboundConnection inbound) throws Throwable {
LoginInboundConnection inboundConnection = (LoginInboundConnection) inbound;
InitialInboundConnection initialInbound = (InitialInboundConnection) DELEGATE_FIELD.invokeExact(inboundConnection);
Expand All @@ -94,6 +87,7 @@ private boolean isPremiumByIdentifiedKey(InboundConnection inbound) throws Throw
return holder.version() != 3;
}
*/

@Subscribe
public void onProxyDisconnect(DisconnectEvent event) {
Expand Down Expand Up @@ -148,7 +142,7 @@ public void onGameProfileRequest(GameProfileRequestEvent event) {
} else if (event.isOnlineMode()) {
try {
UpdateBuilder<RegisteredPlayer, String> updateBuilder = this.playerDao.updateBuilder();
updateBuilder.where().eq(RegisteredPlayer.NICKNAME_FIELD, event.getUsername());
updateBuilder.where().eq(RegisteredPlayer.LOWERCASE_NICKNAME_FIELD, event.getUsername().toLowerCase(Locale.ROOT));
updateBuilder.updateColumnValue(RegisteredPlayer.HASH_FIELD, "");
updateBuilder.update();
} catch (SQLException e) {
Expand All @@ -169,6 +163,7 @@ public void onGameProfileRequest(GameProfileRequestEvent event) {
}
}

/*
static {
try {
DELEGATE_FIELD = MethodHandles.privateLookupIn(LoginInboundConnection.class, MethodHandles.lookup())
Expand All @@ -179,4 +174,5 @@ public void onGameProfileRequest(GameProfileRequestEvent event) {
throw new ReflectionException(e);
}
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class RegisteredPlayer {
@DatabaseField(canBeNull = false, columnName = HASH_FIELD)
private String hash = "";

@DatabaseField(columnName = IP_FIELD)
@DatabaseField(columnName = IP_FIELD, index = true)
private String ip;

@DatabaseField(columnName = TOTP_TOKEN_FIELD)
Expand All @@ -64,7 +64,7 @@ public class RegisteredPlayer {
@DatabaseField(columnName = UUID_FIELD)
private String uuid = "";

@DatabaseField(columnName = RegisteredPlayer.PREMIUM_UUID_FIELD)
@DatabaseField(columnName = RegisteredPlayer.PREMIUM_UUID_FIELD, index = true)
private String premiumUuid = "";

@DatabaseField(columnName = LOGIN_IP_FIELD)
Expand Down

0 comments on commit 1dbca38

Please sign in to comment.