Add Shockwave support to extensions

This commit is contained in:
UnfamiliarLegacy 2024-06-22 06:25:37 +02:00
parent b66968435f
commit deec521ab1
16 changed files with 396 additions and 68 deletions

View File

@ -35,7 +35,10 @@ public class VL64Encoding {
} }
public static int decode(byte[] bzData) { public static int decode(byte[] bzData) {
int pos = 0; return decode(bzData, 0);
}
public static int decode(byte[] bzData, int pos) {
int v = 0; int v = 0;
boolean negative = (bzData[pos] & 4) == 4; boolean negative = (bzData[pos] & 4) == 4;

View File

@ -1,6 +1,7 @@
package gearth.extensions; package gearth.extensions;
import gearth.misc.HostInfo; import gearth.misc.HostInfo;
import gearth.protocol.HPacketFormat;
import gearth.services.extension_handler.extensions.implementations.network.NetworkExtensionMessage; import gearth.services.extension_handler.extensions.implementations.network.NetworkExtensionMessage;
import gearth.services.extension_handler.extensions.implementations.network.NetworkExtensionMessage.Incoming; import gearth.services.extension_handler.extensions.implementations.network.NetworkExtensionMessage.Incoming;
import gearth.services.extension_handler.extensions.implementations.network.NetworkExtensionMessage.Outgoing; import gearth.services.extension_handler.extensions.implementations.network.NetworkExtensionMessage.Outgoing;
@ -174,12 +175,14 @@ public abstract class Extension extends ExtensionBase {
} }
else if (packet.headerId() == Outgoing.PacketIntercept.HEADER_ID) { else if (packet.headerId() == Outgoing.PacketIntercept.HEADER_ID) {
String stringifiedMessage = packet.readLongString(); String stringifiedMessage = packet.readLongString();
HMessage habboMessage = new HMessage(stringifiedMessage); HPacketFormat packetFormat = HPacketFormat.fromId(packet.readInteger());
HMessage habboMessage = new HMessage(packetFormat, stringifiedMessage);
modifyMessage(habboMessage); modifyMessage(habboMessage);
HPacket response = new HPacket(Incoming.ManipulatedPacket.MANIPULATED_PACKET); HPacket response = new HPacket(Incoming.ManipulatedPacket.MANIPULATED_PACKET);
response.appendLongString(habboMessage.stringify()); response.appendLongString(habboMessage.stringify());
response.appendInt(habboMessage.getPacket().getFormat().getId());
writeToStream(response.toBytes()); writeToStream(response.toBytes());
} }
@ -237,6 +240,7 @@ public abstract class Extension extends ExtensionBase {
packet1.appendByte(direction == HMessage.Direction.TOCLIENT ? (byte)0 : (byte)1); packet1.appendByte(direction == HMessage.Direction.TOCLIENT ? (byte)0 : (byte)1);
packet1.appendInt(packet.getBytesLength()); packet1.appendInt(packet.getBytesLength());
packet1.appendBytes(packet.toBytes()); packet1.appendBytes(packet.toBytes());
packet1.appendInt(packet.getFormat().getId());
try { try {
writeToStream(packet1.toBytes()); writeToStream(packet1.toBytes());
return true; return true;

View File

@ -15,7 +15,9 @@ public class HMessage implements StringifyAble {
private boolean isBlocked; private boolean isBlocked;
public HMessage(String fromString) { public HMessage(HPacketFormat format, String fromString) {
// A little bit hacky to get the correct packet class inside constructFromString.
this.hPacket = format.createPacket(0);
constructFromString(fromString); constructFromString(fromString);
} }
@ -65,9 +67,8 @@ public class HMessage implements StringifyAble {
this.isBlocked = parts[0].equals("1"); this.isBlocked = parts[0].equals("1");
this.index = Integer.parseInt(parts[1]); this.index = Integer.parseInt(parts[1]);
this.direction = parts[2].equals("TOCLIENT") ? Direction.TOCLIENT : Direction.TOSERVER; this.direction = parts[2].equals("TOCLIENT") ? Direction.TOCLIENT : Direction.TOSERVER;
HPacket p = new HPacket(new byte[0]); this.hPacket = hPacket.getFormat().createPacket(0);
p.constructFromString(parts[3]); this.hPacket.constructFromString(parts[3]);
this.hPacket = p;
} }
public void constructFromHMessage(HMessage message) { public void constructFromHMessage(HMessage message) {

View File

@ -1,6 +1,5 @@
package gearth.protocol; package gearth.protocol;
import gearth.extensions.parsers.HDirection;
import gearth.misc.StringifyAble; import gearth.misc.StringifyAble;
import gearth.services.packet_info.PacketInfo; import gearth.services.packet_info.PacketInfo;
import gearth.services.packet_info.PacketInfoManager; import gearth.services.packet_info.PacketInfoManager;
@ -17,6 +16,7 @@ import java.util.Optional;
public class HPacket implements StringifyAble { public class HPacket implements StringifyAble {
protected boolean isEdited = false; protected boolean isEdited = false;
protected HPacketFormat packetFormat = HPacketFormat.EVA_WIRE;
protected byte[] packetInBytes; protected byte[] packetInBytes;
protected int readIndex = 6; protected int readIndex = 6;
@ -83,7 +83,9 @@ public class HPacket implements StringifyAble {
isEdited = false; isEdited = false;
} }
public HPacketFormat getFormat() {
return packetFormat;
}
public String toString() { public String toString() {
return PacketStringUtils.toString(packetInBytes); return PacketStringUtils.toString(packetInBytes);

View File

@ -1,15 +1,39 @@
package gearth.protocol; package gearth.protocol;
import gearth.extensions.parsers.HDirection;
import gearth.protocol.connection.HClient; import gearth.protocol.connection.HClient;
import gearth.protocol.packethandler.shockwave.packets.ShockPacketIncoming; import gearth.protocol.packethandler.shockwave.packets.ShockPacketIncoming;
import gearth.protocol.packethandler.shockwave.packets.ShockPacketOutgoing; import gearth.protocol.packethandler.shockwave.packets.ShockPacketOutgoing;
import java.util.HashMap;
import java.util.Map;
public enum HPacketFormat { public enum HPacketFormat {
EVA_WIRE, EVA_WIRE(0),
WEDGIE_INCOMING, WEDGIE_INCOMING(1),
WEDGIE_OUTGOING; WEDGIE_OUTGOING(2);
private static final Map<Integer, HPacketFormat> ID_MAP = new HashMap<>();
static {
for (HPacketFormat format : values()) {
ID_MAP.put(format.id, format);
}
}
public static HPacketFormat fromId(int id) {
return ID_MAP.get(id);
}
private final int id;
HPacketFormat(int id) {
this.id = id;
}
public int getId() {
return id;
}
public static HPacketFormat getFormat(HClient client, HMessage.Direction direction) { public static HPacketFormat getFormat(HClient client, HMessage.Direction direction) {
if (client != HClient.SHOCKWAVE) { if (client != HClient.SHOCKWAVE) {

View File

@ -3,6 +3,7 @@ package gearth.protocol.packethandler.shockwave.buffers;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.protocol.packethandler.ByteArrayUtils; import gearth.protocol.packethandler.ByteArrayUtils;
import gearth.protocol.packethandler.shockwave.packets.ShockPacket; import gearth.protocol.packethandler.shockwave.packets.ShockPacket;
import gearth.protocol.packethandler.shockwave.packets.ShockPacketIncoming;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -31,7 +32,7 @@ public class ShockwaveInBuffer implements ShockwaveBuffer {
for (int i = 0; i < buffer.length; i++) { for (int i = 0; i < buffer.length; i++) {
if (buffer[i] == 1) { if (buffer[i] == 1) {
byte[] packetData = Arrays.copyOfRange(buffer, curPos, i); byte[] packetData = Arrays.copyOfRange(buffer, curPos, i);
packets.add(new ShockPacket(packetData)); packets.add(new ShockPacketIncoming(packetData));
curPos = i + 1; curPos = i + 1;
} }
} }

View File

@ -4,6 +4,7 @@ import gearth.encoding.Base64Encoding;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.protocol.packethandler.ByteArrayUtils; import gearth.protocol.packethandler.ByteArrayUtils;
import gearth.protocol.packethandler.shockwave.packets.ShockPacket; import gearth.protocol.packethandler.shockwave.packets.ShockPacket;
import gearth.protocol.packethandler.shockwave.packets.ShockPacketOutgoing;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -39,7 +40,7 @@ public class ShockwaveOutBuffer implements ShockwaveBuffer {
int endPos = length + PACKET_LENGTH_SIZE; int endPos = length + PACKET_LENGTH_SIZE;
byte[] packet = Arrays.copyOfRange(buffer, PACKET_LENGTH_SIZE, endPos); byte[] packet = Arrays.copyOfRange(buffer, PACKET_LENGTH_SIZE, endPos);
out.add(new ShockPacket(packet)); out.add(new ShockPacketOutgoing(packet));
buffer = Arrays.copyOfRange(buffer, endPos, buffer.length); buffer = Arrays.copyOfRange(buffer, endPos, buffer.length);
} }

View File

@ -10,24 +10,28 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
public class ShockPacket extends HPacket { public abstract class ShockPacket extends HPacket {
public ShockPacket(byte[] packet) { public ShockPacket(HPacketFormat format, byte[] packet) {
super(packet); super(packet);
packetFormat = format;
readIndex = 2; readIndex = 2;
} }
public ShockPacket(HPacket packet) { public ShockPacket(HPacketFormat format, HPacket packet) {
super(packet); super(packet);
packetFormat = format;
readIndex = 2; readIndex = 2;
} }
public ShockPacket(String packet, HPacketFormat format) { public ShockPacket(HPacketFormat format, String packet) {
super(packet, format); super(packet, format);
packetFormat = format;
readIndex = 2; readIndex = 2;
} }
public ShockPacket(int header) { public ShockPacket(HPacketFormat format, int header) {
super(Base64Encoding.encode(header, 2)); super(Base64Encoding.encode(header, 2));
packetFormat = format;
readIndex = 2; readIndex = 2;
} }
@ -106,6 +110,136 @@ public class ShockPacket extends HPacket {
throw new ShockPacketUnsupported(); throw new ShockPacketUnsupported();
} }
@Override
public byte readByte() {
throw new ShockPacketUnsupported();
}
@Override
public byte readByte(int index) {
throw new ShockPacketUnsupported();
}
@Override
public short readShort() {
throw new ShockPacketUnsupported();
}
@Override
public short readShort(int index) {
throw new ShockPacketUnsupported();
}
@Override
public int readUshort() {
throw new ShockPacketUnsupported();
}
@Override
public int readUshort(int index) {
throw new ShockPacketUnsupported();
}
@Override
public int readInteger() {
throw new ShockPacketUnsupported();
}
@Override
public int readInteger(int index) {
throw new ShockPacketUnsupported();
}
@Override
public double readDouble() {
throw new ShockPacketUnsupported();
}
@Override
public double readDouble(int index) {
throw new ShockPacketUnsupported();
}
@Override
public float readFloat() {
throw new ShockPacketUnsupported();
}
@Override
public float readFloat(int index) {
throw new ShockPacketUnsupported();
}
@Override
public byte[] readBytes(int length) {
return super.readBytes(length);
}
@Override
public byte[] readBytes(int length, int index) {
return super.readBytes(length, index);
}
@Override
public long readLong() {
throw new ShockPacketUnsupported();
}
@Override
public long readLong(int index) {
throw new ShockPacketUnsupported();
}
@Override
public String readString(Charset charset) {
throw new ShockPacketUnsupported();
}
@Override
public String readString(int index, Charset charset) {
throw new ShockPacketUnsupported();
}
@Override
public String readString() {
throw new ShockPacketUnsupported();
}
@Override
public String readString(int index) {
throw new ShockPacketUnsupported();
}
@Override
public String readLongString(Charset charset) {
throw new ShockPacketUnsupported();
}
@Override
public String readLongString(int index, Charset charset) {
throw new ShockPacketUnsupported();
}
@Override
public String readLongString() {
throw new ShockPacketUnsupported();
}
@Override
public String readLongString(int index) {
throw new ShockPacketUnsupported();
}
@Override
public boolean readBoolean() {
throw new ShockPacketUnsupported();
}
@Override
public boolean readBoolean(int index) {
throw new ShockPacketUnsupported();
}
@Override @Override
protected void replacePacketId(short headerId) { protected void replacePacketId(short headerId) {
final byte[] header = Base64Encoding.encode(headerId, 2); final byte[] header = Base64Encoding.encode(headerId, 2);
@ -128,8 +262,8 @@ public class ShockPacket extends HPacket {
} }
@Override @Override
public HPacket copy() { public void resetReadIndex() {
return new ShockPacket(this); setReadIndex(2);
} }
@Override @Override

View File

@ -1,42 +1,48 @@
package gearth.protocol.packethandler.shockwave.packets; package gearth.protocol.packethandler.shockwave.packets;
import gearth.encoding.Base64Encoding;
import gearth.encoding.VL64Encoding; import gearth.encoding.VL64Encoding;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.protocol.HPacketFormat; import gearth.protocol.HPacketFormat;
import org.apache.commons.lang3.ArrayUtils;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays;
// Server to Client // Server to Client
public class ShockPacketIncoming extends ShockPacket { public class ShockPacketIncoming extends ShockPacket {
public ShockPacketIncoming(byte[] packet) { public ShockPacketIncoming(byte[] packet) {
super(packet); super(HPacketFormat.WEDGIE_INCOMING, packet);
} }
public ShockPacketIncoming(HPacket packet) { public ShockPacketIncoming(HPacket packet) {
super(packet); super(HPacketFormat.WEDGIE_INCOMING, packet);
} }
public ShockPacketIncoming(String packet) { public ShockPacketIncoming(String packet) {
super(packet, HPacketFormat.WEDGIE_INCOMING); super(HPacketFormat.WEDGIE_INCOMING, packet);
} }
public ShockPacketIncoming(int header) { public ShockPacketIncoming(int header) {
super(header); super(HPacketFormat.WEDGIE_INCOMING, header);
} }
@Override @Override
public HPacket appendBoolean(boolean b) { public HPacket appendBoolean(boolean value) {
isEdited = true; isEdited = true;
appendBytes(VL64Encoding.encode(b ? 1 : 0)); appendBytes(VL64Encoding.encode(value ? 1 : 0));
return this; return this;
} }
@Override @Override
public HPacket appendUShort(int ushort) { public HPacket appendShort(short value) {
return appendUShort(value);
}
@Override
public HPacket appendUShort(int value) {
isEdited = true; isEdited = true;
appendBytes(VL64Encoding.encode(ushort)); appendBytes(VL64Encoding.encode(value));
return this; return this;
} }
@ -48,17 +54,69 @@ public class ShockPacketIncoming extends ShockPacket {
} }
@Override @Override
public HPacket appendString(String s) { public HPacket appendString(String value) {
return appendString(s, StandardCharsets.ISO_8859_1); return appendString(value, StandardCharsets.ISO_8859_1);
} }
@Override @Override
public HPacket appendString(String s, Charset charset) { public HPacket appendString(String value, Charset charset) {
isEdited = true; isEdited = true;
final byte[] data = s.getBytes(charset); final byte[] data = value.getBytes(charset);
appendBytes(data); appendBytes(data);
appendByte((byte) 2); appendByte((byte) 2);
return this; return this;
} }
@Override
public boolean readBoolean() {
return readInteger() == 1;
}
@Override
public short readShort() {
return (short) readInteger();
}
@Override
public int readUshort() {
return this.readShort();
}
@Override
public int readInteger() {
int length = packetInBytes[readIndex] >> 3 & 7;
int value = VL64Encoding.decode(packetInBytes, readIndex);
readIndex += length;
return value;
}
@Override
public String readString() {
return this.readString(StandardCharsets.UTF_8);
}
@Override
public String readString(Charset charset) {
String result;
int startPos = readIndex;
int endPos = ArrayUtils.indexOf(packetInBytes, (byte) 2, startPos);
if (endPos > 0) {
result = new String(Arrays.copyOfRange(packetInBytes, startPos, endPos), charset);
} else {
result = "";
}
readIndex = endPos + 1;
return result;
}
@Override
public HPacket copy() {
return new ShockPacketIncoming(this);
}
} }

View File

@ -11,30 +11,35 @@ import java.nio.charset.StandardCharsets;
// Client to Server // Client to Server
public class ShockPacketOutgoing extends ShockPacket { public class ShockPacketOutgoing extends ShockPacket {
public ShockPacketOutgoing(byte[] packet) { public ShockPacketOutgoing(byte[] packet) {
super(packet); super(HPacketFormat.WEDGIE_OUTGOING, packet);
} }
public ShockPacketOutgoing(HPacket packet) { public ShockPacketOutgoing(HPacket packet) {
super(packet); super(HPacketFormat.WEDGIE_OUTGOING, packet);
} }
public ShockPacketOutgoing(String packet) { public ShockPacketOutgoing(String packet) {
super(packet, HPacketFormat.WEDGIE_OUTGOING); super(HPacketFormat.WEDGIE_OUTGOING, packet);
} }
public ShockPacketOutgoing(int header) { public ShockPacketOutgoing(int header) {
super(header); super(HPacketFormat.WEDGIE_OUTGOING, header);
} }
@Override @Override
public HPacket appendBoolean(boolean b) { public HPacket appendBoolean(boolean value) {
return appendInt(b ? 1 : 0); return appendInt(value ? 1 : 0);
} }
@Override @Override
public HPacket appendUShort(int ushort) { public HPacket appendShort(short value) {
return appendUShort(value);
}
@Override
public HPacket appendUShort(int value) {
isEdited = true; isEdited = true;
appendBytes(Base64Encoding.encode(ushort, 2)); appendBytes(Base64Encoding.encode(value, 2));
return this; return this;
} }
@ -46,17 +51,60 @@ public class ShockPacketOutgoing extends ShockPacket {
} }
@Override @Override
public HPacket appendString(String s) { public HPacket appendString(String value) {
return appendString(s, StandardCharsets.ISO_8859_1); return appendString(value, StandardCharsets.ISO_8859_1);
} }
@Override @Override
public HPacket appendString(String s, Charset charset) { public HPacket appendString(String value, Charset charset) {
isEdited = true; isEdited = true;
final byte[] data = s.getBytes(charset); final byte[] data = value.getBytes(charset);
appendUShort(data.length); appendUShort(data.length);
appendBytes(data); appendBytes(data);
return this; return this;
} }
@Override
public boolean readBoolean() {
return readInteger() == 1;
}
@Override
public short readShort() {
return (short) Base64Encoding.decode(readBytes(2));
}
@Override
public int readUshort() {
return this.readShort();
}
@Override
public int readInteger() {
int length = packetInBytes[readIndex] >> 3 & 7;
int value = VL64Encoding.decode(packetInBytes, readIndex);
readIndex += length;
return value;
}
@Override
public String readString() {
return this.readString(StandardCharsets.UTF_8);
}
@Override
public String readString(Charset charset) {
int length = readUshort();
byte[] data = readBytes(length);
return new String(data, charset);
}
@Override
public HPacket copy() {
return new ShockPacketOutgoing(this);
}
} }

View File

@ -6,18 +6,17 @@ import gearth.misc.listenerpattern.Observable;
import gearth.protocol.HConnection; import gearth.protocol.HConnection;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.protocol.HPacketFormat;
import gearth.protocol.connection.HState; import gearth.protocol.connection.HState;
import gearth.services.extension_handler.extensions.ExtensionListener; import gearth.services.extension_handler.extensions.ExtensionListener;
import gearth.services.extension_handler.extensions.GEarthExtension; import gearth.services.extension_handler.extensions.GEarthExtension;
import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducer; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducer;
import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducerFactory; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducerFactory;
import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducerObserver; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducerObserver;
import gearth.ui.themes.Theme;
import javafx.util.Pair; import javafx.util.Pair;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.function.Consumer;
public class ExtensionHandler { public class ExtensionHandler {
@ -244,7 +243,7 @@ public class ExtensionHandler {
} }
@Override @Override
protected void stringToPacketRequest(String string) { protected void stringToPacketRequest(String string, HPacketFormat format) {
HPacket packet = new HPacket(string); HPacket packet = new HPacket(string);
if (!packet.isPacketComplete()) packet.completePacket(hConnection.getPacketInfoManager()); if (!packet.isPacketComplete()) packet.completePacket(hConnection.getPacketInfoManager());
extension.stringToPacketResponse(packet); extension.stringToPacketResponse(packet);

View File

@ -2,6 +2,7 @@ package gearth.services.extension_handler.extensions;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.protocol.HPacketFormat;
public abstract class ExtensionListener { public abstract class ExtensionListener {
@ -13,6 +14,11 @@ public abstract class ExtensionListener {
protected void hasClosed() {} protected void hasClosed() {}
protected void packetToStringRequest(HPacket packet) {} protected void packetToStringRequest(HPacket packet) {}
protected void stringToPacketRequest(String string) {} @Deprecated
protected void stringToPacketRequest(String string) {
// Kept for backwards compatibility with old extensions
stringToPacketRequest(string, HPacketFormat.EVA_WIRE);
}
protected void stringToPacketRequest(String string, HPacketFormat format) {}
} }

View File

@ -3,6 +3,7 @@ package gearth.services.extension_handler.extensions;
import gearth.misc.HostInfo; import gearth.misc.HostInfo;
import gearth.misc.listenerpattern.Observable; import gearth.misc.listenerpattern.Observable;
import gearth.misc.listenerpattern.SynchronizedObservable; import gearth.misc.listenerpattern.SynchronizedObservable;
import gearth.protocol.HPacketFormat;
import gearth.services.packet_info.PacketInfoManager; import gearth.services.packet_info.PacketInfoManager;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
@ -92,8 +93,8 @@ public abstract class GEarthExtension {
}); });
packet.setReadIndex(orgIndex); packet.setReadIndex(orgIndex);
} }
protected void stringToPacketRequest(String string) { protected void stringToPacketRequest(String string, HPacketFormat format) {
extensionObservable.fireEvent(l -> l.stringToPacketRequest(string)); extensionObservable.fireEvent(l -> l.stringToPacketRequest(string, format));
} }
// -------------------------------------------------------------------- // --------------------------------------------------------------------

View File

@ -3,6 +3,7 @@ package gearth.services.extension_handler.extensions.implementations.network;
import gearth.misc.HostInfo; import gearth.misc.HostInfo;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.protocol.HPacketFormat;
import gearth.protocol.connection.HClient; import gearth.protocol.connection.HClient;
import gearth.services.extension_handler.extensions.ExtensionType; import gearth.services.extension_handler.extensions.ExtensionType;
import gearth.services.extension_handler.extensions.GEarthExtension; import gearth.services.extension_handler.extensions.GEarthExtension;
@ -76,11 +77,13 @@ public final class NetworkExtensionClient extends GEarthExtension {
} else if (incoming instanceof Incoming.ExtensionConsoleLog) { } else if (incoming instanceof Incoming.ExtensionConsoleLog) {
log(((Incoming.ExtensionConsoleLog) incoming).getContents()); log(((Incoming.ExtensionConsoleLog) incoming).getContents());
} else if (incoming instanceof Incoming.PacketToStringRequest) { } else if (incoming instanceof Incoming.PacketToStringRequest) {
final HPacket hPacket = new HPacket(new byte[0]); final Incoming.PacketToStringRequest incomingPacket = (Incoming.PacketToStringRequest) incoming;
final HPacket hPacket = incomingPacket.getFormat().createPacket(0);
hPacket.constructFromString(((Incoming.PacketToStringRequest) incoming).getString()); hPacket.constructFromString(((Incoming.PacketToStringRequest) incoming).getString());
packetToStringRequest(hPacket); packetToStringRequest(hPacket);
} else if (incoming instanceof Incoming.StringToPacketRequest) { } else if (incoming instanceof Incoming.StringToPacketRequest) {
stringToPacketRequest(((Incoming.StringToPacketRequest) incoming).getString()); final Incoming.StringToPacketRequest incomingPacket = (Incoming.StringToPacketRequest) incoming;
stringToPacketRequest(incomingPacket.getString(), incomingPacket.getFormat());
} }
} catch (Exception e){ } catch (Exception e){
LOGGER.error("Failed to handle incoming message {} (channel={})", incoming, channel, e); LOGGER.error("Failed to handle incoming message {} (channel={})", incoming, channel, e);
@ -138,7 +141,9 @@ public final class NetworkExtensionClient extends GEarthExtension {
@Override @Override
public void packetIntercept(HMessage hMessage) { public void packetIntercept(HMessage hMessage) {
final String messageAsString = hMessage.stringify(); final String messageAsString = hMessage.stringify();
channel.writeAndFlush(new Outgoing.PacketIntercept(messageAsString)); final HPacketFormat packetFormat = hMessage.getPacket().getFormat();
channel.writeAndFlush(new Outgoing.PacketIntercept(messageAsString, packetFormat));
} }
@Override @Override

View File

@ -3,6 +3,7 @@ package gearth.services.extension_handler.extensions.implementations.network;
import gearth.misc.HostInfo; import gearth.misc.HostInfo;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.protocol.HPacketFormat;
import gearth.protocol.connection.HClient; import gearth.protocol.connection.HClient;
import gearth.services.extension_handler.extensions.implementations.network.NetworkExtensionMessage.Outgoing; import gearth.services.extension_handler.extensions.implementations.network.NetworkExtensionMessage.Outgoing;
import gearth.services.extension_handler.extensions.implementations.network.NetworkExtensionMessage.Incoming; import gearth.services.extension_handler.extensions.implementations.network.NetworkExtensionMessage.Incoming;
@ -141,10 +142,14 @@ public final class NetworkExtensionCodec {
})); }));
register(Incoming.ManipulatedPacket.MANIPULATED_PACKET, register(Incoming.ManipulatedPacket.MANIPULATED_PACKET,
Incoming.ManipulatedPacket.class, Incoming.ManipulatedPacket.class,
(message, hPacket) -> hPacket.appendLongString(message.gethMessage().stringify()), (message, hPacket) -> {
hPacket.appendLongString(message.gethMessage().stringify());
hPacket.appendInt(message.gethMessage().getPacket().getFormat().getId());
},
(hPacket -> { (hPacket -> {
final String packetString = hPacket.readLongString(6); final String packetString = hPacket.readLongString();
final HMessage hMessage = new HMessage(packetString); final HPacketFormat packetFormat = hPacket.isEOF() == 0 ? HPacketFormat.fromId(hPacket.readInteger()) : HPacketFormat.EVA_WIRE;
final HMessage hMessage = new HMessage(packetFormat, packetString);
return new Incoming.ManipulatedPacket(hMessage); return new Incoming.ManipulatedPacket(hMessage);
})); }));
register(Incoming.SendMessage.HEADER_ID, register(Incoming.SendMessage.HEADER_ID,
@ -153,12 +158,14 @@ public final class NetworkExtensionCodec {
hPacket.appendByte((byte) (message.getDirection() == TOCLIENT ? 0 : 1)); hPacket.appendByte((byte) (message.getDirection() == TOCLIENT ? 0 : 1));
hPacket.appendInt(message.getPacket().getBytesLength()); hPacket.appendInt(message.getPacket().getBytesLength());
hPacket.appendBytes(message.getPacket().toBytes()); hPacket.appendBytes(message.getPacket().toBytes());
hPacket.appendInt(message.getPacket().getFormat().getId());
}), }),
(hPacket -> { (hPacket -> {
final byte side = hPacket.readByte(); final byte side = hPacket.readByte();
final int length = hPacket.readInteger(); final int length = hPacket.readInteger();
final byte[] data = hPacket.readBytes(length); final byte[] data = hPacket.readBytes(length);
final HPacket packet = new HPacket(data); final HPacketFormat format = hPacket.isEOF() == 0 ? HPacketFormat.fromId(hPacket.readInteger()) : HPacketFormat.EVA_WIRE;
final HPacket packet = format.createPacket(data);
return new Incoming.SendMessage(packet, side == 0 ? TOCLIENT : TOSERVER); return new Incoming.SendMessage(packet, side == 0 ? TOCLIENT : TOSERVER);
})); }));
register(Incoming.RequestFlags.HEADER_ID, register(Incoming.RequestFlags.HEADER_ID,
@ -172,12 +179,22 @@ public final class NetworkExtensionCodec {
(hPacket -> new Incoming.ExtensionConsoleLog(hPacket.readString()))); (hPacket -> new Incoming.ExtensionConsoleLog(hPacket.readString())));
register(Incoming.PacketToStringRequest.HEADER_ID, register(Incoming.PacketToStringRequest.HEADER_ID,
Incoming.PacketToStringRequest.class, Incoming.PacketToStringRequest.class,
(message, hPacket) -> hPacket.appendLongString(message.getString()), (message, hPacket) -> {
(hPacket -> new Incoming.PacketToStringRequest(hPacket.readLongString()))); hPacket.appendLongString(message.getString());
hPacket.appendInt(message.getFormat().getId());
},
(hPacket -> new Incoming.PacketToStringRequest(
hPacket.readLongString(),
hPacket.isEOF() == 0 ? HPacketFormat.fromId(hPacket.readInteger()) : HPacketFormat.EVA_WIRE)));
register(Incoming.StringToPacketRequest.HEADER_ID, register(Incoming.StringToPacketRequest.HEADER_ID,
Incoming.StringToPacketRequest.class, Incoming.StringToPacketRequest.class,
(message, hPacket) -> hPacket.appendLongString(message.getString(), StandardCharsets.UTF_8), (message, hPacket) -> {
(hPacket -> new Incoming.StringToPacketRequest(hPacket.readLongString(StandardCharsets.UTF_8)))); hPacket.appendLongString(message.getString(), StandardCharsets.UTF_8);
hPacket.appendInt(message.getFormat().getId());
},
(hPacket -> new Incoming.StringToPacketRequest(
hPacket.readLongString(StandardCharsets.UTF_8),
hPacket.isEOF() == 0 ? HPacketFormat.fromId(hPacket.readInteger()) : HPacketFormat.EVA_WIRE)));
} }
private static void registerOutgoingMessages() { private static void registerOutgoingMessages() {
@ -236,8 +253,13 @@ public final class NetworkExtensionCodec {
(hPacket -> new Outgoing.OnDoubleClick())); (hPacket -> new Outgoing.OnDoubleClick()));
register(Outgoing.PacketIntercept.HEADER_ID, register(Outgoing.PacketIntercept.HEADER_ID,
Outgoing.PacketIntercept.class, Outgoing.PacketIntercept.class,
(message, hPacket) -> hPacket.appendLongString(message.getPacketString()), (message, hPacket) -> {
(hPacket -> new Outgoing.PacketIntercept(hPacket.readLongString()))); hPacket.appendLongString(message.getPacketString());
hPacket.appendInt(message.getPacketFormat().getId());
},
(hPacket -> new Outgoing.PacketIntercept(
hPacket.readLongString(),
hPacket.isEOF() == 0 ? HPacketFormat.fromId(hPacket.readInteger()) : HPacketFormat.EVA_WIRE)));
register(Outgoing.UpdateHostInfo.HEADER_ID, register(Outgoing.UpdateHostInfo.HEADER_ID,
Outgoing.UpdateHostInfo.class, Outgoing.UpdateHostInfo.class,
(message, hPacket) -> message.getHostInfo().appendToPacket(hPacket), (message, hPacket) -> message.getHostInfo().appendToPacket(hPacket),

View File

@ -3,6 +3,7 @@ package gearth.services.extension_handler.extensions.implementations.network;
import gearth.misc.HostInfo; import gearth.misc.HostInfo;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.protocol.HPacketFormat;
import gearth.protocol.connection.HClient; import gearth.protocol.connection.HClient;
import gearth.services.packet_info.PacketInfoManager; import gearth.services.packet_info.PacketInfoManager;
@ -140,14 +141,20 @@ public class NetworkExtensionMessage {
public static final int HEADER_ID = 20; public static final int HEADER_ID = 20;
private final String string; private final String string;
private final HPacketFormat format;
public PacketToStringRequest(String string) { public PacketToStringRequest(String string, HPacketFormat format) {
this.string = string; this.string = string;
this.format = format;
} }
public String getString() { public String getString() {
return string; return string;
} }
public HPacketFormat getFormat() {
return format;
}
} }
/** /**
@ -160,14 +167,20 @@ public class NetworkExtensionMessage {
public static final int HEADER_ID = 21; public static final int HEADER_ID = 21;
private final String string; private final String string;
private final HPacketFormat format;
public StringToPacketRequest(String string) { public StringToPacketRequest(String string, HPacketFormat format) {
this.string = string; this.string = string;
this.format = format;
} }
public String getString() { public String getString() {
return string; return string;
} }
public HPacketFormat getFormat() {
return format;
}
} }
/** /**
@ -239,14 +252,20 @@ public class NetworkExtensionMessage {
public static final int HEADER_ID = 3; public static final int HEADER_ID = 3;
private final String packetString; private final String packetString;
private final HPacketFormat packetFormat;
public PacketIntercept(String packetString) { public PacketIntercept(String packetString, HPacketFormat packetFormat) {
this.packetString = packetString; this.packetString = packetString;
this.packetFormat = packetFormat;
} }
public String getPacketString() { public String getPacketString() {
return packetString; return packetString;
} }
public HPacketFormat getPacketFormat() {
return packetFormat;
}
} }
/** /**