Добавлены DTO для всех entity

This commit is contained in:
serega6531
2020-12-29 14:36:17 +03:00
parent 19ccd3f200
commit 625848cf23
17 changed files with 179 additions and 46 deletions

View File

@@ -35,6 +35,7 @@ dependencies {
compile group: 'org.java-websocket', name: 'Java-WebSocket', version: '1.5.1' compile group: 'org.java-websocket', name: 'Java-WebSocket', version: '1.5.1'
compile group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.65' compile group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.65'
compile group: 'org.bouncycastle', name: 'bctls-jdk15on', version: '1.65' compile group: 'org.bouncycastle', name: 'bctls-jdk15on', version: '1.65'
compile group: 'org.modelmapper', name: 'modelmapper', version: '2.3.0'
compileOnly 'org.projectlombok:lombok' compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'org.postgresql:postgresql' runtimeOnly 'org.postgresql:postgresql'

View File

@@ -5,13 +5,14 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import ru.serega6531.packmate.model.Packet;
import ru.serega6531.packmate.model.Stream; import ru.serega6531.packmate.model.Stream;
import ru.serega6531.packmate.model.pojo.PacketDto;
import ru.serega6531.packmate.service.StreamService; import ru.serega6531.packmate.service.StreamService;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
@RestController @RestController
@RequestMapping("/api/packet/") @RequestMapping("/api/packet/")
@@ -25,10 +26,12 @@ public class PacketController {
} }
@PostMapping("/{streamId}") @PostMapping("/{streamId}")
public List<Packet> getPacketsForStream(@PathVariable long streamId) { public List<PacketDto> getPacketsForStream(@PathVariable long streamId) {
final Optional<Stream> stream = streamService.find(streamId); final Optional<Stream> stream = streamService.find(streamId);
if (stream.isPresent()) { if (stream.isPresent()) {
return stream.get().getPackets(); return stream.get().getPackets().stream()
.map(streamService::packetToDto)
.collect(Collectors.toList());
} else { } else {
return Collections.emptyList(); return Collections.emptyList();
} }

View File

@@ -3,9 +3,11 @@ 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.*; import org.springframework.web.bind.annotation.*;
import ru.serega6531.packmate.model.Pattern; import ru.serega6531.packmate.model.Pattern;
import ru.serega6531.packmate.model.pojo.PatternDto;
import ru.serega6531.packmate.service.PatternService; import ru.serega6531.packmate.service.PatternService;
import java.util.Collection; import java.util.List;
import java.util.stream.Collectors;
@RestController @RestController
@RequestMapping("/api/pattern/") @RequestMapping("/api/pattern/")
@@ -19,8 +21,10 @@ public class PatternController {
} }
@GetMapping @GetMapping
public Collection<Pattern> getPatterns() { public List<PatternDto> getPatterns() {
return service.findAll(); return service.findAll()
.stream().map(service::toDto)
.collect(Collectors.toList());
} }
@PostMapping("/{id}") @PostMapping("/{id}")
@@ -29,9 +33,11 @@ public class PatternController {
} }
@PostMapping @PostMapping
public Pattern addPattern(@RequestBody Pattern pattern) { public PatternDto addPattern(@RequestBody PatternDto dto) {
pattern.setEnabled(true); dto.setEnabled(true);
return service.save(pattern); Pattern pattern = service.fromDto(dto);
Pattern saved = service.save(pattern);
return service.toDto(saved);
} }
} }

View File

@@ -3,9 +3,11 @@ 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.*; import org.springframework.web.bind.annotation.*;
import ru.serega6531.packmate.model.CtfService; import ru.serega6531.packmate.model.CtfService;
import ru.serega6531.packmate.model.pojo.ServiceDto;
import ru.serega6531.packmate.service.ServicesService; import ru.serega6531.packmate.service.ServicesService;
import java.util.Collection; import java.util.List;
import java.util.stream.Collectors;
@RestController @RestController
@RequestMapping("/api/service/") @RequestMapping("/api/service/")
@@ -19,8 +21,10 @@ public class ServiceController {
} }
@GetMapping @GetMapping
public Collection<CtfService> getServices() { public List<ServiceDto> getServices() {
return service.findAll(); return service.findAll().stream()
.map(service::toDto)
.collect(Collectors.toList());
} }
@DeleteMapping("/{port}") @DeleteMapping("/{port}")
@@ -29,8 +33,9 @@ public class ServiceController {
} }
@PostMapping @PostMapping
public CtfService addService(@RequestBody CtfService ctfService) { public CtfService addService(@RequestBody ServiceDto dto) {
return service.save(ctfService); CtfService newService = this.service.fromDto(dto);
return this.service.save(newService);
} }
} }

