Работа над gzip
This commit is contained in:
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,47 +87,49 @@ public class StreamService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean gzipStarted = false;
|
if(unpackGzippedHttp) {
|
||||||
byte[] gzipContent = null;
|
boolean gzipStarted = false;
|
||||||
int gzipStartPacket = 0;
|
//byte[] gzipContent = null;
|
||||||
int gzipEndPacket = 0;
|
int gzipStartPacket = 0;
|
||||||
|
int gzipEndPacket = 0;
|
||||||
|
|
||||||
for (int i = 0; i < packets.size(); i++) {
|
for (int i = 0; i < packets.size(); i++) {
|
||||||
Packet packet = packets.get(i);
|
Packet packet = packets.get(i);
|
||||||
|
|
||||||
if (packet.isIncoming() && gzipStarted) {
|
if (packet.isIncoming() && gzipStarted) {
|
||||||
gzipStarted = false;
|
gzipStarted = false;
|
||||||
gzipEndPacket = i - 1;
|
|
||||||
//TODO end and read gzip stream
|
|
||||||
} else if (!packet.isIncoming()) {
|
|
||||||
String content = new String(packet.getContent());
|
|
||||||
|
|
||||||
int contentPos = content.indexOf("\r\n\r\n");
|
|
||||||
boolean http = content.startsWith("HTTP/");
|
|
||||||
|
|
||||||
if(http && gzipStarted) {
|
|
||||||
gzipEndPacket = i - 1;
|
gzipEndPacket = i - 1;
|
||||||
//TODO end and read gzip stream
|
//TODO end and read gzip stream
|
||||||
}
|
} else if (!packet.isIncoming()) {
|
||||||
|
String content = new String(packet.getContent());
|
||||||
|
|
||||||
if (contentPos != -1) { // начало body
|
int contentPos = content.indexOf("\r\n\r\n");
|
||||||
String headers = content.substring(0, contentPos);
|
boolean http = content.startsWith("HTTP/");
|
||||||
boolean gziped = headers.contains("Content-Encoding: gzip\r\n");
|
|
||||||
if (gziped) {
|
if (http && gzipStarted) {
|
||||||
gzipStarted = true;
|
gzipEndPacket = i - 1;
|
||||||
gzipStartPacket = i;
|
//TODO end and read gzip stream
|
||||||
int gzipStart = Bytes.indexOf(packet.getContent(), GZIP_HEADER);
|
}
|
||||||
gzipContent = Arrays.copyOfRange(packet.getContent(), gzipStart, packet.getContent().length);
|
|
||||||
|
if (contentPos != -1) { // начало body
|
||||||
|
String headers = content.substring(0, contentPos);
|
||||||
|
boolean gziped = headers.contains("Content-Encoding: gzip\r\n");
|
||||||
|
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());
|
||||||
}
|
}
|
||||||
} else if (gzipStarted) { // продолжение body
|
|
||||||
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) {
|
||||||
|
|||||||
@@ -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
|
||||||
Reference in New Issue
Block a user