2017-03-06 12:45:51 +00:00
|
|
|
var Dish = require("./Dish.js"),
|
|
|
|
Recipe = require("./Recipe.js");
|
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* The main controller for CyberChef.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
*/
|
2017-03-06 12:45:51 +00:00
|
|
|
var Chef = module.exports = function() {
|
2016-11-28 10:42:58 +00:00
|
|
|
this.dish = new Dish();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the recipe over the input.
|
|
|
|
*
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {string} inputText - The input data as a string
|
|
|
|
* @param {Object[]} recipeConfig - The recipe configuration object
|
2016-11-28 10:42:58 +00:00
|
|
|
* @param {Object} options - The options object storing various user choices
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {boolean} options.attempHighlight - Whether or not to attempt highlighting
|
2016-11-28 10:42:58 +00:00
|
|
|
* @param {number} progress - The position in the recipe to start from
|
|
|
|
* @param {number} [step] - The number of operations to execute
|
|
|
|
*
|
|
|
|
* @returns {Object} response
|
|
|
|
* @returns {string} response.result - The output of the recipe
|
|
|
|
* @returns {string} response.type - The data type of the result
|
|
|
|
* @returns {number} response.progress - The position that we have got to in the recipe
|
|
|
|
* @returns {number} response.options - The app options object (which may have been changed)
|
|
|
|
* @returns {number} response.duration - The number of ms it took to execute the recipe
|
|
|
|
* @returns {number} response.error - The error object thrown by a failed operation (false if no error)
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
Chef.prototype.bake = function(inputText, recipeConfig, options, progress, step) {
|
|
|
|
var startTime = new Date().getTime(),
|
|
|
|
recipe = new Recipe(recipeConfig),
|
|
|
|
containsFc = recipe.containsFlowControl(),
|
|
|
|
error = false;
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
// Reset attemptHighlight flag
|
|
|
|
if (options.hasOwnProperty("attemptHighlight")) {
|
|
|
|
options.attemptHighlight = true;
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (containsFc) options.attemptHighlight = false;
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
// Clean up progress
|
2017-01-31 18:24:56 +00:00
|
|
|
if (progress >= recipeConfig.length) {
|
2016-11-28 10:42:58 +00:00
|
|
|
progress = 0;
|
|
|
|
}
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
if (step) {
|
|
|
|
// Unset breakpoint on this step
|
2017-01-31 18:24:56 +00:00
|
|
|
recipe.setBreakpoint(progress, false);
|
2016-11-28 10:42:58 +00:00
|
|
|
// Set breakpoint on next step
|
2017-01-31 18:24:56 +00:00
|
|
|
recipe.setBreakpoint(progress + 1, true);
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
// If stepping with flow control, we have to start from the beginning
|
|
|
|
// but still want to skip all previous breakpoints
|
2017-01-31 18:24:56 +00:00
|
|
|
if (progress > 0 && containsFc) {
|
|
|
|
recipe.removeBreaksUpTo(progress);
|
2016-11-28 10:42:58 +00:00
|
|
|
progress = 0;
|
|
|
|
}
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
// If starting from scratch, load data
|
|
|
|
if (progress === 0) {
|
2017-01-31 18:24:56 +00:00
|
|
|
this.dish.set(inputText, Dish.STRING);
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
progress = recipe.execute(this.dish, progress);
|
|
|
|
} catch (err) {
|
2016-12-14 16:39:17 +00:00
|
|
|
// Return the error in the result so that everything else gets correctly updated
|
|
|
|
// rather than throwing it here and losing state info.
|
2016-11-28 10:42:58 +00:00
|
|
|
error = err;
|
|
|
|
progress = err.progress;
|
|
|
|
}
|
2016-12-14 16:39:17 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
result: this.dish.type === Dish.HTML ?
|
|
|
|
this.dish.get(Dish.HTML) :
|
|
|
|
this.dish.get(Dish.STRING),
|
2017-01-31 18:24:56 +00:00
|
|
|
type: Dish.enumLookup(this.dish.type),
|
2016-12-14 16:39:17 +00:00
|
|
|
progress: progress,
|
|
|
|
options: options,
|
2017-01-31 18:24:56 +00:00
|
|
|
duration: new Date().getTime() - startTime,
|
2016-12-14 16:39:17 +00:00
|
|
|
error: error
|
|
|
|
};
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When a browser tab is unfocused and the browser has to run lots of dynamic content in other tabs,
|
|
|
|
* it swaps out the memory for that tab. If the CyberChef tab has been unfocused for more than a
|
|
|
|
* minute, we run a silent bake which will force the browser to load and cache all the relevant
|
|
|
|
* JavaScript code needed to do a real bake.
|
2016-12-14 16:39:17 +00:00
|
|
|
*
|
2016-11-28 10:42:58 +00:00
|
|
|
* This will stop baking taking a long time when the CyberChef browser tab has been unfocused for a
|
|
|
|
* long time and the browser has swapped out all its memory.
|
2016-12-14 16:39:17 +00:00
|
|
|
*
|
2016-11-28 10:42:58 +00:00
|
|
|
* The output will not be modified (hence "silent" bake).
|
2016-12-14 16:39:17 +00:00
|
|
|
*
|
2016-11-28 10:42:58 +00:00
|
|
|
* This will only actually execute the recipe if auto-bake is enabled, otherwise it will just load
|
|
|
|
* the recipe, ingredients and dish.
|
|
|
|
*
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {Object[]} recipeConfig - The recipe configuration object
|
2016-11-28 10:42:58 +00:00
|
|
|
* @returns {number} The time it took to run the silent bake in milliseconds.
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
Chef.prototype.silentBake = function(recipeConfig) {
|
|
|
|
var startTime = new Date().getTime(),
|
|
|
|
recipe = new Recipe(recipeConfig),
|
|
|
|
dish = new Dish("", Dish.STRING);
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
try {
|
|
|
|
recipe.execute(dish);
|
2017-02-09 15:09:33 +00:00
|
|
|
} catch (err) {
|
2016-11-28 10:42:58 +00:00
|
|
|
// Suppress all errors
|
|
|
|
}
|
2017-01-31 18:24:56 +00:00
|
|
|
return new Date().getTime() - startTime;
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|