remove fixed packetlength values

This commit is contained in:
sirjonasxx 2018-10-03 11:05:16 +02:00
parent 065504f6cd
commit 71a2a385c9
3 changed files with 57 additions and 43 deletions

View File

@ -185,6 +185,46 @@ public class HPacket implements StringifyAble {
return new byte[0]; 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() { public byte[] toBytes() {
return packetInBytes; return packetInBytes;
} }
@ -800,34 +840,5 @@ public class HPacket implements StringifyAble {
} }
public static void main(String[] args) { 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));
} }
} }

View File

@ -104,15 +104,12 @@ public abstract class Handler {
* @param message * @param message
*/ */
void notifyListeners(HMessage message) { void notifyListeners(HMessage message) {
for (TrafficListener listener : (List<TrafficListener>)listeners[0]) { for (int x = 0; x < 3; x++) {
listener.onCapture(message); for (int i = ((List<TrafficListener>)listeners[x]).size() - 1; i >= 0; i--) {
((List<TrafficListener>)listeners[x]).get(i).onCapture(message);
} }
for (TrafficListener listener : (List<TrafficListener>)listeners[1]) {
listener.onCapture(message);
}
for (TrafficListener listener : (List<TrafficListener>)listeners[2]) {
listener.onCapture(message);
} }
} }
public void sendToStream(byte[] buffer) { public void sendToStream(byte[] buffer) {

View File

@ -10,16 +10,22 @@ import java.util.List;
public class IncomingHandler extends Handler { public class IncomingHandler extends Handler {
private volatile boolean onlyOnce = true;
public IncomingHandler(OutputStream outputStream, Object[] listeners) { public IncomingHandler(OutputStream outputStream, Object[] listeners) {
super(outputStream, listeners); super(outputStream, listeners);
((List<TrafficListener>)listeners[0]).add(message -> { TrafficListener listener = new TrafficListener() {
if (isDataStream && onlyOnce && (message.getPacket().length() == 261 || message.getPacket().length() == 517)) { @Override
onlyOnce = false; public void onCapture(HMessage message) {
isEncryptedStream = message.getPacket().readBoolean(message.getPacket().length() + 3); 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 @Override