Работа над gzip

This commit is contained in:
serega6531
2019-05-19 08:01:45 +03:00
parent abf5273adf
commit 973815290a
3 changed files with 66 additions and 33 deletions

View File

@@ -188,6 +188,8 @@ public class PcapWorker implements PacketListener {
acks.remove(stream); acks.remove(stream);
} }
} }
} else {
log.trace("{} {}:{} -> {}:{}", protocol.name().toLowerCase(), sourceIpString, sourcePort, destIpString, destPort);
} }
} }
} }

View File

@@ -12,7 +12,11 @@ import org.springframework.transaction.annotation.Transactional;
import ru.serega6531.packmate.model.*; import ru.serega6531.packmate.model.*;
import ru.serega6531.packmate.repository.StreamRepository; import ru.serega6531.packmate.repository.StreamRepository;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipException;
@Service @Service
@Slf4j @Slf4j
@@ -25,6 +29,7 @@ public class StreamService {
private final StreamSubscriptionService subscriptionService; private final StreamSubscriptionService subscriptionService;
private final String localIp; private final String localIp;
private final boolean unpackGzippedHttp;
private final boolean ignoreEmptyPackets; private final boolean ignoreEmptyPackets;
private final byte[] GZIP_HEADER = {0x1f, (byte) 0x8b, 0x08}; private final byte[] GZIP_HEADER = {0x1f, (byte) 0x8b, 0x08};
@@ -36,6 +41,7 @@ public class StreamService {
PacketService packetService, PacketService packetService,
StreamSubscriptionService subscriptionService, StreamSubscriptionService subscriptionService,
@Value("${local-ip}") String localIp, @Value("${local-ip}") String localIp,
@Value("${unpack-gzipped-http}") boolean unpackGzippedHttp,
@Value("${ignore-empty-packets}") boolean ignoreEmptyPackets) { @Value("${ignore-empty-packets}") boolean ignoreEmptyPackets) {
this.repository = repository; this.repository = repository;
this.patternService = patternService; this.patternService = patternService;
@@ -43,6 +49,7 @@ public class StreamService {
this.packetService = packetService; this.packetService = packetService;
this.subscriptionService = subscriptionService; this.subscriptionService = subscriptionService;
this.localIp = localIp; this.localIp = localIp;
this.unpackGzippedHttp = unpackGzippedHttp;
this.ignoreEmptyPackets = ignoreEmptyPackets; this.ignoreEmptyPackets = ignoreEmptyPackets;
} }
@@ -80,8 +87,9 @@ public class StreamService {
} }
} }
if(unpackGzippedHttp) {
boolean gzipStarted = false; boolean gzipStarted = false;
byte[] gzipContent = null; //byte[] gzipContent = null;
int gzipStartPacket = 0; int gzipStartPacket = 0;
int gzipEndPacket = 0; int gzipEndPacket = 0;
@@ -109,19 +117,20 @@ public class StreamService {
if (gziped) { if (gziped) {
gzipStarted = true; gzipStarted = true;
gzipStartPacket = i; gzipStartPacket = i;
int gzipStart = Bytes.indexOf(packet.getContent(), GZIP_HEADER); //int gzipStart = Bytes.indexOf(packet.getContent(), GZIP_HEADER);
gzipContent = Arrays.copyOfRange(packet.getContent(), gzipStart, packet.getContent().length); //gzipContent = Arrays.copyOfRange(packet.getContent(), gzipStart, packet.getContent().length);
} }
} else if (gzipStarted) { // продолжение body } else if (gzipStarted) { // продолжение body
gzipContent = ArrayUtils.addAll(gzipContent, packet.getContent()); //gzipContent = ArrayUtils.addAll(gzipContent, packet.getContent());
} }
} }
} }
if(gzipContent != null) { if (gzipStarted) {
gzipEndPacket = packets.size() - 1; gzipEndPacket = packets.size() - 1;
// TODO end and read gzip stream // TODO end and read gzip stream
} }
}
Stream savedStream = save(stream); Stream savedStream = save(stream);
@@ -142,6 +151,27 @@ public class StreamService {
return true; return true;
} }
private Packet decompressGzipPackets(List<Packet> packets) {
final byte[] content = packets.stream()
.map(Packet::getContent)
.reduce(ArrayUtils::addAll)
.get();
final int gzipStart = Bytes.indexOf(content, GZIP_HEADER);
byte[] gzipBytes = Arrays.copyOfRange(content, gzipStart, content.length);
try {
final GZIPInputStream gzipStream = new GZIPInputStream(new ByteArrayInputStream(gzipBytes));
} catch (ZipException e) {
log.warn("Не удалось разархивировать gzip, оставляем как есть", e);
} catch (IOException e) {
log.error("decompress gzip", e);
}
return null;
}
public Stream save(Stream stream) { public Stream save(Stream stream) {
Stream saved; Stream saved;
if (stream.getId() == null) { if (stream.getId() == null) {

View File

@@ -21,3 +21,4 @@ udp-stream-timeout: 20 # секунд
tcp-stream-timeout: 120 # секунд tcp-stream-timeout: 120 # секунд
timeout-stream-check-interval: 10 # секунд timeout-stream-check-interval: 10 # секунд
ignore-empty-packets: true ignore-empty-packets: true
unpack-gzipped-http: true