Skip to content

Commit

Permalink
added some UX improvements, refactored the code to get rid of shared …
Browse files Browse the repository at this point in the history
…myhttprequests instance and instead use events to pass the list
  • Loading branch information
aress31 committed Dec 26, 2023
1 parent 5cc33c1 commit fd980e1
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 132 deletions.
15 changes: 7 additions & 8 deletions src/main/java/burp/MyContextMenuItemsProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
import burp.http.MyHttpRequest;

public class MyContextMenuItemsProvider implements ContextMenuItemsProvider {
private final MontoyaApi montoyaApi;
private final ParserPanel parserPanel;
private final Logging logging;

public MyContextMenuItemsProvider(MontoyaApi montoyaApi, ParserPanel parserPanel) {
this.montoyaApi = montoyaApi;
this.logging = montoyaApi.logging();

this.parserPanel = parserPanel;
}

Expand All @@ -41,24 +40,24 @@ public List<Component> provideMenuItems(ContextMenuEvent contextMenuEvent) {
openApiMenuItem.addActionListener(e -> {
try {
List<HttpRequestResponse> selectedHttpRequestResponses = contextMenuEvent.selectedRequestResponses();
Worker worker = new Worker(montoyaApi);
Worker worker = new Worker();

selectedHttpRequestResponses.forEach(selectedMessage -> {
HttpRequest selectedRequest = selectedMessage.request();
String url = selectedRequest.url();

ParserTableModel tableModel = (ParserTableModel) parserPanel.getTable().getModel();
List<MyHttpRequest> myHttpRequests = worker.parseOpenAPI(worker.processOpenAPI(url));
ParserTableModel parserTableModel = (ParserTableModel) this.parserPanel.getTable().getModel();
List<MyHttpRequest> parsedMyHttpRequests = worker.parseOpenAPI(worker.processOpenAPI(url));

SwingUtilities.invokeLater(() -> {
myHttpRequests.forEach(myHttpRequest -> tableModel.addRow(myHttpRequest));

parserTableModel.addRows(parsedMyHttpRequests);
parserPanel.getResourceTextField().setText(url);
parserPanel.getStatusPanel().updateStatus(COPYRIGHT, UIManager.getColor("TextField.foreground"));
});
});
} catch (Exception exception) {
logging.logToError(exception);
logging.raiseErrorEvent(exception.getMessage());

String message = String.format(
"Failed to process request %s. Check the extension's error log for the stack trace and report the issue.",
((HttpRequestResponse) contextMenuEvent.selectedRequestResponses().get(0)).request().url());
Expand Down
37 changes: 17 additions & 20 deletions src/main/java/swurg/gui/MainTabGroup.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,53 @@
package swurg.gui;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JTabbedPane;

import burp.api.montoya.MontoyaApi;
import burp.http.MyHttpRequest;
import lombok.Getter;
import swurg.gui.views.AboutPanel;
import swurg.gui.views.ParametersPanel;
import swurg.gui.views.ParserPanel;
import swurg.observers.MyObserver;
import swurg.observers.TableModelObserver;

import lombok.Data;

@Data
public class MainTabGroup extends JTabbedPane implements MyObserver {
public class MainTabGroup extends JTabbedPane implements TableModelObserver {

private final MontoyaApi montoyaApi;

private final List<MyHttpRequest> myHttpRequests;

@Getter
private ParserPanel parserPanel;
@Getter
private ParametersPanel parametersPanel;
private AboutPanel aboutPanel;

public MainTabGroup(MontoyaApi montoyaApi) {
this.montoyaApi = montoyaApi;
this.myHttpRequests = new ArrayList<>();

initComponents();

this.parserPanel.getParserTableModel().registerObserver(this);
this.parserPanel.getParserTableModel().registerObserver(this.parserPanel);
this.parserPanel.getParserTableModel().registerObserver(this.parametersPanel);
}

private void initComponents() {
parserPanel = new ParserPanel(this.montoyaApi, this.myHttpRequests);
parserPanel = new ParserPanel(this.montoyaApi);
aboutPanel = new AboutPanel(this.montoyaApi);
parametersPanel = new ParametersPanel(this.montoyaApi, this.myHttpRequests);
parametersPanel = new ParametersPanel(this.montoyaApi);

addTab("Parser", parserPanel);
addTab("About", aboutPanel);
this.addTab("Parser", parserPanel);
this.addTab("About", aboutPanel);
}

@Override
public void onMyHttpRequestsUpdate() {
if (indexOfComponent(this.parametersPanel) == -1 && !this.myHttpRequests.isEmpty()) {
removeTabAt(indexOfComponent(this.aboutPanel));
addTab("Parameters", this.parametersPanel);
addTab("About", this.aboutPanel);
} else if (indexOfComponent(this.parametersPanel) != -1 && this.myHttpRequests.isEmpty())
removeTabAt(indexOfComponent(this.parametersPanel));
public void onMyHttpRequestsUpdate(int event, List<MyHttpRequest> myHttpRequests) {
if (indexOfComponent(this.parametersPanel) == -1 && !myHttpRequests.isEmpty()) {
this.removeTabAt(indexOfComponent(this.aboutPanel));
this.addTab("Parameters", this.parametersPanel);
this.addTab("About", this.aboutPanel);
} else if (indexOfComponent(this.parametersPanel) != -1 && myHttpRequests.isEmpty())
this.removeTabAt(indexOfComponent(this.parametersPanel));
}
}
4 changes: 2 additions & 2 deletions src/main/java/swurg/gui/components/tables/TablePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@

import burp.api.montoya.ui.editor.HttpRequestEditor;
import burp.http.MyHttpRequest;
import lombok.Data;
import lombok.Getter;
import swurg.gui.components.FilterPanel;
import swurg.gui.components.tables.models.ParserTableModel;

@Data
public class TablePanel extends JPanel {

private final TableModel tableModel;
private final TableCellRenderer cellRenderer;
private final HttpRequestEditor requestViewer;

@Getter
private JTable table;
private TableRowSorter<TableModel> tableRowSorter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,39 @@
import javax.swing.table.AbstractTableModel;

import burp.http.MyHttpParameter;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Data
public class ParametersTableModel extends AbstractTableModel {

private final String[] columnNames = { "#", "Parameter", "Type (BODY, COOKIE, URL)",
"Parsed Value (Example Value or Data type)", "Edited Value" };

@Getter
@Setter
private Set<MyHttpParameter> myHttpParameters;

public ParametersTableModel() {
this.myHttpParameters = new LinkedHashSet<MyHttpParameter>();
}

public MyHttpParameter getHttpParameterAt(int rowIndex) {
return myHttpParameters.stream().skip(rowIndex).findFirst().orElse(null);
return this.myHttpParameters.stream().skip(rowIndex).findFirst().orElse(null);
}

@Override
public int getRowCount() {
return myHttpParameters.size();
return this.myHttpParameters.size();
}

@Override
public int getColumnCount() {
return columnNames.length;
return this.columnNames.length;
}

@Override
public String getColumnName(int column) {
return columnNames[column];
return this.columnNames[column];
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,64 @@
import java.util.List;
import java.util.stream.Collectors;

import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;

import burp.api.montoya.http.message.params.HttpParameterType;
import burp.api.montoya.http.message.params.ParsedHttpParameter;
import burp.http.MyHttpRequest;
import lombok.Data;
import swurg.observers.MyObserver;
import lombok.Getter;
import swurg.observers.TableModelObserver;

@Data
public class ParserTableModel extends AbstractTableModel {

private final String[] columnNames = { "#", "Scheme", "Method", "Server", "Path", "Parameters (COOKIE, URL)",
"Description" };
private final List<MyHttpRequest> myHttpRequests;

private final List<MyObserver> observers = new ArrayList<>();
@Getter
private final List<MyHttpRequest> myHttpRequests = new ArrayList<MyHttpRequest>();

public ParserTableModel(List<MyHttpRequest> myHttpRequests) {
this.myHttpRequests = myHttpRequests;
}
private final List<TableModelObserver> observers = new ArrayList<>();

public void addRow(MyHttpRequest myHttpRequest) {
int rowCount = getRowCount();
myHttpRequests.add(myHttpRequest);
fireTableRowsInserted(rowCount, rowCount);
notifyObservers();
public void addRows(List<MyHttpRequest> myHttpRequests) {
this.myHttpRequests.addAll(myHttpRequests);
fireTableDataChanged();
notifyObservers(TableModelEvent.INSERT);
}

public void removeRow(int rowIndex) {
myHttpRequests.remove(rowIndex);
fireTableRowsDeleted(rowIndex, rowIndex);
notifyObservers();
public void removeRow(int index) {
this.myHttpRequests.remove(index);
fireTableRowsDeleted(index, index);
notifyObservers(TableModelEvent.DELETE);
}

public void clear() {
myHttpRequests.clear();
this.myHttpRequests.clear();
fireTableDataChanged();
notifyObservers();
notifyObservers(TableModelEvent.DELETE);
}

public void registerObserver(MyObserver observer) {
observers.add(observer);
public void registerObserver(TableModelObserver observer) {
this.observers.add(observer);
}

private void notifyObservers() {
observers.forEach(observer -> observer.onMyHttpRequestsUpdate());
private void notifyObservers(int event) {
this.observers.forEach(observer -> observer.onMyHttpRequestsUpdate(event, myHttpRequests));
}

@Override
public int getRowCount() {
return myHttpRequests.size();
return this.myHttpRequests.size();
}

@Override
public int getColumnCount() {
return columnNames.length;
return this.columnNames.length;
}

@Override
public String getColumnName(int column) {
return columnNames[column];
return this.columnNames[column];
}

@Override
Expand All @@ -77,7 +74,7 @@ public Class<?> getColumnClass(int column) {

@Override
public Object getValueAt(int row, int column) {
MyHttpRequest myHttpRequest = myHttpRequests.get(row);
MyHttpRequest myHttpRequest = this.myHttpRequests.get(row);

switch (column) {
case 0:
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/swurg/gui/views/AboutPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public AboutPanel(MontoyaApi montoyaApi) {
this.suiteFrame = montoyaApi.userInterface().swingUtils().suiteFrame();

initComponents();

addComponentListeners();
}

Expand All @@ -73,10 +72,10 @@ public void componentShown(ComponentEvent e) {

private void initComponents() {
this.setLayout(new BorderLayout());
this.setBorder(new EmptyBorder(INSET, INSET, 0, INSET));
this.setBorder(new EmptyBorder(this.INSET, this.INSET, 0, this.INSET));

this.northPanel = createNorthPanel();
this.northPanel.setBorder(new EmptyBorder(0, 0, INSET, 0));
this.northPanel.setBorder(new EmptyBorder(0, 0, this.INSET, 0));

this.add(northPanel, BorderLayout.NORTH);
this.add(createCenterPanel(), BorderLayout.CENTER);
Expand Down Expand Up @@ -110,7 +109,7 @@ private JPanel createCenterPanel() {
scrollPane.setBorder(null);

panel.add(scrollPane);
panel.add(Box.createVerticalStrut(INSET));
panel.add(Box.createVerticalStrut(this.INSET));
panel.add(createButtonPanel());

return panel;
Expand Down
38 changes: 16 additions & 22 deletions src/main/java/swurg/gui/views/ParametersPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import burp.api.montoya.logging.Logging;
import burp.http.MyHttpParameter;
import burp.http.MyHttpRequest;
import burp.api.montoya.http.handler.HttpHandler;
Expand All @@ -39,16 +38,14 @@
import swurg.gui.components.tables.TablePanel;
import swurg.gui.components.tables.models.ParametersTableModel;
import swurg.gui.components.tables.renderers.CustomTableCellRenderer;
import swurg.observers.MyObserver;
import swurg.observers.TableModelObserver;
import swurg.utilities.HtmlResourceLoader;

public class ParametersPanel extends JPanel
implements HttpHandler, MyObserver {
implements HttpHandler, TableModelObserver {

private final Logging logging;
private final Frame suiteFrame;

private final List<MyHttpRequest> myHttpRequests;
private final ParametersTableModel parametersTableModel;

private final List<ToolType> toolsInScope = new ArrayList<>();
Expand All @@ -63,15 +60,12 @@ public class ParametersPanel extends JPanel

private JScrollPane scrollPane;

public ParametersPanel(MontoyaApi montoyaApi, List<MyHttpRequest> myHttpRequests) {
this.logging = montoyaApi.logging();
public ParametersPanel(MontoyaApi montoyaApi) {
this.suiteFrame = montoyaApi.userInterface().swingUtils().suiteFrame();

this.myHttpRequests = myHttpRequests;
this.parametersTableModel = new ParametersTableModel();

initComponents();

addComponentListeners();
}

Expand All @@ -96,19 +90,6 @@ public void componentShown(ComponentEvent e) {
});
}

@Override
public void onMyHttpRequestsUpdate() {
Set<MyHttpParameter> myHttpParameters = myHttpRequests.stream()
.flatMap(myHttpRequest -> myHttpRequest.getHttpRequest().parameters().stream()
.map(myHttpParameter -> MyHttpParameter.builder()
.httpParameter(HttpParameter.parameter(myHttpParameter.name(), myHttpParameter.value(),
myHttpParameter.type()))
.build()))
.collect(Collectors.toSet());

parametersTableModel.setMyHttpParameters(myHttpParameters);
}

private void initComponents() {
this.setLayout(new BorderLayout());

Expand Down Expand Up @@ -220,4 +201,17 @@ private HttpRequest updateHttpRequestToBeSent(HttpRequestToBeSent httpRequestToB
public ResponseReceivedAction handleHttpResponseReceived(HttpResponseReceived responseReceived) {
return ResponseReceivedAction.continueWith(responseReceived);
}

@Override
public void onMyHttpRequestsUpdate(int event, List<MyHttpRequest> myHttpRequests) {
Set<MyHttpParameter> myHttpParameters = myHttpRequests.stream()
.flatMap(myHttpRequest -> myHttpRequest.getHttpRequest().parameters().stream()
.map(myHttpParameter -> MyHttpParameter.builder()
.httpParameter(HttpParameter.parameter(myHttpParameter.name(), myHttpParameter.value(),
myHttpParameter.type()))
.build()))
.collect(Collectors.toSet());

parametersTableModel.setMyHttpParameters(myHttpParameters);
}
}
Loading

0 comments on commit fd980e1

Please sign in to comment.