Поиск по subbytes
This commit is contained in:
@@ -27,7 +27,6 @@ dependencies {
|
||||
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.+'
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
|
||||
@@ -4,16 +4,18 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.security.crypto.codec.Hex;
|
||||
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.enums.PatternDirectionType;
|
||||
import ru.serega6531.packmate.model.Stream;
|
||||
import ru.serega6531.packmate.model.enums.PatternDirectionType;
|
||||
import ru.serega6531.packmate.model.enums.PatternSearchType;
|
||||
import ru.serega6531.packmate.model.enums.SubscriptionMessageType;
|
||||
import ru.serega6531.packmate.model.pojo.SubscriptionMessage;
|
||||
import ru.serega6531.packmate.repository.PatternRepository;
|
||||
import ru.serega6531.packmate.utils.Bytes;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -51,20 +53,19 @@ public class PatternService {
|
||||
}
|
||||
|
||||
public Set<FoundPattern> findMatches(byte[] bytes, boolean incoming) {
|
||||
String content = new String(bytes);
|
||||
|
||||
return patterns.values().stream()
|
||||
.filter(p -> p.getDirectionType() == (incoming ? PatternDirectionType.INPUT : PatternDirectionType.OUTPUT)
|
||||
|| p.getDirectionType() == PatternDirectionType.BOTH)
|
||||
.map(pattern -> match(pattern, content))
|
||||
.map(pattern -> match(pattern, bytes))
|
||||
.flatMap(List::stream)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private List<FoundPattern> match(Pattern pattern, String content) {
|
||||
private List<FoundPattern> match(Pattern pattern, byte[] bytes) {
|
||||
List<FoundPattern> found = new ArrayList<>();
|
||||
|
||||
if (pattern.getSearchType() == PatternSearchType.REGEX) {
|
||||
String content = new String(bytes);
|
||||
final java.util.regex.Pattern regex = compilePattern(pattern);
|
||||
final Matcher matcher = regex.matcher(content);
|
||||
|
||||
@@ -78,9 +79,10 @@ public class PatternService {
|
||||
|
||||
return found;
|
||||
} else if (pattern.getSearchType() == PatternSearchType.SUBSTRING) {
|
||||
String content = new String(bytes);
|
||||
int startSearch = 0;
|
||||
|
||||
final String value = pattern.getValue();
|
||||
|
||||
while (true) {
|
||||
int start = StringUtils.indexOfIgnoreCase(content, value, startSearch);
|
||||
|
||||
@@ -98,8 +100,25 @@ public class PatternService {
|
||||
startSearch = end + 1;
|
||||
}
|
||||
} else { // SUBBYTES
|
||||
// TODO
|
||||
return found;
|
||||
int startSearch = 0;
|
||||
final byte[] value = Hex.decode(pattern.getValue());
|
||||
|
||||
while (true) {
|
||||
int start = Bytes.indexOf(bytes, value, startSearch, bytes.length);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package ru.serega6531.packmate.service;
|
||||
|
||||
import com.google.common.primitives.Bytes;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
@@ -17,6 +16,7 @@ import ru.serega6531.packmate.model.pojo.Pagination;
|
||||
import ru.serega6531.packmate.model.pojo.SubscriptionMessage;
|
||||
import ru.serega6531.packmate.model.pojo.UnfinishedStream;
|
||||
import ru.serega6531.packmate.repository.StreamRepository;
|
||||
import ru.serega6531.packmate.utils.Bytes;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
26
src/main/java/ru/serega6531/packmate/utils/Bytes.java
Normal file
26
src/main/java/ru/serega6531/packmate/utils/Bytes.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package ru.serega6531.packmate.utils;
|
||||
|
||||
public class Bytes {
|
||||
|
||||
public static int indexOf(byte[] array, byte[] target) {
|
||||
return indexOf(array, target, 0, array.length);
|
||||
}
|
||||
|
||||
public static int indexOf(byte[] array, byte[] target, int start, int end) {
|
||||
if (target.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
outer:
|
||||
for (int i = start; i < end - target.length + 1; i++) {
|
||||
for (int j = 0; j < target.length; j++) {
|
||||
if (array[i + j] != target[j]) {
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user