Добавлен эндпоинт для запуска 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

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
src/main/resources/static/*
*.pcap
HELP.md
.gradle

View File

@@ -7,6 +7,8 @@ services:
DB_NAME: ${PACKMATE_DB_NAME:-packmate}
INTERFACE: ${PACKMATE_INTERFACE}
LOCAL_IP: ${PACKMATE_LOCAL_IP}
MODE: ${PACKMATE_MODE:-LIVE}
PCAP_FILE: ${PACKMATE_PCAP_FILE}
WEB_LOGIN: ${PACKMATE_WEB_LOGIN:-BinaryBears}
WEB_PASSWORD: ${PACKMATE_WEB_PASSWORD:-123456}
env_file:
@@ -21,6 +23,7 @@ services:
"java", "-Djava.net.preferIPv4Stack=true", "-Djava.net.preferIPv4Addresses=true",
"-jar", "/app/app.jar", "--spring.datasource.url=jdbc:postgresql://127.0.0.1:65001/$${DB_NAME}",
"--spring.datasource.username=$${DB_USER}", "--spring.datasource.password=$${DB_PASSWORD}",
"--capture-mode=$${MODE}", "--pcap-file=$${PCAP_FILE}",
"--interface-name=$${INTERFACE}", "--local-ip=$${LOCAL_IP}", "--account-login=$${WEB_LOGIN}",
"--account-password=$${WEB_PASSWORD}", "--server.port=65000", "--server.address=0.0.0.0"
]

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();
}
}
}

View File

@@ -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