Merge pull request #14 from XePeleato/development

Fix the injector for multiline packets
This commit is contained in:
sirjonasxx 2019-01-01 23:07:10 +01:00 committed by GitHub
commit 522c811b3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,6 +11,8 @@ import gearth.protocol.HConnection;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.ui.SubForm; import gearth.ui.SubForm;
import java.util.LinkedList;
public class Injection extends SubForm { public class Injection extends SubForm {
public TextArea inputPacket; public TextArea inputPacket;
public Text lbl_corrruption; public Text lbl_corrruption;
@ -24,16 +26,40 @@ public class Injection extends SubForm {
inputPacket.textProperty().addListener(event -> Platform.runLater(this::updateUI)); inputPacket.textProperty().addListener(event -> Platform.runLater(this::updateUI));
} }
private boolean isPacketIncomplete(String line) {
int unmatchedBraces = 0;
for (int i = 0; i < line.length(); i++)
if (line.charAt(i) == '{')
unmatchedBraces++;
else if (line.charAt(i) == '}')
unmatchedBraces--;
return unmatchedBraces != 0;
}
private HPacket[] parsePackets(String fullText) {
LinkedList<HPacket> packets = new LinkedList<>();
String[] lines = fullText.split("\n");
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
while (isPacketIncomplete(line) && i < lines.length - 1)
line += lines[++i];
packets.add(new HPacket(line));
}
return packets.toArray(new HPacket[0]);
}
private void updateUI() { private void updateUI() {
boolean dirty = false; boolean dirty = false;
String[] rawPackets = inputPacket.getText().split("\n");
HPacket[] packets = new HPacket[rawPackets.length];
lbl_corrruption.setText("isCorrupted: False"); lbl_corrruption.setText("isCorrupted: False");
lbl_corrruption.setFill(Paint.valueOf("Green")); lbl_corrruption.setFill(Paint.valueOf("Green"));
HPacket[] packets = parsePackets(inputPacket.getText());
for (int i = 0; i < packets.length; i++) { for (int i = 0; i < packets.length; i++) {
packets[i] = new HPacket(rawPackets[i]);
if (packets[i].isCorrupted()) { if (packets[i].isCorrupted()) {
if (!dirty) { if (!dirty) {
lbl_corrruption.setText("isCorrupted: True -> " + i); lbl_corrruption.setText("isCorrupted: True -> " + i);
@ -59,18 +85,16 @@ public class Injection extends SubForm {
} }
public void sendToServer_clicked(ActionEvent actionEvent) { public void sendToServer_clicked(ActionEvent actionEvent) {
String[] rawPackets = inputPacket.getText().split("\n"); HPacket[] packets = parsePackets(inputPacket.getText());
for (String rawPacket : rawPackets) { for (HPacket packet : packets) {
HPacket packet = new HPacket(rawPacket);
getHConnection().sendToServerAsync(packet); getHConnection().sendToServerAsync(packet);
writeToLog(Color.BLUE, "SS -> packet with id: " + packet.headerId()); writeToLog(Color.BLUE, "SS -> packet with id: " + packet.headerId());
} }
} }
public void sendToClient_clicked(ActionEvent actionEvent) { public void sendToClient_clicked(ActionEvent actionEvent) {
String[] rawPackets = inputPacket.getText().split("\n"); HPacket[] packets = parsePackets(inputPacket.getText());
for (String rawPacket : rawPackets) { for (HPacket packet : packets) {
HPacket packet = new HPacket(rawPacket);
getHConnection().sendToClientAsync(packet); getHConnection().sendToClientAsync(packet);
writeToLog(Color.RED, "CS -> packet with id: " + packet.headerId()); writeToLog(Color.RED, "CS -> packet with id: " + packet.headerId());
} }