2018-05-15 17:36:45 +00:00
|
|
|
/**
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-07-09 12:23:59 +01:00
|
|
|
import HTMLOperation from "../HTMLOperation.mjs";
|
2018-05-15 17:36:45 +00:00
|
|
|
import Sortable from "sortablejs";
|
2021-02-12 13:51:51 +00:00
|
|
|
import {fuzzyMatch, calcMatchRanges} from "../../core/lib/FuzzyMatch.mjs";
|
2023-06-16 17:08:43 +12:00
|
|
|
import {COperationList} from "../components/c-operation-list.mjs";
|
2018-05-15 17:36:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Waiter to handle events related to the operations.
|
|
|
|
*/
|
|
|
|
class OperationsWaiter {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OperationsWaiter constructor.
|
|
|
|
*
|
|
|
|
* @param {App} app - The main view object for CyberChef.
|
|
|
|
* @param {Manager} manager - The CyberChef event manager.
|
|
|
|
*/
|
|
|
|
constructor(app, manager) {
|
|
|
|
this.app = app;
|
|
|
|
this.manager = manager;
|
|
|
|
|
|
|
|
this.options = {};
|
|
|
|
this.removeIntent = false;
|
|
|
|
}
|
|
|
|
|
2023-04-20 19:21:15 +12:00
|
|
|
|
2018-05-15 17:36:45 +00:00
|
|
|
/**
|
|
|
|
* Handler for search events.
|
|
|
|
* Finds operations which match the given search term and displays them under the search box.
|
|
|
|
*
|
2023-06-16 17:08:43 +12:00
|
|
|
* @param {Event} e
|
2018-05-15 17:36:45 +00:00
|
|
|
*/
|
|
|
|
searchOperations(e) {
|
2023-04-20 19:21:15 +12:00
|
|
|
let ops, selected;
|
2023-04-17 20:44:47 +12:00
|
|
|
|
2023-05-10 21:53:29 +12:00
|
|
|
if (e.type === "keyup") {
|
2023-04-20 19:45:37 +12:00
|
|
|
const searchResults = document.getElementById("search-results");
|
|
|
|
|
2023-05-23 20:15:15 +12:00
|
|
|
this.openOpsDropdown();
|
2023-04-20 19:45:37 +12:00
|
|
|
|
2023-05-10 21:53:29 +12:00
|
|
|
if (e.target.value.length !== 0) {
|
2023-05-12 17:20:11 +12:00
|
|
|
this.app.setElementVisibility(searchResults, true);
|
2023-04-17 20:44:47 +12:00
|
|
|
}
|
|
|
|
}
|
2018-05-15 17:36:45 +00:00
|
|
|
|
2018-08-20 00:10:57 +01:00
|
|
|
if (e.type === "search" || e.keyCode === 13) { // Search or Return
|
2018-05-15 17:36:45 +00:00
|
|
|
e.preventDefault();
|
|
|
|
ops = document.querySelectorAll("#search-results li");
|
|
|
|
if (ops.length) {
|
|
|
|
selected = this.getSelectedOp(ops);
|
|
|
|
if (selected > -1) {
|
2023-04-27 14:40:08 +12:00
|
|
|
this.manager.recipe.addOperation(ops[selected].getAttribute("data-name"));
|
2018-05-15 17:36:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 21:53:29 +12:00
|
|
|
if (e.type === "click" && !e.target.value.length) {
|
2023-05-23 20:15:15 +12:00
|
|
|
this.openOpsDropdown();
|
2023-05-10 21:53:29 +12:00
|
|
|
} else if (e.keyCode === 27) { // Escape
|
2023-05-23 20:15:15 +12:00
|
|
|
this.closeOpsDropdown();
|
2023-04-20 20:54:14 +12:00
|
|
|
} else if (e.keyCode === 40) { // Down
|
2018-05-15 17:36:45 +00:00
|
|
|
e.preventDefault();
|
|
|
|
ops = document.querySelectorAll("#search-results li");
|
|
|
|
if (ops.length) {
|
|
|
|
selected = this.getSelectedOp(ops);
|
|
|
|
if (selected > -1) {
|
|
|
|
ops[selected].classList.remove("selected-op");
|
|
|
|
}
|
|
|
|
if (selected === ops.length-1) selected = -1;
|
|
|
|
ops[selected+1].classList.add("selected-op");
|
|
|
|
}
|
|
|
|
} else if (e.keyCode === 38) { // Up
|
|
|
|
e.preventDefault();
|
|
|
|
ops = document.querySelectorAll("#search-results li");
|
|
|
|
if (ops.length) {
|
|
|
|
selected = this.getSelectedOp(ops);
|
|
|
|
if (selected > -1) {
|
|
|
|
ops[selected].classList.remove("selected-op");
|
|
|
|
}
|
|
|
|
if (selected === 0) selected = ops.length;
|
|
|
|
ops[selected-1].classList.add("selected-op");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const searchResultsEl = document.getElementById("search-results");
|
|
|
|
const el = e.target;
|
|
|
|
const str = el.value;
|
|
|
|
|
|
|
|
while (searchResultsEl.firstChild) {
|
|
|
|
try {
|
2018-06-09 10:43:36 +01:00
|
|
|
$(searchResultsEl.firstChild).popover("dispose");
|
2018-05-15 17:36:45 +00:00
|
|
|
} catch (err) {}
|
|
|
|
searchResultsEl.removeChild(searchResultsEl.firstChild);
|
|
|
|
}
|
|
|
|
|
2018-06-09 10:43:36 +01:00
|
|
|
$("#categories .show").collapse("hide");
|
2018-05-15 17:36:45 +00:00
|
|
|
if (str) {
|
|
|
|
const matchedOps = this.filterOperations(str, true);
|
|
|
|
const matchedOpsHtml = matchedOps
|
|
|
|
.map(v => v.toStubHtml())
|
|
|
|
.join("");
|
|
|
|
|
|
|
|
searchResultsEl.innerHTML = matchedOpsHtml;
|
|
|
|
searchResultsEl.dispatchEvent(this.manager.oplistcreate);
|
|
|
|
}
|
2023-06-16 12:09:04 +12:00
|
|
|
|
2023-05-23 20:15:15 +12:00
|
|
|
this.manager.ops.updateListItemsClasses("#rec-list", "selected");
|
2018-05-15 17:36:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters operations based on the search string and returns the matching ones.
|
|
|
|
*
|
|
|
|
* @param {string} searchStr
|
2023-05-23 20:15:15 +12:00
|
|
|
* @param {boolean} highlight - Whether to highlight the matching string in the operation
|
2018-05-15 17:36:45 +00:00
|
|
|
* name and description
|
|
|
|
* @returns {string[]}
|
|
|
|
*/
|
|
|
|
filterOperations(inStr, highlight) {
|
|
|
|
const matchedOps = [];
|
|
|
|
const matchedDescs = [];
|
|
|
|
|
2022-05-30 18:06:15 +01:00
|
|
|
// Create version with no whitespace for the fuzzy match
|
|
|
|
// Helps avoid missing matches e.g. query "TCP " would not find "Parse TCP"
|
|
|
|
const inStrNWS = inStr.replace(/\s/g, "");
|
|
|
|
|
2018-05-15 17:36:45 +00:00
|
|
|
for (const opName in this.app.operations) {
|
|
|
|
const op = this.app.operations[opName];
|
2021-02-05 17:54:57 +00:00
|
|
|
|
|
|
|
// Match op name using fuzzy match
|
2022-05-30 18:06:15 +01:00
|
|
|
const [nameMatch, score, idxs] = fuzzyMatch(inStrNWS, opName);
|
2021-02-05 17:54:57 +00:00
|
|
|
|
|
|
|
// Match description based on exact match
|
2021-02-09 11:37:25 +00:00
|
|
|
const descPos = op.description.toLowerCase().indexOf(inStr.toLowerCase());
|
2018-05-15 17:36:45 +00:00
|
|
|
|
2021-02-05 17:54:57 +00:00
|
|
|
if (nameMatch || descPos >= 0) {
|
2018-05-15 17:36:45 +00:00
|
|
|
const operation = new HTMLOperation(opName, this.app.operations[opName], this.app, this.manager);
|
2023-04-27 21:13:14 +12:00
|
|
|
|
2018-05-15 17:36:45 +00:00
|
|
|
if (highlight) {
|
2021-02-10 13:13:19 +00:00
|
|
|
operation.highlightSearchStrings(calcMatchRanges(idxs), [[descPos, inStr.length]]);
|
2018-05-15 17:36:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 17:54:57 +00:00
|
|
|
if (nameMatch) {
|
|
|
|
matchedOps.push([operation, score]);
|
2018-05-15 17:36:45 +00:00
|
|
|
} else {
|
|
|
|
matchedDescs.push(operation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 17:54:57 +00:00
|
|
|
// Sort matched operations based on fuzzy score
|
|
|
|
matchedOps.sort((a, b) => b[1] - a[1]);
|
|
|
|
|
|
|
|
return matchedOps.map(a => a[0]).concat(matchedDescs);
|
2018-05-15 17:36:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds the operation which has been selected using keyboard shortcuts. This will have the class
|
|
|
|
* 'selected-op' set. Returns the index of the operation within the given list.
|
|
|
|
*
|
|
|
|
* @param {element[]} ops
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
getSelectedOp(ops) {
|
|
|
|
for (let i = 0; i < ops.length; i++) {
|
|
|
|
if (ops[i].classList.contains("selected-op")) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for oplistcreate events.
|
|
|
|
*
|
|
|
|
* @listens Manager#oplistcreate
|
2023-06-16 17:08:43 +12:00
|
|
|
* @param {Event} e
|
2018-05-15 17:36:45 +00:00
|
|
|
*/
|
|
|
|
opListCreate(e) {
|
2023-05-12 17:50:46 +12:00
|
|
|
if (this.app.isMobileView()) {
|
2023-06-16 12:09:04 +12:00
|
|
|
$(document.querySelectorAll(".op-list .operation")).popover("disable");
|
2023-04-23 11:20:01 +12:00
|
|
|
} else {
|
2023-06-16 12:09:04 +12:00
|
|
|
$(document.querySelectorAll(".op-list .operation")).popover("enable");
|
|
|
|
this.enableOpPopover(e.target);
|
|
|
|
this.manager.recipe.createSortableSeedList(e.target);
|
2023-04-23 11:20:01 +12:00
|
|
|
}
|
2018-05-15 17:36:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-24 11:32:34 +12:00
|
|
|
* Enable the target operation popover itself to gain focus which
|
|
|
|
* enables scrolling and other interactions.
|
2018-05-15 17:36:45 +00:00
|
|
|
*
|
|
|
|
* @param {Element} el - The element to start selecting from
|
|
|
|
*/
|
2023-04-24 11:32:34 +12:00
|
|
|
enableOpPopover(el) {
|
2023-06-16 12:09:04 +12:00
|
|
|
$(el)
|
|
|
|
.find("[data-toggle=popover]")
|
|
|
|
.addBack("[data-toggle=popover]")
|
2018-05-15 17:36:45 +00:00
|
|
|
.popover({trigger: "manual"})
|
|
|
|
.on("mouseenter", function(e) {
|
2019-10-16 15:38:20 +01:00
|
|
|
if (e.buttons > 0) return; // Mouse button held down - likely dragging an operation
|
2018-05-15 17:36:45 +00:00
|
|
|
const _this = this;
|
|
|
|
$(this).popover("show");
|
|
|
|
$(".popover").on("mouseleave", function () {
|
|
|
|
$(_this).popover("hide");
|
|
|
|
});
|
|
|
|
}).on("mouseleave", function () {
|
|
|
|
const _this = this;
|
|
|
|
setTimeout(function() {
|
|
|
|
// Determine if the popover associated with this element is being hovered over
|
|
|
|
if ($(_this).data("bs.popover") &&
|
2018-06-09 10:43:36 +01:00
|
|
|
($(_this).data("bs.popover").tip && !$($(_this).data("bs.popover").tip).is(":hover"))) {
|
2018-05-15 17:36:45 +00:00
|
|
|
$(_this).popover("hide");
|
|
|
|
}
|
|
|
|
}, 50);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for edit favourites click events.
|
|
|
|
* Sets up the 'Edit favourites' pane and displays it.
|
|
|
|
*/
|
2023-06-16 17:08:43 +12:00
|
|
|
editFavouritesClick() {
|
2023-07-21 11:48:43 +12:00
|
|
|
const div = document.getElementById("editable-favourites");
|
|
|
|
|
|
|
|
// Remove c-operation-list if there already was one
|
|
|
|
if ( div.querySelector("c-operation-list") ){
|
|
|
|
div.removeChild(div.querySelector("c-operation-list"));
|
|
|
|
}
|
|
|
|
|
2023-06-16 17:08:43 +12:00
|
|
|
// Get list of Favourite operation names
|
|
|
|
const favCatConfig = this.app.categories.find(catConfig => catConfig.name === "Favourites");
|
|
|
|
|
|
|
|
if(favCatConfig !== undefined) {
|
|
|
|
const opList = new COperationList(
|
|
|
|
this.app,
|
|
|
|
favCatConfig.ops,
|
|
|
|
false,
|
|
|
|
{
|
|
|
|
class: "remove-icon",
|
|
|
|
innerText: "delete"
|
2018-05-15 17:36:45 +00:00
|
|
|
}
|
2023-06-16 17:08:43 +12:00
|
|
|
)
|
2018-05-15 17:36:45 +00:00
|
|
|
|
2023-06-16 17:08:43 +12:00
|
|
|
opList.build();
|
2018-05-15 17:36:45 +00:00
|
|
|
|
2023-07-21 11:48:43 +12:00
|
|
|
// Append the new opList<COperationList>
|
2023-06-16 17:08:43 +12:00
|
|
|
div.appendChild(opList);
|
2023-04-24 16:13:02 +12:00
|
|
|
}
|
2023-06-16 17:08:43 +12:00
|
|
|
|
|
|
|
// this.removeIntent = false;
|
|
|
|
//
|
|
|
|
// const editableList = Sortable.create(ul, {
|
|
|
|
// filter: ".remove-icon",
|
|
|
|
// onFilter: function (evt) {
|
|
|
|
// const el = editableList.closest(evt.item);
|
|
|
|
// if (el && el.parentNode) {
|
|
|
|
// $(el).popover("dispose");
|
|
|
|
// el.parentNode.removeChild(el);
|
|
|
|
// }
|
|
|
|
// },
|
|
|
|
// onEnd: function(evt) {
|
|
|
|
// if (this.removeIntent) {
|
|
|
|
// $(evt.item).popover("dispose");
|
|
|
|
// evt.item.remove();
|
|
|
|
// }
|
|
|
|
// }.bind(this),
|
|
|
|
// });
|
|
|
|
//
|
|
|
|
// Sortable.utils.on(ul, "dragleave", function() {
|
|
|
|
// this.removeIntent = true;
|
|
|
|
// }.bind(this));
|
|
|
|
//
|
|
|
|
// Sortable.utils.on(ul, "dragover", function() {
|
|
|
|
// this.removeIntent = false;
|
|
|
|
// }.bind(this));
|
|
|
|
|
|
|
|
// if (!this.app.isMobileView()) {
|
|
|
|
// $("#editable-favourites [data-toggle=popover]").popover();
|
|
|
|
// }
|
2018-05-15 17:36:45 +00:00
|
|
|
$("#favourites-modal").modal();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-17 20:44:47 +12:00
|
|
|
/**
|
2023-04-20 19:45:37 +12:00
|
|
|
* Open operations dropdown
|
2023-04-17 20:44:47 +12:00
|
|
|
*/
|
2023-05-23 20:15:15 +12:00
|
|
|
openOpsDropdown() {
|
2023-05-09 22:14:36 +12:00
|
|
|
// the 'close' ( dropdown ) icon in Operations component mobile UI
|
2023-05-23 20:15:15 +12:00
|
|
|
const closeOpsDropdownIcon = document.getElementById("close-ops-dropdown-icon");
|
2023-04-20 19:45:37 +12:00
|
|
|
const categories = document.getElementById("categories");
|
2023-04-17 20:44:47 +12:00
|
|
|
|
2023-05-12 17:20:11 +12:00
|
|
|
this.app.setElementVisibility(categories, true);
|
2023-05-23 20:15:15 +12:00
|
|
|
this.app.setElementVisibility(closeOpsDropdownIcon, true);
|
2023-04-20 19:45:37 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hide any operation lists ( #categories or #search-results ) and the close-operations-dropdown
|
|
|
|
* icon itself, clear any search input
|
|
|
|
*/
|
2023-05-23 20:15:15 +12:00
|
|
|
closeOpsDropdown() {
|
2023-04-20 19:45:37 +12:00
|
|
|
const search = document.getElementById("search");
|
2023-04-17 20:44:47 +12:00
|
|
|
|
|
|
|
// if any input remains in #search, clear it
|
|
|
|
if (search.value.length) {
|
2023-05-10 21:53:29 +12:00
|
|
|
search.value = "";
|
2023-04-17 20:44:47 +12:00
|
|
|
}
|
|
|
|
|
2023-05-12 17:20:11 +12:00
|
|
|
this.app.setElementVisibility(document.getElementById("categories"), false);
|
|
|
|
this.app.setElementVisibility(document.getElementById("search-results"), false);
|
2023-05-23 20:15:15 +12:00
|
|
|
this.app.setElementVisibility(document.getElementById("close-ops-dropdown-icon"), false);
|
2023-04-17 20:44:47 +12:00
|
|
|
}
|
|
|
|
|
2018-05-15 17:36:45 +00:00
|
|
|
/**
|
|
|
|
* Handler for save favourites click events.
|
|
|
|
* Saves the selected favourites and reloads them.
|
|
|
|
*/
|
|
|
|
saveFavouritesClick() {
|
2023-06-16 17:08:43 +12:00
|
|
|
const listItems = document.querySelectorAll("#editable-favourites c-operation-li > li");
|
|
|
|
const favourites = Array.from(listItems, li => li.getAttribute("data-name"));
|
2018-05-15 17:36:45 +00:00
|
|
|
|
2023-06-16 17:08:43 +12:00
|
|
|
this.app.updateFavourites(favourites);
|
2018-05-15 17:36:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for reset favourites click events.
|
|
|
|
* Resets favourites to their defaults.
|
|
|
|
*/
|
|
|
|
resetFavouritesClick() {
|
|
|
|
this.app.resetFavourites();
|
2023-05-09 22:14:36 +12:00
|
|
|
}
|
|
|
|
|
2023-05-23 20:15:15 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update classes in the #dropdown-operations op-lists based on the
|
|
|
|
* list items of a srcListSelector.
|
|
|
|
*
|
|
|
|
* e.g: the operations currently listed in the recipe-list and the appropriate
|
|
|
|
* list items in operations-dropdown that need to have the 'selected' class added
|
|
|
|
* or removed. Another use case is using the current 'Favourite' category op-list
|
|
|
|
* as a source and handle the 'favourite' class on operations-dropdown op-lists
|
|
|
|
* accordingly
|
|
|
|
*
|
|
|
|
* @param {string} srcListSelector - the UL element list to compare to
|
|
|
|
* @param {string} className - the className to update
|
|
|
|
*/
|
|
|
|
updateListItemsClasses(srcListSelector, className) {
|
2023-07-24 12:14:59 +12:00
|
|
|
const listItems = document.querySelectorAll(`${srcListSelector} li`);
|
2023-07-21 11:48:43 +12:00
|
|
|
const ops = document.querySelectorAll("#categories c-operation-li > li.operation");
|
2023-05-23 20:15:15 +12:00
|
|
|
|
|
|
|
this.removeClassFromOps(className);
|
|
|
|
|
|
|
|
if (listItems.length !== 0) {
|
|
|
|
listItems.forEach((item => {
|
|
|
|
const targetDataName = item.getAttribute("data-name");
|
|
|
|
|
|
|
|
ops.forEach((op) => {
|
|
|
|
if (targetDataName === op.getAttribute("data-name")) {
|
|
|
|
this.addClassToOp(targetDataName, className);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic function to remove a class from > ALL < operation list items
|
|
|
|
*
|
|
|
|
* @param {string} className - the class to remove
|
|
|
|
*/
|
|
|
|
removeClassFromOps(className) {
|
2023-07-21 11:48:43 +12:00
|
|
|
const ops = document.querySelectorAll("#categories c-operation-li > li.operation");
|
2023-05-23 20:15:15 +12:00
|
|
|
|
|
|
|
ops.forEach((op => {
|
|
|
|
this.removeClassFromOp(op.getAttribute("data-name"), className);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic function to remove a class from target operation list item
|
|
|
|
*
|
2023-06-16 12:09:04 +12:00
|
|
|
* @param {string} opName - operation name through data-name attribute of the target operation
|
2023-05-23 20:15:15 +12:00
|
|
|
* @param {string} className - the class to remove
|
|
|
|
*/
|
2023-06-16 12:09:04 +12:00
|
|
|
removeClassFromOp(opName, className) {
|
2023-07-21 11:48:43 +12:00
|
|
|
const ops = document.querySelectorAll(`#categories c-operation-li > li.operation[data-name="${opName}"].${className}`);
|
2023-05-23 20:15:15 +12:00
|
|
|
|
|
|
|
// the same operation may occur twice if it is also in #catFavourites
|
|
|
|
ops.forEach((op) => {
|
|
|
|
op.classList.remove(`${className}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic function to add a class to an operation list item
|
|
|
|
*
|
2023-06-16 12:09:04 +12:00
|
|
|
* @param {string} opName - operation name through data-name attribute of the target operation
|
2023-05-23 20:15:15 +12:00
|
|
|
* @param {string} className - the class to add to the operation list item
|
|
|
|
*/
|
2023-06-16 12:09:04 +12:00
|
|
|
addClassToOp(opName, className) {
|
2023-07-21 11:48:43 +12:00
|
|
|
const ops = document.querySelectorAll(`#categories c-operation-li > li.operation[data-name="${opName}"]`);
|
2023-05-23 20:15:15 +12:00
|
|
|
|
|
|
|
// the same operation may occur twice if it is also in #catFavourites
|
|
|
|
ops.forEach((op => {
|
|
|
|
op.classList.add(`${className}`);
|
|
|
|
}));
|
2018-05-15 17:36:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default OperationsWaiter;
|