From 83d3de6c6fb7ab72cb4ea28c0b3654efa4573506 Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Tue, 26 Jun 2018 02:08:36 +0200 Subject: [PATCH] fixes but still dont work --- src/main/extensions/Extension.java | 30 ++++++++------ src/main/protocol/HPacket.java | 40 +++++++++++++------ .../packethandler/IncomingHandler.java | 2 - .../packethandler/OutgoingHandler.java | 2 - src/main/ui/extensions/Extensions.java | 26 +++++++----- src/main/ui/extensions/GEarthExtension.java | 32 ++++++++++----- 6 files changed, 83 insertions(+), 49 deletions(-) diff --git a/src/main/extensions/Extension.java b/src/main/extensions/Extension.java index 8463c1e..a2ed39b 100644 --- a/src/main/extensions/Extension.java +++ b/src/main/extensions/Extension.java @@ -51,6 +51,11 @@ public abstract class Extension { } } + HPacket lastwrapper = null; + HMessage lastM = null; + HPacket last = null; + + Socket gEarthExtensionServer = null; try { gEarthExtensionServer = new Socket("localhost", port); @@ -63,9 +68,11 @@ public abstract class Extension { int length = dIn.readInt(); byte[] headerandbody = new byte[length + 4]; - int amountRead = dIn.read(headerandbody, 4, length); - if (amountRead != length) break; + int amountRead = 0; + while (amountRead < length) { + amountRead += dIn.read(headerandbody, 4 + amountRead, Math.min(dIn.available(), length - amountRead)); + } HPacket packet = new HPacket(headerandbody); packet.fixLength(); @@ -107,13 +114,13 @@ public abstract class Extension { onDoubleClick(); } else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.PACKETINTERCEPT) { - String stringifiedMessage = packet.readString(); + String stringifiedMessage = packet.readLongString(); HMessage habboMessage = new HMessage(stringifiedMessage); HPacket habboPacket = habboMessage.getPacket(); -// -// System.out.println("----------"); -// System.out.println(stringifiedMessage); -// System.out.println(habboPacket); + + lastwrapper = packet; + lastM = habboMessage; + last = habboPacket; Map> listeners = habboMessage.getDestination() == HMessage.Side.TOCLIENT ? @@ -125,6 +132,7 @@ public abstract class Extension { listeners.get(-1).get(i).act(habboMessage); } } + 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); @@ -132,16 +140,15 @@ public abstract class Extension { } HPacket response = new HPacket(Extensions.INCOMING_MESSAGES_IDS.MANIPULATEDPACKET); - response.appendString(habboMessage.stringify()); + response.appendLongString(habboMessage.stringify()); writeToStream(response.toBytes()); } } - } catch (IOException e) { -// e.printStackTrace(); - System.out.println("ERROR"); + } catch (IOException | ArrayIndexOutOfBoundsException e) { + e.printStackTrace(); } finally { if (gEarthExtensionServer != null && !gEarthExtensionServer.isClosed()) { @@ -157,7 +164,6 @@ public abstract class Extension { private void writeToStream(byte[] bytes) throws IOException { synchronized (out) { out.write(bytes); - out.flush(); } } diff --git a/src/main/protocol/HPacket.java b/src/main/protocol/HPacket.java index 9dc7d6b..5f071a3 100644 --- a/src/main/protocol/HPacket.java +++ b/src/main/protocol/HPacket.java @@ -280,27 +280,37 @@ public class HPacket implements StringifyAble { } public String readString() { - int length = readUshort(); - byte[] x = new byte[length]; - for (int i = 0; i < x.length; i++) x[i] = readByte(); + String r = readString(readIndex); + readIndex += (2 + r.length()); + return r; + } + public String readString(int index) { + int length = readUshort(index); + index+=2; + return readString(index, length); + } + + private String readString(int index, int length) { + byte[] x = new byte[length]; + for (int i = 0; i < x.length; i++) { x[i] = readByte(index); index++; } try { return new String(x, "ISO-8859-1"); } catch (UnsupportedEncodingException e) { } return null; } - public String readString(int index) { - int length = readUshort(index); - index+=2; - byte[] x = new byte[length]; - for (int i = 0; i < x.length; i++) { x[i] = readByte(index); index++; } - try { - return new String(x, "ISO-8859-1"); - } catch (UnsupportedEncodingException e) { } + public String readLongString() { + String r = readLongString(readIndex); + readIndex += (4 + r.length()); + return r; + } + public String readLongString(int index) { + int length = readInteger(index); + index += 4; - return null; + return readString(index, length); } public boolean readBoolean() { @@ -466,6 +476,12 @@ public class HPacket implements StringifyAble { appendBytes(s.getBytes(StandardCharsets.ISO_8859_1)); return this; } + public HPacket appendLongString(String s) { + isEdited = true; + appendInt(s.length()); + appendBytes(s.getBytes(StandardCharsets.ISO_8859_1)); + return this; + } public HPacket removeFrom(int index) { return removeRange(index, packetInBytes.length - index); diff --git a/src/main/protocol/packethandler/IncomingHandler.java b/src/main/protocol/packethandler/IncomingHandler.java index eb04ca2..a085e6a 100644 --- a/src/main/protocol/packethandler/IncomingHandler.java +++ b/src/main/protocol/packethandler/IncomingHandler.java @@ -20,7 +20,6 @@ public class IncomingHandler extends Handler { synchronized (lock) { try { out.write(buffer); - out.flush(); } catch (IOException e) { e.printStackTrace(); } @@ -38,7 +37,6 @@ public class IncomingHandler extends Handler { if (!hMessage.isBlocked()) { out.write(hMessage.getPacket().toBytes()); - out.flush(); } currentIndex++; } diff --git a/src/main/protocol/packethandler/OutgoingHandler.java b/src/main/protocol/packethandler/OutgoingHandler.java index ef6578d..ece5193 100644 --- a/src/main/protocol/packethandler/OutgoingHandler.java +++ b/src/main/protocol/packethandler/OutgoingHandler.java @@ -64,7 +64,6 @@ public class OutgoingHandler extends Handler { synchronized (lock) { try { out.write(servercipher.rc4(buffer)); - out.flush(); } catch (IOException e) { e.printStackTrace(); } @@ -104,7 +103,6 @@ public class OutgoingHandler extends Handler { currentIndex < encryptOffset ? hMessage.getPacket().toBytes() : servercipher.rc4(hMessage.getPacket().toBytes()) ); - out.flush(); } currentIndex ++; } diff --git a/src/main/ui/extensions/Extensions.java b/src/main/ui/extensions/Extensions.java index 1b9763d..a799501 100644 --- a/src/main/ui/extensions/Extensions.java +++ b/src/main/ui/extensions/Extensions.java @@ -139,16 +139,16 @@ public class Extensions extends SubForm { String stringified = message.stringify(); HPacket manipulatePacketRequest = new HPacket(OUTGOING_MESSAGES_IDS.PACKETINTERCEPT); - manipulatePacketRequest.appendString(stringified); + manipulatePacketRequest.appendLongString(stringified); boolean[] isblock = new boolean[1]; - for (GEarthExtension extension : gEarthExtensions) { + for (GEarthExtension extension : collection) { GEarthExtension.ReceiveMessageListener respondCallback = new GEarthExtension.ReceiveMessageListener() { @Override public void act(HPacket packet) { if (packet.headerId() == INCOMING_MESSAGES_IDS.MANIPULATEDPACKET) { - String stringifiedresponse = packet.readString(); + String stringifiedresponse = packet.readLongString(); HMessage responseMessage = new HMessage(stringifiedresponse); if (responseMessage.getDestination() == message.getDestination() && responseMessage.getIndex() == message.getIndex()) { if (!message.equals(responseMessage)) { @@ -157,7 +157,10 @@ public class Extensions extends SubForm { isblock[0] = true; } } - collection.remove(extension); + synchronized (collection) { + collection.remove(extension); + } + extension.removeOnReceiveMessageListener(this); } } @@ -171,12 +174,15 @@ public class Extensions extends SubForm { //block untill all extensions have responded List willdelete = new ArrayList<>(); while (!collection.isEmpty()) { - for (GEarthExtension extension : collection) { - if (!gEarthExtensions.contains(extension)) willdelete.add(extension); - } - for (int i = willdelete.size() - 1; i >= 0; i--) { - collection.remove(willdelete.get(i)); - willdelete.remove(i); + + synchronized (collection) { + for (GEarthExtension extension : collection) { + if (!gEarthExtensions.contains(extension)) willdelete.add(extension); + } + for (int i = willdelete.size() - 1; i >= 0; i--) { + collection.remove(willdelete.get(i)); + willdelete.remove(i); + } } try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();} diff --git a/src/main/ui/extensions/GEarthExtension.java b/src/main/ui/extensions/GEarthExtension.java index 252ad0a..9ff4fbe 100644 --- a/src/main/ui/extensions/GEarthExtension.java +++ b/src/main/ui/extensions/GEarthExtension.java @@ -5,9 +5,11 @@ import main.protocol.HPacket; import main.protocol.packethandler.PayloadBuffer; import java.io.DataInputStream; +import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.net.Socket; +import java.net.SocketException; import java.util.ArrayList; import java.util.List; @@ -28,8 +30,9 @@ public class GEarthExtension { new Thread(() -> { try { - connection.getOutputStream().write((new HPacket(Extensions.OUTGOING_MESSAGES_IDS.INFOREQUEST)).toBytes()); - connection.getOutputStream().flush(); + synchronized (connection.getOutputStream()) { + connection.getOutputStream().write((new HPacket(Extensions.OUTGOING_MESSAGES_IDS.INFOREQUEST)).toBytes()); + } InputStream inputStream = connection.getInputStream(); DataInputStream dIn = new DataInputStream(inputStream); @@ -38,9 +41,12 @@ public class GEarthExtension { int length = dIn.readInt(); byte[] headerandbody = new byte[length + 4]; - int amountRead = dIn.read(headerandbody, 4, length); - if (amountRead != length) break; + int amountRead = 0; + while (amountRead < length) { + amountRead += dIn.read(headerandbody, 4 + amountRead, Math.min(dIn.available(), length - amountRead)); + } + HPacket packet = new HPacket(headerandbody); packet.fixLength(); @@ -79,9 +85,12 @@ public class GEarthExtension { while (!connection.isClosed()) { int length = dIn.readInt(); byte[] headerandbody = new byte[length + 4]; - int amountRead = dIn.read(headerandbody, 4, length); - if (amountRead != length) break; + int amountRead = 0; + while (amountRead < length) { + amountRead += dIn.read(headerandbody, 4 + amountRead, Math.min(dIn.available(), length - amountRead)); + } + HPacket packet = new HPacket(headerandbody); packet.fixLength(); @@ -90,16 +99,16 @@ public class GEarthExtension { } } - onDisconnectedCallback.act(selff); } catch (IOException e) { - e.printStackTrace(); + // An extension disconnected, which is OK } finally { + onDisconnectedCallback.act(selff); if (!connection.isClosed()) { try { connection.close(); } catch (IOException e) { - e.printStackTrace(); +// e.printStackTrace(); } } } @@ -141,8 +150,9 @@ public class GEarthExtension { public boolean sendMessage(HPacket message) { try { - connection.getOutputStream().write(message.toBytes()); - connection.getOutputStream().flush(); + synchronized (connection.getOutputStream()) { + connection.getOutputStream().write(message.toBytes()); + } return true; } catch (IOException e) { return false;