Implement pattern removal

This commit is contained in:
Sergey
2023-04-27 23:19:16 +00:00
parent 145f3e63c8
commit 4cd5e72fee
6 changed files with 33 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
package ru.serega6531.packmate.controller; package ru.serega6531.packmate.controller;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
@@ -32,11 +33,16 @@ public class PatternController {
.toList(); .toList();
} }
@PostMapping("/{id}") @PostMapping("/{id}/enable")
public void enable(@PathVariable int id, @RequestParam boolean enabled) { public void enable(@PathVariable int id, @RequestParam boolean enabled) {
service.enable(id, enabled); service.enable(id, enabled);
} }
@DeleteMapping("/{id}")
public void delete(@PathVariable int id) {
service.delete(id);
}
@PostMapping("/{id}/lookback") @PostMapping("/{id}/lookback")
public void lookBack(@PathVariable int id, @RequestBody int minutes) { public void lookBack(@PathVariable int id, @RequestBody int minutes) {
if (minutes < 1) { if (minutes < 1) {

View File

@@ -1,5 +1,10 @@
package ru.serega6531.packmate.model; package ru.serega6531.packmate.model;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.Setter; import lombok.Setter;
@@ -11,14 +16,13 @@ import ru.serega6531.packmate.model.enums.PatternActionType;
import ru.serega6531.packmate.model.enums.PatternDirectionType; import ru.serega6531.packmate.model.enums.PatternDirectionType;
import ru.serega6531.packmate.model.enums.PatternSearchType; import ru.serega6531.packmate.model.enums.PatternSearchType;
import jakarta.persistence.*;
import java.util.Objects; import java.util.Objects;
@Getter @Getter
@Setter @Setter
@RequiredArgsConstructor @RequiredArgsConstructor
@ToString @ToString
@Entity @Entity(name = "pattern")
@GenericGenerator( @GenericGenerator(
name = "pattern_generator", name = "pattern_generator",
strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
@@ -34,8 +38,12 @@ public class Pattern {
@GeneratedValue(generator = "pattern_generator") @GeneratedValue(generator = "pattern_generator")
private Integer id; private Integer id;
@Column(nullable = false)
private boolean enabled; private boolean enabled;
@Column(nullable = false)
private boolean deleted = false;
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;

View File

@@ -2,7 +2,7 @@ package ru.serega6531.packmate.model.enums;
public enum SubscriptionMessageType { public enum SubscriptionMessageType {
SAVE_SERVICE, SAVE_PATTERN, SAVE_SERVICE, SAVE_PATTERN,
DELETE_SERVICE, DELETE_PATTERN, DELETE_SERVICE,
NEW_STREAM, NEW_STREAM,
FINISH_LOOKBACK, FINISH_LOOKBACK,
COUNTERS_UPDATE, COUNTERS_UPDATE,

View File

@@ -10,6 +10,7 @@ public class PatternDto {
private int id; private int id;
private boolean enabled; private boolean enabled;
private boolean deleted;
private String name; private String name;
private String value; private String value;
private String color; private String color;

View File

@@ -62,7 +62,7 @@ public class PatternService {
public Set<FoundPattern> findMatches(byte[] bytes, CtfService service, PatternDirectionType directionType, PatternActionType actionType) { public Set<FoundPattern> findMatches(byte[] bytes, CtfService service, PatternDirectionType directionType, PatternActionType actionType) {
final List<Pattern> list = patterns.values().stream() final List<Pattern> list = patterns.values().stream()
.filter(Pattern::isEnabled) .filter(pattern -> pattern.isEnabled() && !pattern.isDeleted())
.filter(p -> p.getServiceId() == null || p.getServiceId().equals(service.getPort())) .filter(p -> p.getServiceId() == null || p.getServiceId().equals(service.getPort()))
.filter(p -> p.getActionType() == actionType) .filter(p -> p.getActionType() == actionType)
.filter(p -> p.getDirectionType() == directionType || p.getDirectionType() == PatternDirectionType.BOTH) .filter(p -> p.getDirectionType() == directionType || p.getDirectionType() == PatternDirectionType.BOTH)
@@ -91,6 +91,18 @@ public class PatternService {
} }
} }
public void delete(int id) {
final Pattern pattern = find(id);
if (pattern != null) {
pattern.setDeleted(true);
final Pattern saved = repository.save(pattern);
patterns.put(id, saved);
log.info("Deleted pattern '{}' with value '{}'", pattern.getName(), pattern.getValue());
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.SAVE_PATTERN, toDto(saved)));
}
}
public Pattern save(Pattern pattern) { public Pattern save(Pattern pattern) {
try { try {
PatternMatcher.compilePattern(pattern); PatternMatcher.compilePattern(pattern);