View File

@@ -2,42 +2,47 @@ 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.*; import org.springframework.web.bind.annotation.*;
import ru.serega6531.packmate.model.Stream;
import ru.serega6531.packmate.model.pojo.Pagination; import ru.serega6531.packmate.model.pojo.Pagination;
import ru.serega6531.packmate.model.pojo.StreamDto;
import ru.serega6531.packmate.service.StreamService; import ru.serega6531.packmate.service.StreamService;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
@RestController @RestController
@RequestMapping("/api/stream/") @RequestMapping("/api/stream/")
public class StreamController { public class StreamController {
private final StreamService streamService; private final StreamService service;
@Autowired @Autowired
public StreamController(StreamService streamService) { public StreamController(StreamService service) {
this.streamService = streamService; this.service = service;
} }
@PostMapping("/all") @PostMapping("/all")
public List<Stream> getStreams(@RequestBody Pagination pagination) { public List<StreamDto> getStreams(@RequestBody Pagination pagination) {
return streamService.findAll(pagination, Optional.empty(), pagination.isFavorites()); return service.findAll(pagination, Optional.empty(), pagination.isFavorites()).stream()
.map(service::streamToDto)
.collect(Collectors.toList());
} }
@PostMapping("/{port}") @PostMapping("/{port}")
public List<Stream> getStreams(@PathVariable int port, @RequestBody Pagination pagination) { public List<StreamDto> getStreams(@PathVariable int port, @RequestBody Pagination pagination) {
return streamService.findAll(pagination, Optional.of(port), pagination.isFavorites()); return service.findAll(pagination, Optional.of(port), pagination.isFavorites()).stream()
.map(service::streamToDto)
.collect(Collectors.toList());
} }
@PostMapping("/{id}/favorite") @PostMapping("/{id}/favorite")
public void favoriteStream(@PathVariable long id) { public void favoriteStream(@PathVariable long id) {
streamService.setFavorite(id, true); service.setFavorite(id, true);
} }
@PostMapping("/{id}/unfavorite") @PostMapping("/{id}/unfavorite")
public void unfavoriteStream(@PathVariable long id) { public void unfavoriteStream(@PathVariable long id) {
streamService.setFavorite(id, false); service.setFavorite(id, false);
} }
} }

View File

@@ -1,6 +1,5 @@
package ru.serega6531.packmate.model; package ru.serega6531.packmate.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*; import lombok.*;
import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.GenericGenerator;
@@ -28,10 +27,12 @@ public class FoundPattern {
@Id @Id
@GeneratedValue(generator = "found_pattern_generator") @GeneratedValue(generator = "found_pattern_generator")
@JsonIgnore
private int id; private int id;
private int patternId; private int patternId;
private int startPosition; private int startPosition;
private int endPosition; private int endPosition;
} }

View File

