IMPROVE config + FIX hashing in console which improves too ig

This commit is contained in:
ChomeNS 2023-03-21 11:36:47 +07:00
parent b75c4c29ab
commit 03e20a5476
11 changed files with 61 additions and 26 deletions

View file

@ -12,18 +12,19 @@ import me.chayapak1.chomensbot_mabe.plugins.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.Map;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class Bot { public class Bot {
private final ArrayList<SessionListener>listeners = new ArrayList<>(); private final ArrayList<SessionListener> listeners = new ArrayList<>();
@Getter private final String host; @Getter private final String host;
@Getter private final int port; @Getter private final int port;
@Getter private final String username; @Getter private final String username;
@Getter private final List<Bot> allBots; @Getter private final List<Bot> allBots;
@Getter private final Map<String, String> keys;
@Getter private Session session; @Getter private Session session;
@ -42,12 +43,13 @@ public class Bot {
@Getter private final HashingPlugin hashing = new HashingPlugin(this); @Getter private final HashingPlugin hashing = new HashingPlugin(this);
@Getter private final MusicPlayerPlugin music = new MusicPlayerPlugin(this); @Getter private final MusicPlayerPlugin music = new MusicPlayerPlugin(this);
public Bot (String host, int port, int reconnectDelay, String username, List<Bot> allBots) { public Bot (String host, int port, int reconnectDelay, String username, List<Bot> allBots, Map<String, String> keys) {
this.host = host; this.host = host;
this.port = port; this.port = port;
this.reconnectDelay = reconnectDelay; this.reconnectDelay = reconnectDelay;
this.username = username; this.username = username;
this.allBots = allBots; this.allBots = allBots;
this.keys = keys;
reconnect(); reconnect();
} }

View file

@ -0,0 +1,17 @@
package me.chayapak1.chomensbot_mabe;
import lombok.Getter;
import java.util.Map;
public class Configuration {
@Getter public int reconnectDelay = 7000;
@Getter public Map<String, String> keys;
@Getter public Bots[] bots = new Bots[]{};
public static class Bots {
@Getter public String host;
@Getter public int port;
@Getter public String username;
}
}

View file

@ -2,6 +2,7 @@ package me.chayapak1.chomensbot_mabe;
import me.chayapak1.chomensbot_mabe.plugins.ConsolePlugin; import me.chayapak1.chomensbot_mabe.plugins.ConsolePlugin;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import java.io.*; import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
@ -12,8 +13,9 @@ import java.util.concurrent.CountDownLatch;
public class Main { public class Main {
public static void main(String[] args) throws IOException, InterruptedException { public static void main(String[] args) throws IOException, InterruptedException {
final File file = new File("config.yml"); final File file = new File("config.yml");
final Yaml yaml = new Yaml(); final Constructor constructor = new Constructor(Configuration.class);
Map<String, List<Map<String, Object>>> config; final Yaml yaml = new Yaml(constructor);
Configuration config;
if (!file.exists()) { if (!file.exists()) {
// creates config file from default-config.yml // creates config file from default-config.yml
@ -41,21 +43,21 @@ public class Main {
config = yaml.load(reader); config = yaml.load(reader);
final int reconnectDelay = (int) ((Object) config.get("reconnectDelay")); final int reconnectDelay = config.reconnectDelay();
final Map<String, String> keys = config.keys();
List<Map<String, Object>> botsOptions = config.get("bots"); Configuration.Bots[] botsOptions = config.bots();
final List<Bot> allBots = new ArrayList<>(); final List<Bot> allBots = new ArrayList<>();
final CountDownLatch latch = new CountDownLatch(botsOptions.size()); final CountDownLatch latch = new CountDownLatch(botsOptions.length);
for (Map<String, Object> botOption : botsOptions) { for (Configuration.Bots botOption : botsOptions) {
final String host = (String) botOption.get("host"); final String host = botOption.host();
final int port = (int) botOption.get("port"); final int port = botOption.port();
final String username = (String) botOption.get("username"); final String username = botOption.username();
new Thread(() -> { new Thread(() -> {
final Bot bot = new Bot(host, port, reconnectDelay, username, allBots); final Bot bot = new Bot(host, port, reconnectDelay, username, allBots, keys);
allBots.add(bot); allBots.add(bot);
latch.countDown(); latch.countDown();

View file

@ -7,8 +7,13 @@ import net.kyori.adventure.text.Component;
public class CommandContext { public class CommandContext {
@Getter public final Bot bot; @Getter public final Bot bot;
public CommandContext(Bot bot) { @Getter private final String hash;
@Getter private final String ownerHash;
public CommandContext(Bot bot, String hash, String ownerHash) {
this.bot = bot; this.bot = bot;
this.hash = hash;
this.ownerHash = ownerHash;
} }
public Component displayName () { return Component.empty(); } public Component displayName () { return Component.empty(); }

View file

@ -1,5 +1,6 @@
package me.chayapak1.chomensbot_mabe.command; package me.chayapak1.chomensbot_mabe.command;
import lombok.Getter;
import me.chayapak1.chomensbot_mabe.Bot; import me.chayapak1.chomensbot_mabe.Bot;
import me.chayapak1.chomensbot_mabe.util.ComponentUtilities; import me.chayapak1.chomensbot_mabe.util.ComponentUtilities;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -8,8 +9,8 @@ import net.kyori.adventure.text.format.NamedTextColor;
public class ConsoleCommandContext extends CommandContext { public class ConsoleCommandContext extends CommandContext {
private final Bot bot; private final Bot bot;
public ConsoleCommandContext (Bot bot) { public ConsoleCommandContext (Bot bot, String hash, String ownerHash) {
super(bot); super(bot, hash, ownerHash);
this.bot = bot; this.bot = bot;
} }

View file

@ -9,8 +9,8 @@ public class PlayerCommandContext extends CommandContext {
private final Bot bot; private final Bot bot;
public PlayerCommandContext (Bot bot, String playerName) { public PlayerCommandContext (Bot bot, String playerName, String hash, String ownerHash) {
super(bot); super(bot, hash, ownerHash);
this.bot = bot; this.bot = bot;
this.playerName = playerName; this.playerName = playerName;
} }

View file

@ -38,8 +38,8 @@ public class ValidateCommand implements Command {
final Bot bot = context.bot(); final Bot bot = context.bot();
final String hash = fullArgs[0]; final String hash = fullArgs[0];
if (hash.equals(bot.hashing().hash())) context.sendOutput(Component.text("Valid hash").color(NamedTextColor.GREEN)); if (hash.equals(context.hash())) context.sendOutput(Component.text("Valid hash").color(NamedTextColor.GREEN));
else if (hash.equals(bot.hashing().ownerHash())) context.sendOutput(Component.text("Valid OwnerHash").color(NamedTextColor.GREEN)); else if (hash.equals(context.ownerHash())) context.sendOutput(Component.text("Valid OwnerHash").color(NamedTextColor.GREEN));
return Component.text("success"); return Component.text("success");
} }

View file

@ -31,7 +31,7 @@ public class ChatCommandHandlerPlugin extends ChatPlugin.ChatListener {
if (!contents.startsWith(prefix)) return; if (!contents.startsWith(prefix)) return;
final String commandString = contents.substring(prefix.length()); final String commandString = contents.substring(prefix.length());
final PlayerCommandContext context = new PlayerCommandContext(bot, displayName); final PlayerCommandContext context = new PlayerCommandContext(bot, displayName, bot.hashing().hash(), bot.hashing().ownerHash());
final Component output = bot.commandHandler().executeCommand(commandString, context, bot.hashing().hash(), bot.hashing().ownerHash()); final Component output = bot.commandHandler().executeCommand(commandString, context, bot.hashing().hash(), bot.hashing().ownerHash());
final String textOutput = ((TextComponent) output).content(); final String textOutput = ((TextComponent) output).content();

View file

@ -77,7 +77,7 @@ public class ConsolePlugin {
if (!bot.host().equals(consoleServer) && !consoleServer.equals("all")) continue; if (!bot.host().equals(consoleServer) && !consoleServer.equals("all")) continue;
if (line.startsWith(prefix)) { if (line.startsWith(prefix)) {
final ConsoleCommandContext context = new ConsoleCommandContext(bot); final ConsoleCommandContext context = new ConsoleCommandContext(bot, "h", "o"); // ? should the hashes be hardcoded?
final Component output = bot.commandHandler().executeCommand(line.substring(prefix.length()), context, "h", "o"); final Component output = bot.commandHandler().executeCommand(line.substring(prefix.length()), context, "h", "o");
final String textOutput = ((TextComponent) output).content(); final String textOutput = ((TextComponent) output).content();

View file

@ -9,17 +9,22 @@ import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class HashingPlugin { public class HashingPlugin {
private final Bot bot;
@Getter private String hash; @Getter private String hash;
@Getter private String ownerHash; @Getter private String ownerHash;
public HashingPlugin (Bot bot) { public HashingPlugin (Bot bot) {
this.bot = bot;
bot.executor().schedule(this::update, 2, TimeUnit.SECONDS); bot.executor().schedule(this::update, 2, TimeUnit.SECONDS);
} }
public void update () { public void update () {
final String ownerHashKey = "b)R<><52>nF<6E>CW<43><57><EFBFBD>#<23>\\[<5B>S*8\"t^eia<69>Z<EFBFBD><5A>k<EFBFBD><6B><EFBFBD><EFBFBD>K1<4B>8zȢ<7A>"; // final String ownerHashKey = "b)R<><52>nF<6E>CW<43><57><EFBFBD>#<23>\\[<5B>S*8\"t^eia<69>Z<EFBFBD><5A>k<EFBFBD><6B><EFBFBD><EFBFBD>K1<4B>8zȢ<7A>";
final String normalHashKey = "<EFBFBD>iB_D<EFBFBD><EFBFBD><EFBFBD>k<EFBFBD><EFBFBD>j8H<EFBFBD>{?[/ڭ<>f<EFBFBD><>^-=<3D>Ț<EFBFBD><C89A>v]<5D><>g><3E><>=c"; // final String normalHashKey = "<EFBFBD>iB_D<EFBFBD><EFBFBD><EFBFBD>k<EFBFBD><EFBFBD>j8H<EFBFBD>{?[/ڭ<>f<EFBFBD><>^-=<3D>Ț<EFBFBD><C89A>v]<5D><>g><3E><>=c";
final String normalHashKey = bot.keys().get("normalKey");
final String ownerHashKey = bot.keys().get("ownerKey");
final String hashValue = System.currentTimeMillis() / 10_000 + normalHashKey; final String hashValue = System.currentTimeMillis() / 10_000 + normalHashKey;
hash = Hashing.sha256() hash = Hashing.sha256()

View file

@ -1,4 +1,7 @@
reconnectDelay: 7000 reconnectDelay: 7000
keys:
normalKey: '<27>iB_D<5F><44><EFBFBD>k<EFBFBD><6B>j8H<38>{?[/ڭ<>f<EFBFBD><>^-=<3D>Ț<EFBFBD><C89A>v]<5D><>g><3E><>=c'
ownerKey: 'b)R<><52>nF<6E>CW<43><57><EFBFBD>#<23>\\[<5B>S*8"t^eia<69>Z<EFBFBD><5A>k<EFBFBD><6B><EFBFBD><EFBFBD>K1<4B>8zȢ<7A>'
bots: bots:
- host: 'localhost' - host: 'localhost'
port: 25565 port: 25565