new expressions

This commit is contained in:
sirjonasxx 2021-04-23 17:38:10 +02:00
parent e8c0671664
commit 74a72d199b
12 changed files with 84 additions and 61 deletions

View File

@ -1,5 +1,6 @@
package gearth.misc.packetrepresentation;
import gearth.misc.packet_info.PacketInfo;
import gearth.misc.packetrepresentation.prediction.StructurePredictor;
import gearth.protocol.HPacket;
@ -178,14 +179,21 @@ public class PacketStringUtils {
}
// generates an expression for a packet from a packet structure (ex. "i(isi(b))iBd")
public static String toExpressionFromGivenStructure(HPacket packet, String struct) {
public static String toExpressionFromGivenStructure(HPacket packet, String struct, PacketInfo packetInfo) {
int oldReadIndex = packet.getReadIndex();
packet.resetReadIndex();
StringBuilder builder = new StringBuilder();
builder.append("{l}{h:").append(packet.headerId()).append("}");
if (packetInfo != null) {
String identifier = packetInfo.getName() == null ? packetInfo.getHash() : packetInfo.getName();
builder.append("{").append(identifier).append("}");
}
else {
builder.append("{l}{h:").append(packet.headerId()).append("}");
}
buildExpressionFromGivenStructure(packet, struct, 0, builder);
packet.setReadIndex(oldReadIndex);
return builder.toString();
}
@ -219,9 +227,12 @@ public class PacketStringUtils {
else return;
}
}
public static String predictedExpression(HPacket packet) {
public static String predictedExpression(HPacket packet, PacketInfo packetInfo) {
StructurePredictor structurePredictor = new StructurePredictor(packet);
return structurePredictor.getExpression();
String structure = structurePredictor.getStructure();
if (structure == null) return "";
return PacketStringUtils.toExpressionFromGivenStructure(packet, structure, packetInfo);
}
public static boolean structureEquals(HPacket packet, String struct) {

View File

@ -65,13 +65,6 @@ public class StructurePredictor {
structure = stringBuilder.reverse().toString();
}
public String getExpression() {
if (structure == null) {
return "";
}
return PacketStringUtils.toExpressionFromGivenStructure(packet, structure);
}
public String getStructure() {
return structure;
}

View File

@ -1,6 +1,8 @@
package gearth.protocol;
import gearth.misc.StringifyAble;
import gearth.misc.packet_info.PacketInfo;
import gearth.misc.packet_info.PacketInfoManager;
import gearth.misc.packetrepresentation.InvalidPacketException;
import gearth.misc.packetrepresentation.PacketStringUtils;
@ -9,9 +11,12 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.InvalidParameterException;
import java.util.Arrays;
import java.util.Optional;
public class HPacket implements StringifyAble {
private boolean isEdited = false;
private byte[] packetInBytes;
private int readIndex = 6;
@ -577,50 +582,52 @@ public class HPacket implements StringifyAble {
isEdited = edited;
}
// private String getHarbleStructure(HMessage.Direction direction) {
// PacketInfoManager.HarbleMessage msg;
// if (HarbleAPIFetcher.HARBLEAPI != null &&
// ((msg = HarbleAPIFetcher.HARBLEAPI.getHarbleMessageFromHeaderId(direction, headerId())) != null)) {
// if (msg.getStructure() != null && structureEquals(msg.getStructure())) {
// return msg.getStructure();
// }
// }
//
// return null;
// }
private PacketInfo getPacketInfo(HMessage.Direction direction, PacketInfoManager packetInfoManager) {
if (packetInfoManager == null) return null;
// public String toExpression(HMessage.Direction direction) {
// if (isCorrupted()) return "";
//
// String structure = getHarbleStructure(direction);
// if (structure != null) {
// return PacketStringUtils.toExpressionFromGivenStructure(this, structure);
// }
//
// return PacketStringUtils.predictedExpression(this);
// }
// prefer packetinfo with structure information (not available in at time of writing)
Optional<PacketInfo> packetInfoMaybe = packetInfoManager.getAllPacketInfoFromHeaderId(direction, headerId())
.stream().filter(packetInfo -> packetInfo.getStructure() != null).findFirst();
return packetInfoMaybe.orElseGet(() -> packetInfoManager.getPacketInfoFromHeaderId(direction, headerId()));
}
// /**
// * returns "" if not found or not sure enough
// */
// public String toExpression() {
// if (isCorrupted()) return "";
//
// String structure1 = getHarbleStructure(HMessage.Direction.TOCLIENT);
// String structure2 = getHarbleStructure(HMessage.Direction.TOSERVER);
// if (structure1 != null && structure2 == null) {
// return PacketStringUtils.toExpressionFromGivenStructure(this, structure1);
// }
// else if (structure1 == null && structure2 != null) {
// return PacketStringUtils.toExpressionFromGivenStructure(this, structure2);
// }
//
// return PacketStringUtils.predictedExpression(this);
// }
public String toExpression(HMessage.Direction direction, PacketInfoManager packetInfoManager, boolean removeShuffle) {
if (isCorrupted()) return "";
PacketInfo packetInfo = getPacketInfo(direction, packetInfoManager);
if (packetInfo.getStructure() != null) {
return PacketStringUtils.toExpressionFromGivenStructure(this, packetInfo.getStructure(), removeShuffle ? packetInfo : null);
}
return PacketStringUtils.predictedExpression(this, removeShuffle ? packetInfo : null);
}
/**
* returns "" if not found or not sure enough
*/
public String toExpression(PacketInfoManager packetInfoManager, boolean removeShuffle) {
if (isCorrupted()) return "";
PacketInfo maybe1 = getPacketInfo(HMessage.Direction.TOCLIENT, packetInfoManager);
PacketInfo maybe2 = getPacketInfo(HMessage.Direction.TOSERVER, packetInfoManager);
PacketInfo packetInfo = null;
if (maybe1 != null && maybe2 == null) {
packetInfo = maybe1;
}
else if (maybe1 == null && maybe2 != null) {
packetInfo = maybe2;
}
if (packetInfo != null && packetInfo.getStructure() != null) {
return PacketStringUtils.toExpressionFromGivenStructure(this, packetInfo.getStructure(), removeShuffle ? packetInfo : null);
}
return PacketStringUtils.predictedExpression(this, removeShuffle ? packetInfo : null);
}
public String toExpression() {
if (isCorrupted()) return "";
return PacketStringUtils.predictedExpression(this);
return PacketStringUtils.predictedExpression(this, null);
}
@Override

View File

@ -220,7 +220,7 @@ public class ExtensionHandler {
try {
s = packet.toString();
if (packet.length() < 3000) {
expression = packet.toExpression();
expression = packet.toExpression(hConnection.getPacketInfoManager(), true);
}
}
finally {

View File

@ -1,5 +1,6 @@
package gearth.ui.logger;
import gearth.extensions.parsers.HDirection;
import gearth.protocol.connection.HState;
import javafx.application.Platform;
import javafx.event.ActionEvent;
@ -69,7 +70,7 @@ public class LoggerController extends SubForm {
packetLogger.appendMessage(message.getPacket(), types);
if (cbx_showstruct.isSelected() && message.getPacket().length() < packetLimit) {
packetLogger.appendStructure(message.getPacket());
packetLogger.appendStructure(message.getPacket(), message.getDestination());
}
});
});

View File

@ -1,5 +1,6 @@
package gearth.ui.logger.loggerdisplays;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket;
import java.util.HashMap;
@ -60,8 +61,8 @@ class LinuxTerminalLogger extends SimpleTerminalLogger {
}
@Override
public void appendStructure(HPacket packet) {
String expr = packet.toExpression();
public void appendStructure(HPacket packet, HMessage.Direction direction) {
String expr = packet.toExpression(direction, packetInfoManager, true);
if (!expr.equals("")) {
System.out.println(
colorizePackets.get("EXPRESSION") +

View File

@ -1,6 +1,7 @@
package gearth.ui.logger.loggerdisplays;
import gearth.protocol.HConnection;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket;
/**
@ -33,6 +34,6 @@ public interface PacketLogger {
void appendSplitLine();
void appendMessage(HPacket packet, int types);
void appendStructure(HPacket packet);
void appendStructure(HPacket packet, HMessage.Direction direction);
}

View File

@ -1,14 +1,20 @@
package gearth.ui.logger.loggerdisplays;
import gearth.misc.packet_info.PacketInfoManager;
import gearth.protocol.HConnection;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket;
/**
* Created by Jonas on 04/04/18.
*/
class SimpleTerminalLogger implements PacketLogger {
protected PacketInfoManager packetInfoManager = null;
@Override
public void start(HConnection hConnection) {
packetInfoManager = hConnection.getPacketInfoManager();
// System.out.println("-- START OF SESSION --");
}
@ -54,8 +60,8 @@ class SimpleTerminalLogger implements PacketLogger {
}
@Override
public void appendStructure(HPacket packet) {
String expr = packet.toExpression();
public void appendStructure(HPacket packet, HMessage.Direction direction) {
String expr = packet.toExpression(direction, packetInfoManager, true);
if (!expr.equals("")) {
System.out.println(expr);
}

View File

@ -1,6 +1,8 @@
package gearth.ui.logger.loggerdisplays.uilogger;
import gearth.extensions.parsers.HDirection;
import gearth.protocol.HConnection;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket;
import gearth.ui.logger.loggerdisplays.PacketLogger;
import javafx.event.ActionEvent;
@ -95,7 +97,7 @@ public class UiLogger implements PacketLogger {
}
@Override
public void appendStructure(HPacket packet) {
public void appendStructure(HPacket packet, HMessage.Direction direction) {
}
}

View File

@ -35,6 +35,7 @@ public class UiLoggerController implements Initializable {
public CheckMenuItem chkMessageName;
public CheckMenuItem chkMessageHash;
public Label lblPacketInfo;
public CheckMenuItem chkUseNewStructures;
private StyleClassedTextArea area;
@ -156,8 +157,7 @@ public class UiLoggerController implements Initializable {
}
if (packet.length() <= 2000) {
// String expr = packet.toExpression(isIncoming ? HMessage.Direction.TOCLIENT : HMessage.Direction.TOSERVER);
String expr = packet.toExpression();
String expr = packet.toExpression(isIncoming ? HMessage.Direction.TOCLIENT : HMessage.Direction.TOSERVER, packetInfoManager, chkUseNewStructures.isSelected());
String cleaned = cleanTextContent(expr);
if (cleaned.equals(expr)) {
if (!expr.equals("") && displayStructure)

View File

@ -139,7 +139,7 @@ public class ToolsController extends SubForm {
public void btn_toExpr_clicked(ActionEvent actionEvent) {
txt_exprArea.setText(new HPacket(txt_packetArea.getText()).toExpression());
txt_exprArea.setText(new HPacket(txt_packetArea.getText()).toExpression(getHConnection().getPacketInfoManager(), true));
}
public void btn_toPacket_clicked(ActionEvent actionEvent) {

View File

@ -14,6 +14,7 @@
<CheckMenuItem fx:id="chkDisplayStructure" mnemonicParsing="false" onAction="#toggleDisplayStructure" selected="true" text="Structure" />
<CheckMenuItem fx:id="chkMessageName" mnemonicParsing="false" onAction="#toggleMessageName" selected="true" text="Message Name" />
<CheckMenuItem fx:id="chkMessageHash" mnemonicParsing="false" onAction="#toggleMessageHash" text="Message Hash" />
<CheckMenuItem fx:id="chkUseNewStructures" mnemonicParsing="false" onAction="#toggleMessageHash" selected="true" text="New structures" />
</items>
</Menu>
<CheckMenuItem fx:id="chkViewIncoming" mnemonicParsing="false" onAction="#toggleViewIncoming" selected="true" text="View Incoming">