@@ -1,6 +1,5 @@
package ru.serega6531.packmate.model; package ru.serega6531.packmate.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
@@ -32,16 +31,13 @@ public class Packet {
private Long id; private Long id;
@Transient @Transient
@JsonIgnore
private Long tempId; private Long tempId;
@Transient @Transient
@JsonIgnore
private byte ttl; private byte ttl;
@ManyToOne @ManyToOne
@JoinColumn(name = "stream_id", nullable = false) @JoinColumn(name = "stream_id", nullable = false)
@JsonIgnore
private Stream stream; private Stream stream;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@@ -60,7 +56,6 @@ public class Packet {
private byte[] content; private byte[] content;
@Transient @Transient
@JsonIgnore
public String getContentString() { public String getContentString() {
return new String(content); return new String(content);
} }

View File

@@ -1,6 +1,5 @@
package ru.serega6531.packmate.model; package ru.serega6531.packmate.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data; import lombok.Data;
import lombok.ToString; import lombok.ToString;
import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.GenericGenerator;
@@ -41,7 +40,6 @@ public class Pattern {
private PatternDirectionType directionType; private PatternDirectionType directionType;
@ManyToMany(mappedBy = "foundPatterns", fetch = FetchType.LAZY) @ManyToMany(mappedBy = "foundPatterns", fetch = FetchType.LAZY)
@JsonIgnore
private List<Stream> matchedStreams; private List<Stream> matchedStreams;
} }

View File

@@ -1,6 +1,5 @@
package ru.serega6531.packmate.model; package ru.serega6531.packmate.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data; import lombok.Data;
import lombok.ToString; import lombok.ToString;
import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.GenericGenerator;
@@ -34,7 +33,6 @@ public class Stream {
private Protocol protocol; private Protocol protocol;
@OneToMany(mappedBy = "stream", cascade = CascadeType.ALL) @OneToMany(mappedBy = "stream", cascade = CascadeType.ALL)
@JsonIgnore
@OrderBy("id") @OrderBy("id")
private List<Packet> packets; private List<Packet> packets;

View File

@@ -0,0 +1,12 @@
package ru.serega6531.packmate.model.pojo;
import lombok.Data;
@Data
public class FoundPatternDto {
private int patternId;
private int startPosition;
private int endPosition;
}

View File

@@ -0,0 +1,19 @@
package ru.serega6531.packmate.model.pojo;
import lombok.Data;
import java.util.Set;
@Data
public class PacketDto {
private Long id;
private Set<FoundPatternDto> matches;
private long timestamp;
private boolean incoming;
private boolean ungzipped;
private boolean webSocketParsed;
private boolean tlsDecrypted;
private byte[] content;
}

View File

@@ -0,0 +1,18 @@
package ru.serega6531.packmate.model.pojo;
import lombok.Data;
import ru.serega6531.packmate.model.enums.PatternDirectionType;
import ru.serega6531.packmate.model.enums.PatternSearchType;
@Data
public class PatternDto {
private int id;
private boolean enabled;
private String name;
private String value;
private String color; // для вставки в css
private PatternSearchType searchType;
private PatternDirectionType directionType;
}

View File

@@ -0,0 +1,17 @@
package ru.serega6531.packmate.model.pojo;
import lombok.Data;
@Data
public class ServiceDto {
private int port;
private String name;
private boolean decryptTls;
private boolean processChunkedEncoding;
private boolean ungzipHttp;
private boolean urldecodeHttpRequests;
private boolean mergeAdjacentPackets;
private boolean parseWebSockets;
}

View File

@@ -0,0 +1,21 @@
package ru.serega6531.packmate.model.pojo;
import lombok.Data;
import ru.serega6531.packmate.model.enums.Protocol;
import java.util.Set;
@Data
public class StreamDto {
private Long id;
private int service;
private Protocol protocol;
private long startTimestamp;
private long endTimestamp;
private Set<PatternDto> foundPatterns;
private boolean favorite;
private byte ttl;
private String userAgentHash;
}

View File

@@ -1,6 +1,7 @@
package ru.serega6531.packmate.service; package ru.serega6531.packmate.service;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.serega6531.packmate.model.FoundPattern; import ru.serega6531.packmate.model.FoundPattern;
@@ -8,6 +9,7 @@ import ru.serega6531.packmate.model.Pattern;
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 ru.serega6531.packmate.model.enums.SubscriptionMessageType; import ru.serega6531.packmate.model.enums.SubscriptionMessageType;
import ru.serega6531.packmate.model.pojo.PatternDto;
import ru.serega6531.packmate.model.pojo.SubscriptionMessage; import ru.serega6531.packmate.model.pojo.SubscriptionMessage;
import ru.serega6531.packmate.repository.PatternRepository; import ru.serega6531.packmate.repository.PatternRepository;
@@ -23,6 +25,7 @@ public class PatternService {
private final SubscriptionService subscriptionService; private final SubscriptionService subscriptionService;
private final Map<Integer, Pattern> patterns = new HashMap<>(); private final Map<Integer, Pattern> patterns = new HashMap<>();
private final ModelMapper modelMapper = new ModelMapper();
@Autowired @Autowired
public PatternService(PatternRepository repository, public PatternService(PatternRepository repository,
@@ -80,8 +83,16 @@ public class PatternService {
final Pattern saved = repository.save(pattern); final Pattern saved = repository.save(pattern);
patterns.put(saved.getId(), saved); patterns.put(saved.getId(), saved);
log.info("Added new pattern '{}' with value '{}'", pattern.getName(), pattern.getValue()); log.info("Added new pattern '{}' with value '{}'", pattern.getName(), pattern.getValue());
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.SAVE_PATTERN, saved)); subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.SAVE_PATTERN, toDto(saved)));
return saved; return saved;
} }
public Pattern fromDto(PatternDto dto) {
return modelMapper.map(dto, Pattern.class);
}
public PatternDto toDto(Pattern pattern) {
return modelMapper.map(pattern, PatternDto.class);
}
} }

