Рефакторинг
This commit is contained in:
@@ -23,7 +23,6 @@ import javax.annotation.PreDestroy;
|
|||||||
import java.net.Inet4Address;
|
import java.net.Inet4Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
@@ -81,7 +80,7 @@ public class PcapWorker implements PacketListener {
|
|||||||
loopExecutorService.execute(() -> {
|
loopExecutorService.execute(() -> {
|
||||||
try {
|
try {
|
||||||
log.info("Intercept started");
|
log.info("Intercept started");
|
||||||
pcap.loop(-1, this); // использовать другой executor?
|
pcap.loop(-1, this); // использовать другой executor?
|
||||||
} catch (InterruptedException ignored) {
|
} catch (InterruptedException ignored) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
// выходим
|
// выходим
|
||||||
@@ -192,11 +191,10 @@ public class PcapWorker implements PacketListener {
|
|||||||
|
|
||||||
private UnfinishedStream addNewPacket(Inet4Address sourceIp, Inet4Address destIp,
|
private UnfinishedStream addNewPacket(Inet4Address sourceIp, Inet4Address destIp,
|
||||||
int sourcePort, int destPort, byte ttl, byte[] content, Protocol protocol) {
|
int sourcePort, int destPort, byte ttl, byte[] content, Protocol protocol) {
|
||||||
boolean incoming = destIp.equals(localIp);
|
var incoming = destIp.equals(localIp);
|
||||||
|
var stream = new UnfinishedStream(sourceIp, destIp, sourcePort, destPort, protocol);
|
||||||
|
|
||||||
UnfinishedStream stream = new UnfinishedStream(sourceIp, destIp, sourcePort, destPort, protocol);
|
var packet = ru.serega6531.packmate.model.Packet.builder()
|
||||||
|
|
||||||
ru.serega6531.packmate.model.Packet packet = ru.serega6531.packmate.model.Packet.builder()
|
|
||||||
.tempId(packetIdCounter++)
|
.tempId(packetIdCounter++)
|
||||||
.ttl(ttl)
|
.ttl(ttl)
|
||||||
.timestamp(System.currentTimeMillis())
|
.timestamp(System.currentTimeMillis())
|
||||||
@@ -204,8 +202,7 @@ public class PcapWorker implements PacketListener {
|
|||||||
.content(content)
|
.content(content)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
final ListMultimap<UnfinishedStream, ru.serega6531.packmate.model.Packet> streams =
|
final var streams = (protocol == Protocol.TCP) ? this.unfinishedTcpStreams : this.unfinishedUdpStreams;
|
||||||
(protocol == Protocol.TCP) ? this.unfinishedTcpStreams : this.unfinishedUdpStreams;
|
|
||||||
|
|
||||||
if (!streams.containsKey(stream)) {
|
if (!streams.containsKey(stream)) {
|
||||||
log.debug("Начат новый стрим");
|
log.debug("Начат новый стрим");
|
||||||
@@ -247,20 +244,18 @@ public class PcapWorker implements PacketListener {
|
|||||||
int streamsClosed = 0;
|
int streamsClosed = 0;
|
||||||
|
|
||||||
final long time = System.currentTimeMillis();
|
final long time = System.currentTimeMillis();
|
||||||
final ListMultimap<UnfinishedStream, ru.serega6531.packmate.model.Packet> streams =
|
final var streams = (protocol == Protocol.TCP) ? this.unfinishedTcpStreams : this.unfinishedUdpStreams;
|
||||||
(protocol == Protocol.TCP) ? this.unfinishedTcpStreams : this.unfinishedUdpStreams;
|
|
||||||
|
|
||||||
final Map<UnfinishedStream, List<ru.serega6531.packmate.model.Packet>> oldStreams =
|
final var oldStreams = Multimaps.asMap(streams).entrySet().stream()
|
||||||
Multimaps.asMap(streams).entrySet().stream()
|
.filter(entry -> {
|
||||||
.filter(entry -> {
|
final var packets = entry.getValue();
|
||||||
final List<ru.serega6531.packmate.model.Packet> packets = entry.getValue();
|
return time - packets.get(packets.size() - 1).getTimestamp() > timeoutMillis;
|
||||||
return time - packets.get(packets.size() - 1).getTimestamp() > timeoutMillis;
|
})
|
||||||
})
|
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
|
||||||
|
|
||||||
for (Map.Entry<UnfinishedStream, List<ru.serega6531.packmate.model.Packet>> entry : oldStreams.entrySet()) {
|
for (var entry : oldStreams.entrySet()) {
|
||||||
final UnfinishedStream stream = entry.getKey();
|
final UnfinishedStream stream = entry.getKey();
|
||||||
final List<ru.serega6531.packmate.model.Packet> packets = entry.getValue();
|
final var packets = entry.getValue();
|
||||||
|
|
||||||
if (streamService.saveNewStream(stream, packets)) {
|
if (streamService.saveNewStream(stream, packets)) {
|
||||||
streamsClosed++;
|
streamsClosed++;
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ public class PatternMatcher {
|
|||||||
|
|
||||||
private void match(Pattern pattern) {
|
private void match(Pattern pattern) {
|
||||||
if (pattern.getSearchType() == PatternSearchType.REGEX) {
|
if (pattern.getSearchType() == PatternSearchType.REGEX) {
|
||||||
final java.util.regex.Pattern regex = compilePattern(pattern);
|
final var regex = compilePattern(pattern);
|
||||||
final Matcher matcher = regex.matcher(content);
|
final Matcher matcher = regex.matcher(content);
|
||||||
int startPos = 0;
|
int startPos = 0;
|
||||||
|
|
||||||
@@ -99,7 +99,6 @@ public class PatternMatcher {
|
|||||||
return a <= x && x <= b;
|
return a <= x && x <= b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private java.util.regex.Pattern compilePattern(Pattern pattern) {
|
private java.util.regex.Pattern compilePattern(Pattern pattern) {
|
||||||
return compiledPatterns.computeIfAbsent(pattern.getValue(), java.util.regex.Pattern::compile);
|
return compiledPatterns.computeIfAbsent(pattern.getValue(), java.util.regex.Pattern::compile);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,14 +56,14 @@ public class StreamService {
|
|||||||
*/
|
*/
|
||||||
@Transactional
|
@Transactional
|
||||||
public boolean saveNewStream(UnfinishedStream unfinishedStream, List<Packet> packets) {
|
public boolean saveNewStream(UnfinishedStream unfinishedStream, List<Packet> packets) {
|
||||||
final Optional<CtfService> serviceOptional = servicesService.findService(
|
final var serviceOptional = servicesService.findService(
|
||||||
unfinishedStream.getFirstIp(),
|
unfinishedStream.getFirstIp(),
|
||||||
unfinishedStream.getFirstPort(),
|
unfinishedStream.getFirstPort(),
|
||||||
unfinishedStream.getSecondIp(),
|
unfinishedStream.getSecondIp(),
|
||||||
unfinishedStream.getSecondPort()
|
unfinishedStream.getSecondPort()
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!serviceOptional.isPresent()) {
|
if (serviceOptional.isEmpty()) {
|
||||||
log.warn("Не удалось сохранить стрим: сервиса на порту {} или {} не существует",
|
log.warn("Не удалось сохранить стрим: сервиса на порту {} или {} не существует",
|
||||||
unfinishedStream.getFirstPort(), unfinishedStream.getSecondPort());
|
unfinishedStream.getFirstPort(), unfinishedStream.getSecondPort());
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user