// Path: src/main/java/com/redcanari/ui/JSLoginDialog.java
// public class JSLoginDialog extends Dialog<Pair<String, String>> {
// 
//     private final TextField username;
// 
//     public JSLoginDialog() {
//         setTitle("Login Dialog");
// 
//         // Set the icon (must be included in the project).
// //        setGraphic(new ImageView(this.getClass().getResource("login.png").toString()));
// 
//         // Set the button types.
//         ButtonType loginButtonType = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE);
//         getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
// 
//         // Create the username and password labels and fields.
//         GridPane grid = new GridPane();
//         grid.setHgap(10);
//         grid.setVgap(10);
//         grid.setPadding(new Insets(20, 150, 10, 10));
// 
//         username = new TextField();
//         username.setPromptText("Username");
//         PasswordField password = new PasswordField();
//         password.setPromptText("Password");
// 
//         grid.add(new Label("Username:"), 0, 0);
//         grid.add(username, 1, 0);
//         grid.add(new Label("Password:"), 0, 1);
//         grid.add(password, 1, 1);
// 
//         // Enable/Disable login button depending on whether a username was entered.
//         Node loginButton = getDialogPane().lookupButton(loginButtonType);
// //        loginButton.setDisable(true);
// 
//         // Do some validation (using the Java 8 lambda syntax).
// //        username.textProperty().addListener((observable, oldValue, newValue) -> {
// //            loginButton.setDisable(newValue.trim().isEmpty());
// //        });
// 
//         getDialogPane().setContent(grid);
// 
//         // Convert the result to a username-password-pair when the login button is clicked.
//         setResultConverter(dialogButton -> {
//             if (dialogButton == loginButtonType) {
//                 return new Pair<>(username.getText(), password.getText());
//             }
//             return null;
//         });
//     }
// 
//     public Pair<String, String> login(String loginPrompt) {
//         // Set the masthead to the user's prompt message.
//         setHeaderText(loginPrompt);
// 
//         // Request focus on the textfield so the user can type their response right away.
//         // Request focus on the username field by default.
//         Platform.runLater(username::requestFocus);
// 
//         // Show and wait for the result and then return a string or null
//         Optional<Pair<String, String>> result = super.showAndWait();
//         return (result.isPresent())?result.get():null;
//     }
// }
// 
// Path: src/main/java/com/redcanari/util/ResourceUtils.java
// public class ResourceUtils {
// 
//     public static String getResourceContentsAsString(String filename) {
//         InputStream inputStream = ResourceUtils.class.getResourceAsStream(filename);
//         return new Scanner(inputStream).useDelimiter("\\Z").next();
//     }
// }


import com.redcanari.ui.JSInputDialog;
import com.redcanari.ui.JSLoginDialog;
import com.redcanari.util.ResourceUtils;
import javafx.application.Platform;
import javafx.scene.web.PromptData;
import javafx.scene.web.WebEngine;
import javafx.stage.FileChooser;
import javafx.util.Pair;
import netscape.javascript.JSObject;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Optional;
/*
 * BurpKit - WebKit-based penetration testing plugin for BurpSuite
 * Copyright (C) 2015  Red Canari, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.redcanari.js;



/**
 * Created by ndouba on 15-05-16.
 */
public class BurpKitBridge {


    private final LocalJSObject locals;
    private final LocalJSObject globals;

    private final WebEngine webEngine;

    public BurpKitBridge(WebEngine webEngine) {
        this.webEngine = webEngine;
        globals = GlobalJSObject.getGlobalJSObject(webEngine);
        locals = new LocalJSObject(webEngine);
    }

    public byte[] httpGetBytes(String url) throws IOException {
        URLConnection uc = new URL(url).openConnection();
        uc.setRequestProperty("User-Agent", webEngine.getUserAgent());
        return Helpers.convertStreamToBytes(uc.getInputStream());
    }

    public String httpGetString(String url) throws IOException {
        URLConnection uc = new URL(url).openConnection();
        uc.setRequestProperty("User-Agent", webEngine.getUserAgent());
        return Helpers.convertStreamToString(uc.getInputStream());
    }

    public void require(String url) throws IOException {
        webEngine.executeScript(httpGetString(url));
    }

