feat: *auth so you don't have to type hash every time (this can be abused idk)

This commit is contained in:
ChomeNS
2025-05-22 18:50:28 +07:00
parent 8fd2f2ebb3
commit ad5e203417
12 changed files with 104 additions and 15 deletions

View File

@@ -279,8 +279,10 @@ public class CommandContext {
};
}
public <T extends Enum<T>> T getEnum (final Class<T> enumClass) throws CommandException {
final String string = getString(false, true, enumClass.getSimpleName());
public <T extends Enum<T>> T getEnum (final boolean required, final Class<T> enumClass) throws CommandException {
final String string = getString(false, required, enumClass.getSimpleName());
if (string.isEmpty()) return null;
try {
return Enum.valueOf(enumClass, string.toUpperCase());

View File

@@ -15,6 +15,8 @@ public enum TrustLevel {
ADMIN(2, Component.text(I18nUtilities.get("trust_level.admin")).color(NamedTextColor.DARK_RED)),
OWNER(3, Component.text(I18nUtilities.get("trust_level.owner")).color(NamedTextColor.LIGHT_PURPLE));
public static final TrustLevel MAX = values()[values().length - 1];
public final int level;
public final Component component;

View File

@@ -0,0 +1,55 @@
package me.chayapak1.chomens_bot.commands;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.command.Command;
import me.chayapak1.chomens_bot.command.CommandContext;
import me.chayapak1.chomens_bot.command.CommandException;
import me.chayapak1.chomens_bot.command.TrustLevel;
import me.chayapak1.chomens_bot.data.player.PlayerEntry;
import net.kyori.adventure.text.Component;
import java.util.Arrays;
public class AuthCommand extends Command {
public AuthCommand () {
super(
"auth",
new String[] {},
new String[] {},
TrustLevel.TRUSTED
);
final String trustLevels = String.join(" | ", Arrays.stream(TrustLevel.values()).map(Enum::name).toList());
this.usages = new String[] { "[player] [" + trustLevels + "]" };
}
@Override
public Component execute (final CommandContext context) throws CommandException {
final Bot bot = context.bot;
final String playerString = context.getString(false, false);
PlayerEntry player = null;
if (!playerString.isEmpty()) player = bot.players.getEntry(playerString);
if (player == null) player = context.sender;
TrustLevel trustLevel = context.getEnum(false, TrustLevel.class);
if (trustLevel == null) trustLevel = context.trustLevel;
if (trustLevel.level > context.trustLevel.level) throw new CommandException(
Component.translatable(
"commands.auth.error.privilege_escalate"
)
);
player.authenticatedTrustLevel = trustLevel;
return Component.translatable(
"commands.auth.output",
bot.colorPalette.defaultColor,
Component.text(player.profile.getName(), bot.colorPalette.username),
player.authenticatedTrustLevel.component
);
}
}

View File

@@ -35,7 +35,7 @@ public class CloopCommand extends Command {
long interval = context.getLong(true);
if (interval < 1) interval = 1;
final ChronoUnit unit = context.getEnum(ChronoUnit.class);
final ChronoUnit unit = context.getEnum(true, ChronoUnit.class);
if (unit == ChronoUnit.NANOS && interval < 1000)
throw new CommandException(Component.translatable("commands.cloop.add.error.too_low_nanoseconds"));

View File

@@ -30,7 +30,7 @@ public class KickCommand extends Command {
public Component execute (final CommandContext context) throws CommandException {
final Bot bot = context.bot;
final Kick method = context.getEnum(Kick.class);
final Kick method = context.getEnum(true, Kick.class);
final PlayerEntry entry = bot.players.getEntry(context.getString(true, true));

View File

@@ -251,7 +251,7 @@ public class MusicCommand extends Command implements Listener {
final Bot bot = context.bot;
final Loop loop = context.getEnum(Loop.class);
final Loop loop = context.getEnum(true, Loop.class);
bot.music.loop = loop;

View File

@@ -36,7 +36,9 @@ public class ValidateCommand extends Command {
else if (context instanceof ConsoleCommandContext) return Component
.translatable("commands.validate.console", NamedTextColor.GREEN);
else return Component.translatable(
"commands.validate.player",
context.sender.authenticatedTrustLevel != TrustLevel.PUBLIC
? "commands.validate.player_authenticated"
: "commands.validate.player",
NamedTextColor.GREEN,
trustLevelComponent
);

View File

@@ -1,5 +1,6 @@
package me.chayapak1.chomens_bot.data.player;
import me.chayapak1.chomens_bot.command.TrustLevel;
import net.kyori.adventure.text.Component;
import org.geysermc.mcprotocollib.auth.GameProfile;
import org.geysermc.mcprotocollib.protocol.data.game.PlayerListEntry;
@@ -20,6 +21,7 @@ public class PlayerEntry {
public final byte[] keySignature;
public boolean listed;
public String ip;
public TrustLevel authenticatedTrustLevel = TrustLevel.PUBLIC;
public PlayerEntry (
final GameProfile profile,

View File

@@ -1,6 +1,7 @@
package me.chayapak1.chomens_bot.plugins;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.command.TrustLevel;
import me.chayapak1.chomens_bot.data.listener.Listener;
import me.chayapak1.chomens_bot.data.logging.LogType;
import me.chayapak1.chomens_bot.data.player.PlayerEntry;
@@ -42,6 +43,8 @@ public class AuthPlugin implements Listener {
cleanup();
target.authenticatedTrustLevel = TrustLevel.MAX;
bot.logger.log(
LogType.AUTH,
Component

View File

@@ -8,6 +8,7 @@ import me.chayapak1.chomens_bot.command.CommandException;
import me.chayapak1.chomens_bot.command.TrustLevel;
import me.chayapak1.chomens_bot.command.contexts.*;
import me.chayapak1.chomens_bot.commands.*;
import me.chayapak1.chomens_bot.data.chat.ChatPacketType;
import me.chayapak1.chomens_bot.data.listener.Listener;
import me.chayapak1.chomens_bot.util.ExceptionUtilities;
import net.dv8tion.jda.api.entities.Member;
@@ -62,7 +63,8 @@ public class CommandHandlerPlugin implements Listener {
new GrepLogCommand(),
new FindAltsCommand(),
new RestartCommand(),
new NetCommandCommand()
new NetCommandCommand(),
new AuthCommand()
);
public static Command findCommand (final String searchTerm) {
@@ -160,19 +162,27 @@ public class CommandHandlerPlugin implements Listener {
}
}
final TrustLevel authenticatedTrustLevel = context.sender.authenticatedTrustLevel;
final boolean authenticated = context instanceof final PlayerCommandContext playerContext
&& playerContext.packetType == ChatPacketType.PLAYER
&& authenticatedTrustLevel != TrustLevel.PUBLIC;
final TrustLevel trustLevel = command.trustLevel;
if (trustLevel != TrustLevel.PUBLIC && splitInput.length < 2 && inGame) {
if (trustLevel != TrustLevel.PUBLIC && splitInput.length < 2 && inGame && !authenticated) {
context.sendOutput(Component.translatable("command_handler.no_hash_provided", NamedTextColor.RED));
return;
}
String userHash = "";
if (trustLevel != TrustLevel.PUBLIC && inGame) userHash = splitInput[1];
final boolean needsHash = trustLevel != TrustLevel.PUBLIC && inGame && !authenticated;
final String userHash = needsHash ? splitInput[1] : "";
final String[] fullArgs = Arrays.copyOfRange(splitInput, 1, splitInput.length);
final String[] args = Arrays.copyOfRange(splitInput, (trustLevel != TrustLevel.PUBLIC && inGame) ? 2 : 1, splitInput.length);
final String[] args = needsHash
? Arrays.copyOfRange(splitInput, 2, splitInput.length)
: fullArgs;
// LoL ohio spaghetti code
if (command.trustLevel != TrustLevel.PUBLIC && !bypass) {
@@ -207,6 +217,15 @@ public class CommandHandlerPlugin implements Listener {
}
context.trustLevel = userTrustLevel;
} else if (authenticated) {
if (trustLevel.level > authenticatedTrustLevel.level) {
context.sendOutput(
Component.translatable("command_handler.not_enough_roles", NamedTextColor.RED)
);
return;
}
context.trustLevel = authenticatedTrustLevel;
} else {
final TrustLevel userTrustLevel = bot.hashing.getTrustLevel(userHash, splitInput[0], context.sender);
@@ -225,8 +244,7 @@ public class CommandHandlerPlugin implements Listener {
context.trustLevel = userTrustLevel;
}
} else if (bypass) {
final TrustLevel[] values = TrustLevel.values();
context.trustLevel = values[values.length - 1]; // highest level
context.trustLevel = TrustLevel.MAX;
}
// should i give access to all bypass contexts instead of only console?