Добавлен эндпоинт для запуска pcap
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,6 +17,7 @@ spring:
|
||||
enable-capture: true
|
||||
capture-mode: LIVE # LIVE, FILE
|
||||
interface-name: enp0s31f6
|
||||
pcap-file: file.pcap
|
||||
local-ip: "192.168.0.125"
|
||||
account-login: BinaryBears
|
||||
account-password: 123456
|
||||
|
||||
Reference in New Issue
Block a user