Работа над фильтрацией через libpcap
This commit is contained in:
@@ -59,10 +59,10 @@ public class PatternService {
|
||||
patterns.put(id, saved);
|
||||
|
||||
if (enabled) {
|
||||
log.info("Включен паттерн {} со значением {}", pattern.getName(), pattern.getValue());
|
||||
log.info("Enabled pattern '{}' with value '{}'", pattern.getName(), pattern.getValue());
|
||||
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.ENABLE_PATTERN, id));
|
||||
} else {
|
||||
log.info("Выключен паттерн {} со значением {}", pattern.getName(), pattern.getValue());
|
||||
log.info("Disabled pattern '{}' with value '{}'", pattern.getName(), pattern.getValue());
|
||||
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.DISABLE_PATTERN, id));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,20 @@
|
||||
package ru.serega6531.packmate.service;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.pcap4j.core.PcapNativeException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.serega6531.packmate.model.CtfService;
|
||||
import ru.serega6531.packmate.model.enums.SubscriptionMessageType;
|
||||
import ru.serega6531.packmate.model.pojo.SubscriptionMessage;
|
||||
import ru.serega6531.packmate.pcap.PcapWorker;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class PcapService {
|
||||
|
||||
@Getter
|
||||
@@ -31,4 +37,18 @@ public class PcapService {
|
||||
}
|
||||
}
|
||||
|
||||
public void updateFilter(Collection<CtfService> services) {
|
||||
final String ports = services.stream()
|
||||
.map(CtfService::getPort)
|
||||
.map(p -> "port " + p)
|
||||
.collect(Collectors.joining(" or "));
|
||||
|
||||
final String format = "(tcp or udp) and (%s)";
|
||||
String filter = String.format(format, ports);
|
||||
|
||||
log.info("New filter: " + filter);
|
||||
|
||||
worker.setFilter(filter);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ public class ServicesService {
|
||||
|
||||
private final ServiceRepository repository;
|
||||
private final SubscriptionService subscriptionService;
|
||||
private final PcapService pcapService;
|
||||
|
||||
private final InetAddress localIp;
|
||||
|
||||
@@ -31,9 +32,11 @@ public class ServicesService {
|
||||
@Autowired
|
||||
public ServicesService(ServiceRepository repository,
|
||||
SubscriptionService subscriptionService,
|
||||
PcapService pcapService,
|
||||
@Value("${local-ip}") String localIpString) throws UnknownHostException {
|
||||
this.repository = repository;
|
||||
this.subscriptionService = subscriptionService;
|
||||
this.pcapService = pcapService;
|
||||
this.localIp = InetAddress.getByName(localIpString);
|
||||
|
||||
repository.findAll().forEach(s -> services.put(s.getPort(), s));
|
||||
@@ -67,9 +70,13 @@ public class ServicesService {
|
||||
|
||||
public CtfService save(CtfService service) {
|
||||
log.info("Added or edited service '{}' at port {}", service.getName(), service.getPort());
|
||||
|
||||
final CtfService saved = repository.save(service);
|
||||
services.put(saved.getPort(), saved);
|
||||
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.SAVE_SERVICE, saved));
|
||||
|
||||
pcapService.updateFilter(findAll());
|
||||
|
||||
return saved;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user