View File

@@ -1,12 +1,14 @@
package ru.serega6531.packmate.service; package ru.serega6531.packmate.service;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.serega6531.packmate.model.CtfService; import ru.serega6531.packmate.model.CtfService;
import ru.serega6531.packmate.model.enums.SubscriptionMessageType; import ru.serega6531.packmate.model.enums.SubscriptionMessageType;
import ru.serega6531.packmate.model.pojo.ServiceDto;
import ru.serega6531.packmate.model.pojo.SubscriptionMessage; import ru.serega6531.packmate.model.pojo.SubscriptionMessage;
import ru.serega6531.packmate.repository.ServiceRepository; import ru.serega6531.packmate.repository.ServiceRepository;
@@ -29,6 +31,7 @@ public class ServicesService {
private final InetAddress localIp; private final InetAddress localIp;
private final Map<Integer, CtfService> services = new HashMap<>(); private final Map<Integer, CtfService> services = new HashMap<>();
private final ModelMapper modelMapper = new ModelMapper();
@Autowired @Autowired
public ServicesService(ServiceRepository repository, public ServicesService(ServiceRepository repository,
@@ -44,6 +47,10 @@ public class ServicesService {
log.info("Loaded {} services", services.size()); log.info("Loaded {} services", services.size());
} }
public CtfService find(int id) {
return services.get(id);
}
public Optional<CtfService> findService(Inet4Address firstIp, int firstPort, Inet4Address secondIp, int secondPort) { public Optional<CtfService> findService(Inet4Address firstIp, int firstPort, Inet4Address secondIp, int secondPort) {
if (firstIp.equals(localIp)) { if (firstIp.equals(localIp)) {
return findByPort(firstPort); return findByPort(firstPort);
@@ -79,11 +86,19 @@ public class ServicesService {
final CtfService saved = repository.save(service); final CtfService saved = repository.save(service);
services.put(saved.getPort(), saved); services.put(saved.getPort(), saved);
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.SAVE_SERVICE, saved)); subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.SAVE_SERVICE, toDto(saved)));
pcapService.updateFilter(findAll()); pcapService.updateFilter(findAll());
return saved; return saved;
} }
public ServiceDto toDto(CtfService service) {
return modelMapper.map(service, ServiceDto.class);
}
public CtfService fromDto(ServiceDto dto) {
return modelMapper.map(dto, CtfService.class);
}
} }

View File

