ControlsWaiter.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /**
  2. * Waiter to handle events related to the CyberChef controls (i.e. Bake, Step, Save, Load etc.)
  3. *
  4. * @author n1474335 [n1474335@gmail.com]
  5. * @copyright Crown Copyright 2016
  6. * @license Apache-2.0
  7. *
  8. * @constructor
  9. * @param {HTMLApp} app - The main view object for CyberChef.
  10. * @param {Manager} manager - The CyberChef event manager.
  11. */
  12. var ControlsWaiter = function(app, manager) {
  13. this.app = app;
  14. this.manager = manager;
  15. };
  16. /**
  17. * Adjusts the display properties of the control buttons so that they fit within the current width
  18. * without wrapping or overflowing.
  19. */
  20. ControlsWaiter.prototype.adjust_width = function() {
  21. var controls = document.getElementById("controls"),
  22. step = document.getElementById("step"),
  23. clr_breaks = document.getElementById("clr-breaks"),
  24. save_img = document.querySelector("#save img"),
  25. load_img = document.querySelector("#load img"),
  26. step_img = document.querySelector("#step img"),
  27. clr_recip_img = document.querySelector("#clr-recipe img"),
  28. clr_breaks_img = document.querySelector("#clr-breaks img");
  29. if (controls.clientWidth < 470) {
  30. step.childNodes[1].nodeValue = " Step";
  31. } else {
  32. step.childNodes[1].nodeValue = " Step through";
  33. }
  34. if (controls.clientWidth < 400) {
  35. save_img.style.display = "none";
  36. load_img.style.display = "none";
  37. step_img.style.display = "none";
  38. clr_recip_img.style.display = "none";
  39. clr_breaks_img.style.display = "none";
  40. } else {
  41. save_img.style.display = "inline";
  42. load_img.style.display = "inline";
  43. step_img.style.display = "inline";
  44. clr_recip_img.style.display = "inline";
  45. clr_breaks_img.style.display = "inline";
  46. }
  47. if (controls.clientWidth < 330) {
  48. clr_breaks.childNodes[1].nodeValue = " Clear breaks";
  49. } else {
  50. clr_breaks.childNodes[1].nodeValue = " Clear breakpoints";
  51. }
  52. };
  53. /**
  54. * Checks or unchecks the Auto Bake checkbox based on the given value.
  55. *
  56. * @param {boolean} value - The new value for Auto Bake.
  57. */
  58. ControlsWaiter.prototype.set_auto_bake = function(value) {
  59. var auto_bake_checkbox = document.getElementById("auto-bake");
  60. if (auto_bake_checkbox.checked !== value) {
  61. auto_bake_checkbox.click();
  62. }
  63. };
  64. /**
  65. * Handler to trigger baking.
  66. */
  67. ControlsWaiter.prototype.bake_click = function() {
  68. this.app.bake();
  69. $("#output-text").selectRange(0);
  70. };
  71. /**
  72. * Handler for the 'Step through' command. Executes the next step of the recipe.
  73. */
  74. ControlsWaiter.prototype.step_click = function() {
  75. this.app.bake(true);
  76. $("#output-text").selectRange(0);
  77. };
  78. /**
  79. * Handler for changes made to the Auto Bake checkbox.
  80. */
  81. ControlsWaiter.prototype.auto_bake_change = function() {
  82. var auto_bake_label = document.getElementById("auto-bake-label"),
  83. auto_bake_checkbox = document.getElementById("auto-bake");
  84. this.app.auto_bake_ = auto_bake_checkbox.checked;
  85. if (auto_bake_checkbox.checked) {
  86. auto_bake_label.classList.remove("btn-default");
  87. auto_bake_label.classList.add("btn-success");
  88. } else {
  89. auto_bake_label.classList.remove("btn-success");
  90. auto_bake_label.classList.add("btn-default");
  91. }
  92. };
  93. /**
  94. * Handler for the 'Clear recipe' command. Removes all operations from the recipe.
  95. */
  96. ControlsWaiter.prototype.clear_recipe_click = function() {
  97. this.manager.recipe.clear_recipe();
  98. };
  99. /**
  100. * Handler for the 'Clear breakpoints' command. Removes all breakpoints from operations in the
  101. * recipe.
  102. */
  103. ControlsWaiter.prototype.clear_breaks_click = function() {
  104. var bps = document.querySelectorAll("#rec_list li.operation .breakpoint");
  105. for (var i = 0; i < bps.length; i++) {
  106. bps[i].setAttribute("break", "false");
  107. bps[i].classList.remove("breakpoint-selected");
  108. }
  109. };
  110. /**
  111. * Populates the save disalog box with a URL incorporating the recipe and input.
  112. *
  113. * @param {Object[]} [recipe_config] - The recipe configuration object array.
  114. */
  115. ControlsWaiter.prototype.initialise_save_link = function(recipe_config) {
  116. recipe_config = recipe_config || this.app.get_recipe_config();
  117. var include_recipe = document.getElementById("save-link-recipe-checkbox").checked,
  118. include_input = document.getElementById("save-link-input-checkbox").checked,
  119. save_link_el = document.getElementById("save-link"),
  120. save_link = this.generate_state_url(include_recipe, include_input, recipe_config);
  121. save_link_el.innerHTML = Utils.truncate(save_link, 120);
  122. save_link_el.setAttribute("href", save_link);
  123. };
  124. /**
  125. * Generates a URL containing the current recipe and input state.
  126. *
  127. * @param {boolean} include_recipe - Whether to include the recipe in the URL.
  128. * @param {boolean} include_input - Whether to include the input in the URL.
  129. * @param {Object[]} [recipe_config] - The recipe configuration object array.
  130. * @returns {string}
  131. */
  132. ControlsWaiter.prototype.generate_state_url = function(include_recipe, include_input, recipe_config) {
  133. recipe_config = recipe_config || this.app.get_recipe_config();
  134. var link = window.location.protocol + "//" +
  135. window.location.host +
  136. window.location.pathname,
  137. recipe_str = JSON.stringify(recipe_config),
  138. input_str = Utils.to_base64(this.app.get_input(), "A-Za-z0-9+/"); // B64 alphabet with no padding
  139. include_recipe = include_recipe && (recipe_config.length > 0);
  140. include_input = include_input && (input_str.length > 0) && (input_str.length < 8000);
  141. if (include_recipe) {
  142. link += "?recipe=" + encodeURIComponent(recipe_str);
  143. }
  144. if (include_recipe && include_input) {
  145. link += "&input=" + encodeURIComponent(input_str);
  146. } else if (include_input) {
  147. link += "?input=" + encodeURIComponent(input_str);
  148. }
  149. return link;
  150. };
  151. /**
  152. * Handler for changes made to the save dialog text area. Re-initialises the save link.
  153. */
  154. ControlsWaiter.prototype.save_text_change = function() {
  155. try {
  156. var recipe_config = JSON.parse(document.getElementById("save-text").value);
  157. this.initialise_save_link(recipe_config);
  158. } catch(err) {}
  159. };
  160. /**
  161. * Handler for the 'Save' command. Pops up the save dialog box.
  162. */
  163. ControlsWaiter.prototype.save_click = function() {
  164. var recipe_config = this.app.get_recipe_config();
  165. var recipe_str = JSON.stringify(recipe_config).replace(/},{/g, "},\n{");
  166. document.getElementById("save-text").value = recipe_str;
  167. this.initialise_save_link(recipe_config);
  168. $("#save-modal").modal();
  169. };
  170. /**
  171. * Handler for the save link recipe checkbox change event.
  172. */
  173. ControlsWaiter.prototype.slr_check_change = function() {
  174. this.initialise_save_link();
  175. };
  176. /**
  177. * Handler for the save link input checkbox change event.
  178. */
  179. ControlsWaiter.prototype.sli_check_change = function() {
  180. this.initialise_save_link();
  181. };
  182. /**
  183. * Handler for the 'Load' command. Pops up the load dialog box.
  184. */
  185. ControlsWaiter.prototype.load_click = function() {
  186. this.populate_load_recipes_list();
  187. $("#load-modal").modal();
  188. };
  189. /**
  190. * Saves the recipe specified in the save textarea to local storage.
  191. */
  192. ControlsWaiter.prototype.save_button_click = function() {
  193. var recipe_name = document.getElementById("save-name").value,
  194. recipe_str = document.getElementById("save-text").value;
  195. if (!recipe_name) {
  196. this.app.alert("Please enter a recipe name", "danger", 2000);
  197. return;
  198. }
  199. var saved_recipes = localStorage.saved_recipes ?
  200. JSON.parse(localStorage.saved_recipes) : [],
  201. recipe_id = localStorage.recipe_id || 0;
  202. saved_recipes.push({
  203. id: ++recipe_id,
  204. name: recipe_name,
  205. recipe: recipe_str
  206. });
  207. localStorage.saved_recipes = JSON.stringify(saved_recipes);
  208. localStorage.recipe_id = recipe_id;
  209. this.app.alert("Recipe saved as \"" + recipe_name + "\".", "success", 2000);
  210. };
  211. /**
  212. * Populates the list of saved recipes in the load dialog box from local storage.
  213. */
  214. ControlsWaiter.prototype.populate_load_recipes_list = function() {
  215. var load_name_el = document.getElementById("load-name");
  216. // Remove current recipes from select
  217. var i = load_name_el.options.length;
  218. while (i--) {
  219. load_name_el.remove(i);
  220. }
  221. // Add recipes to select
  222. var saved_recipes = localStorage.saved_recipes ?
  223. JSON.parse(localStorage.saved_recipes) : [];
  224. for (i = 0; i < saved_recipes.length; i++) {
  225. var opt = document.createElement("option");
  226. opt.value = saved_recipes[i].id;
  227. opt.innerHTML = saved_recipes[i].name;
  228. load_name_el.appendChild(opt);
  229. }
  230. // Populate textarea with first recipe
  231. document.getElementById("load-text").value = saved_recipes.length ? saved_recipes[0].recipe : "";
  232. };
  233. /**
  234. * Removes the currently selected recipe from local storage.
  235. */
  236. ControlsWaiter.prototype.load_delete_click = function() {
  237. var id = document.getElementById("load-name").value,
  238. saved_recipes = localStorage.saved_recipes ?
  239. JSON.parse(localStorage.saved_recipes) : [];
  240. saved_recipes = saved_recipes.filter(function(r) {
  241. return r.id !== id;
  242. });
  243. localStorage.saved_recipes = JSON.stringify(saved_recipes);
  244. this.populate_load_recipes_list();
  245. };
  246. /**
  247. * Displays the selected recipe in the load text box.
  248. */
  249. ControlsWaiter.prototype.load_name_change = function(e) {
  250. var el = e.target,
  251. saved_recipes = localStorage.saved_recipes ?
  252. JSON.parse(localStorage.saved_recipes) : [],
  253. id = parseInt(el.value, 10);
  254. var recipe = saved_recipes.filter(function(r) {
  255. return r.id === id;
  256. })[0];
  257. document.getElementById("load-text").value = recipe.recipe;
  258. };
  259. /**
  260. * Loads the selected recipe and populates the Recipe with its operations.
  261. */
  262. ControlsWaiter.prototype.load_button_click = function() {
  263. try {
  264. var recipe_config = JSON.parse(document.getElementById("load-text").value);
  265. this.app.set_recipe_config(recipe_config);
  266. $("#rec_list [data-toggle=popover]").popover();
  267. } catch(e) {
  268. this.app.alert("Invalid recipe", "danger", 2000);
  269. }
  270. };