Работа над распаковкой websocket

This commit is contained in:
serega6531
2020-04-05 00:36:52 +03:00
parent e25e66d727
commit 2688e1fc56

View File

@@ -37,11 +37,10 @@ public class WebSocketsParser {
private static final String WEBSOCKET_CONNECTION_HEADER = "connection: upgrade\r\n"; private static final String WEBSOCKET_CONNECTION_HEADER = "connection: upgrade\r\n";
private final List<Packet> packets; private final List<Packet> packets;
private List<Framedata> frames;
@Getter @Getter
private boolean parsed = false; private boolean parsed = false;
private int httpEnd = -1; private List<Packet> parsedPackets;
public WebSocketsParser(List<Packet> packets) { public WebSocketsParser(List<Packet> packets) {
this.packets = packets; this.packets = packets;
@@ -58,6 +57,7 @@ public class WebSocketsParser {
return; return;
} }
int httpEnd = -1;
for (int i = clientHandshakePackets.size(); i < packets.size(); i++) { for (int i = clientHandshakePackets.size(); i < packets.size(); i++) {
if (packets.get(i).getContentString().endsWith("\r\n\r\n")) { if (packets.get(i).getContentString().endsWith("\r\n\r\n")) {
httpEnd = i + 1; httpEnd = i + 1;
@@ -92,47 +92,47 @@ public class WebSocketsParser {
return; return;
} }
final List<Packet> wsPackets = this.packets.subList( final List<Packet> wsPackets = packets.subList(
httpEnd, httpEnd,
this.packets.size()); packets.size());
final byte[] wsContent = wsPackets.stream() if(wsPackets.isEmpty()) {
.map(Packet::getContent)
.reduce(ArrayUtils::addAll)
.orElse(null);
if (wsContent == null) {
return; return;
} }
final ByteBuffer frame = ByteBuffer.wrap(wsContent); final List<Packet> handshakes = packets.subList(0, httpEnd);
parse(wsPackets, handshakes, draft);
parsed = true;
}
private void parse(final List<Packet> wsPackets, final List<Packet> handshakes, Draft_6455 draft) {
List<List<Packet>> sides = sliceToSides(wsPackets);
parsedPackets = new ArrayList<>(handshakes);
for (List<Packet> side : sides) {
final Packet lastPacket = side.get(0);
final byte[] wsContent = side.stream()
.map(Packet::getContent)
.reduce(ArrayUtils::addAll)
.get();
final ByteBuffer buffer = ByteBuffer.wrap(wsContent);
List<Framedata> frames;
try { try {
frames = draft.translateFrame(frame); frames = draft.translateFrame(buffer);
} catch (InvalidDataException e) { } catch (InvalidDataException e) {
log.warn("WebSocket data", e); log.warn("WebSocket data", e);
return; return;
} }
parsed = true;
}
public List<Packet> getParsedPackets() {
if (!parsed) {
throw new IllegalStateException("WS is not parsed");
}
final List<Packet> handshakes = packets.subList(0, httpEnd);
List<Packet> newPackets = new ArrayList<>(handshakes.size() + frames.size());
newPackets.addAll(handshakes);
final Packet lastPacket = packets.get(packets.size() - 1);
for (Framedata frame : frames) { for (Framedata frame : frames) {
if(frame instanceof DataFrame) { if(frame instanceof DataFrame) {
newPackets.add(Packet.builder() parsedPackets.add(Packet.builder()
.content(frame.getPayloadData().array()) .content(frame.getPayloadData().array())
.incoming(true) //TODO .incoming(lastPacket.isIncoming())
.timestamp(lastPacket.getTimestamp()) .timestamp(lastPacket.getTimestamp())
.ttl(lastPacket.getTtl()) .ttl(lastPacket.getTtl())
.ungzipped(lastPacket.isUngzipped()) .ungzipped(lastPacket.isUngzipped())
@@ -140,8 +140,40 @@ public class WebSocketsParser {
); );
} }
} }
}
}
return newPackets; public List<Packet> getParsedPackets() {
if (!parsed) {
throw new IllegalStateException("WS is not parsed");
}
return parsedPackets;
}
private List<List<Packet>> sliceToSides(List<Packet> packets) {
List<List<Packet>> result = new ArrayList<>();
List<Packet> side = new ArrayList<>();
boolean incoming = true;
for (Packet packet : packets) {
if(packet.isIncoming() != incoming) {
incoming = packet.isIncoming();
if(!side.isEmpty()) {
result.add(side);
side = new ArrayList<>();
}
}
side.add(packet);
}
if(!side.isEmpty()) {
result.add(side);
}
return result;
} }
private String getHandshake(final List<Packet> packets) { private String getHandshake(final List<Packet> packets) {