Files
HaE/src/main/java/hae/component/board/Databoard.java

293 lines
11 KiB
Java
Raw Normal View History

2024-05-06 12:56:56 +08:00
package hae.component.board;
2023-10-12 21:38:27 +08:00
2024-05-06 12:56:56 +08:00
import burp.api.montoya.MontoyaApi;
import hae.Config;
import hae.component.board.message.MessageTableModel;
import hae.component.board.message.MessageTableModel.MessageTable;
2024-05-12 19:02:38 +08:00
import hae.utils.config.ConfigLoader;
import hae.utils.string.StringProcessor;
2023-10-18 00:44:50 +08:00
2024-05-12 19:02:38 +08:00
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
2023-10-12 21:38:27 +08:00
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.awt.event.*;
2023-10-18 00:44:50 +08:00
import java.util.List;
2024-05-12 19:02:38 +08:00
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
2023-10-12 21:38:27 +08:00
public class Databoard extends JPanel {
2024-05-06 12:56:56 +08:00
private final MontoyaApi api;
private final ConfigLoader configLoader;
private final MessageTableModel messageTableModel;
2023-10-12 21:38:27 +08:00
private JTextField hostTextField;
2023-11-07 11:15:20 +08:00
private JTabbedPane dataTabbedPane;
2023-10-12 21:38:27 +08:00
private JSplitPane splitPane;
2024-05-06 12:56:56 +08:00
private MessageTable messageTable;
private static Boolean isMatchHost = false;
2024-05-12 19:02:38 +08:00
private final DefaultComboBoxModel comboBoxModel = new DefaultComboBoxModel();
private final JComboBox hostComboBox = new JComboBox(comboBoxModel);
2023-11-07 11:15:20 +08:00
2024-05-06 12:56:56 +08:00
public Databoard(MontoyaApi api, ConfigLoader configLoader, MessageTableModel messageTableModel) {
this.api = api;
this.configLoader = configLoader;
this.messageTableModel = messageTableModel;
2023-11-07 11:15:20 +08:00
2023-10-12 21:38:27 +08:00
initComponents();
}
private void initComponents() {
setLayout(new GridBagLayout());
2024-05-12 19:02:38 +08:00
((GridBagLayout) getLayout()).columnWidths = new int[]{25, 0, 0, 0, 20, 0};
((GridBagLayout) getLayout()).rowHeights = new int[]{0, 65, 20, 0};
((GridBagLayout) getLayout()).columnWeights = new double[]{0.0, 0.0, 1.0, 0.0, 0.0, 1.0E-4};
((GridBagLayout) getLayout()).rowWeights = new double[]{0.0, 1.0, 0.0, 1.0E-4};
2023-10-12 21:38:27 +08:00
2024-05-06 12:56:56 +08:00
JLabel hostLabel = new JLabel("Host:");
2023-11-27 14:55:28 +08:00
2024-05-06 12:56:56 +08:00
JButton clearButton = new JButton("Clear");
JButton actionButton = new JButton("Action");
JPanel menuPanel = new JPanel(new GridLayout(1, 1));
menuPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
JPopupMenu menu = new JPopupMenu();
menuPanel.add(clearButton);
menu.add(menuPanel);
2023-10-12 21:38:27 +08:00
2024-05-06 12:56:56 +08:00
hostTextField = new JTextField();
2023-10-12 21:38:27 +08:00
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
2024-05-06 12:56:56 +08:00
dataTabbedPane = new JTabbedPane(JTabbedPane.TOP);
actionButton.addActionListener(e -> {
int x = 0;
int y = actionButton.getHeight();
menu.show(actionButton, x, y);
});
clearButton.addActionListener(this::clearActionPerformed);
2023-10-12 21:38:27 +08:00
2024-02-02 19:07:03 +08:00
splitPane.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
resizePanel();
}
});
2024-05-06 12:56:56 +08:00
splitPane.setVisible(false);
add(hostLabel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(8, 0, 5, 5), 0, 0));
add(hostTextField, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(8, 0, 5, 5), 0, 0));
2024-05-12 19:02:38 +08:00
add(actionButton, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
2024-05-06 12:56:56 +08:00
new Insets(8, 0, 5, 5), 0, 0));
add(splitPane, new GridBagConstraints(1, 1, 3, 3, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(8, 0, 5, 5), 0, 0));
hostComboBox.setMaximumRowCount(5);
add(hostComboBox, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(8, 0, 5, 5), 0, 0));
2023-10-12 21:38:27 +08:00
setAutoMatch();
}
2024-02-02 19:07:03 +08:00
private void resizePanel() {
splitPane.setDividerLocation(0.4);
2024-05-06 12:56:56 +08:00
TableColumnModel columnModel = messageTable.getColumnModel();
2024-02-02 19:07:03 +08:00
int totalWidth = (int) (getWidth() * 0.6);
columnModel.getColumn(0).setPreferredWidth((int) (totalWidth * 0.1));
columnModel.getColumn(1).setPreferredWidth((int) (totalWidth * 0.3));
columnModel.getColumn(2).setPreferredWidth((int) (totalWidth * 0.3));
columnModel.getColumn(3).setPreferredWidth((int) (totalWidth * 0.1));
columnModel.getColumn(4).setPreferredWidth((int) (totalWidth * 0.1));
columnModel.getColumn(5).setPreferredWidth((int) (totalWidth * 0.1));
}
2023-10-12 21:38:27 +08:00
private void setAutoMatch() {
hostComboBox.setSelectedItem(null);
2023-11-07 11:15:20 +08:00
hostComboBox.addActionListener(this::handleComboBoxAction);
2023-10-12 21:38:27 +08:00
hostTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
2023-11-07 11:15:20 +08:00
handleKeyEvents(e);
2023-10-12 21:38:27 +08:00
}
});
hostTextField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
2023-11-27 14:55:28 +08:00
filterComboBoxList();
2023-10-12 21:38:27 +08:00
}
@Override
public void removeUpdate(DocumentEvent e) {
2023-11-27 14:55:28 +08:00
filterComboBoxList();
2023-10-12 21:38:27 +08:00
}
@Override
public void changedUpdate(DocumentEvent e) {
2023-11-07 11:15:20 +08:00
filterComboBoxList();
}
2023-11-27 14:55:28 +08:00
2023-11-07 11:15:20 +08:00
});
}
private void handleComboBoxAction(ActionEvent e) {
if (!isMatchHost && hostComboBox.getSelectedItem() != null) {
String selectedHost = hostComboBox.getSelectedItem().toString();
hostTextField.setText(selectedHost);
populateTabbedPaneByHost(selectedHost);
}
}
2024-05-06 12:56:56 +08:00
2023-11-07 11:15:20 +08:00
private void handleKeyEvents(KeyEvent e) {
isMatchHost = true;
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_SPACE && hostComboBox.isPopupVisible()) {
e.setKeyCode(KeyEvent.VK_ENTER);
}
2023-11-27 14:55:28 +08:00
if (Arrays.asList(KeyEvent.VK_DOWN, KeyEvent.VK_UP).contains(keyCode)) {
2023-11-07 11:15:20 +08:00
hostComboBox.dispatchEvent(e);
2023-11-27 14:55:28 +08:00
}
if (keyCode == KeyEvent.VK_ENTER) {
isMatchHost = false;
handleComboBoxAction(null);
hostComboBox.setPopupVisible(false);
2023-11-07 11:15:20 +08:00
}
if (keyCode == KeyEvent.VK_ESCAPE) {
hostComboBox.setPopupVisible(false);
}
isMatchHost = false;
}
private void filterComboBoxList() {
isMatchHost = true;
comboBoxModel.removeAllElements();
String input = hostTextField.getText().toLowerCase();
if (!input.isEmpty()) {
for (String host : getHostByList()) {
String lowerCaseHost = host.toLowerCase();
if (lowerCaseHost.contains(input)) {
if (lowerCaseHost.equals(input)) {
comboBoxModel.insertElementAt(lowerCaseHost, 0);
comboBoxModel.setSelectedItem(lowerCaseHost);
} else {
comboBoxModel.addElement(host);
2023-10-12 21:38:27 +08:00
}
}
}
2023-11-07 11:15:20 +08:00
}
hostComboBox.setPopupVisible(comboBoxModel.getSize() > 0);
isMatchHost = false;
2023-10-12 21:38:27 +08:00
}
2023-10-18 00:44:50 +08:00
private void populateTabbedPaneByHost(String selectedHost) {
if (!Objects.equals(selectedHost, "")) {
2024-05-06 12:56:56 +08:00
ConcurrentHashMap<String, Map<String, List<String>>> dataMap = Config.globalDataMap;
2023-10-12 21:38:27 +08:00
Map<String, List<String>> selectedDataMap;
2023-11-27 14:55:28 +08:00
dataTabbedPane.removeAll();
2024-05-12 19:02:38 +08:00
dataTabbedPane.setPreferredSize(new Dimension(500, 0));
2023-11-27 14:55:28 +08:00
dataTabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
splitPane.setLeftComponent(dataTabbedPane);
2023-10-12 21:38:27 +08:00
if (selectedHost.contains("*")) {
// 通配符数据
selectedDataMap = new HashMap<>();
2024-05-06 12:56:56 +08:00
String hostPattern = StringProcessor.replaceFirstOccurrence(selectedHost, "*.", "");
2023-10-12 21:38:27 +08:00
for (String key : dataMap.keySet()) {
if (key.contains(hostPattern) || selectedHost.equals("*")) {
Map<String, List<String>> ruleMap = dataMap.get(key);
for (String ruleKey : ruleMap.keySet()) {
List<String> dataList = ruleMap.get(ruleKey);
if (selectedDataMap.containsKey(ruleKey)) {
List<String> mergedList = new ArrayList<>(selectedDataMap.get(ruleKey));
mergedList.addAll(dataList);
HashSet<String> uniqueSet = new HashSet<>(mergedList);
selectedDataMap.put(ruleKey, new ArrayList<>(uniqueSet));
} else {
selectedDataMap.put(ruleKey, dataList);
}
}
}
}
} else {
selectedDataMap = dataMap.get(selectedHost);
}
2024-05-06 12:56:56 +08:00
for (Map.Entry<String, List<String>> entry : selectedDataMap.entrySet()) {
String tabTitle = String.format("%s (%s)", entry.getKey(), entry.getValue().size());
Datatable datatablePanel = new Datatable(api, entry.getKey(), entry.getValue());
datatablePanel.setTableListener(messageTableModel);
dataTabbedPane.addTab(tabTitle, datatablePanel);
2023-10-12 21:38:27 +08:00
}
// 展示请求消息表单
2024-05-06 12:56:56 +08:00
JSplitPane messageSplitPane = messageTableModel.getSplitPane();
2023-10-12 21:38:27 +08:00
this.splitPane.setRightComponent(messageSplitPane);
2024-05-06 12:56:56 +08:00
messageTable = messageTableModel.getMessageTable();
2023-10-12 21:38:27 +08:00
2024-02-02 19:07:03 +08:00
resizePanel();
2023-10-12 21:38:27 +08:00
splitPane.setVisible(true);
2024-01-18 12:07:20 +08:00
2023-10-12 21:38:27 +08:00
applyHostFilter(selectedHost);
2024-05-06 12:56:56 +08:00
hostTextField.setText(selectedHost);
}
}
2023-10-12 21:38:27 +08:00
2024-05-06 12:56:56 +08:00
private void applyHostFilter(String filterText) {
TableRowSorter<TableModel> sorter = (TableRowSorter<TableModel>) messageTable.getRowSorter();
2023-10-12 21:38:27 +08:00
2024-05-06 12:56:56 +08:00
String cleanedText = StringProcessor.replaceFirstOccurrence(filterText, "*.", "");
2024-05-23 12:00:13 +08:00
RowFilter<Object, Object> rowFilter = new RowFilter<Object, Object>() {
public boolean include(Entry<?, ?> entry) {
if (cleanedText.equals("*")) {
return true;
} else {
String host = StringProcessor.getHostByUrl((String) entry.getValue(1));
return StringProcessor.matchFromEnd(host, cleanedText);
}
}
};
2024-05-06 12:56:56 +08:00
2024-05-23 12:00:13 +08:00
sorter.setRowFilter(rowFilter);
2024-05-06 12:56:56 +08:00
messageTableModel.applyHostFilter(filterText);
}
private List<String> getHostByList() {
return new ArrayList<>(Config.globalDataMap.keySet());
}
private void clearActionPerformed(ActionEvent e) {
int retCode = JOptionPane.showConfirmDialog(null, "Do you want to clear data?", "Info",
JOptionPane.YES_NO_OPTION);
String host = hostTextField.getText();
if (retCode == JOptionPane.YES_OPTION && !host.isEmpty()) {
dataTabbedPane.removeAll();
splitPane.setVisible(false);
String cleanedHost = StringProcessor.replaceFirstOccurrence(host, "*.", "");
if (host.contains("*")) {
Config.globalDataMap.keySet().removeIf(i -> i.contains(cleanedHost) || cleanedHost.contains("*"));
} else {
Config.globalDataMap.remove(host);
}
2023-10-23 21:51:12 +08:00
2024-05-06 12:56:56 +08:00
messageTableModel.deleteByHost(cleanedHost);
2023-10-12 21:38:27 +08:00
}
}
2024-05-06 12:56:56 +08:00
}