    public void requireLib(String library) throws IOException {
        webEngine.executeScript(
// Path: src/main/java/com/redcanari/ui/JSLoginDialog.java
// public class JSLoginDialog extends Dialog<Pair<String, String>> {
// 
//     private final TextField username;
// 
//     public JSLoginDialog() {
//         setTitle("Login Dialog");
// 
//         // Set the icon (must be included in the project).
// //        setGraphic(new ImageView(this.getClass().getResource("login.png").toString()));
// 
//         // Set the button types.
//         ButtonType loginButtonType = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE);
//         getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
// 
//         // Create the username and password labels and fields.
//         GridPane grid = new GridPane();
//         grid.setHgap(10);
//         grid.setVgap(10);
//         grid.setPadding(new Insets(20, 150, 10, 10));
// 
//         username = new TextField();
//         username.setPromptText("Username");
//         PasswordField password = new PasswordField();
//         password.setPromptText("Password");
// 
//         grid.add(new Label("Username:"), 0, 0);
//         grid.add(username, 1, 0);
//         grid.add(new Label("Password:"), 0, 1);
//         grid.add(password, 1, 1);
// 
//         // Enable/Disable login button depending on whether a username was entered.
//         Node loginButton = getDialogPane().lookupButton(loginButtonType);
// //        loginButton.setDisable(true);
// 
//         // Do some validation (using the Java 8 lambda syntax).
// //        username.textProperty().addListener((observable, oldValue, newValue) -> {
// //            loginButton.setDisable(newValue.trim().isEmpty());
// //        });
// 
//         getDialogPane().setContent(grid);
// 
//         // Convert the result to a username-password-pair when the login button is clicked.
//         setResultConverter(dialogButton -> {
//             if (dialogButton == loginButtonType) {
//                 return new Pair<>(username.getText(), password.getText());
//             }
//             return null;
//         });
//     }
// 
//     public Pair<String, String> login(String loginPrompt) {
//         // Set the masthead to the user's prompt message.
//         setHeaderText(loginPrompt);
// 
//         // Request focus on the textfield so the user can type their response right away.
//         // Request focus on the username field by default.
//         Platform.runLater(username::requestFocus);
// 
//         // Show and wait for the result and then return a string or null
//         Optional<Pair<String, String>> result = super.showAndWait();
//         return (result.isPresent())?result.get():null;
//     }
// }
// 
// Path: src/main/java/com/redcanari/util/ResourceUtils.java
// public class ResourceUtils {
// 
//     public static String getResourceContentsAsString(String filename) {
//         InputStream inputStream = ResourceUtils.class.getResourceAsStream(filename);
//         return new Scanner(inputStream).useDelimiter("\\Z").next();
//     }
// }


// Path: src/main/java/com/redcanari/js/BurpKitBridge.java
import com.redcanari.ui.JSInputDialog;
import com.redcanari.ui.JSLoginDialog;
import com.redcanari.util.ResourceUtils;
import javafx.application.Platform;
import javafx.scene.web.PromptData;
import javafx.scene.web.WebEngine;
import javafx.stage.FileChooser;
import javafx.util.Pair;
import netscape.javascript.JSObject;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Optional;

/*
 * BurpKit - WebKit-based penetration testing plugin for BurpSuite
 * Copyright (C) 2015  Red Canari, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.redcanari.js;



/**
 * Created by ndouba on 15-05-16.
 */
public class BurpKitBridge {


    private final LocalJSObject locals;
    private final LocalJSObject globals;

    private final WebEngine webEngine;

    public BurpKitBridge(WebEngine webEngine) {
        this.webEngine = webEngine;
        globals = GlobalJSObject.getGlobalJSObject(webEngine);
        locals = new LocalJSObject(webEngine);
    }

    public byte[] httpGetBytes(String url) throws IOException {
        URLConnection uc = new URL(url).openConnection();
        uc.setRequestProperty("User-Agent", webEngine.getUserAgent());
        return Helpers.convertStreamToBytes(uc.getInputStream());
    }

    public String httpGetString(String url) throws IOException {
        URLConnection uc = new URL(url).openConnection();
        uc.setRequestProperty("User-Agent", webEngine.getUserAgent());
        return Helpers.convertStreamToString(uc.getInputStream());
    }

    public void require(String url) throws IOException {
        webEngine.executeScript(httpGetString(url));
    }

    public void requireLib(String library) throws IOException {
        webEngine.executeScript(
                ResourceUtils.getResourceContentsAsString("/scripts/" + library + ".js")