diff --git a/G-Earth/src/main/java/gearth/protocol/HConnection.java b/G-Earth/src/main/java/gearth/protocol/HConnection.java index 94cb1a7..a07f102 100644 --- a/G-Earth/src/main/java/gearth/protocol/HConnection.java +++ b/G-Earth/src/main/java/gearth/protocol/HConnection.java @@ -6,10 +6,8 @@ import gearth.protocol.hostreplacer.HostReplacer; import gearth.protocol.hostreplacer.HostReplacerFactory; import gearth.protocol.memory.Rc4Obtainer; import gearth.protocol.misc.ConnectionInfoOverrider; -import gearth.protocol.packethandler.Handler; -import gearth.protocol.packethandler.IncomingHandler; -import gearth.protocol.packethandler.OutgoingHandler; -import org.json.JSONArray; +import gearth.protocol.packethandler.IncomingPacketHandler; +import gearth.protocol.packethandler.OutgoingPacketHandler; import java.io.*; import java.net.InetAddress; @@ -122,8 +120,8 @@ public class HConnection { private volatile ServerSocket proxy_server = null; //listener for the client - private volatile IncomingHandler inHandler = null; //connection with client (only initialized when verified habbo connection) - private volatile OutgoingHandler outHandler = null; //connection with server (only initialized when verified habbo connection) + private volatile IncomingPacketHandler inHandler = null; //connection with client (only initialized when verified habbo connection) + private volatile OutgoingPacketHandler outHandler = null; //connection with server (only initialized when verified habbo connection) public Proxy(String input_domain, String actual_domain, int actual_port, int intercept_port, String intercept_host) { @@ -138,7 +136,7 @@ public class HConnection { this.proxy_server = socket; } - public void verifyProxy(IncomingHandler incomingHandler, OutgoingHandler outgoingHandler) { + public void verifyProxy(IncomingPacketHandler incomingHandler, OutgoingPacketHandler outgoingHandler) { this.inHandler = incomingHandler; this.outHandler = outgoingHandler; } @@ -167,11 +165,11 @@ public class HConnection { return intercept_host; } - public IncomingHandler getInHandler() { + public IncomingPacketHandler getInHandler() { return inHandler; } - public OutgoingHandler getOutHandler() { + public OutgoingPacketHandler getOutHandler() { return outHandler; } } @@ -323,11 +321,9 @@ public class HConnection { final boolean[] aborted = new boolean[1]; Rc4Obtainer rc4Obtainer = new Rc4Obtainer(this); - OutgoingHandler outgoingHandler = new OutgoingHandler(habbo_server_out, trafficListeners); - rc4Obtainer.setOutgoingHandler(outgoingHandler); - - IncomingHandler incomingHandler = new IncomingHandler(client_out, trafficListeners); - rc4Obtainer.setIncomingHandler(incomingHandler); + OutgoingPacketHandler outgoingHandler = new OutgoingPacketHandler(habbo_server_out, trafficListeners); + IncomingPacketHandler incomingHandler = new IncomingPacketHandler(client_out, trafficListeners); + rc4Obtainer.setPacketHandlers(outgoingHandler, incomingHandler); outgoingHandler.addOnDatastreamConfirmedListener(hotelVersion -> { incomingHandler.setAsDataStream(); diff --git a/G-Earth/src/main/java/gearth/protocol/memory/Rc4Obtainer.java b/G-Earth/src/main/java/gearth/protocol/memory/Rc4Obtainer.java index 8af8793..3c7526a 100644 --- a/G-Earth/src/main/java/gearth/protocol/memory/Rc4Obtainer.java +++ b/G-Earth/src/main/java/gearth/protocol/memory/Rc4Obtainer.java @@ -7,10 +7,7 @@ import gearth.protocol.HPacket; import gearth.protocol.crypto.RC4; import gearth.protocol.memory.habboclient.HabboClient; import gearth.protocol.memory.habboclient.HabboClientFactory; -import gearth.protocol.packethandler.Handler; -import gearth.protocol.packethandler.IncomingHandler; -import gearth.protocol.packethandler.OutgoingHandler; -import gearth.protocol.packethandler.PayloadBuffer; +import gearth.protocol.packethandler.*; import javafx.application.Platform; import javafx.scene.control.Alert; import javafx.scene.control.ButtonType; @@ -22,47 +19,45 @@ import javafx.scene.web.WebView; import java.util.Arrays; import java.util.List; +import java.util.function.Consumer; public class Rc4Obtainer { public static final boolean DEBUG = false; - HabboClient client = null; - OutgoingHandler outgoingHandler = null; - IncomingHandler incomingHandler = null; + private HabboClient client; + private List packetHandlers; public Rc4Obtainer(HConnection hConnection) { client = HabboClientFactory.get(hConnection); } - private boolean hashappened1 = false; - public void setOutgoingHandler(OutgoingHandler handler) { - outgoingHandler = handler; - handler.addBufferListener((int addedbytes) -> { - if (!hashappened1 && handler.isEncryptedStream()) { - hashappened1 = true; - onSendFirstEncryptedMessage(outgoingHandler); - } - }); - } - private boolean hashappened2 = false; - public void setIncomingHandler(IncomingHandler handler) { - incomingHandler = handler; - handler.addBufferListener((int addedbytes) -> { - if (!hashappened2 && handler.isEncryptedStream()) { - hashappened2 = true; - onSendFirstEncryptedMessage(incomingHandler); - } - }); + public void setPacketHandlers(PacketHandler... packetHandlers) { + this.packetHandlers = Arrays.asList(packetHandlers); + + for (PacketHandler handler : packetHandlers) { + BufferChangeListener bufferChangeListener = new BufferChangeListener() { + @Override + public void act() { + if (handler.isEncryptedStream()) { + onSendFirstEncryptedMessage(handler); + handler.removeBufferChangeListener(this); + } + } + }; + handler.onBufferChanged(bufferChangeListener); + } + + } - private void onSendFirstEncryptedMessage(Handler handler) { + + private void onSendFirstEncryptedMessage(PacketHandler packetHandler) { if (!HConnection.DECRYPTPACKETS) return; - outgoingHandler.block(); - incomingHandler.block(); + packetHandlers.forEach(PacketHandler::block); new Thread(() -> { @@ -72,8 +67,8 @@ public class Rc4Obtainer { int i = 0; while (!worked && i < 4) { worked = (i % 2 == 0) ? - onSendFirstEncryptedMessage(handler, client.getRC4cached()) : - onSendFirstEncryptedMessage(handler, client.getRC4possibilities()); + onSendFirstEncryptedMessage(packetHandler, client.getRC4cached()) : + onSendFirstEncryptedMessage(packetHandler, client.getRC4possibilities()); i++; } @@ -107,17 +102,16 @@ public class Rc4Obtainer { } - incomingHandler.unblock(); - outgoingHandler.unblock(); + packetHandlers.forEach(PacketHandler::unblock); }).start(); } - private boolean onSendFirstEncryptedMessage(Handler handler, List potentialRC4tables) { + private boolean onSendFirstEncryptedMessage(PacketHandler packetHandler, List potentialRC4tables) { for (byte[] possible : potentialRC4tables) { - byte[] encBuffer = new byte[handler.getEncryptedBuffer().size()]; + byte[] encBuffer = new byte[packetHandler.getEncryptedBuffer().size()]; for (int i = 0; i < encBuffer.length; i++) { - encBuffer[i] = handler.getEncryptedBuffer().get(i); + encBuffer[i] = packetHandler.getEncryptedBuffer().get(i); } for (int i = 0; i < 256; i++) { @@ -125,7 +119,7 @@ public class Rc4Obtainer { byte[] keycpy = Arrays.copyOf(possible, possible.length); RC4 rc4Tryout = new RC4(keycpy, i, j); - if (handler.getMessageSide() == HMessage.Side.TOSERVER) rc4Tryout.undoRc4(encBuffer); + if (packetHandler.getMessageSide() == HMessage.Side.TOSERVER) rc4Tryout.undoRc4(encBuffer); if (rc4Tryout.couldBeFresh()) { byte[] encDataCopy = Arrays.copyOf(encBuffer, encBuffer.length); RC4 rc4TryCopy = rc4Tryout.deepCopy(); @@ -136,7 +130,7 @@ public class Rc4Obtainer { HPacket[] checker = payloadBuffer.pushAndReceive(decoded); if (payloadBuffer.peak().length == 0) { - handler.setRc4(rc4Tryout); + packetHandler.setRc4(rc4Tryout); return true; } diff --git a/G-Earth/src/main/java/gearth/protocol/packethandler/BufferChangeListener.java b/G-Earth/src/main/java/gearth/protocol/packethandler/BufferChangeListener.java new file mode 100644 index 0000000..d004cb8 --- /dev/null +++ b/G-Earth/src/main/java/gearth/protocol/packethandler/BufferChangeListener.java @@ -0,0 +1,8 @@ +package gearth.protocol.packethandler; + + +public interface BufferChangeListener { + + void act(); + +} diff --git a/G-Earth/src/main/java/gearth/protocol/packethandler/BufferListener.java b/G-Earth/src/main/java/gearth/protocol/packethandler/BufferListener.java deleted file mode 100644 index ce79c49..0000000 --- a/G-Earth/src/main/java/gearth/protocol/packethandler/BufferListener.java +++ /dev/null @@ -1,8 +0,0 @@ -package gearth.protocol.packethandler; - - -public interface BufferListener { - - void act(int addedbytes); - -} diff --git a/G-Earth/src/main/java/gearth/protocol/packethandler/IncomingHandler.java b/G-Earth/src/main/java/gearth/protocol/packethandler/IncomingPacketHandler.java similarity index 91% rename from G-Earth/src/main/java/gearth/protocol/packethandler/IncomingHandler.java rename to G-Earth/src/main/java/gearth/protocol/packethandler/IncomingPacketHandler.java index 0ecbfa2..a94d849 100644 --- a/G-Earth/src/main/java/gearth/protocol/packethandler/IncomingHandler.java +++ b/G-Earth/src/main/java/gearth/protocol/packethandler/IncomingPacketHandler.java @@ -8,9 +8,9 @@ import java.io.IOException; import java.io.OutputStream; import java.util.List; -public class IncomingHandler extends Handler { +public class IncomingPacketHandler extends PacketHandler { - public IncomingHandler(OutputStream outputStream, Object[] listeners) { + public IncomingPacketHandler(OutputStream outputStream, Object[] listeners) { super(outputStream, listeners); TrafficListener listener = new TrafficListener() { diff --git a/G-Earth/src/main/java/gearth/protocol/packethandler/OutgoingHandler.java b/G-Earth/src/main/java/gearth/protocol/packethandler/OutgoingPacketHandler.java similarity index 92% rename from G-Earth/src/main/java/gearth/protocol/packethandler/OutgoingHandler.java rename to G-Earth/src/main/java/gearth/protocol/packethandler/OutgoingPacketHandler.java index ff65bcc..f397b58 100644 --- a/G-Earth/src/main/java/gearth/protocol/packethandler/OutgoingHandler.java +++ b/G-Earth/src/main/java/gearth/protocol/packethandler/OutgoingPacketHandler.java @@ -8,9 +8,9 @@ import java.io.OutputStream; import java.util.ArrayList; import java.util.List; -public class OutgoingHandler extends Handler { +public class OutgoingPacketHandler extends PacketHandler { - public OutgoingHandler(OutputStream outputStream, Object[] listeners) { + public OutgoingPacketHandler(OutputStream outputStream, Object[] listeners) { super(outputStream, listeners); } diff --git a/G-Earth/src/main/java/gearth/protocol/packethandler/Handler.java b/G-Earth/src/main/java/gearth/protocol/packethandler/PacketHandler.java similarity index 88% rename from G-Earth/src/main/java/gearth/protocol/packethandler/Handler.java rename to G-Earth/src/main/java/gearth/protocol/packethandler/PacketHandler.java index 1d7888e..e479c53 100644 --- a/G-Earth/src/main/java/gearth/protocol/packethandler/Handler.java +++ b/G-Earth/src/main/java/gearth/protocol/packethandler/PacketHandler.java @@ -11,7 +11,7 @@ import java.io.OutputStream; import java.util.ArrayList; import java.util.List; -public abstract class Handler { +public abstract class PacketHandler { protected static final boolean DEBUG = false; @@ -31,7 +31,7 @@ public abstract class Handler { protected volatile boolean isEncryptedStream = false; - public Handler(OutputStream outputStream, Object[] listeners) { + public PacketHandler(OutputStream outputStream, Object[] listeners) { this.listeners = listeners; out = outputStream; } @@ -47,7 +47,7 @@ public abstract class Handler { public abstract void act(byte[] buffer) throws IOException; protected void continuedAct(byte[] buffer) throws IOException { - notifyBufferListeners(buffer.length); + notifyBufferListeners(); if (!isEncryptedStream) { payloadBuffer.push(buffer); @@ -164,16 +164,16 @@ public abstract class Handler { protected abstract void printForDebugging(byte[] bytes); - private List bufferListeners = new ArrayList<>(); - public void addBufferListener(BufferListener listener) { - bufferListeners.add(listener); + private List bufferChangeListeners = new ArrayList<>(); + public void onBufferChanged(BufferChangeListener listener) { + bufferChangeListeners.add(listener); } - public void removeBufferListener(BufferListener listener) { - bufferListeners.remove(listener); + public void removeBufferChangeListener(BufferChangeListener listener) { + bufferChangeListeners.remove(listener); } - void notifyBufferListeners(int addedbytes) { - for (int i = bufferListeners.size() - 1; i >= 0; i -= 1) { - bufferListeners.get(i).act(addedbytes); + void notifyBufferListeners() { + for (int i = bufferChangeListeners.size() - 1; i >= 0; i -= 1) { + bufferChangeListeners.get(i).act(); } }