injection history

This commit is contained in:
sirjonasxx 2021-04-27 06:18:58 +02:00
parent c166402298
commit e0ab62bb33
4 changed files with 259 additions and 91 deletions

View File

@ -0,0 +1,85 @@
package gearth.ui.injection;
import gearth.misc.StringifyAble;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket;
import gearth.services.packet_info.PacketInfo;
import gearth.services.packet_info.PacketInfoManager;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
public class InjectedPackets implements StringifyAble {
private String packetsAsString;
private String description;
public InjectedPackets(String packetsAsString, int amountPackets, PacketInfoManager packetInfoManager, HMessage.Direction direction) {
String description;
if (amountPackets > 1) {
description = String.format("(packets: %d, length: %d)", amountPackets, packetsAsString.length());
}
else { // assume 1 packet
HPacket packet = new HPacket(packetsAsString);
String identifier = null;
if (!packet.isPacketComplete()) {
identifier = packet.getIdentifier();
}
else {
Optional<PacketInfo> maybeInfo = packetInfoManager.getAllPacketInfoFromHeaderId(direction, packet.headerId())
.stream().filter(packetInfo -> packetInfo.getName() != null).findFirst();
if (maybeInfo.isPresent()) {
PacketInfo packetInfo = maybeInfo.get();
identifier = packetInfo.getName();
}
}
if (identifier != null) {
description = String.format("%s", identifier);
}
else {
description = String.format("(id: %d, length: %d)", packet.headerId(), packet.length());
}
}
this.description = description;
this.packetsAsString = packetsAsString;
}
public InjectedPackets(String fromString) {
constructFromString(fromString);
}
public String getPacketsAsString() {
return packetsAsString;
}
public String getDescription() {
return description;
}
@Override
public String stringify() {
Map<String, String> info = new HashMap<>();
info.put("packetsAsString", packetsAsString);
info.put("description", description);
return new JSONObject(info).toString();
}
@Override
public void constructFromString(String str) {
JSONObject jsonObject = new JSONObject(str);
this.packetsAsString = jsonObject.getString("packetsAsString");
this.description = jsonObject.getString("description");
}
@Override
public String toString() {
return description;
}
}

View File

