Добавлены DTO для всех entity
This commit is contained in:
@@ -35,6 +35,7 @@ dependencies {
|
||||
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: 'bctls-jdk15on', version: '1.65'
|
||||
compile group: 'org.modelmapper', name: 'modelmapper', version: '2.3.0'
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
|
||||
runtimeOnly 'org.postgresql:postgresql'
|
||||
|
||||
@@ -5,13 +5,14 @@ import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.serega6531.packmate.model.Packet;
|
||||
import ru.serega6531.packmate.model.Stream;
|
||||
import ru.serega6531.packmate.model.pojo.PacketDto;
|
||||
import ru.serega6531.packmate.service.StreamService;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/packet/")
|
||||
@@ -25,10 +26,12 @@ public class PacketController {
|
||||
}
|
||||
|
||||
@PostMapping("/{streamId}")
|
||||
public List<Packet> getPacketsForStream(@PathVariable long streamId) {
|
||||
public List<PacketDto> getPacketsForStream(@PathVariable long streamId) {
|
||||
final Optional<Stream> stream = streamService.find(streamId);
|
||||
if (stream.isPresent()) {
|
||||
return stream.get().getPackets();
|
||||
return stream.get().getPackets().stream()
|
||||
.map(streamService::packetToDto)
|
||||
.collect(Collectors.toList());
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@@ -3,9 +3,11 @@ package ru.serega6531.packmate.controller;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.serega6531.packmate.model.Pattern;
|
||||
import ru.serega6531.packmate.model.pojo.PatternDto;
|
||||
import ru.serega6531.packmate.service.PatternService;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/pattern/")
|
||||
@@ -19,8 +21,10 @@ public class PatternController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public Collection<Pattern> getPatterns() {
|
||||
return service.findAll();
|
||||
public List<PatternDto> getPatterns() {
|
||||
return service.findAll()
|
||||
.stream().map(service::toDto)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@PostMapping("/{id}")
|
||||
@@ -29,9 +33,11 @@ public class PatternController {
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Pattern addPattern(@RequestBody Pattern pattern) {
|
||||
pattern.setEnabled(true);
|
||||
return service.save(pattern);
|
||||
public PatternDto addPattern(@RequestBody PatternDto dto) {
|
||||
dto.setEnabled(true);
|
||||
Pattern pattern = service.fromDto(dto);
|
||||
Pattern saved = service.save(pattern);
|
||||
return service.toDto(saved);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,9 +3,11 @@ package ru.serega6531.packmate.controller;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.serega6531.packmate.model.CtfService;
|
||||
import ru.serega6531.packmate.model.pojo.ServiceDto;
|
||||
import ru.serega6531.packmate.service.ServicesService;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/service/")
|
||||
@@ -19,8 +21,10 @@ public class ServiceController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public Collection<CtfService> getServices() {
|
||||
return service.findAll();
|
||||
public List<ServiceDto> getServices() {
|
||||
return service.findAll().stream()
|
||||
.map(service::toDto)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@DeleteMapping("/{port}")
|
||||
@@ -29,8 +33,9 @@ public class ServiceController {
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public CtfService addService(@RequestBody CtfService ctfService) {
|
||||
return service.save(ctfService);
|
||||
public CtfService addService(@RequestBody ServiceDto dto) {
|
||||
CtfService newService = this.service.fromDto(dto);
|
||||
return this.service.save(newService);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,42 +2,47 @@ package ru.serega6531.packmate.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.StreamDto;
|
||||
import ru.serega6531.packmate.service.StreamService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/stream/")
|
||||
public class StreamController {
|
||||
|
||||
private final StreamService streamService;
|
||||
private final StreamService service;
|
||||
|
||||
@Autowired
|
||||
public StreamController(StreamService streamService) {
|
||||
this.streamService = streamService;
|
||||
public StreamController(StreamService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@PostMapping("/all")
|
||||
public List<Stream> getStreams(@RequestBody Pagination pagination) {
|
||||
return streamService.findAll(pagination, Optional.empty(), pagination.isFavorites());
|
||||
public List<StreamDto> getStreams(@RequestBody Pagination pagination) {
|
||||
return service.findAll(pagination, Optional.empty(), pagination.isFavorites()).stream()
|
||||
.map(service::streamToDto)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@PostMapping("/{port}")
|
||||
public List<Stream> getStreams(@PathVariable int port, @RequestBody Pagination pagination) {
|
||||
return streamService.findAll(pagination, Optional.of(port), pagination.isFavorites());
|
||||
public List<StreamDto> getStreams(@PathVariable int port, @RequestBody Pagination pagination) {
|
||||
return service.findAll(pagination, Optional.of(port), pagination.isFavorites()).stream()
|
||||
.map(service::streamToDto)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/favorite")
|
||||
public void favoriteStream(@PathVariable long id) {
|
||||
streamService.setFavorite(id, true);
|
||||
service.setFavorite(id, true);
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/unfavorite")
|
||||
public void unfavoriteStream(@PathVariable long id) {
|
||||
streamService.setFavorite(id, false);
|
||||
service.setFavorite(id, false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package ru.serega6531.packmate.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import lombok.*;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
|
||||
@@ -28,10 +27,12 @@ public class FoundPattern {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(generator = "found_pattern_generator")
|
||||
@JsonIgnore
|
||||
private int id;
|
||||
|
||||
private int patternId;
|
||||
|
||||
private int startPosition;
|
||||
|
||||
private int endPosition;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package ru.serega6531.packmate.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
@@ -32,16 +31,13 @@ public class Packet {
|
||||
private Long id;
|
||||
|
||||
@Transient
|
||||
@JsonIgnore
|
||||
private Long tempId;
|
||||
|
||||
@Transient
|
||||
@JsonIgnore
|
||||
private byte ttl;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "stream_id", nullable = false)
|
||||
@JsonIgnore
|
||||
private Stream stream;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
@@ -60,7 +56,6 @@ public class Packet {
|
||||
private byte[] content;
|
||||
|
||||
@Transient
|
||||
@JsonIgnore
|
||||
public String getContentString() {
|
||||
return new String(content);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package ru.serega6531.packmate.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
@@ -41,7 +40,6 @@ public class Pattern {
|
||||
private PatternDirectionType directionType;
|
||||
|
||||
@ManyToMany(mappedBy = "foundPatterns", fetch = FetchType.LAZY)
|
||||
@JsonIgnore
|
||||
private List<Stream> matchedStreams;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package ru.serega6531.packmate.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
@@ -34,7 +33,6 @@ public class Stream {
|
||||
private Protocol protocol;
|
||||
|
||||
@OneToMany(mappedBy = "stream", cascade = CascadeType.ALL)
|
||||
@JsonIgnore
|
||||
@OrderBy("id")
|
||||
private List<Packet> packets;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package ru.serega6531.packmate.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
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.PatternSearchType;
|
||||
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.repository.PatternRepository;
|
||||
|
||||
@@ -23,6 +25,7 @@ public class PatternService {
|
||||
private final SubscriptionService subscriptionService;
|
||||
|
||||
private final Map<Integer, Pattern> patterns = new HashMap<>();
|
||||
private final ModelMapper modelMapper = new ModelMapper();
|
||||
|
||||
@Autowired
|
||||
public PatternService(PatternRepository repository,
|
||||
@@ -80,8 +83,16 @@ public class PatternService {
|
||||
final Pattern saved = repository.save(pattern);
|
||||
patterns.put(saved.getId(), saved);
|
||||
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;
|
||||
}
|
||||
|
||||
public Pattern fromDto(PatternDto dto) {
|
||||
return modelMapper.map(dto, Pattern.class);
|
||||
}
|
||||
|
||||
public PatternDto toDto(Pattern pattern) {
|
||||
return modelMapper.map(pattern, PatternDto.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package ru.serega6531.packmate.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.serega6531.packmate.model.CtfService;
|
||||
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.repository.ServiceRepository;
|
||||
|
||||
@@ -29,6 +31,7 @@ public class ServicesService {
|
||||
private final InetAddress localIp;
|
||||
|
||||
private final Map<Integer, CtfService> services = new HashMap<>();
|
||||
private final ModelMapper modelMapper = new ModelMapper();
|
||||
|
||||
@Autowired
|
||||
public ServicesService(ServiceRepository repository,
|
||||
@@ -44,6 +47,10 @@ public class ServicesService {
|
||||
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) {
|
||||
if (firstIp.equals(localIp)) {
|
||||
return findByPort(firstPort);
|
||||
@@ -79,11 +86,19 @@ public class ServicesService {
|
||||
final CtfService saved = repository.save(service);
|
||||
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());
|
||||
|
||||
return saved;
|
||||
}
|
||||
|
||||
public ServiceDto toDto(CtfService service) {
|
||||
return modelMapper.map(service, ServiceDto.class);
|
||||
}
|
||||
|
||||
public CtfService fromDto(ServiceDto dto) {
|
||||
return modelMapper.map(dto, CtfService.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package ru.serega6531.packmate.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
@@ -11,9 +12,7 @@ import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.serega6531.packmate.model.*;
|
||||
import ru.serega6531.packmate.model.enums.SubscriptionMessageType;
|
||||
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.model.pojo.*;
|
||||
import ru.serega6531.packmate.repository.StreamRepository;
|
||||
import ru.serega6531.packmate.service.optimization.RsaKeysHolder;
|
||||
import ru.serega6531.packmate.service.optimization.StreamOptimizer;
|
||||
@@ -39,6 +38,7 @@ public class StreamService {
|
||||
private final boolean ignoreEmptyPackets;
|
||||
|
||||
private final java.util.regex.Pattern userAgentPattern = java.util.regex.Pattern.compile("User-Agent: (.+)\\r\\n");
|
||||
private final ModelMapper modelMapper = new ModelMapper();
|
||||
|
||||
@Autowired
|
||||
public StreamService(StreamRepository repository,
|
||||
@@ -108,7 +108,7 @@ public class StreamService {
|
||||
savedStream.setPackets(packets);
|
||||
savedStream = save(savedStream);
|
||||
|
||||
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.NEW_STREAM, savedStream));
|
||||
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.NEW_STREAM, streamToDto(savedStream)));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@ public class StreamService {
|
||||
char[] alphabet = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
|
||||
int l = alphabet.length;
|
||||
int hashCode = ua.hashCode();
|
||||
if(hashCode == Integer.MIN_VALUE) { // abs(MIN_VALUE) вернет то же значение
|
||||
if (hashCode == Integer.MIN_VALUE) { // abs(MIN_VALUE) вернет то же значение
|
||||
hashCode = Integer.MAX_VALUE;
|
||||
}
|
||||
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");
|
||||
|
||||
Specification<Stream> spec;
|
||||
if(pagination.getDirection() == Sort.Direction.ASC) {
|
||||
if (pagination.getDirection() == Sort.Direction.ASC) {
|
||||
spec = streamIdGreaterThan(pagination.getStartingFrom());
|
||||
} else {
|
||||
spec = streamIdLessThan(pagination.getStartingFrom());
|
||||
}
|
||||
|
||||
if(service.isPresent()) {
|
||||
if (service.isPresent()) {
|
||||
spec = spec.and(streamServiceEquals(service.get()));
|
||||
}
|
||||
|
||||
if(onlyFavorites) {
|
||||
if (onlyFavorites) {
|
||||
spec = spec.and(streamIsFavorite());
|
||||
}
|
||||
|
||||
if(pagination.getPattern() != null) {
|
||||
if (pagination.getPattern() != null) {
|
||||
spec = spec.and(streamPatternsContains(pagination.getPattern()));
|
||||
}
|
||||
|
||||
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) {
|
||||
return (root, query, cb) -> cb.equal(root.get("service"), service);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user