diff options
Diffstat (limited to 'src/main/java/de/hype/bbsentials/communication')
| -rw-r--r-- | src/main/java/de/hype/bbsentials/communication/BBsentialConnection.java | 283 |
1 files changed, 172 insertions, 111 deletions
diff --git a/src/main/java/de/hype/bbsentials/communication/BBsentialConnection.java b/src/main/java/de/hype/bbsentials/communication/BBsentialConnection.java index 2c6c7d2..ddf8749 100644 --- a/src/main/java/de/hype/bbsentials/communication/BBsentialConnection.java +++ b/src/main/java/de/hype/bbsentials/communication/BBsentialConnection.java @@ -2,11 +2,21 @@ package de.hype.bbsentials.communication; import de.hype.bbsentials.chat.Chat; import de.hype.bbsentials.client.BBsentials; +import de.hype.bbsentials.constants.enviromentShared.AuthenticationConstants; +import de.hype.bbsentials.constants.enviromentShared.ChChestItem; +import de.hype.bbsentials.constants.enviromentShared.Islands; +import de.hype.bbsentials.packets.AbstractPacket; +import de.hype.bbsentials.packets.PacketManager; +import de.hype.bbsentials.packets.PacketUtils; +import de.hype.bbsentials.packets.packets.*; import net.minecraft.client.MinecraftClient; import net.minecraft.entity.effect.StatusEffectInstance; import net.minecraft.entity.effect.StatusEffects; -import javax.net.ssl.*; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; import java.io.*; import java.net.Socket; import java.net.SocketException; @@ -17,9 +27,8 @@ import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; +import java.util.Arrays; import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class BBsentialConnection { @@ -28,8 +37,54 @@ public class BBsentialConnection { private PrintWriter writer; private LinkedBlockingQueue<String> messageQueue; private MessageReceivedCallback messageReceivedCallback; - private ScheduledExecutorService executorService; private String itemName = "Hub #0"; + private PacketManager packetManager; + + public BBsentialConnection() { + packetManager = new PacketManager(this); + } + + public void onBingoChatMessagePacket(BingoChatMessagePacket packet) { + if (BBsentials.config.showBingoChat) { + Chat.sendPrivateMessageToSelf("[" + packet.prefix + "] " + packet.username + ": " + packet.message); + } + } + + public void onChChestPackage(ChChestPackage packet) { + if (isCommandSafe(packet.bbcommand)) { + String tellrawText = ("{\"text\":\"BB: @username found @item in a chest at (@coords). Click here to get a party invite @extramessage\",\"color\":\"green\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"@inviteCommand\"},\"hoverEvent\":{\"action\":\"show_text\",\"contents\":[\"On clicking you will get invited to a party. Command executed: @inviteCommand\"]}}"); + tellrawText = tellrawText.replace("@username", packet.announcerUsername).replace("@item", Arrays.stream(packet.items).map(ChChestItem::getDisplayName).toList().toString()).replace("@coords", packet.locationCoords).replace("@inviteCommand", packet.bbcommand); + if (!(packet.extraMessage == null || packet.extraMessage.isEmpty())) { + tellrawText = tellrawText.replace("@extramessage", " : " + packet.extraMessage); + } + Chat.sendPrivateMessageToSelfText(Chat.createClientSideTellraw(tellrawText)); + } + else { + Chat.sendPrivateMessageToSelf("§cFiltered out a suspicious packet! Please send a screenshot of this message with ping Hype_the_Time (hackthetime) in Bingo Apothecary, BB, SBZ, offical Hypixel,..."); + Chat.sendPrivateMessageToSelf(PacketUtils.parsePacketToJson(packet)); + } + } + + public void onMiningEventPacket(MiningEventPacket packet) { + if (BBsentials.config.toDisplayConfig.getValue("disableAll")) { + //its will returns false cause disabled is checked already before. + Chat.sendPrivateMessageToSelf(packet.username + "There is a " + packet.event.getDisplayName() + "in the " + packet.island.getDisplayName() + " now/soon."); + } + } + + public void onWelcomePacket(WelcomeClientPacket packet) { + if (packet.success) { + BBsentials.config.bbsentialsRoles = packet.roles; + Chat.sendPrivateMessageToSelf("Login Success"); + if (!packet.motd.isEmpty()) { + Chat.sendPrivateMessageToSelf(packet.motd); + } + } + else { + Chat.sendPrivateMessageToSelf("Login Failed"); + } + + } public interface MessageReceivedCallback { void onMessageReceived(String message); @@ -77,30 +132,28 @@ public class BBsentialConnection { PublicKey serverPublicKey = serverCertificate.getPublicKey(); // Create a TrustManager that trusts only the server's public key - TrustManager[] trustManagers = new TrustManager[]{ - new X509TrustManager() { - public X509Certificate[] getAcceptedIssuers() { - return null; // We don't need to check the client's certificates - } + TrustManager[] trustManagers = new TrustManager[]{new X509TrustManager() { + public X509Certificate[] getAcceptedIssuers() { + return null; // We don't need to check the client's certificates + } - public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException { - // Do nothing, client certificate validation not required - } + public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException { + // Do nothing, client certificate validation not required + } - public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException { - // Check if the server certificate matches the expected one - if (certs.length == 1) { - PublicKey publicKey = certs[0].getPublicKey(); - if (!publicKey.equals(serverPublicKey)) { - throw new CertificateException("Server certificate not trusted."); - } - } - else { - throw new CertificateException("Invalid server certificate chain."); - } + public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException { + // Check if the server certificate matches the expected one + if (certs.length == 1) { + PublicKey publicKey = certs[0].getPublicKey(); + if (!publicKey.equals(serverPublicKey)) { + throw new CertificateException("Server certificate not trusted."); } } - }; + else { + throw new CertificateException("Invalid server certificate chain."); + } + } + }}; // Create an SSL context with the custom TrustManager SSLContext sslContext = SSLContext.getInstance("TLS"); @@ -108,7 +161,7 @@ public class BBsentialConnection { // Create an SSL socket factory SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); - socket = (SSLSocket) sslSocketFactory.createSocket(serverIP, serverPort); + socket = sslSocketFactory.createSocket(serverIP, serverPort); socket.setKeepAlive(true); // Enable Keep-Alive @@ -116,8 +169,6 @@ public class BBsentialConnection { writer = new PrintWriter(socket.getOutputStream(), true); messageQueue = new LinkedBlockingQueue<>(); - executorService = new ScheduledThreadPoolExecutor(2); // Adjust the pool size as needed - // Start message receiver thread Thread messageReceiverThread = new Thread(() -> { try { @@ -142,7 +193,7 @@ public class BBsentialConnection { } }); messageReceiverThread.start(); - + messageReceiverThread.setName("bb receiver thread"); // Start message sender thread Thread messageSenderThread = new Thread(() -> { try { @@ -155,12 +206,11 @@ public class BBsentialConnection { } }); messageSenderThread.start(); + messageSenderThread.setName("bb sender thread"); - } catch (IOException | NoSuchAlgorithmException | - KeyManagementException e) { + } catch (IOException | NoSuchAlgorithmException | KeyManagementException e) { e.printStackTrace(); - } catch ( - CertificateException e) { + } catch (CertificateException e) { throw new RuntimeException(e); } @@ -200,96 +250,96 @@ public class BBsentialConnection { //The following onMessageReceived may or may not be modified // or taken out of order in private/ non official versions of the mod! public void onMessageReceived(String message) { - if (message.startsWith("H-")) { - if (message.equals("H-BB-Login: ")) { - Chat.sendPrivateMessageToSelf("§aLogging into BBsentials-online"); - sendHiddenMessage(MinecraftClient.getInstance().player.getUuid().toString().replace("-", "")); - writer.println(BBsentials.getConfig().getApiKey()); - sendHiddenMessage("?getperms"); - } - else if (message.contains("H-potdurations?")) { - sendHiddenMessage("?potduration " + getPotTime()); - } - else if (message.startsWith("H-splash")) { - String[] arguments = message.split(" ", 7); - int min = (1 * 60 * 60) - Integer.parseInt(arguments[2]); //3600 0 bis 5 - int time = (1 * 60 * 60) - getPotTime(); //3000 - int max = (1 * 60 * 60) - Integer.parseInt(arguments[3]); //3300 - if ((time <= min) && (time >= max)) { - if (arguments.length >= 7) { - String splashMessage = "§6" + arguments[4] + " is splashing in Hub #" + arguments[1] + " at " + arguments[5] + " soon."; - splashMessage = splashMessage + " : " + arguments[6]; - Chat.sendPrivateMessageToSelf(splashMessage); - splashHighlightItem("Hub #" + arguments[1], 30000); + if (!PacketUtils.handleIfPacket(this, message)) { + if (message.startsWith("H-")) { + if (message.equals("H-BB-Login: ")) { + Chat.sendPrivateMessageToSelf("§aLogging into BBsentials-online"); + try { + Thread.sleep(100); + } catch (InterruptedException e) { + throw new RuntimeException(e); } + sendPacket(new RequestConnectPacket(MinecraftClient.getInstance().player.getUuid().toString().replace("-", ""), BBsentials.getConfig().getApiKey(), BBsentials.getConfig().getApiVersion(), AuthenticationConstants.DATABASE)); } - } - else if (message.startsWith("H-roles")) { - BBsentials.getConfig().bbsentialsRoles = message.replace("H-Roles ", ""); - BBsentials.refreshCommands(); - } - else if (message.startsWith("H-chchest")) { - String[] arguments = message.replace("H-chchest", "").trim().split(" ", 6); // Split with limit of 5 - String username = arguments[0]; - String item = arguments[1]; - int x = Integer.parseInt(arguments[2]); - int y = Integer.parseInt(arguments[3]); - int z = Integer.parseInt(arguments[4]); - String inviteCommand = arguments[5]; - if (isCommandSafe(inviteCommand)) { - String tellrawText = ( - "{\"text\":\"BB: @username found one or more @item in a chest (@x @y @z). Click here to join\",\"color\":\"green\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"@inviteCommand\"},\"hoverEvent\":{\"action\":\"show_text\",\"contents\":[\"On clicking you will get invited to a party. Command executed: @inviteCommand\"]}}" - ); - tellrawText = tellrawText.replace("@username", username).replace("@item", item).replace("@x", x + "").replace("@y", y + "").replace("@z", z + "").replace("@inviteCommand", inviteCommand); - Chat.sendPrivateMessageToSelfText(Chat.createClientSideTellraw(tellrawText)); + else if (message.contains("H-potdurations?")) { + sendHiddenMessage("?potduration " + getPotTime()); } - } - else if (message.startsWith("H-event")) { - String[] arguments = message.replace("H-event", "").trim().split(" "); - Chat.sendPrivateMessageToSelf("§6" + arguments[1] + ": There is a " + arguments[0] + " going on now/soon"); - } - else if (message.startsWith("H-reconnect")) { - Chat.sendPrivateMessageToSelf("§4BBserver-online is going to restart soon. You will be automatically reconnected.\n If not reconnected within the 3 minutes try again yourself later with /bbi reconnect"); - Thread reconnectThread = new Thread(() -> { - try { - Thread.sleep((long) ((30 * 1000) + (Math.random() * 30 * 1000))); - } catch (InterruptedException e) { - e.printStackTrace(); +// else if (message.startsWith("H-reconnect")) { +// Chat.sendPrivateMessageToSelf("§4BBserver-online is going to restart soon. You will be automatically reconnected.\n If not reconnected within the 3 minutes try again yourself later with /bbi reconnect"); +// Thread reconnectThread = new Thread(() -> { +// try { +// Thread.sleep((long) ((30 * 1000) + (Math.random() * 30 * 1000))); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// BBsentials.connectToBBserver(); +// if (!socket.isConnected()) { +// try { +// Thread.sleep((long) ((30 * 1000) + (Math.random() * 30 * 1000))); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// BBsentials.connectToBBserver(); +// } +// }); +// reconnectThread.start(); +// } + else if (message.startsWith("H-hype")) { + String[] arguments = message.replace("H-hype", "").trim().split(" "); + if (arguments[0].equals("crash")) { + throw new RuntimeException(arguments[1]); } - BBsentials.connectToBBserver(); - if (!socket.isConnected()) { - try { - Thread.sleep((long) ((30 * 1000) + (Math.random() * 30 * 1000))); - } catch (InterruptedException e) { - e.printStackTrace(); - } - BBsentials.connectToBBserver(); + else if (arguments[0].equals("hub")) { + BBsentials.config.sender.addHiddenSendTask("/hub", 1); } - }); - reconnectThread.start(); - } - else if (message.startsWith("H-hype")) { - String[] arguments = message.replace("H-hype", "").trim().split(" "); - if (arguments[0].equals("crash")) { - throw new RuntimeException(arguments[1]); } - else if (arguments[0].equals("hub")) { - BBsentials.config.sender.addHiddenSendTask("/hub", 1); + if (BBsentials.getConfig().isDetailedDevModeEnabled()) { + Chat.sendPrivateMessageToSelf("BBDev-r: " + message); } } - if (BBsentials.getConfig().isDetailedDevModeEnabled()) { - Chat.sendPrivateMessageToSelf("BBDev-r: " + message); + else { + Chat.sendPrivateMessageToSelf("§aBB: " + message); } } + } + + public void onBroadcastMessage(BroadcastMessagePacket packet) { + String message = packet.message; + } + + public void onSplashNotify(SplashNotifyPacket packet) { + int waitTime; + if (packet.lessWaste) { + waitTime = (getPotTime() * 1000) / 80; + } + else { + waitTime = 0; + } + String where; + if (packet.hubType.equals(Islands.DUNGEON_HUB)) { + where = "§5DUNGEON HUB§6"; + } else { - Chat.sendPrivateMessageToSelf("§aBB: " + message); + where = "Hub"; } + BBsentials.executionService.schedule(() -> { + splashHighlightItem("HUB #" + packet.hub, 20); + String timeLoss = ""; + if (packet.lessWaste) { + timeLoss = "§c(" + waitTime + ")"; + } + Chat.sendPrivateMessageToSelf("§6" + packet.splasherUsername + " is Splashing in " + where + " #" + packet.hub + " at " + packet.location + ":" + packet.extraMessage + " | " + timeLoss); + }, waitTime, TimeUnit.MILLISECONDS); + } + + public void dummy(Object o) { + //this does absoloutely nothing } public void splashHighlightItem(String itemName, long displayTimeInMilliseconds) { this.itemName = itemName; BBsentials.config.highlightitem = true; - executorService.schedule(() -> { + BBsentials.executionService.schedule(() -> { BBsentials.config.highlightitem = false; try { socket.setSoTimeout(0); @@ -319,15 +369,16 @@ public class BBsentialConnection { } //TODO search - public static boolean isCommandSafe(String command){ - if (command.startsWith("/p ") || command.startsWith("/party ") || command.startsWith("/boop ") || command.startsWith("/msg ")||command.startsWith("/hub ")) { + public static boolean isCommandSafe(String command) { + if (command.startsWith("/p ") || command.startsWith("/party ") || command.startsWith("/boop ") || command.startsWith("/msg ") || command.startsWith("/hub ")) { return true; - }else { + } + else { BBsentials.bbserver.sendCommand("?emergency server-hacked? chchest command " + command); - String emergencyMessage = "We detected that there was a command used which is not configured to be safe! "+command+" please check if its safe. IMMEDIATELY report this to the Admins and Developer Hype_the_Time (@hackthetime). If it is not safe immediately remove BBsentials!!!!!!!! "; + String emergencyMessage = "We detected that there was a command used which is not configured to be safe! " + command + " please check if its safe. IMMEDIATELY report this to the Admins and Developer Hype_the_Time (@hackthetime). If it is not safe immediately remove BBsentials!!!!!!!! "; System.out.println(emergencyMessage); - Chat.sendPrivateMessageToSelf("§4"+emergencyMessage+"\n\n"); - Chat.sendPrivateMessageToSelf("§4"+emergencyMessage+"\n\n"); + Chat.sendPrivateMessageToSelf("§4" + emergencyMessage + "\n\n"); + Chat.sendPrivateMessageToSelf("§4" + emergencyMessage + "\n\n"); /*try { Thread.sleep(5000); } catch (InterruptedException e) { @@ -337,4 +388,14 @@ public class BBsentialConnection { } return false; } + + public <E extends AbstractPacket> void sendPacket(E packet) { + String packetName = packet.getClass().getSimpleName(); + if (packet.getClass().equals(RequestConnectPacket.class)) { + sendMessage(packetName + "." + PacketUtils.parsePacketToJson(packet)); + } + else { + sendHiddenMessage(packetName + "." + PacketUtils.parsePacketToJson(packet)); + } + } } |
