Skip to content

Commit

Permalink
Some fixes on side effects & completion more in bash style
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4144 committed May 12, 2021
1 parent e647318 commit aca0cff
Show file tree
Hide file tree
Showing 3 changed files with 506 additions and 265 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8

compileJava.options.encoding = 'UTF-8'

repositories {
mavenCentral()
}
Expand Down
101 changes: 72 additions & 29 deletions src/main/java/com/hopla/Completer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import javax.swing.text.DefaultCaret;
import java.awt.event.MouseEvent;

public class Completer implements DocumentListener, CaretListener {

Expand Down Expand Up @@ -61,13 +62,18 @@ public class Completer implements DocumentListener, CaretListener {
public ArrayList<String> keywords;
private Boolean mode_insert = false;
private Boolean in_selection = false;
private Boolean in_completion = false;
private int selection_size = -1;
private Boolean no_caret_update = false;

/**
* This listener follows the caret and updates where we should draw the suggestions box
*/
@Override
public void caretUpdate(CaretEvent e) {
if (no_caret_update){
return;
}
pos = e.getDot();
if (e.getMark() != e.getDot()){
in_selection = true;
Expand Down Expand Up @@ -190,14 +196,15 @@ public void mouseClicked(MouseEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (startPos == 0){
if (startPos == 0){
source.select(start,pos);
}else{
source.select(start+1,pos);
}
source.replaceSelection(selectedCompletion);
source.setCaretPosition(source.getSelectionEnd());
suggestionPane.setVisible(false);
suggestionsModel.removeAllElements();
startPos = -1;
mode_insert = false;
}
Expand All @@ -223,15 +230,15 @@ public void keyPressed(KeyEvent e){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (startPos == 0){
if (startPos == 0){
source.select(start,pos);
}else{
source.select(start+1,pos);
}

source.replaceSelection(val);
source.setCaretPosition(source.getSelectionEnd());
suggestionPane.setVisible(false);
suggestionsModel.removeAllElements();
startPos = -1;
mode_insert = false;
}
Expand Down Expand Up @@ -276,6 +283,7 @@ public void focusLost(FocusEvent e) {
});


stdout.println(KeyStroke.getKeyStroke("control Q"));

// Show Payload library on ctrl + q
this.source.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_DOWN_MASK),
Expand All @@ -297,11 +305,11 @@ public void actionPerformed(ActionEvent e) {

}
});

// Source JTextarea listener
this.source.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e){
if (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK || e.getModifiersEx() == (KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK)){
if (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK || e.getModifiersEx() == (KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK)|| e.getModifiersEx() == (KeyEvent.CTRL_DOWN_MASK | MouseEvent.BUTTON1_DOWN_MASK)){
mode_insert = true;
startPos = -1;
pos -= 1;
Expand Down Expand Up @@ -331,28 +339,47 @@ public void keyPressed(KeyEvent e){
break;

case KeyEvent.VK_TAB:
// Pick the first payload on tab completion
suggestions.setSelectedIndex(0);
List<String> values = suggestions.getSelectedValuesList();
// Pick the common prefix
ArrayList<String> all_values = new ArrayList<String>();
for (int i = 0; i < suggestions.getModel().getSize(); i++) {
all_values.add(String.valueOf(suggestions.getModel().getElementAt(i))) ;
}
suggestionPane.setVisible(false);
mode_insert = true;

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (startPos == 0){
source.select(start,pos);
}else{
source.select(start+1,pos);
}
source.replaceSelection(values.get(0).toString());
source.setCaretPosition(source.getSelectionEnd());
suggestionPane.setVisible(false);
startPos = -1;
mode_insert = false;

}
});
suggestionPane.setVisible(false);
if (all_values.size() > 0 && startPos+1 != pos && startPos != -1 ){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
String prefix = longestCommonPrefix(all_values);
if (prefix.equals("")){
mode_insert = false;
suggestionPane.setVisible(true);
return;
}

in_completion = true;
no_caret_update = true;
if (startPos == 0){
source.select(start,pos);
}else{
source.select(start+1,pos);
}
source.replaceSelection(prefix);
no_caret_update = false;
source.setCaretPosition(source.getSelectionEnd());
if (all_values.size() == 1){
in_completion = false;
suggestionsModel.removeAllElements();
suggestionPane.setVisible(false);
}else{
suggestionPane.setVisible(true);
}
mode_insert = false;
}
});
}

s.requestFocus();
e.consume();
break;
Expand Down Expand Up @@ -385,6 +412,20 @@ public void run() {
});
}

private String longestCommonPrefix(List<String> values) {
if (values.size() == 0) return "";

String prefix = values.get(0);
for (int i = 1; i < values.size(); i++) {
while (values.get(i).indexOf(prefix) != 0) {
prefix = prefix.substring(0, prefix.length() - 1);
if (prefix.equals("")) return "";
}
}
return prefix;
}



public CompleterActionListener getActionListener() {
return new CompleterActionListener() {
Expand Down Expand Up @@ -422,7 +463,10 @@ private ArrayList<String> prefixSearcher(String search) {
ArrayList<String> results = new ArrayList<>();
for(String in : this.keywords) {
if( !in.toLowerCase().equals(search.trim()) && in.toLowerCase().startsWith(search.trim()) ) {

// don't make burp slower
if (results.size() == 50){
break;
}
results.add(in);
}
}
Expand Down Expand Up @@ -461,7 +505,7 @@ public void changedUpdate(DocumentEvent e) {


private void Completions(int offset) {

if (mode_insert){
return;
}
Expand Down Expand Up @@ -491,7 +535,7 @@ private void Completions(int offset) {
if (start == startPos){
break;
}
if (Character.isWhitespace(content.charAt(start))){
if (Character.isWhitespace(content.charAt(start)) && !in_completion){
startPos = start;
break;
}
Expand All @@ -508,7 +552,6 @@ private void Completions(int offset) {
suggestionPane.setVisible(false);
return;
}

// corner case for http method
if (startPos == 0){
start = -1;
Expand Down
Loading

0 comments on commit aca0cff

Please sign in to comment.