Рефакторинг

This commit is contained in:
serega6531
2020-02-04 23:06:44 +03:00
parent bc847f2045
commit 5d4ab29f98
4 changed files with 15 additions and 13 deletions

View File

@@ -27,8 +27,8 @@ dependencies {
implementation 'org.springframework.session:spring-session-core' implementation 'org.springframework.session:spring-session-core'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7' compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7'
compile group: 'commons-io', name: 'commons-io', version: '2.6' compile group: 'commons-io', name: 'commons-io', version: '2.6'
compile 'org.pcap4j:pcap4j-core:1.+' compile 'org.pcap4j:pcap4j-core:1.8.2'
compile 'org.pcap4j:pcap4j-packetfactory-static:1.+' compile 'org.pcap4j:pcap4j-packetfactory-static:1.8.2'
compile group: 'com.google.guava', name: 'guava', version: '28.2-jre' compile group: 'com.google.guava', name: 'guava', version: '28.2-jre'
compileOnly 'org.projectlombok:lombok' compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'org.springframework.boot:spring-boot-devtools'

View File

@@ -33,7 +33,8 @@ public class ApplicationConfiguration extends WebSecurityConfigurerAdapter imple
@Autowired @Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication() auth.inMemoryAuthentication()
.withUser(login).password(passwordEncoder().encode(password)) .withUser(login)
.password(passwordEncoder().encode(password))
.authorities("ROLE_USER"); .authorities("ROLE_USER");
} }

View File

@@ -44,7 +44,7 @@ public class PcapWorker implements PacketListener {
private final ListMultimap<UnfinishedStream, ru.serega6531.packmate.model.Packet> unfinishedStreams = ArrayListMultimap.create(); private final ListMultimap<UnfinishedStream, ru.serega6531.packmate.model.Packet> unfinishedStreams = ArrayListMultimap.create();
// в следующих мапах в Set находится srcIp соответствующего пакета // в следующих мапах в значениях находится srcIp соответствующего пакета
private final SetMultimap<UnfinishedStream, ImmutablePair<Inet4Address, Integer>> fins = HashMultimap.create(); private final SetMultimap<UnfinishedStream, ImmutablePair<Inet4Address, Integer>> fins = HashMultimap.create();
private final SetMultimap<UnfinishedStream, ImmutablePair<Inet4Address, Integer>> acks = HashMultimap.create(); private final SetMultimap<UnfinishedStream, ImmutablePair<Inet4Address, Integer>> acks = HashMultimap.create();

View File

@@ -58,8 +58,7 @@ class StreamOptimizer {
Packet packet = packets.get(i); Packet packet = packets.get(i);
if (packet.isIncoming() != incoming) { if (packet.isIncoming() != incoming) {
if (packetsInRow > 1) { if (packetsInRow > 1) {
final List<Packet> cut = packets.subList(start, i); compress(start, i);
compress(cut, incoming);
i++; // продвигаем указатель на следующий после склеенного блок i++; // продвигаем указатель на следующий после склеенного блок
} }
@@ -73,17 +72,18 @@ class StreamOptimizer {
} }
if (packetsInRow > 1) { if (packetsInRow > 1) {
final List<Packet> cut = packets.subList(start, packets.size()); compress(start, packets.size());
compress(cut, incoming);
} }
} }
/** /**
* Сжать кусок cut в один пакет * Сжать кусок со start по end в один пакет
*/ */
private void compress(List<Packet> cut, boolean incoming) { private void compress(int start, int end) {
final List<Packet> cut = packets.subList(start, end);
final long timestamp = cut.get(0).getTimestamp(); final long timestamp = cut.get(0).getTimestamp();
final boolean ungzipped = cut.stream().anyMatch(Packet::isUngzipped); final boolean ungzipped = cut.stream().anyMatch(Packet::isUngzipped);
boolean incoming = cut.get(0).isIncoming();
//noinspection OptionalGetWithoutIsPresent //noinspection OptionalGetWithoutIsPresent
final byte[] content = cut.stream() final byte[] content = cut.stream()
.map(Packet::getContent) .map(Packet::getContent)
@@ -91,7 +91,7 @@ class StreamOptimizer {
.get(); .get();
packets.removeAll(cut); packets.removeAll(cut);
packets.add(Packet.builder() packets.add(start, Packet.builder()
.incoming(incoming) .incoming(incoming)
.timestamp(timestamp) .timestamp(timestamp)
.ungzipped(ungzipped) .ungzipped(ungzipped)
@@ -140,7 +140,7 @@ class StreamOptimizer {
if (packet.isIncoming() && gzipStarted) { // поток gzip закончился if (packet.isIncoming() && gzipStarted) { // поток gzip закончился
gzipEndPacket = i - 1; gzipEndPacket = i - 1;
if(extractGzip(gzipStartPacket, gzipEndPacket)) { if (extractGzip(gzipStartPacket, gzipEndPacket)) {
gzipStarted = false; gzipStarted = false;
i = gzipStartPacket + 1; // продвигаем указатель на следующий после склеенного блок i = gzipStartPacket + 1; // продвигаем указатель на следующий после склеенного блок
} }
@@ -152,7 +152,7 @@ class StreamOptimizer {
if (http && gzipStarted) { // начался новый http пакет, заканчиваем старый gzip поток if (http && gzipStarted) { // начался новый http пакет, заканчиваем старый gzip поток
gzipEndPacket = i - 1; gzipEndPacket = i - 1;
if(extractGzip(gzipStartPacket, gzipEndPacket)) { if (extractGzip(gzipStartPacket, gzipEndPacket)) {
gzipStarted = false; gzipStarted = false;
i = gzipStartPacket + 1; // продвигаем указатель на следующий после склеенного блок i = gzipStartPacket + 1; // продвигаем указатель на следующий после склеенного блок
} }
@@ -176,6 +176,7 @@ class StreamOptimizer {
/** /**
* Попытаться распаковать кусок пакетов с gzip body и вставить результат на их место * Попытаться распаковать кусок пакетов с gzip body и вставить результат на их место
*
* @return получилось ли распаковать * @return получилось ли распаковать
*/ */
private boolean extractGzip(int gzipStartPacket, int gzipEndPacket) { private boolean extractGzip(int gzipStartPacket, int gzipEndPacket) {