Добавлен вывод позиций найденных паттернов
This commit is contained in:
@@ -6,12 +6,14 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.serega6531.packmate.model.FoundPattern;
|
||||
import ru.serega6531.packmate.model.Pattern;
|
||||
import ru.serega6531.packmate.model.PatternType;
|
||||
import ru.serega6531.packmate.model.Stream;
|
||||
import ru.serega6531.packmate.repository.PatternRepository;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@@ -32,26 +34,61 @@ public class PatternService {
|
||||
log.info("Loaded {} patterns", patterns.size());
|
||||
}
|
||||
|
||||
public Pattern find(int id) {
|
||||
return patterns.get(id);
|
||||
}
|
||||
|
||||
public Collection<Pattern> findAll() {
|
||||
return patterns.values();
|
||||
}
|
||||
|
||||
public List<Pattern> findMatching(byte[] bytes, boolean incoming) {
|
||||
public Set<FoundPattern> findMatches(byte[] bytes, boolean incoming) {
|
||||
String content = new String(bytes);
|
||||
|
||||
return patterns.values().stream()
|
||||
.filter(p -> p.getType() == (incoming ? PatternType.INPUT : PatternType.OUTPUT)
|
||||
|| p.getType() == PatternType.BOTH)
|
||||
.filter(pattern -> matches(pattern, content))
|
||||
.collect(Collectors.toList());
|
||||
.map(pattern -> match(pattern, content))
|
||||
.flatMap(List::stream)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private boolean matches(Pattern pattern, String content) {
|
||||
private List<FoundPattern> match(Pattern pattern, String content) {
|
||||
List<FoundPattern> found = new ArrayList<>();
|
||||
|
||||
if (pattern.isRegex()) {
|
||||
final java.util.regex.Pattern regex = compilePattern(pattern);
|
||||
return regex.matcher(content).find();
|
||||
final Matcher matcher = regex.matcher(content);
|
||||
|
||||
while (matcher.find()) {
|
||||
found.add(FoundPattern.builder()
|
||||
.patternId(pattern.getId())
|
||||
.startPosition(matcher.start())
|
||||
.endPosition(matcher.end())
|
||||
.build());
|
||||
}
|
||||
|
||||
return found;
|
||||
} else {
|
||||
return StringUtils.containsIgnoreCase(content, pattern.getValue());
|
||||
int startSearch = 0;
|
||||
|
||||
final String value = pattern.getValue();
|
||||
while (true) {
|
||||
int start = StringUtils.indexOfIgnoreCase(content, value, startSearch);
|
||||
|
||||
if (start == -1) {
|
||||
return found;
|
||||
}
|
||||
|
||||
int end = start + value.length() - 1;
|
||||
found.add(FoundPattern.builder()
|
||||
.patternId(pattern.getId())
|
||||
.startPosition(start)
|
||||
.endPosition(end)
|
||||
.build());
|
||||
|
||||
startSearch = end + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +105,7 @@ public class PatternService {
|
||||
}
|
||||
|
||||
pattern.getMatchedStreams().clear();
|
||||
patterns.remove(pattern.getId());
|
||||
patterns.remove(id);
|
||||
compiledPatterns.remove(pattern.getValue());
|
||||
repository.delete(pattern);
|
||||
}
|
||||
@@ -76,8 +113,9 @@ public class PatternService {
|
||||
|
||||
public Pattern save(Pattern pattern) {
|
||||
log.info("Добавлен новый паттерн {} со значением {}", pattern.getName(), pattern.getValue());
|
||||
patterns.put(pattern.getId(), pattern);
|
||||
return repository.save(pattern);
|
||||
final Pattern saved = repository.save(pattern);
|
||||
patterns.put(saved.getId(), pattern);
|
||||
return saved;
|
||||
}
|
||||
|
||||
private java.util.regex.Pattern compilePattern(Pattern pattern) {
|
||||
|
||||
@@ -50,9 +50,10 @@ public class ServicesService {
|
||||
}
|
||||
|
||||
public CtfService save(CtfService service) {
|
||||
log.info("Добавлен новый сервис {} на порту {}", service.getName(), service.getPort());
|
||||
services.put(service.getPort(), service);
|
||||
return repository.save(service);
|
||||
log.info("Добавлен или изменен сервис {} на порту {}", service.getName(), service.getPort());
|
||||
final CtfService saved = repository.save(service);
|
||||
services.put(saved.getPort(), service);
|
||||
return saved;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import java.util.zip.ZipException;
|
||||
|
||||
@@ -85,7 +86,7 @@ public class StreamService {
|
||||
stream.setTtl(firstIncoming.isPresent() ? firstIncoming.get().getTtl() : 0);
|
||||
stream.setStartTimestamp(packets.get(0).getTimestamp());
|
||||
stream.setEndTimestamp(packets.get(packets.size() - 1).getTimestamp());
|
||||
stream.setService(serviceOptional.get());
|
||||
stream.setService(serviceOptional.get().getPort());
|
||||
|
||||
if (ignoreEmptyPackets) {
|
||||
packets.removeIf(packet -> packet.getContent().length == 0);
|
||||
@@ -174,14 +175,19 @@ public class StreamService {
|
||||
|
||||
Stream savedStream = save(stream);
|
||||
|
||||
Set<Pattern> matches = new HashSet<>();
|
||||
Set<Pattern> foundPatterns = new HashSet<>();
|
||||
|
||||
for (ru.serega6531.packmate.model.Packet packet : packets) {
|
||||
packet.setStream(savedStream);
|
||||
matches.addAll(patternService.findMatching(packet.getContent(), packet.isIncoming()));
|
||||
final Set<FoundPattern> matches = patternService.findMatches(packet.getContent(), packet.isIncoming());
|
||||
packet.setMatches(matches);
|
||||
foundPatterns.addAll(matches.stream()
|
||||
.map(FoundPattern::getPatternId)
|
||||
.map(patternService::find)
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
savedStream.setFoundPatterns(new ArrayList<>(matches));
|
||||
savedStream.setFoundPatterns(foundPatterns);
|
||||
savedStream.setPackets(packetService.saveAll(packets));
|
||||
savedStream = save(savedStream);
|
||||
|
||||
@@ -277,7 +283,7 @@ public class StreamService {
|
||||
}
|
||||
}
|
||||
|
||||
public List<Stream> findFavoritesByService(Pagination pagination, CtfService service) {
|
||||
public List<Stream> findFavoritesByService(Pagination pagination, int service) {
|
||||
PageRequest page = PageRequest.of(0, pagination.getPageSize(), pagination.getDirection(), "id");
|
||||
|
||||
if (pagination.getPattern() != null) { // задан паттерн для поиска
|
||||
@@ -313,7 +319,7 @@ public class StreamService {
|
||||
}
|
||||
}
|
||||
|
||||
public List<Stream> findAllByService(Pagination pagination, CtfService service) {
|
||||
public List<Stream> findAllByService(Pagination pagination, int service) {
|
||||
PageRequest page = PageRequest.of(0, pagination.getPageSize(), pagination.getDirection(), "id");
|
||||
|
||||
if (pagination.getPattern() != null) { // задан паттерн для поиска
|
||||
|
||||
Reference in New Issue
Block a user