commit 360afde8aadd97e184c920aa8450e68a0a1909b1 Author: eetnaviation Date: Fri Jun 27 12:39:56 2025 +0300 Initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..ca206d5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/jna-5.14.0.jar b/jna-5.14.0.jar new file mode 100644 index 0000000..e27f9c5 Binary files /dev/null and b/jna-5.14.0.jar differ diff --git a/jna-platform-5.14.0.jar b/jna-platform-5.14.0.jar new file mode 100644 index 0000000..05984f7 Binary files /dev/null and b/jna-platform-5.14.0.jar differ diff --git a/pcap4j-core-1.8.2.jar b/pcap4j-core-1.8.2.jar new file mode 100644 index 0000000..9663471 Binary files /dev/null and b/pcap4j-core-1.8.2.jar differ diff --git a/pcap4j-packetfactory-static-1.8.2.jar b/pcap4j-packetfactory-static-1.8.2.jar new file mode 100644 index 0000000..e5cf3ab Binary files /dev/null and b/pcap4j-packetfactory-static-1.8.2.jar differ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..8d32e0d --- /dev/null +++ b/pom.xml @@ -0,0 +1,40 @@ + + 4.0.0 + + eu.velend + bcow-client + 1.0-SNAPSHOT + + + 11 + 11 + + + + + pcap4j-repo + https://clojars.org/repo/ + + + slf4j-logger + https://clojars.org/repo/ + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 11 + 11 + + + + + diff --git a/slf4j-api-1.7.30.jar b/slf4j-api-1.7.30.jar new file mode 100644 index 0000000..29ac26f Binary files /dev/null and b/slf4j-api-1.7.30.jar differ diff --git a/slf4j-simple-1.7.30.jar b/slf4j-simple-1.7.30.jar new file mode 100644 index 0000000..6debaa9 Binary files /dev/null and b/slf4j-simple-1.7.30.jar differ diff --git a/src/main/java/BcowClient.java b/src/main/java/BcowClient.java new file mode 100644 index 0000000..19032e1 --- /dev/null +++ b/src/main/java/BcowClient.java @@ -0,0 +1,93 @@ +import org.pcap4j.core.*; + +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.nio.charset.StandardCharsets; +import java.util.List; + +public class BcowClient { + + private ClientConfig config; + private static final int SNAPLEN = 65536; + private static final int TIMEOUT = 10; // in milliseconds + + public BcowClient(ClientConfig config) { + this.config = config; + } + + public void sniffAndSendPackets() { + System.out.println("Sending inital heartbeat data..."); + System.out.println("Starting packet sniff func..."); + PcapNetworkInterface device = null; + try { + List allDevs = Pcaps.findAllDevs(); + System.out.println(allDevs.toString()); + if (allDevs.isEmpty()) { + System.err.println("No devices found."); + return; + } + device = allDevs.get(4); // Get the first available network interface + } catch (PcapNativeException e) { + e.printStackTrace(); + } + + if (device == null) { + System.err.println("No device found."); + return; + } + + PcapHandle handle; + System.out.println("Opening live capture on device: " + device.getName()); + try { + handle = device.openLive(SNAPLEN, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, TIMEOUT); + System.out.println("Live capture handle opened successfully."); + } catch (PcapNativeException e) { + System.err.println("Error opening live capture: " + e.getMessage()); + e.printStackTrace(); + return; + } + + PacketListener listener = packet -> { + System.out.println("Received packet: " + packet); + String packetData = packet.toString(); + + String[] lines = packetData.split("\\r?\\n"); + + for (String line : lines) { + //sendPacketData(line); + } + }; + + + try { + handle.loop(-1, listener); + } catch (InterruptedException | PcapNativeException | NotOpenException e) { + e.printStackTrace(); + } finally { + if (handle != null && handle.isOpen()) { + handle.close(); + } + } + } + + private void sendPacketData(String line) { + try (Socket socket = new Socket(config.getServerIp(), 9999); + DataOutputStream dos = new DataOutputStream(socket.getOutputStream())) { + + String dataToSend = String.format("ID:%s|NAME:%s|TIMEZONE:%s|DATA:%s", + config.getId(), config.getName(), config.getTimezone(), line); + dos.write(dataToSend.getBytes(StandardCharsets.UTF_8)); + dos.flush(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + public static void main(String[] args) { + ClientConfig config = new ClientConfig("001", "Client1", "127.0.0.1", "auto"); + BcowClient client = new BcowClient(config); + client.sniffAndSendPackets(); + } +} diff --git a/src/main/java/ClientConfig.java b/src/main/java/ClientConfig.java new file mode 100644 index 0000000..6df383e --- /dev/null +++ b/src/main/java/ClientConfig.java @@ -0,0 +1,29 @@ +public class ClientConfig { + private String id; + private String name; + private String serverIp; + private String timezone; + + public ClientConfig(String id, String name, String serverIp, String timezone) { + this.id = id; + this.name = name; + this.serverIp = serverIp; + this.timezone = timezone; + } + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public String getServerIp() { + return serverIp; + } + + public String getTimezone() { + return timezone; + } +}