From 669bcaa873ea3829866799af92c14bfe8241ff2c Mon Sep 17 00:00:00 2001 From: serega6531 Date: Sun, 19 May 2019 14:37:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=D0=BD?= =?UTF-8?q?=D0=B0=D0=B4=20=D0=B4=D0=B5=D0=BA=D0=BE=D0=B4=D0=B8=D0=BD=D0=B3?= =?UTF-8?q?=D0=BE=D0=BC=20gzip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + .../packmate/service/StreamService.java | 44 +++++++++++++++---- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/build.gradle b/build.gradle index 0a7cfde..902461f 100644 --- a/build.gradle +++ b/build.gradle @@ -26,6 +26,7 @@ dependencies { implementation "org.springframework.boot:spring-boot-starter-websocket" implementation 'org.springframework.session:spring-session-core' compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7' + compile group: 'commons-io', name: 'commons-io', version: '2.6' compile group: 'com.google.guava', name: 'guava', version: '11.0.2' compile 'org.pcap4j:pcap4j-core:1.+' compile 'org.pcap4j:pcap4j-packetfactory-static:1.+' diff --git a/src/main/java/ru/serega6531/packmate/service/StreamService.java b/src/main/java/ru/serega6531/packmate/service/StreamService.java index 67fd190..804b5bc 100644 --- a/src/main/java/ru/serega6531/packmate/service/StreamService.java +++ b/src/main/java/ru/serega6531/packmate/service/StreamService.java @@ -2,6 +2,7 @@ package ru.serega6531.packmate.service; import com.google.common.primitives.Bytes; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.ArrayUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -13,8 +14,11 @@ import ru.serega6531.packmate.model.*; import ru.serega6531.packmate.repository.StreamRepository; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.nio.file.Files; import java.util.*; +import java.util.stream.Collectors; import java.util.zip.GZIPInputStream; import java.util.zip.ZipException; @@ -91,15 +95,21 @@ public class StreamService { boolean gzipStarted = false; //byte[] gzipContent = null; int gzipStartPacket = 0; - int gzipEndPacket = 0; + int gzipEndPacket; for (int i = 0; i < packets.size(); i++) { Packet packet = packets.get(i); if (packet.isIncoming() && gzipStarted) { - gzipStarted = false; gzipEndPacket = i - 1; - //TODO end and read gzip stream + + List cut = packets.subList(gzipStartPacket, gzipEndPacket + 1); + + Packet decompressed = decompressGzipPackets(cut); + packets.removeAll(cut); + packets.add(gzipStartPacket, decompressed); + gzipStarted = false; + i = gzipStartPacket + 1; } else if (!packet.isIncoming()) { String content = new String(packet.getContent()); @@ -108,7 +118,13 @@ public class StreamService { if (http && gzipStarted) { gzipEndPacket = i - 1; - //TODO end and read gzip stream + List cut = packets.subList(gzipStartPacket, gzipEndPacket + 1); + + Packet decompressed = decompressGzipPackets(cut); + packets.removeAll(cut); + packets.add(gzipStartPacket, decompressed); + gzipStarted = false; + i = gzipStartPacket + 1; } if (contentPos != -1) { // начало body @@ -117,18 +133,18 @@ public class StreamService { if (gziped) { gzipStarted = true; gzipStartPacket = i; - //int gzipStart = Bytes.indexOf(packet.getContent(), GZIP_HEADER); - //gzipContent = Arrays.copyOfRange(packet.getContent(), gzipStart, packet.getContent().length); } - } else if (gzipStarted) { // продолжение body - //gzipContent = ArrayUtils.addAll(gzipContent, packet.getContent()); } } } if (gzipStarted) { gzipEndPacket = packets.size() - 1; - // TODO end and read gzip stream + List cut = packets.subList(gzipStartPacket, gzipEndPacket + 1); + + Packet decompressed = decompressGzipPackets(cut); + packets.removeAll(cut); + packets.add(gzipStartPacket, decompressed); } } @@ -158,11 +174,21 @@ public class StreamService { .get(); final int gzipStart = Bytes.indexOf(content, GZIP_HEADER); + byte[] httpHeader = Arrays.copyOfRange(content, 0, gzipStart); byte[] gzipBytes = Arrays.copyOfRange(content, gzipStart, content.length); try { final GZIPInputStream gzipStream = new GZIPInputStream(new ByteArrayInputStream(gzipBytes)); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + IOUtils.copy(gzipStream, out); + byte[] newContent = ArrayUtils.addAll(httpHeader, out.toByteArray()); + return Packet.builder() + .incoming(false) + .timestamp(packets.get(0).getTimestamp()) + .ungzipped(true) + .content(newContent) + .build(); } catch (ZipException e) { log.warn("Не удалось разархивировать gzip, оставляем как есть", e); } catch (IOException e) {