From 2c80e2f2c90c612cd926ed6938b4104fccb35c11 Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Tue, 26 Jun 2018 20:14:39 +0200 Subject: [PATCH] ok extension support works --- src/main/extensions/Extension.java | 10 ++--- src/main/extensions/SimpleTestExtension.java | 19 ++++++++++ src/main/protocol/HMessage.java | 2 +- src/main/protocol/HPacket.java | 37 ++++++++++++++++++- src/main/ui/extensions/Extensions.java | 8 ++-- src/main/ui/extensions/GEarthExtension.java | 5 ++- .../extensions/GEarthExtensionsRegistrer.java | 21 ++++++++++- 7 files changed, 86 insertions(+), 16 deletions(-) diff --git a/src/main/extensions/Extension.java b/src/main/extensions/Extension.java index a2ed39b..5d4c174 100644 --- a/src/main/extensions/Extension.java +++ b/src/main/extensions/Extension.java @@ -70,6 +70,7 @@ public abstract class Extension { byte[] headerandbody = new byte[length + 4]; int amountRead = 0; + while (amountRead < length) { amountRead += dIn.read(headerandbody, 4 + amountRead, Math.min(dIn.available(), length - amountRead)); } @@ -118,10 +119,6 @@ public abstract class Extension { HMessage habboMessage = new HMessage(stringifiedMessage); HPacket habboPacket = habboMessage.getPacket(); - lastwrapper = packet; - lastM = habboMessage; - last = habboPacket; - Map> listeners = habboMessage.getDestination() == HMessage.Side.TOCLIENT ? incomingMessageListeners : @@ -130,12 +127,14 @@ public abstract class Extension { if (listeners.containsKey(-1)) { // registered on all packets for (int i = listeners.get(-1).size() - 1; i >= 0; i--) { listeners.get(-1).get(i).act(habboMessage); + habboMessage.getPacket().setReadIndex(6); } } if (listeners.containsKey(habboPacket.headerId())) { for (int i = listeners.get(habboPacket.headerId()).size() - 1; i >= 0; i--) { listeners.get(habboPacket.headerId()).get(i).act(habboMessage); + habboMessage.getPacket().setReadIndex(6); } } @@ -143,6 +142,7 @@ public abstract class Extension { response.appendLongString(habboMessage.stringify()); writeToStream(response.toBytes()); + } } @@ -162,7 +162,7 @@ public abstract class Extension { } private void writeToStream(byte[] bytes) throws IOException { - synchronized (out) { + synchronized (this) { out.write(bytes); } } diff --git a/src/main/extensions/SimpleTestExtension.java b/src/main/extensions/SimpleTestExtension.java index 75bc7bf..e54b190 100644 --- a/src/main/extensions/SimpleTestExtension.java +++ b/src/main/extensions/SimpleTestExtension.java @@ -1,5 +1,8 @@ package main.extensions; +import main.protocol.HMessage; +import main.protocol.HPacket; + /** * Created by Jonas on 24/06/18. */ @@ -16,6 +19,22 @@ public class SimpleTestExtension extends Extension { @Override protected void init() { System.out.println("init"); + + intercept(HMessage.Side.TOSERVER, 1926, this::onSendMessage); + } + + private void onSendMessage(HMessage message) { + HPacket packet = message.getPacket(); + + String watchasaid = packet.readString(); + + System.out.println("you said: " + watchasaid); + + if (watchasaid.equals("blocked")) { + message.setBlocked(true); + } + + packet.replaceString(6, "@cyan@" + watchasaid); } @Override diff --git a/src/main/protocol/HMessage.java b/src/main/protocol/HMessage.java index 56cc384..159fa4b 100644 --- a/src/main/protocol/HMessage.java +++ b/src/main/protocol/HMessage.java @@ -57,7 +57,7 @@ public class HMessage implements StringifyAble { @Override public void constructFromString(String str) { - String[] parts = str.split("\t"); + String[] parts = str.split("\t", 4); this.isBlocked = parts[0].equals("1"); this.index = Integer.parseInt(parts[1]); this.side = parts[2].equals("TOCLIENT") ? Side.TOCLIENT : Side.TOSERVER; diff --git a/src/main/protocol/HPacket.java b/src/main/protocol/HPacket.java index 5f071a3..63ef9bd 100644 --- a/src/main/protocol/HPacket.java +++ b/src/main/protocol/HPacket.java @@ -780,14 +780,15 @@ public class HPacket implements StringifyAble { @Override public String stringify() { - String st = (isEdited ? "1" : "0") + this.toString(); + String st = null; + st = (isEdited ? "1" : "0") + new String(packetInBytes, StandardCharsets.ISO_8859_1); return st; } @Override public void constructFromString(String str) { this.isEdited = str.charAt(0) == '1'; - packetInBytes = fromStringToBytes(str.substring(1)); + packetInBytes = str.substring(1).getBytes(StandardCharsets.ISO_8859_1); } @Override @@ -797,4 +798,36 @@ public class HPacket implements StringifyAble { HPacket packet2 = (HPacket) object; return Arrays.equals(packetInBytes, packet2.packetInBytes) && (isEdited == packet2.isEdited); } + + public static void main(String[] args) { +// HPacket packet = new HPacket("{l}{u:500}{i:4}{s:heey}{b:false}"); +// System.out.println(packet); +// +// String stringified = packet.stringify(); +// System.out.println("stringified: " + stringified); +// System.out.println(stringified.length()); +// +// +// HPacket packet1 = new HPacket(new byte[0]); +// packet1.constructFromString(stringified); +// +// System.out.println(packet1); +// System.out.println(packet.equals(packet1)); + + HPacket packet = new HPacket(555); + for (int i = -128; i < 128; i++) { + packet.appendByte((byte)i); + } + System.out.println(packet); + + String stringified = packet.stringify(); + System.out.println(stringified.length()); + + + HPacket packet1 = new HPacket(new byte[0]); + packet1.constructFromString(stringified); + + System.out.println(packet1); + System.out.println(packet.equals(packet1)); + } } \ No newline at end of file diff --git a/src/main/ui/extensions/Extensions.java b/src/main/ui/extensions/Extensions.java index a799501..6872984 100644 --- a/src/main/ui/extensions/Extensions.java +++ b/src/main/ui/extensions/Extensions.java @@ -148,14 +148,14 @@ public class Extensions extends SubForm { @Override public void act(HPacket packet) { if (packet.headerId() == INCOMING_MESSAGES_IDS.MANIPULATEDPACKET) { - String stringifiedresponse = packet.readLongString(); + String stringifiedresponse = packet.readLongString(6); HMessage responseMessage = new HMessage(stringifiedresponse); if (responseMessage.getDestination() == message.getDestination() && responseMessage.getIndex() == message.getIndex()) { if (!message.equals(responseMessage)) { message.constructFromString(stringifiedresponse); - if (responseMessage.isBlocked()) { - isblock[0] = true; - } + } + if (responseMessage.isBlocked()) { + isblock[0] = true; } synchronized (collection) { collection.remove(extension); diff --git a/src/main/ui/extensions/GEarthExtension.java b/src/main/ui/extensions/GEarthExtension.java index 9ff4fbe..e5391b0 100644 --- a/src/main/ui/extensions/GEarthExtension.java +++ b/src/main/ui/extensions/GEarthExtension.java @@ -30,7 +30,7 @@ public class GEarthExtension { new Thread(() -> { try { - synchronized (connection.getOutputStream()) { + synchronized (connection) { connection.getOutputStream().write((new HPacket(Extensions.OUTGOING_MESSAGES_IDS.INFOREQUEST)).toBytes()); } @@ -96,6 +96,7 @@ public class GEarthExtension { for (int i = receiveMessageListeners.size() - 1; i >= 0; i--) { receiveMessageListeners.get(i).act(packet); + packet.setReadIndex(6); } } @@ -150,7 +151,7 @@ public class GEarthExtension { public boolean sendMessage(HPacket message) { try { - synchronized (connection.getOutputStream()) { + synchronized (this) { connection.getOutputStream().write(message.toBytes()); } return true; diff --git a/src/main/ui/extensions/GEarthExtensionsRegistrer.java b/src/main/ui/extensions/GEarthExtensionsRegistrer.java index f9938e3..4787f1a 100644 --- a/src/main/ui/extensions/GEarthExtensionsRegistrer.java +++ b/src/main/ui/extensions/GEarthExtensionsRegistrer.java @@ -9,11 +9,19 @@ import java.net.Socket; */ public class GEarthExtensionsRegistrer { - final ServerSocket serverSocket; + private ServerSocket serverSocket; GEarthExtensionsRegistrer(ExtensionRegisterObserver observer) throws IOException { - serverSocket = new ServerSocket(0); +// serverSocket = new ServerSocket(0); + int port = 9092; + boolean serverSetup = false; + while (!serverSetup) { + serverSetup = createServer(port); + port++; + } + + new Thread(() -> { try { while (!serverSocket.isClosed()) { @@ -24,6 +32,15 @@ public class GEarthExtensionsRegistrer { }).start(); } + private boolean createServer(int port) { + try { + serverSocket = new ServerSocket(port); + return true; + } catch (IOException e) { + return false; + } + } + public int getPort() { return serverSocket.getLocalPort(); }