From 18dfcf50250c38fc58ea5684717f050cec160ffe Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 17 Dec 2023 18:28:53 +0100 Subject: [PATCH] WepPW Command --- CommonCore | 2 +- .../commands/PunishmentCommand.java | 2 +- .../bungeecore/commands/TokensCommand.java | 116 ------------------ .../commands/WebpasswordCommand.java | 33 ++--- .../bungeecore/listeners/BanListener.java | 2 +- 5 files changed, 10 insertions(+), 145 deletions(-) delete mode 100644 src/de/steamwar/bungeecore/commands/TokensCommand.java diff --git a/CommonCore b/CommonCore index b66c18b8..e83ab476 160000 --- a/CommonCore +++ b/CommonCore @@ -1 +1 @@ -Subproject commit b66c18b8b3f1faa3fe97fd8c3f16ce19259dc0d2 +Subproject commit e83ab4762d77567a01b0b71bdf12b65bf2aca3bd diff --git a/src/de/steamwar/bungeecore/commands/PunishmentCommand.java b/src/de/steamwar/bungeecore/commands/PunishmentCommand.java index 47ed67af..5f4724d1 100644 --- a/src/de/steamwar/bungeecore/commands/PunishmentCommand.java +++ b/src/de/steamwar/bungeecore/commands/PunishmentCommand.java @@ -66,7 +66,7 @@ public class PunishmentCommand { return null; } - return SteamwarUser.getOrCreate(uuid, name, u -> {}, (o, n) -> {}); + return SteamwarUser.getOrCreate(uuid, name, u -> {}); } private static UUID getUUIDofOfflinePlayer(String playerName) { diff --git a/src/de/steamwar/bungeecore/commands/TokensCommand.java b/src/de/steamwar/bungeecore/commands/TokensCommand.java deleted file mode 100644 index 00e01a92..00000000 --- a/src/de/steamwar/bungeecore/commands/TokensCommand.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2023 SteamWar.de-Serverteam - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package de.steamwar.bungeecore.commands; - -import de.steamwar.bungeecore.BungeeCore; -import de.steamwar.bungeecore.Message; -import de.steamwar.bungeecore.inventory.SWInventory; -import de.steamwar.bungeecore.inventory.SWItem; -import de.steamwar.bungeecore.inventory.SWListInv; -import de.steamwar.command.SWCommand; -import de.steamwar.sql.SteamwarUser; -import de.steamwar.sql.Token; -import lombok.SneakyThrows; -import net.md_5.bungee.api.chat.ClickEvent; -import net.md_5.bungee.api.connection.ProxiedPlayer; - -import java.security.MessageDigest; -import java.util.Base64; -import java.util.List; -import java.util.Random; -import java.util.stream.Collectors; - -public class TokensCommand extends SWCommand { - public TokensCommand() { - super("tokens", null, "token"); - } - - private static final Random random = new Random(); - - @SneakyThrows - private static String generateCode() { - byte[] hash = new byte[16]; - for(int i = 0; i < hash.length; i++) { - hash[i] = (byte) (random.nextInt(256) - 128); - } - return Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA-1").digest(hash)); - } - - @SneakyThrows - private static String generateHash(String code) { - MessageDigest md = MessageDigest.getInstance("SHA-512"); - return Base64.getEncoder().encodeToString(md.digest(code.getBytes())); - } - - @Register - public void genericCommand(ProxiedPlayer p) { - SteamwarUser user = SteamwarUser.get(p.getUniqueId()); - List tokens = Token.listUser(user); - - List> entries = tokens - .stream() - .map(token -> new SWListInv.SWListEntry<>(new SWItem("STONE", token.getName()), token)) - .collect(Collectors.toList()); - - SWListInv inv = new SWListInv<>(p, Message.parse("TOKENS_TITLE", p), entries, (click, element) -> { - SWInventory deleteInv = new SWInventory(p, 9, Message.parse("TOKENS_DELETE", p)); - deleteInv.addItem(0, new SWItem(Message.parse("TOKENS_DELETE_CONFIRM", p), 2), deleteClick -> { - Token.delete(element); - Message.send("TOKENS_DELETED", p); - deleteInv.close(); - }); - - deleteInv.addItem(8, new SWItem(Message.parse("TOKENS_DELETE_CANCEL", p), 1), deleteClick -> { - deleteInv.close(); - }); - - deleteInv.open(); - }); - - inv.open(); - } - - @Register("create") - public void createToken(ProxiedPlayer p, String name) { - SteamwarUser user = SteamwarUser.get(p.getUniqueId()); - - if(name.length() > 32) { - Message.send("TOKENS_NAME_TOO_LONG", p); - return; - } - - if(name.length() < 3) { - Message.send("TOKENS_NAME_TOO_SHORT", p); - return; - } - - if(Token.listUser(user).stream().anyMatch(token -> token.getName().equalsIgnoreCase(name))) { - Message.send("TOKENS_NAME_ALREADY_EXISTS", p); - return; - } - - String code = generateCode(); - String hash = generateHash(code); - - Token token = Token.create(name, user, hash); - - Message.send("TOKENS_NAME_CREATED", p, "TOKENS_NAME_CREATED_HOVER", new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, code)); - } -} diff --git a/src/de/steamwar/bungeecore/commands/WebpasswordCommand.java b/src/de/steamwar/bungeecore/commands/WebpasswordCommand.java index 2e45c274..edbfd05f 100644 --- a/src/de/steamwar/bungeecore/commands/WebpasswordCommand.java +++ b/src/de/steamwar/bungeecore/commands/WebpasswordCommand.java @@ -19,8 +19,11 @@ package de.steamwar.bungeecore.commands; +import de.steamwar.bungeecore.BungeeCore; import de.steamwar.command.SWCommand; import de.steamwar.messages.ChatSender; +import de.steamwar.sql.SteamwarUser; +import de.steamwar.sql.Token; import net.md_5.bungee.api.connection.ProxiedPlayer; import java.io.BufferedReader; @@ -41,33 +44,11 @@ public class WebpasswordCommand extends SWCommand { return; } - ProcessBuilder pb = new ProcessBuilder("php", "/var/www/register.php", player.getName(), password); - pb.redirectErrorStream(true); - try { - Process regProcess = pb.start(); - BufferedReader reader = new BufferedReader(new InputStreamReader(regProcess.getInputStream())); - String errorLine; - if((errorLine = reader.readLine()) != null) { - if ("updated".equals(errorLine)) { - ChatSender.of(player).system("WEB_UPDATED"); - return; - } else { - throw new SecurityException("Could not create webaccount " + errorLine); - } - } + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + user.setPassword(password); - ChatSender.of(player).system("WEB_CREATED"); - } catch (IOException e) { - throw new SecurityException("Could not create webaccount", e); - } - } + Token.listUser(user).stream().filter(token -> token.getName().startsWith("Website: ")).forEach(token -> token.delete()); - public static void changeUsername(String oldUsername, String newUsername){ - ProcessBuilder pb = new ProcessBuilder("php", "/var/www/changename.php", oldUsername, newUsername); - try { - pb.start(); - } catch (IOException e) { - throw new SecurityException("Could not change username", e); - } + ChatSender.of(player).system("WEB_UPDATED"); } } diff --git a/src/de/steamwar/bungeecore/listeners/BanListener.java b/src/de/steamwar/bungeecore/listeners/BanListener.java index df8fa0ea..975e86c4 100644 --- a/src/de/steamwar/bungeecore/listeners/BanListener.java +++ b/src/de/steamwar/bungeecore/listeners/BanListener.java @@ -46,7 +46,7 @@ public class BanListener extends BasicListener { event.registerIntent(BungeeCore.get()); ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { PendingConnection connection = event.getConnection(); - SteamwarUser user = SteamwarUser.getOrCreate(connection.getUniqueId(), connection.getName(), ConnectionListener::newPlayer, WebpasswordCommand::changeUsername); + SteamwarUser user = SteamwarUser.getOrCreate(connection.getUniqueId(), connection.getName(), ConnectionListener::newPlayer); if (user.isPunished(Punishment.PunishmentType.Ban)) { event.setCancelled(true); BannedUserIPs.banIP(user.getId(), connection.getAddress().getAddress().getHostAddress());