Добавлен эндпоинт для запуска pcap

This commit is contained in:
serega6531
2020-04-06 01:46:06 +03:00
parent a0ceda4cb4
commit 0391d55e91
8 changed files with 83 additions and 6 deletions

View File

@@ -6,7 +6,8 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import ru.serega6531.packmate.pcap.PcapWorker;
import ru.serega6531.packmate.model.enums.CaptureMode;
import ru.serega6531.packmate.service.PcapService;
@SpringBootApplication
public class PackmateApplication {
@@ -14,15 +15,18 @@ public class PackmateApplication {
@Value("${enable-capture}")
private boolean enableCapture;
@Value("${capture-mode}")
private CaptureMode captureMode;
public static void main(String[] args) {
SpringApplication.run(PackmateApplication.class, args);
}
@EventListener(ApplicationReadyEvent.class)
public void afterStartup(ApplicationReadyEvent event) throws PcapNativeException {
if (enableCapture) {
final PcapWorker pcapWorker = event.getApplicationContext().getBean(PcapWorker.class);
pcapWorker.start();
if (enableCapture && captureMode == CaptureMode.LIVE) {
final PcapService pcapService = event.getApplicationContext().getBean(PcapService.class);
pcapService.start();
}
}

View File

@@ -1,4 +1,4 @@
package ru.serega6531.packmate;
package ru.serega6531.packmate.configuration;
import org.pcap4j.core.PcapNativeException;
import org.springframework.beans.factory.annotation.Autowired;
@@ -15,6 +15,7 @@ import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import ru.serega6531.packmate.WebSocketHandler;
import ru.serega6531.packmate.model.enums.CaptureMode;
import ru.serega6531.packmate.pcap.FilePcapWorker;
import ru.serega6531.packmate.pcap.LivePcapWorker;

View File

@@ -0,0 +1,36 @@
package ru.serega6531.packmate.controller;
import org.pcap4j.core.PcapNativeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
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.service.PcapService;
@RestController
@RequestMapping("/api/pcap/")
public class PcapController {
private final PcapService service;
@Autowired
public PcapController(PcapService service) {
this.service = service;
}
@GetMapping("/started")
public boolean started() {
return service.isStarted();
}
public boolean isRunning() {
return true; //TODO
}
@PostMapping("/start")
public void start() throws PcapNativeException {
service.start();
}
}

View File

@@ -42,6 +42,7 @@ public class FilePcapWorker extends AbstractPcapWorker {
log.error("Pcap read", e);
Thread.sleep(100);
} catch (EOFException e) {
log.info("All packets processed");
stop();
break;
}
@@ -54,6 +55,7 @@ public class FilePcapWorker extends AbstractPcapWorker {
pcap.close();
}
log.info("Intercept stopped");
//TODO закрывать все стримы
log.info("Pcap closed");
}
}

View File

@@ -0,0 +1,29 @@
package ru.serega6531.packmate.service;
import lombok.Getter;
import org.pcap4j.core.PcapNativeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ru.serega6531.packmate.pcap.PcapWorker;
@Service
public class PcapService {
@Getter
private boolean started = false;
private final PcapWorker worker;
@Autowired
public PcapService(PcapWorker worker) {
this.worker = worker;
}
public synchronized void start() throws PcapNativeException {
if(!started) {
started = true;
worker.start();
}
}
}