-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFXMLController.java
488 lines (424 loc) · 15.8 KB
/
FXMLController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MainJavaFX;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.animation.TranslateTransition;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javax.swing.JOptionPane;
import javafx.scene.control.Alert;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TableRow;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.util.Duration;
/**
* FXML Controller class
*
* @author Kavish-PC
*/
public class FXMLController implements Initializable
{
ThreadHandler THREAD;
static Operations operations = new Operations();
static InterfaceMenuController interfaceMenu = new InterfaceMenuController();
static RulesManagerMenuController rulesManagerMenu = new RulesManagerMenuController();
static StatisticsMenuController statisticsMenu;
static LoggingEngine loggingEngine = new LoggingEngine();
static LogManagerMenuController logManagerMenu;
static int selected = 0;
static boolean running = false;
static boolean simpleMode = false;
int packetNum = 1;
int alertNum = 1;
final ObservableList<Packet> packetData = FXCollections.observableArrayList();
final ObservableList<IntrusionAlert> alertData = FXCollections.observableArrayList();
static Stage interfaceMenuStage = new Stage();
static Stage rulesManagerMenuStage = new Stage();
static Stage statisticsMenuStage = new Stage();
static Stage logManagerMenuStage = new Stage();
static TranslateTransition openNav, closeNav;
@FXML
private Button interfaceButton;
@FXML
private Button exitButton;
@FXML
private Button stopButton;
@FXML
private Button memoryButton;
@FXML
private AnchorPane anchor;
@FXML
private Label statusLabel;
@FXML
private Label modeLabel;
@FXML
private TextField filterField;
@FXML
private TableView<Packet> packetTable;
@FXML
private TableColumn<Packet, Integer> number;
@FXML
private TableColumn<Packet, String> protocol;
@FXML
private TableColumn<Packet, String> source_ip;
@FXML
private TableColumn<Packet, Integer> source_port;
@FXML
private TableColumn<Packet, String> destination_ip;
@FXML
private TableColumn<Packet, Integer> destination_port;
@FXML
private TableView alertTable;
@FXML
private TableColumn<IntrusionAlert, String> alertTime;
@FXML
private TableColumn<IntrusionAlert, String> alertDetails;
@FXML
private TableColumn<IntrusionAlert, Integer> packetReference;
@FXML
private AnchorPane navList;
@FXML
private Button menu;
@Override
public void initialize(URL location, ResourceBundle resources)
{
System.out.println("Main UI Initialized!");
//init packets table
number.setCellValueFactory(new PropertyValueFactory<>("referenceNumber"));
protocol.setCellValueFactory(new PropertyValueFactory<>("protocol"));
source_ip.setCellValueFactory(new PropertyValueFactory<>("source_ip"));
source_port.setCellValueFactory(new PropertyValueFactory<>("source_port"));
destination_ip.setCellValueFactory(new PropertyValueFactory<>("destination_ip"));
destination_port.setCellValueFactory(new PropertyValueFactory<>("destination_port"));
packetTable.setItems(packetData);
//init alerts table
alertDetails.setCellValueFactory(new PropertyValueFactory<>("details"));
alertTime.setCellValueFactory(new PropertyValueFactory<>("timestamp"));
packetReference.setCellValueFactory(new PropertyValueFactory<>("packetReference"));
alertTable.setItems(alertData);
alertTable.setRowFactory( tv -> {
TableRow<IntrusionAlert> row = new TableRow<>();
row.setOnMouseClicked(event -> {
if (event.getClickCount() == 2 && (! row.isEmpty()) )
{
IntrusionAlert rowData = row.getItem();
int ref = rowData.getPacketReference();
int focus = 0;
for(int i = 0; i<packetData.size(); i++)
{
if(packetData.get(i).getReferenceNumber() == ref)
{
focus = i;
}
}
packetTable.requestFocus();
packetTable.getSelectionModel().select(focus);
packetTable.getFocusModel().focus(focus);
packetTable.scrollTo(focus);
}
});
return row;
});
Parent interfaceMenuRoot, rulesManagerMenuRoot, statisticsMenuRoot, logManagerMenuRoot;
try
{
interfaceMenuStage.setTitle("Interface Selection");
rulesManagerMenuStage.setTitle("Intrusion Detection Rules Manager");
statisticsMenuStage.setTitle("IDS Statistics");
logManagerMenuStage.setTitle("IDS Log Manager");
interfaceMenuRoot = FXMLLoader.load(getClass().getResource("InterfaceMenu.fxml"));
rulesManagerMenuRoot = FXMLLoader.load(getClass().getResource("RulesManagerMenu.fxml"));
FXMLLoader statisticsLoader = new FXMLLoader();
statisticsLoader.setLocation(FXMLController.class.getResource("StatisticsMenu.fxml"));
statisticsMenuRoot = statisticsLoader.load();
statisticsMenu = statisticsLoader.getController();
FXMLLoader logManagerLoader = new FXMLLoader();
logManagerLoader.setLocation(FXMLController.class.getResource("LogManagerMenu.fxml"));
logManagerMenuRoot = logManagerLoader.load();
logManagerMenu = logManagerLoader.getController();
Scene interfaceMenuScene = new Scene(interfaceMenuRoot);
Scene rulesManagerMenuScene = new Scene(rulesManagerMenuRoot);
Scene statisticsMenuScene = new Scene(statisticsMenuRoot);
Scene logManagerMenuScene = new Scene(logManagerMenuRoot);
interfaceMenuStage.setScene(interfaceMenuScene);
rulesManagerMenuStage.setScene(rulesManagerMenuScene);
statisticsMenuStage.setScene(statisticsMenuScene);
logManagerMenuStage.setScene(logManagerMenuScene);
interfaceMenuStage.initModality(Modality.APPLICATION_MODAL);
rulesManagerMenuStage.initModality(Modality.APPLICATION_MODAL);
statisticsMenuStage.initModality(Modality.APPLICATION_MODAL);
logManagerMenuStage.initModality(Modality.APPLICATION_MODAL);
interfaceMenuStage.getIcons().add(new Image(getClass().getResourceAsStream("logo.png")));
rulesManagerMenuStage.getIcons().add(new Image(getClass().getResourceAsStream("logo.png")));
statisticsMenuStage.getIcons().add(new Image(getClass().getResourceAsStream("logo.png")));
logManagerMenuStage.getIcons().add(new Image(getClass().getResourceAsStream("logo.png")));
//interfaceMenuStage.setResizable(false);
//rulesManagerMenuStage.setResizable(false);
//statisticsMenuStage.setResizable(false);
//logManagerMenuStage.setResizable(false);
logManagerMenuStage.sizeToScene();
statisticsMenuStage.sizeToScene();
rulesManagerMenuStage.sizeToScene();
interfaceMenuStage.sizeToScene();
operations.setStatistics(statisticsMenu);
operations.setLoggingEngine(loggingEngine);
statusLabel.setTextFill(Color.web("#f2f2f2"));
logManagerMenu.setLogEngine(loggingEngine);
logManagerMenu.updateStatus();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
public StatisticsMenuController getStatisticsMenu()
{
return statisticsMenu;
}
public LoggingEngine getLoggingEngine()
{
return loggingEngine;
}
public void exit()
{
System.exit(0);
}
public void addEntry(int num, String protocol, String source_ip, int source_port, String destination_ip, int destination_port)
{
packetData.add(new Packet(num, protocol, source_ip, source_port, destination_ip, destination_port));
}
public void addAlert(String alert, String time, int ref)
{
alertData.add(new IntrusionAlert(alert, time, ref));
}
public void capture()
{
operations.getDetectionEngine().updateRulesFile(rulesManagerMenu.getRulesFileLocation()); //update location of rules file
THREAD = new ThreadHandler()
{
public Object construct()
{
startCapture();
return 0;
}
public void finished()
{
this.interrupt();
}
};
THREAD.start();
}
public void receiveSelected(int num)
{
selected = num;
capture();
}
public void startCapture()
{
changeStatus();
operations.capturePackets(selected, this);
}
@FXML
public void showInterfaceMenu()
{
if(!running)
{
showSideMenu();
interfaceMenu.setFXMLController(this);
interfaceMenuStage.showAndWait();
}
else
{
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("Capture Process Manager");
alert.setHeaderText("The capturing process could not be started!");
alert.setContentText("The current capturing process needs to be stopped before starting a new one.");
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image(this.getClass().getResource("logo.png").toString()));
alert.showAndWait();
}
}
@FXML
public void showRulesManagerMenu()
{
showSideMenu();
rulesManagerMenu.setFXMLController(this);
rulesManagerMenuStage.showAndWait();
}
@FXML
public void showLogManagerMenu()
{
showSideMenu();
logManagerMenu.setFXMLController(this);
logManagerMenuStage.showAndWait();
}
@FXML
public void showStatisticsMenu()
{
showSideMenu();
statisticsMenu.setFXMLController(this);
statisticsMenuStage.showAndWait();
}
@FXML
public void hideStatisticsMenu()
{
statisticsMenuStage.hide();
}
public void hideInterfaceMenu()
{
interfaceMenuStage.hide();
}
public void hideRulesManagerMenu()
{
rulesManagerMenuStage.hide();
}
public void hideLogManagerMenu()
{
logManagerMenuStage.hide();
}
@FXML
public void stopCapture()
{
if(running)
{
operations.pcap.breakloop();
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText(null);
alert.setContentText("Packet Capturing has been stopped.");
changeStatus();
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image(this.getClass().getResource("logo.png").toString()));
alert.showAndWait();
}
else
{
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error Dialog");
alert.setHeaderText("Packet Capturing has not started.");
alert.setContentText("Select a network interface to initiate the packet capturing process.");
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image(this.getClass().getResource("logo.png").toString()));
alert.showAndWait();
}
}
public void changeStatus()
{
if(running)
{
Platform.runLater(new Runnable()
{
@Override
public void run()
{
running = false;
statusLabel.setText("STOPPED");
statusLabel.setTextFill(Color.web("#f2f2f2"));
}
});
}
else
{
Platform.runLater(new Runnable()
{
@Override
public void run()
{
running = true;
statusLabel.setText("RUNNING");
statusLabel.setTextFill(Color.web("#1aff1a"));
}
});
}
}
public boolean getStatus()
{
return running;
}
@FXML
public void checkMemory()
{
// Get current size of heap in bytes
long heapSize = Runtime.getRuntime().totalMemory();
System.out.println("Current: " +heapSize);
// Get maximum size of heap in bytes. The heap cannot grow beyond this size.// Any attempt will result in an OutOfMemoryException.
long heapMaxSize = Runtime.getRuntime().maxMemory();
System.out.println("Max: " + heapMaxSize);
}
public void setMode(boolean m)
{
simpleMode = m;
operations.setMode(m);
if(m)
{
Platform.runLater(new Runnable()
{
@Override
public void run()
{
modeLabel.setText("HIGH EFFICIENCY");
}
});
}
else
{
Platform.runLater(new Runnable()
{
@Override
public void run()
{
modeLabel.setText("NORMAL");
}
});
}
}
@FXML
private void showSideMenu()
{
TranslateTransition moveUI = new TranslateTransition(new Duration(350), anchor);
moveUI.setToX(120);
TranslateTransition revertUI = new TranslateTransition(new Duration(350), anchor);
revertUI.setToX(0);
if(anchor.getTranslateX()==0)
{
//openNav.play();
moveUI.play();
}
else
{
//closeNav.play();
revertUI.play();
}
}
}