Skip to content

Commit

Permalink
remove fixed packetlength values
Browse files Browse the repository at this point in the history
  • Loading branch information
sirjonasxx committed Oct 3, 2018
1 parent 065504f commit 71a2a38
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 43 deletions.
69 changes: 40 additions & 29 deletions src/main/protocol/HPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,46 @@ private static byte[] fromStringToBytes(String curstring) {
return new byte[0];
}

public boolean structureEquals(String structure) {
if (isCorrupted()) return false;

int indexbuffer = readIndex;
readIndex = 6;

String[] split = structure.split(",");

for (int i = 0; i < split.length; i++) {
String s = split[i];

if (s.equals("s")) {
if (readUshort(readIndex) + 2 + readIndex > getBytesLength()) return false;
readString();
}
else if (s.equals("i")) {
if (readIndex + 4 > getBytesLength()) return false;
readInteger();
}
else if (s.equals("u")) {
if (readIndex + 2 > getBytesLength()) return false;
readUshort();
}
else if (s.equals("b")) {
if (readIndex + 1 > getBytesLength()) return false;
readBoolean();
}
}

boolean result = (isEOF() == 1);
readIndex = indexbuffer;
return result;
}

public int isEOF() {
if (readIndex < getBytesLength()) return 0;
if (readIndex == getBytesLength()) return 1;
return 2;
}

public byte[] toBytes() {
return packetInBytes;
}
Expand Down Expand Up @@ -800,34 +840,5 @@ public boolean equals(Object object) {
}

public static void main(String[] args) {
// HPacket packet = new HPacket("{l}{u:500}{i:4}{s:heey}{b:false}");
// System.out.println(packet);
//
// String stringified = packet.stringify();
// System.out.println("stringified: " + stringified);
// System.out.println(stringified.length());
//
//
// HPacket packet1 = new HPacket(new byte[0]);
// packet1.constructFromString(stringified);
//
// System.out.println(packet1);
// System.out.println(packet.equals(packet1));

HPacket packet = new HPacket(555);
for (int i = -128; i < 128; i++) {
packet.appendByte((byte)i);
}
System.out.println(packet);

String stringified = packet.stringify();
System.out.println(stringified.length());


HPacket packet1 = new HPacket(new byte[0]);
packet1.constructFromString(stringified);

System.out.println(packet1);
System.out.println(packet.equals(packet1));
}
}
13 changes: 5 additions & 8 deletions src/main/protocol/packethandler/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,12 @@ public void unblock() {
* @param message
*/
void notifyListeners(HMessage message) {
for (TrafficListener listener : (List<TrafficListener>)listeners[0]) {
listener.onCapture(message);
}
for (TrafficListener listener : (List<TrafficListener>)listeners[1]) {
listener.onCapture(message);
}
for (TrafficListener listener : (List<TrafficListener>)listeners[2]) {
listener.onCapture(message);
for (int x = 0; x < 3; x++) {
for (int i = ((List<TrafficListener>)listeners[x]).size() - 1; i >= 0; i--) {
((List<TrafficListener>)listeners[x]).get(i).onCapture(message);
}
}

}

public void sendToStream(byte[] buffer) {
Expand Down
18 changes: 12 additions & 6 deletions src/main/protocol/packethandler/IncomingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@

public class IncomingHandler extends Handler {

private volatile boolean onlyOnce = true;
public IncomingHandler(OutputStream outputStream, Object[] listeners) {
super(outputStream, listeners);

((List<TrafficListener>)listeners[0]).add(message -> {
if (isDataStream && onlyOnce && (message.getPacket().length() == 261 || message.getPacket().length() == 517)) {
onlyOnce = false;
isEncryptedStream = message.getPacket().readBoolean(message.getPacket().length() + 3);
TrafficListener listener = new TrafficListener() {
@Override
public void onCapture(HMessage message) {
if (isDataStream && message.getPacket().structureEquals("s,b")) {
((List<TrafficListener>)listeners[0]).remove(this);
HPacket packet = message.getPacket();
packet.readString();
isEncryptedStream = packet.readBoolean();
}
}
});
};

((List<TrafficListener>)listeners[0]).add(listener);
}

@Override
Expand Down

0 comments on commit 71a2a38

Please sign in to comment.