@ -1,29 +1,42 @@
package gearth.ui.injection;
import gearth.misc.Cacher;
import gearth.services.packet_info.PacketInfoManager;
import gearth.protocol.HMessage;
import gearth.protocol.connection.HState;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.event.EventHandler;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Text;
import gearth.protocol.HPacket;
import gearth.ui.SubForm;
import sun.misc.Cache;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class InjectionController extends SubForm {
private static final String HISTORY_CACHE_KEY = "INJECTED_HISTORY";
private static final int historylimit = 69;
public TextArea inputPacket;
public Text lbl_corrruption;
public Text lbl_pcktInfo;
public Button btn_sendToServer;
public Button btn_sendToClient;
public ListView<InjectedPackets> history;
public Label lblHistory;
protected void onParentSet() {
getHConnection().getStateObservable().addListener((oldState, newState) -> Platform.runLater(this::updateUI));
@ -31,6 +44,29 @@ public class InjectionController extends SubForm {
inputPacket.textProperty().addListener(event -> Platform.runLater(this::updateUI));
}
public void initialize() {
history.setOnMouseClicked(event -> {
if(event.getButton().equals(MouseButton.PRIMARY) && event.getClickCount() == 2) {
InjectedPackets injectedPackets = history.getSelectionModel().getSelectedItem();
if (injectedPackets != null) {
Platform.runLater(() -> {
inputPacket.setText(injectedPackets.getPacketsAsString());
updateUI();
});
}
}
});
lblHistory.setTooltip(new Tooltip("Double click a packet to restore it"));
List<Object> rawHistory = Cacher.getList(HISTORY_CACHE_KEY);
if (rawHistory != null) {
List<InjectedPackets> history = rawHistory.stream()
.map(o -> (String)o).limit(historylimit - 1).map(InjectedPackets::new).collect(Collectors.toList());
updateHistoryView(history);
}
}
private static boolean isPacketIncomplete(String line) {
boolean unmatchedBrace = false;
@ -157,6 +193,8 @@ public class InjectionController extends SubForm {
getHConnection().sendToServerAsync(packet);
writeToLog(Color.BLUE, "SS -> packet with id: " + packet.headerId());
}
addToHistory(packets, inputPacket.getText(), HMessage.Direction.TOSERVER);
}
public void sendToClient_clicked(ActionEvent actionEvent) {
@ -165,6 +203,40 @@ public class InjectionController extends SubForm {
getHConnection().sendToClientAsync(packet);
writeToLog(Color.RED, "CS -> packet with id: " + packet.headerId());
}
addToHistory(packets, inputPacket.getText(), HMessage.Direction.TOCLIENT);
}
private void addToHistory(HPacket[] packets, String packetsAsString, HMessage.Direction direction) {
InjectedPackets injectedPackets = new InjectedPackets(packetsAsString, packets.length, getHConnection().getPacketInfoManager(), direction);
List<InjectedPackets> newHistory = new ArrayList<>();
newHistory.add(injectedPackets);
List<Object> rawOldHistory = Cacher.getList(HISTORY_CACHE_KEY);
if (rawOldHistory != null) {
List<InjectedPackets> history = rawOldHistory.stream()
.map(o -> (String)o).limit(historylimit - 1).map(InjectedPackets::new).collect(Collectors.toList());
// dont add to history if its equal to the latest added packet
if (history.size() != 0 && history.get(0).getPacketsAsString().equals(injectedPackets.getPacketsAsString())) {
return;
}
newHistory.addAll(history);
}
List<String> historyAsStrings = newHistory.stream().map(InjectedPackets::stringify).collect(Collectors.toList());
Cacher.put(HISTORY_CACHE_KEY, historyAsStrings);
updateHistoryView(newHistory);
}
private void updateHistoryView(List<InjectedPackets> allHistoryItems) {
Platform.runLater(() -> {
history.getItems().clear();
history.getItems().addAll(allHistoryItems);
});
}

View File

@ -1,19 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<GridPane alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="258.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/8.0.241" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gearth.ui.connection.ConnectionController">
<GridPane alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="258.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gearth.ui.connection.ConnectionController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
@ -148,7 +140,7 @@
<RowConstraints minHeight="20.0" prefHeight="34.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Client type:">
<Label maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Client type:" textFill="#000000cd">
<padding>
<Insets left="12.0" />
</padding>

View File

@ -1,81 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.text.TextFlow?>
<GridPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="258.0" prefWidth="650.0"
xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="gearth.ui.injection.InjectionController">
<GridPane prefHeight="258.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gearth.ui.injection.InjectionController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="180.0" minWidth="180.0" prefWidth="180.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="232.0" minHeight="10.0" prefHeight="36.0" vgrow="SOMETIMES"/>
<RowConstraints maxHeight="232.0" minHeight="10.0" prefHeight="194.0" vgrow="SOMETIMES"/>
<RowConstraints maxHeight="121.0" minHeight="10.0" prefHeight="32.0" vgrow="SOMETIMES"/>
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<GridPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="32.0" minHeight="32.0" prefHeight="32.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="232.0" minHeight="10.0" prefHeight="194.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="121.0" minHeight="10.0" prefHeight="32.0" vgrow="SOMETIMES" />
</rowConstraints>
<padding>
<Insets bottom="7.0"/>
<Insets bottom="7.0" />
</padding>
<GridPane GridPane.rowIndex="0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<GridPane.margin>
<Insets left="13.0" right="13.0" top="4.0"/>
<Insets left="13.0" right="13.0" top="4.0" />
</GridPane.margin>
<Text fx:id="lbl_corrruption" fill="#ee0404b2" strokeType="OUTSIDE" strokeWidth="0.0"
text="isCorrupted: True">
<Text fx:id="lbl_corrruption" fill="#ee0404b2" strokeType="OUTSIDE" strokeWidth="0.0" text="isCorrupted: True">
<font>
<Font name="System Italic" size="11.0"/>
<Font name="System Italic" size="11.0" />
</font>
</Text>
<Text fx:id="lbl_pcktInfo" fill="#000000b2" nodeOrientation="LEFT_TO_RIGHT" strokeType="OUTSIDE"
strokeWidth="0.0" text="header (id:NULL, length:0)" GridPane.columnIndex="1"
GridPane.halignment="RIGHT">
<Text fx:id="lbl_pcktInfo" fill="#000000b2" nodeOrientation="LEFT_TO_RIGHT" strokeType="OUTSIDE" strokeWidth="0.0" text="header (id:NULL, length:0)" GridPane.columnIndex="1" GridPane.halignment="RIGHT">
<font>
<Font name="System Italic" size="11.0"/>
<Font name="System Italic" size="11.0" />
</font>
</Text>
</GridPane>
<TextArea fx:id="inputPacket" prefHeight="185.0" prefWidth="545.0" wrapText="true" GridPane.rowIndex="1">
<GridPane.margin>
<Insets bottom="5.0" left="10.0" right="10.0"/>
<Insets bottom="5.0" left="10.0" right="10.0" />
</GridPane.margin>
</TextArea>
<GridPane GridPane.rowIndex="2">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<Button fx:id="btn_sendToServer" disable="true" maxHeight="1.7976931348623157E308"
maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#sendToServer_clicked"
text="Send to server" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
<Button fx:id="btn_sendToServer" disable="true" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#sendToServer_clicked" text="Send to server" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
<GridPane.margin>
<Insets bottom="5.0" left="10.0" right="10.0" top="5.0"/>
<Insets bottom="5.0" left="10.0" right="10.0" top="5.0" />
</GridPane.margin>
</Button>
<Button fx:id="btn_sendToClient" disable="true" maxHeight="1.7976931348623157E308"
maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#sendToClient_clicked"
text="Send to client" GridPane.columnIndex="1" GridPane.halignment="CENTER"
GridPane.valignment="CENTER">
<Button fx:id="btn_sendToClient" disable="true" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#sendToClient_clicked" text="Send to client" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
<GridPane.margin>
<Insets bottom="5.0" left="10.0" right="10.0" top="5.0"/>
<Insets bottom="5.0" left="10.0" right="10.0" top="5.0" />
</GridPane.margin>
</Button>
</GridPane>
<GridPane.margin>
<Insets />
</GridPane.margin>
</GridPane>
<GridPane GridPane.columnIndex="1">
<rowConstraints>
<RowConstraints maxHeight="32.0" minHeight="32.0" prefHeight="32.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ListView fx:id="history" prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1" />
<Label fx:id="lblHistory" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="History:" textFill="#000000cc">
<GridPane.margin>
<Insets top="4.0" />
</GridPane.margin>
</Label>
</children>
<GridPane.margin>
<Insets bottom="5.0" left="6.0" right="10.0" />
</GridPane.margin>
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
</GridPane>
</children>
</GridPane>