Добавлен PcapWorker
This commit is contained in:
@@ -23,6 +23,9 @@ dependencies {
|
|||||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
implementation 'org.springframework.session:spring-session-core'
|
implementation 'org.springframework.session:spring-session-core'
|
||||||
|
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7'
|
||||||
|
compile 'org.pcap4j:pcap4j-core:1.+'
|
||||||
|
compile 'org.pcap4j:pcap4j-packetfactory-static:1.+'
|
||||||
compileOnly 'org.projectlombok:lombok'
|
compileOnly 'org.projectlombok:lombok'
|
||||||
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
|
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
|
||||||
runtimeOnly 'org.postgresql:postgresql'
|
runtimeOnly 'org.postgresql:postgresql'
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
package ru.serega6531.packmate;
|
package ru.serega6531.packmate;
|
||||||
|
|
||||||
|
import org.pcap4j.core.PcapNativeException;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
|
import org.springframework.context.event.EventListener;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class PackmateApplication {
|
public class PackmateApplication {
|
||||||
@@ -10,4 +13,10 @@ public class PackmateApplication {
|
|||||||
SpringApplication.run(PackmateApplication.class, args);
|
SpringApplication.run(PackmateApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@EventListener(ApplicationReadyEvent.class)
|
||||||
|
public void afterStartup(ApplicationReadyEvent event) throws PcapNativeException {
|
||||||
|
final PcapWorker pcapWorker = event.getApplicationContext().getBean(PcapWorker.class);
|
||||||
|
pcapWorker.start();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
90
src/main/java/ru/serega6531/packmate/PcapWorker.java
Normal file
90
src/main/java/ru/serega6531/packmate/PcapWorker.java
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
package ru.serega6531.packmate;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
|
||||||
|
import org.pcap4j.core.PcapHandle;
|
||||||
|
import org.pcap4j.core.PcapNativeException;
|
||||||
|
import org.pcap4j.core.PcapNetworkInterface;
|
||||||
|
import org.pcap4j.core.Pcaps;
|
||||||
|
import org.pcap4j.packet.Packet;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import ru.serega6531.packmate.service.PacketService;
|
||||||
|
import ru.serega6531.packmate.service.PatternService;
|
||||||
|
import ru.serega6531.packmate.service.ServicesService;
|
||||||
|
import ru.serega6531.packmate.service.StreamService;
|
||||||
|
|
||||||
|
import javax.annotation.PreDestroy;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class PcapWorker {
|
||||||
|
|
||||||
|
private final ServicesService servicesService;
|
||||||
|
private final StreamService streamService;
|
||||||
|
private final PacketService packetService;
|
||||||
|
private final PatternService patternService;
|
||||||
|
|
||||||
|
private PcapNetworkInterface device;
|
||||||
|
private PcapHandle pcap = null;
|
||||||
|
private ExecutorService executorService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public PcapWorker(ServicesService servicesService,
|
||||||
|
StreamService streamService,
|
||||||
|
PacketService packetService,
|
||||||
|
PatternService patternService,
|
||||||
|
@Value("${interface-name}") String interfaceName) throws PcapNativeException {
|
||||||
|
this.servicesService = servicesService;
|
||||||
|
this.streamService = streamService;
|
||||||
|
this.packetService = packetService;
|
||||||
|
this.patternService = patternService;
|
||||||
|
|
||||||
|
BasicThreadFactory factory = new BasicThreadFactory.Builder()
|
||||||
|
.namingPattern("pcap-worker").build();
|
||||||
|
executorService = Executors.newSingleThreadExecutor(factory);
|
||||||
|
device = Pcaps.getDevByName(interfaceName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() throws PcapNativeException {
|
||||||
|
System.out.println("Using interface " + device.getName());
|
||||||
|
pcap = device.openLive(65536, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, 100);
|
||||||
|
|
||||||
|
executorService.execute(() -> {
|
||||||
|
try {
|
||||||
|
log.info("Intercept started");
|
||||||
|
while (true) {
|
||||||
|
if (pcap.isOpen()) {
|
||||||
|
try {
|
||||||
|
final Packet packet = pcap.getNextPacketEx();
|
||||||
|
processPacket(packet);
|
||||||
|
} catch (TimeoutException ignored) {
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Error while capturing packet", e);
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreDestroy
|
||||||
|
public void stop() {
|
||||||
|
if (pcap != null && pcap.isOpen()) {
|
||||||
|
pcap.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("Intercept stopped");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processPacket(Packet packet) {
|
||||||
|
System.out.println(packet);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,14 +2,12 @@ package ru.serega6531.packmate.model;
|
|||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import javax.persistence.CascadeType;
|
import javax.persistence.*;
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
import javax.persistence.OneToMany;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Entity
|
@Entity
|
||||||
|
@Table(name = "service")
|
||||||
public class CtfService {
|
public class CtfService {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package ru.serega6531.packmate.service;
|
|||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
import ru.serega6531.packmate.model.Pattern;
|
import ru.serega6531.packmate.model.Pattern;
|
||||||
import ru.serega6531.packmate.repository.PatternRepository;
|
import ru.serega6531.packmate.repository.PatternRepository;
|
||||||
|
|
||||||
|
|||||||
@@ -6,4 +6,5 @@ spring:
|
|||||||
driver-class-name: org.postgresql.Driver
|
driver-class-name: org.postgresql.Driver
|
||||||
jpa:
|
jpa:
|
||||||
hibernate:
|
hibernate:
|
||||||
ddl-auto: update
|
ddl-auto: update
|
||||||
|
interface-name: enp0s31f6
|
||||||
Reference in New Issue
Block a user