InputWaiter.mjs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import LoaderWorker from "worker-loader?inline&fallback=false!./LoaderWorker";
  7. import Utils from "../core/Utils";
  8. /**
  9. * Waiter to handle events related to the input.
  10. */
  11. class InputWaiter {
  12. /**
  13. * InputWaiter constructor.
  14. *
  15. * @param {App} app - The main view object for CyberChef.
  16. * @param {Manager} manager - The CyberChef event manager.
  17. */
  18. constructor(app, manager) {
  19. this.app = app;
  20. this.manager = manager;
  21. // Define keys that don't change the input so we don't have to autobake when they are pressed
  22. this.badKeys = [
  23. 16, //Shift
  24. 17, //Ctrl
  25. 18, //Alt
  26. 19, //Pause
  27. 20, //Caps
  28. 27, //Esc
  29. 33, 34, 35, 36, //PgUp, PgDn, End, Home
  30. 37, 38, 39, 40, //Directional
  31. 44, //PrntScrn
  32. 91, 92, //Win
  33. 93, //Context
  34. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, //F1-12
  35. 144, //Num
  36. 145, //Scroll
  37. ];
  38. this.loaderWorker = null;
  39. this.fileBuffer = null;
  40. }
  41. /**
  42. * Gets the user's input from the input textarea.
  43. *
  44. * @returns {string}
  45. */
  46. get() {
  47. return this.fileBuffer || document.getElementById("input-text").value;
  48. }
  49. /**
  50. * Sets the input in the input area.
  51. *
  52. * @param {string|File} input
  53. * @param {boolean} [silent=false] - Suppress statechange event
  54. *
  55. * @fires Manager#statechange
  56. */
  57. set(input, silent=false) {
  58. const inputText = document.getElementById("input-text");
  59. if (input instanceof File) {
  60. this.setFile(input);
  61. inputText.value = "";
  62. this.setInputInfo(input.size, null);
  63. } else {
  64. inputText.value = input;
  65. this.closeFile();
  66. if (!silent) window.dispatchEvent(this.manager.statechange);
  67. const lines = input.length < (this.app.options.ioDisplayThreshold * 1024) ?
  68. input.count("\n") + 1 : null;
  69. this.setInputInfo(input.length, lines);
  70. }
  71. }
  72. /**
  73. * Shows file details.
  74. *
  75. * @param {File} file
  76. */
  77. setFile(file) {
  78. // Display file overlay in input area with details
  79. const fileOverlay = document.getElementById("input-file"),
  80. fileName = document.getElementById("input-file-name"),
  81. fileSize = document.getElementById("input-file-size"),
  82. fileType = document.getElementById("input-file-type"),
  83. fileLoaded = document.getElementById("input-file-loaded");
  84. this.fileBuffer = new ArrayBuffer();
  85. fileOverlay.style.display = "block";
  86. fileName.textContent = file.name;
  87. fileSize.textContent = file.size.toLocaleString() + " bytes";
  88. fileType.textContent = file.type || "unknown";
  89. fileLoaded.textContent = "0%";
  90. }
  91. /**
  92. * Displays information about the input.
  93. *
  94. * @param {number} length - The length of the current input string
  95. * @param {number} lines - The number of the lines in the current input string
  96. */
  97. setInputInfo(length, lines) {
  98. let width = length.toString().length;
  99. width = width < 2 ? 2 : width;
  100. const lengthStr = length.toString().padStart(width, " ").replace(/ /g, "&nbsp;");
  101. let msg = "length: " + lengthStr;
  102. if (typeof lines === "number") {
  103. const linesStr = lines.toString().padStart(width, " ").replace(/ /g, "&nbsp;");
  104. msg += "<br>lines: " + linesStr;
  105. }
  106. document.getElementById("input-info").innerHTML = msg;
  107. }
  108. /**
  109. * Handler for input change events.
  110. *
  111. * @param {event} e
  112. *
  113. * @fires Manager#statechange
  114. */
  115. inputChange(e) {
  116. // Ignore this function if the input is a File
  117. if (this.fileBuffer) return;
  118. // Remove highlighting from input and output panes as the offsets might be different now
  119. this.manager.highlighter.removeHighlights();
  120. // Reset recipe progress as any previous processing will be redundant now
  121. this.app.progress = 0;
  122. // Update the input metadata info
  123. const inputText = this.get();
  124. const lines = inputText.length < (this.app.options.ioDisplayThreshold * 1024) ?
  125. inputText.count("\n") + 1 : null;
  126. this.setInputInfo(inputText.length, lines);
  127. if (e && this.badKeys.indexOf(e.keyCode) < 0) {
  128. // Fire the statechange event as the input has been modified
  129. window.dispatchEvent(this.manager.statechange);
  130. }
  131. }
  132. /**
  133. * Handler for input paste events.
  134. * Checks that the size of the input is below the display limit, otherwise treats it as a file/blob.
  135. *
  136. * @param {event} e
  137. */
  138. inputPaste(e) {
  139. const pastedData = e.clipboardData.getData("Text");
  140. if (pastedData.length < (this.app.options.ioDisplayThreshold * 1024)) {
  141. this.inputChange(e);
  142. } else {
  143. e.preventDefault();
  144. e.stopPropagation();
  145. const file = new File([pastedData], "PastedData", {
  146. type: "text/plain",
  147. lastModified: Date.now()
  148. });
  149. this.loaderWorker = new LoaderWorker();
  150. this.loaderWorker.addEventListener("message", this.handleLoaderMessage.bind(this));
  151. this.loaderWorker.postMessage({"file": file});
  152. this.set(file);
  153. return false;
  154. }
  155. }
  156. /**
  157. * Handler for input dragover events.
  158. * Gives the user a visual cue to show that items can be dropped here.
  159. *
  160. * @param {event} e
  161. */
  162. inputDragover(e) {
  163. // This will be set if we're dragging an operation
  164. if (e.dataTransfer.effectAllowed === "move")
  165. return false;
  166. e.stopPropagation();
  167. e.preventDefault();
  168. e.target.closest("#input-text,#input-file").classList.add("dropping-file");
  169. }
  170. /**
  171. * Handler for input dragleave events.
  172. * Removes the visual cue.
  173. *
  174. * @param {event} e
  175. */
  176. inputDragleave(e) {
  177. e.stopPropagation();
  178. e.preventDefault();
  179. document.getElementById("input-text").classList.remove("dropping-file");
  180. document.getElementById("input-file").classList.remove("dropping-file");
  181. }
  182. /**
  183. * Handler for input drop events.
  184. * Loads the dragged data into the input textarea.
  185. *
  186. * @param {event} e
  187. */
  188. inputDrop(e) {
  189. // This will be set if we're dragging an operation
  190. if (e.dataTransfer.effectAllowed === "move")
  191. return false;
  192. e.stopPropagation();
  193. e.preventDefault();
  194. const file = e.dataTransfer.files[0];
  195. const text = e.dataTransfer.getData("Text");
  196. document.getElementById("input-text").classList.remove("dropping-file");
  197. document.getElementById("input-file").classList.remove("dropping-file");
  198. if (text) {
  199. this.closeFile();
  200. this.set(text);
  201. return;
  202. }
  203. if (file) {
  204. this.loadFile(file);
  205. }
  206. }
  207. /**
  208. * Handler for open input button events
  209. * Loads the opened data into the input textarea
  210. *
  211. * @param {event} e
  212. */
  213. inputOpen(e) {
  214. e.preventDefault();
  215. const file = e.srcElement.files[0];
  216. this.loadFile(file);
  217. }
  218. /**
  219. * Handler for messages sent back by the LoaderWorker.
  220. *
  221. * @param {MessageEvent} e
  222. */
  223. handleLoaderMessage(e) {
  224. const r = e.data;
  225. if (r.hasOwnProperty("progress")) {
  226. const fileLoaded = document.getElementById("input-file-loaded");
  227. fileLoaded.textContent = r.progress + "%";
  228. }
  229. if (r.hasOwnProperty("error")) {
  230. this.app.alert(r.error, 10000);
  231. }
  232. if (r.hasOwnProperty("fileBuffer")) {
  233. log.debug("Input file loaded");
  234. this.fileBuffer = r.fileBuffer;
  235. this.displayFilePreview();
  236. window.dispatchEvent(this.manager.statechange);
  237. }
  238. }
  239. /**
  240. * Shows a chunk of the file in the input behind the file overlay.
  241. */
  242. displayFilePreview() {
  243. const inputText = document.getElementById("input-text"),
  244. fileSlice = this.fileBuffer.slice(0, 4096);
  245. inputText.style.overflow = "hidden";
  246. inputText.classList.add("blur");
  247. inputText.value = Utils.printable(Utils.arrayBufferToStr(fileSlice));
  248. if (this.fileBuffer.byteLength > 4096) {
  249. inputText.value += "[truncated]...";
  250. }
  251. }
  252. /**
  253. * Handler for file close events.
  254. */
  255. closeFile() {
  256. if (this.loaderWorker) this.loaderWorker.terminate();
  257. this.fileBuffer = null;
  258. document.getElementById("input-file").style.display = "none";
  259. const inputText = document.getElementById("input-text");
  260. inputText.style.overflow = "auto";
  261. inputText.classList.remove("blur");
  262. }
  263. /**
  264. * Loads a file into the input.
  265. *
  266. * @param {File} file
  267. */
  268. loadFile(file) {
  269. if (file) {
  270. this.closeFile();
  271. this.loaderWorker = new LoaderWorker();
  272. this.loaderWorker.addEventListener("message", this.handleLoaderMessage.bind(this));
  273. this.loaderWorker.postMessage({"file": file});
  274. this.set(file);
  275. }
  276. }
  277. /**
  278. * Handler for clear IO events.
  279. * Resets the input, output and info areas.
  280. *
  281. * @fires Manager#statechange
  282. */
  283. clearIoClick() {
  284. this.closeFile();
  285. this.manager.output.closeFile();
  286. this.manager.highlighter.removeHighlights();
  287. document.getElementById("input-text").value = "";
  288. document.getElementById("output-text").value = "";
  289. document.getElementById("input-info").innerHTML = "";
  290. document.getElementById("output-info").innerHTML = "";
  291. document.getElementById("input-selection-info").innerHTML = "";
  292. document.getElementById("output-selection-info").innerHTML = "";
  293. window.dispatchEvent(this.manager.statechange);
  294. }
  295. }
  296. export default InputWaiter;