Skip to content

Commit

Permalink
feat: filter for unique results
Browse files Browse the repository at this point in the history
  • Loading branch information
beryxz committed Oct 25, 2024
1 parent b29fde9 commit f6e6ed3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
66 changes: 47 additions & 19 deletions src/main/java/com/cys4/sensitivediscoverer/ui/tab/LoggerTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
Expand Down Expand Up @@ -201,6 +202,7 @@ private JPanel createResultsFilterBar() {
}));
gbc = createGridConstraints(2, 0, 0, 0, GridBagConstraints.HORIZONTAL);
resultsFilterBar.add(totalCountValueLabel, gbc);

JLabel resultsCountSeparator = new JLabel("│");
gbc = createGridConstraints(3, 0, 0, 0, GridBagConstraints.HORIZONTAL);
gbc.insets = new Insets(0, 10, 0, 10);
Expand Down Expand Up @@ -233,7 +235,24 @@ private JPanel createResultsFilterBar() {
gbc.insets = new Insets(0, 10, 0, 0);
resultsFilterBar.add(URLCheckbox, gbc);

Runnable doUpdateRowFilter = () -> updateRowFilter(searchField.getText(), regexCheckbox.isSelected(), matchCheckbox.isSelected(), URLCheckbox.isSelected());
JLabel filterUniqueSeparator = new JLabel("│");
gbc = createGridConstraints(9, 0, 0, 0, GridBagConstraints.HORIZONTAL);
gbc.insets = new Insets(0, 10, 0, 0);
resultsFilterBar.add(filterUniqueSeparator, gbc);

JCheckBox UniqueCheckbox = new JCheckBox(getLocaleString("logger-uniqueResults-label"));
UniqueCheckbox.setSelected(false);
gbc = createGridConstraints(10, 0, 0, 0, GridBagConstraints.HORIZONTAL);
gbc.insets = new Insets(0, 10, 0, 0);
resultsFilterBar.add(UniqueCheckbox, gbc);

Runnable doUpdateRowFilter = () -> updateRowFilter(
searchField.getText(),
regexCheckbox.isSelected(),
matchCheckbox.isSelected(),
URLCheckbox.isSelected(),
UniqueCheckbox.isSelected()
);
regexCheckbox.addActionListener(event -> doUpdateRowFilter.run());
matchCheckbox.addActionListener(event -> doUpdateRowFilter.run());
URLCheckbox.addActionListener(event -> doUpdateRowFilter.run());
Expand All @@ -253,6 +272,7 @@ public void changedUpdate(DocumentEvent documentEvent) {
doUpdateRowFilter.run();
}
});
UniqueCheckbox.addActionListener(event -> doUpdateRowFilter.run());

return resultsFilterBar;
}
Expand Down Expand Up @@ -301,27 +321,35 @@ private JPanel createAnalysisBar(JScrollPane logEntriesPane) {
/**
* Filter rows of LogsTable that contains text string
*
* @param text text to search
* @param includeRegex if true, also search in Regex column
* @param includeMatch if true, also search in Match column
* @param includeURL if true, also search in URL column
* @param text text to search
* @param includeRegex if true, also search in Regex column
* @param includeMatch if true, also search in Match column
* @param includeURL if true, also search in URL column
* @param uniqueResults if true, remove duplicate results
*/
private void updateRowFilter(String text, boolean includeRegex, boolean includeMatch, boolean includeURL) {
private void updateRowFilter(String text, boolean includeRegex, boolean includeMatch, boolean includeURL, boolean uniqueResults) {
SwingUtilities.invokeLater(() -> {
if (text.isBlank()) {
logsTableRowSorter.setRowFilter(null);
} else {
logsTableRowSorter.setRowFilter(new RowFilter<>() {
@Override
public boolean include(Entry<? extends LogsTableModel, ? extends Integer> entry) {
List<LogsTableModel.Column> places = new ArrayList<>();
if (includeRegex) places.add(LogsTableModel.Column.REGEX);
if (includeMatch) places.add(LogsTableModel.Column.MATCH);
if (includeURL) places.add(LogsTableModel.Column.URL);
return places.stream().anyMatch(column -> entry.getStringValue(column.getIndex()).toLowerCase().contains(text.toLowerCase()));
// hashmap to keep track of unique rows in the table
final HashSet<Integer> uniqueResultsMap = new HashSet<>();

logsTableRowSorter.setRowFilter(new RowFilter<>() {
@Override
public boolean include(Entry<? extends LogsTableModel, ? extends Integer> entry) {
if (uniqueResults) {
int hashcode = entry.getModel().getRowHashcode(entry.getIdentifier());
if (uniqueResultsMap.contains(hashcode)) return false;
uniqueResultsMap.add(hashcode);
}
});
}
if (text.isBlank()) {
return true;
}
List<LogsTableModel.Column> places = new ArrayList<>();
if (includeRegex) places.add(LogsTableModel.Column.REGEX);
if (includeMatch) places.add(LogsTableModel.Column.MATCH);
if (includeURL) places.add(LogsTableModel.Column.URL);
return places.stream().anyMatch(column -> entry.getStringValue(column.getIndex()).toLowerCase().contains(text.toLowerCase()));
}
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javax.swing.table.AbstractTableModel;
import java.util.List;
import java.util.Objects;

import static com.cys4.sensitivediscoverer.utils.Messages.getLocaleString;

Expand Down Expand Up @@ -49,6 +50,16 @@ public Object getValueAt(int rowIndex, int columnIndex) {
};
}

public int getRowHashcode(int rowIndex) {
LogEntity logEntity = logEntries.get(rowIndex);
return Objects.hash(
logEntity.getRequestUrl(),
logEntity.getRegexEntity().getRegex(),
logEntity.getMatchedSection().toString(),
logEntity.getMatch()
);
}

/**
* Enum representing the columns of the table model for logs
*/
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/TextUI_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ logger-clearLogs-title=Clear list?
logger-exportLogs-label=Export list logs...
logger-searchBar-label=Filter results:
logger-resultsCount-label=Results:
logger-uniqueResults-label=Unique results
logger-ctxMenu-sendToRepeater=Send to Repeater
logger-ctxMenu-sendToIntruder=Send to Intruder
logger-ctxMenu-sendToOrganizer=Send to Organizer
Expand Down

0 comments on commit f6e6ed3

Please sign in to comment.