@@ -1,6 +1,7 @@
package ru.serega6531.packmate.service; package ru.serega6531.packmate.service;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
@@ -11,9 +12,7 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import ru.serega6531.packmate.model.*; import ru.serega6531.packmate.model.*;
import ru.serega6531.packmate.model.enums.SubscriptionMessageType; import ru.serega6531.packmate.model.enums.SubscriptionMessageType;
import ru.serega6531.packmate.model.pojo.Pagination; import ru.serega6531.packmate.model.pojo.*;
import ru.serega6531.packmate.model.pojo.SubscriptionMessage;
import ru.serega6531.packmate.model.pojo.UnfinishedStream;
import ru.serega6531.packmate.repository.StreamRepository; import ru.serega6531.packmate.repository.StreamRepository;
import ru.serega6531.packmate.service.optimization.RsaKeysHolder; import ru.serega6531.packmate.service.optimization.RsaKeysHolder;
import ru.serega6531.packmate.service.optimization.StreamOptimizer; import ru.serega6531.packmate.service.optimization.StreamOptimizer;
@@ -39,6 +38,7 @@ public class StreamService {
private final boolean ignoreEmptyPackets; private final boolean ignoreEmptyPackets;
private final java.util.regex.Pattern userAgentPattern = java.util.regex.Pattern.compile("User-Agent: (.+)\\r\\n"); private final java.util.regex.Pattern userAgentPattern = java.util.regex.Pattern.compile("User-Agent: (.+)\\r\\n");
private final ModelMapper modelMapper = new ModelMapper();
@Autowired @Autowired
public StreamService(StreamRepository repository, public StreamService(StreamRepository repository,
@@ -108,7 +108,7 @@ public class StreamService {
savedStream.setPackets(packets); savedStream.setPackets(packets);
savedStream = save(savedStream); savedStream = save(savedStream);
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.NEW_STREAM, savedStream)); subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.NEW_STREAM, streamToDto(savedStream)));
return true; return true;
} }
@@ -132,7 +132,7 @@ public class StreamService {
char[] alphabet = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray(); char[] alphabet = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
int l = alphabet.length; int l = alphabet.length;
int hashCode = ua.hashCode(); int hashCode = ua.hashCode();
if(hashCode == Integer.MIN_VALUE) { // abs(MIN_VALUE) вернет то же значение if (hashCode == Integer.MIN_VALUE) { // abs(MIN_VALUE) вернет то же значение
hashCode = Integer.MAX_VALUE; hashCode = Integer.MAX_VALUE;
} }
final int hash = Math.abs(hashCode) % (l * l * l); final int hash = Math.abs(hashCode) % (l * l * l);
@@ -182,27 +182,35 @@ public class StreamService {
PageRequest page = PageRequest.of(0, pagination.getPageSize(), pagination.getDirection(), "id"); PageRequest page = PageRequest.of(0, pagination.getPageSize(), pagination.getDirection(), "id");
Specification<Stream> spec; Specification<Stream> spec;
if(pagination.getDirection() == Sort.Direction.ASC) { if (pagination.getDirection() == Sort.Direction.ASC) {
spec = streamIdGreaterThan(pagination.getStartingFrom()); spec = streamIdGreaterThan(pagination.getStartingFrom());
} else { } else {
spec = streamIdLessThan(pagination.getStartingFrom()); spec = streamIdLessThan(pagination.getStartingFrom());
} }
if(service.isPresent()) { if (service.isPresent()) {
spec = spec.and(streamServiceEquals(service.get())); spec = spec.and(streamServiceEquals(service.get()));
} }
if(onlyFavorites) { if (onlyFavorites) {
spec = spec.and(streamIsFavorite()); spec = spec.and(streamIsFavorite());
} }
if(pagination.getPattern() != null) { if (pagination.getPattern() != null) {
spec = spec.and(streamPatternsContains(pagination.getPattern())); spec = spec.and(streamPatternsContains(pagination.getPattern()));
} }
return repository.findAll(spec, page).getContent(); return repository.findAll(spec, page).getContent();
} }
public StreamDto streamToDto(Stream stream) {
return modelMapper.map(stream, StreamDto.class);
}
public PacketDto packetToDto(Packet packet) {
return modelMapper.map(packet, PacketDto.class);
}
private Specification<Stream> streamServiceEquals(long service) { private Specification<Stream> streamServiceEquals(long service) {
return (root, query, cb) -> cb.equal(root.get("service"), service); return (root, query, cb) -> cb.equal(root.get("service"), service);
} }