jNetPcap 기능 활성화
유의사항으로는
JavaFX는 Modulepath에 넣어주어야 하고
JNetPcap은 Classpath에 넣어주어야
javafx exception in application start method 이와같은 오류가 생기지 않는다
이것을 몰라 몇시간동안 고생했다
활성화 하는 것까지 코드를 올려보겠다
Controller.java
package controller;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import org.jnetpcap.Pcap;
import org.jnetpcap.PcapIf;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
public class Controller implements Initializable {
@FXML
private ListView<String> networkListView;
@FXML
private TextArea textArea;
@FXML
private Button pickButton;
ObservableList<String> networkList = FXCollections.observableArrayList();
private ArrayList<PcapIf> allDevs = null;
@SuppressWarnings("deprecation")
@Override
public void initialize(URL location, ResourceBundle resources) {
allDevs = new ArrayList<PcapIf>();
StringBuilder errbuf = new StringBuilder();
int r = Pcap.findAllDevs(allDevs, errbuf);
if (r == Pcap.NOT_OK || allDevs.isEmpty()) {
textArea.appendText("네트워크 장치를 찾을 수 없습니다.\n" + errbuf.toString() + "\n");
return;
}
textArea.appendText("네트워크 장치를 찾았습니다.\n원하시는 장치를 선택해주세요.\n");
for (PcapIf device : allDevs) {
networkList.add(device.getName() + " " +
((device.getDescription() != null) ? device.getDescription() : "설명 없음"));
}
networkListView.setItems(networkList);
}
public void networkPickAction() {
if(networkListView.getSelectionModel().getSelectedIndex() < 0) {
return;
}
Main.device = allDevs.get(networkListView.getSelectionModel().getSelectedIndex());
networkListView.setDisable(true);
pickButton.setDisable(true);
int snaplen = 64 * 1024;
int flags = Pcap.MODE_PROMISCUOUS;
int timeout = 1;
StringBuilder errbuf = new StringBuilder();
Main.pcap = Pcap.openLive(Main.device.getName(), snaplen, flags, timeout, errbuf);
if (Main.pcap == null) {
textArea.appendText("네트워크 장치를 열 수 없습니다.\n" + errbuf.toString() + "\n");
return;
}
textArea.appendText("장치선택 : " + Main.device.getName() + "\n");
textArea.appendText("네트워크 장치를 활성화했습니다");
}
}
Main.java
package controller;
import org.jnetpcap.Pcap;
import org.jnetpcap.PcapIf;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
public static Pcap pcap = null;
public static PcapIf device = null;
private Stage primaryStage;
private AnchorPane layout;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("JavaFX ARP SPoofing");
setLayout();
}
public void setLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("../view/View.fxml"));
layout = (AnchorPane) loader.load();
Scene scene = new Scene(layout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
View.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.collections.*?>
<AnchorPane prefHeight="480" prefWidth="490" fx:controller="controller.Controller"
xmlns:fx="http://javafx.com/fxml/1">
<children>
<ListView fx:id="networkListView" layoutX="15" layoutY="14"
prefHeight="78" prefWidth="462">
<items>
<FXCollections fx:factory="observableArrayList"/>
</items>
</ListView>
<Button fx:id="pickButton" onAction="#networkPickAction" layoutX="395" layoutY="103"
prefHeight="29" prefWidth="82" text="PICK"></Button>
<TextArea fx:id="textArea" editable="false" layoutX="15" layoutY="144"
prefHeight="325" prefWidth="462"></TextArea>
</children>
</AnchorPane>