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; package gearth.misc.packetrepresentation;
import gearth.misc.packet_info.PacketInfo;
import gearth.misc.packetrepresentation.prediction.StructurePredictor; import gearth.misc.packetrepresentation.prediction.StructurePredictor;
import gearth.protocol.HPacket; 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") // 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(); int oldReadIndex = packet.getReadIndex();
packet.resetReadIndex(); packet.resetReadIndex();
StringBuilder builder = new StringBuilder(); 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); buildExpressionFromGivenStructure(packet, struct, 0, builder);
packet.setReadIndex(oldReadIndex); packet.setReadIndex(oldReadIndex);
return builder.toString(); return builder.toString();
} }
@ -219,9 +227,12 @@ public class PacketStringUtils {
else return; else return;
} }
} }
public static String predictedExpression(HPacket packet) { public static String predictedExpression(HPacket packet, PacketInfo packetInfo) {
StructurePredictor structurePredictor = new StructurePredictor(packet); 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) { public static boolean structureEquals(HPacket packet, String struct) {

View File

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

View File

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

View File

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

View File

@ -1,5 +1,6 @@
package gearth.ui.logger; package gearth.ui.logger;
import gearth.extensions.parsers.HDirection;
import gearth.protocol.connection.HState; import gearth.protocol.connection.HState;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
@ -69,7 +70,7 @@ public class LoggerController extends SubForm {
packetLogger.appendMessage(message.getPacket(), types); packetLogger.appendMessage(message.getPacket(), types);
if (cbx_showstruct.isSelected() && message.getPacket().length() < packetLimit) { 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; package gearth.ui.logger.loggerdisplays;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import java.util.HashMap; import java.util.HashMap;
@ -60,8 +61,8 @@ class LinuxTerminalLogger extends SimpleTerminalLogger {
} }
@Override @Override
public void appendStructure(HPacket packet) { public void appendStructure(HPacket packet, HMessage.Direction direction) {
String expr = packet.toExpression(); String expr = packet.toExpression(direction, packetInfoManager, true);
if (!expr.equals("")) { if (!expr.equals("")) {
System.out.println( System.out.println(
colorizePackets.get("EXPRESSION") + colorizePackets.get("EXPRESSION") +

View File

@ -1,6 +1,7 @@
package gearth.ui.logger.loggerdisplays; package gearth.ui.logger.loggerdisplays;
import gearth.protocol.HConnection; import gearth.protocol.HConnection;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
/** /**
@ -33,6 +34,6 @@ public interface PacketLogger {
void appendSplitLine(); void appendSplitLine();
void appendMessage(HPacket packet, int types); 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; package gearth.ui.logger.loggerdisplays;
import gearth.misc.packet_info.PacketInfoManager;
import gearth.protocol.HConnection; import gearth.protocol.HConnection;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
/** /**
* Created by Jonas on 04/04/18. * Created by Jonas on 04/04/18.
*/ */
class SimpleTerminalLogger implements PacketLogger { class SimpleTerminalLogger implements PacketLogger {
protected PacketInfoManager packetInfoManager = null;
@Override @Override
public void start(HConnection hConnection) { public void start(HConnection hConnection) {
packetInfoManager = hConnection.getPacketInfoManager();
// System.out.println("-- START OF SESSION --"); // System.out.println("-- START OF SESSION --");
} }
@ -54,8 +60,8 @@ class SimpleTerminalLogger implements PacketLogger {
} }
@Override @Override
public void appendStructure(HPacket packet) { public void appendStructure(HPacket packet, HMessage.Direction direction) {
String expr = packet.toExpression(); String expr = packet.toExpression(direction, packetInfoManager, true);
if (!expr.equals("")) { if (!expr.equals("")) {
System.out.println(expr); System.out.println(expr);
} }

View File

@ -1,6 +1,8 @@
package gearth.ui.logger.loggerdisplays.uilogger; package gearth.ui.logger.loggerdisplays.uilogger;
import gearth.extensions.parsers.HDirection;
import gearth.protocol.HConnection; import gearth.protocol.HConnection;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.ui.logger.loggerdisplays.PacketLogger; import gearth.ui.logger.loggerdisplays.PacketLogger;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
@ -95,7 +97,7 @@ public class UiLogger implements PacketLogger {
} }
@Override @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 chkMessageName;
public CheckMenuItem chkMessageHash; public CheckMenuItem chkMessageHash;
public Label lblPacketInfo; public Label lblPacketInfo;
public CheckMenuItem chkUseNewStructures;
private StyleClassedTextArea area; private StyleClassedTextArea area;
@ -156,8 +157,7 @@ public class UiLoggerController implements Initializable {
} }
if (packet.length() <= 2000) { if (packet.length() <= 2000) {
// String expr = packet.toExpression(isIncoming ? HMessage.Direction.TOCLIENT : HMessage.Direction.TOSERVER); String expr = packet.toExpression(isIncoming ? HMessage.Direction.TOCLIENT : HMessage.Direction.TOSERVER, packetInfoManager, chkUseNewStructures.isSelected());
String expr = packet.toExpression();
String cleaned = cleanTextContent(expr); String cleaned = cleanTextContent(expr);
if (cleaned.equals(expr)) { if (cleaned.equals(expr)) {
if (!expr.equals("") && displayStructure) if (!expr.equals("") && displayStructure)

View File

@ -139,7 +139,7 @@ public class ToolsController extends SubForm {
public void btn_toExpr_clicked(ActionEvent actionEvent) { 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) { 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="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="chkMessageName" mnemonicParsing="false" onAction="#toggleMessageName" selected="true" text="Message Name" />
<CheckMenuItem fx:id="chkMessageHash" mnemonicParsing="false" onAction="#toggleMessageHash" text="Message Hash" /> <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> </items>
</Menu> </Menu>
<CheckMenuItem fx:id="chkViewIncoming" mnemonicParsing="false" onAction="#toggleViewIncoming" selected="true" text="View Incoming"> <CheckMenuItem fx:id="chkViewIncoming" mnemonicParsing="false" onAction="#toggleViewIncoming" selected="true" text="View Incoming">