Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

upload.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. The MIT License (MIT)
  3. Copyright (C) 2017 Vsevolod Stakhov <vsevolod@highsecure.ru>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. /* global require */
  21. define(["jquery", "app/rspamd"],
  22. ($, rspamd) => {
  23. "use strict";
  24. const ui = {};
  25. function cleanTextUpload(source) {
  26. $("#" + source + "TextSource").val("");
  27. }
  28. // @upload text
  29. function uploadText(data, source, headers) {
  30. let url = null;
  31. if (source === "spam") {
  32. url = "learnspam";
  33. } else if (source === "ham") {
  34. url = "learnham";
  35. } else if (source === "fuzzy") {
  36. url = "fuzzyadd";
  37. } else if (source === "scan") {
  38. url = "checkv2";
  39. }
  40. function server() {
  41. if (rspamd.getSelector("selSrv") === "All SERVERS" &&
  42. rspamd.getSelector("selLearnServers") === "random") {
  43. const servers = $("#selSrv option").slice(1).map((_, o) => o.value);
  44. return servers[Math.floor(Math.random() * servers.length)];
  45. }
  46. return null;
  47. }
  48. rspamd.query(url, {
  49. data: data,
  50. params: {
  51. processData: false,
  52. },
  53. method: "POST",
  54. headers: headers,
  55. success: function (json, jqXHR) {
  56. cleanTextUpload(source);
  57. rspamd.alertMessage("alert-success", "Data successfully uploaded");
  58. if (jqXHR.status !== 200) {
  59. rspamd.alertMessage("alert-info", jqXHR.statusText);
  60. }
  61. },
  62. server: server()
  63. });
  64. }
  65. function columns_v2() {
  66. return [{
  67. name: "id",
  68. title: "ID",
  69. style: {
  70. minWidth: 130,
  71. overflow: "hidden",
  72. textOverflow: "ellipsis",
  73. wordBreak: "break-all",
  74. whiteSpace: "normal"
  75. }
  76. }, {
  77. name: "action",
  78. title: "Action",
  79. style: {minwidth: 82}
  80. }, {
  81. name: "passthrough_module",
  82. title: '<div title="The module that has set the pre-result">Pass-through module</div>'
  83. }, {
  84. name: "score",
  85. title: "Score",
  86. style: {maxWidth: 110},
  87. sortValue: function (val) { return Number(val.options.sortValue); }
  88. }, {
  89. name: "symbols",
  90. title: "Symbols" +
  91. '<div class="sym-order-toggle">' +
  92. '<br><span style="font-weight:normal;">Sort by:</span><br>' +
  93. '<div class="btn-group btn-group-xs btn-sym-order-scan">' +
  94. '<label type="button" class="btn btn-outline-secondary btn-sym-scan-magnitude">' +
  95. '<input type="radio" class="btn-check" value="magnitude">Magnitude</label>' +
  96. '<label type="button" class="btn btn-outline-secondary btn-sym-scan-score">' +
  97. '<input type="radio" class="btn-check" value="score">Value</label>' +
  98. '<label type="button" class="btn btn-outline-secondary btn-sym-scan-name">' +
  99. '<input type="radio" class="btn-check" value="name">Name</label>' +
  100. "</div>" +
  101. "</div>",
  102. breakpoints: "all",
  103. style: {width: 550, maxWidth: 550}
  104. }, {
  105. name: "time_real",
  106. title: "Scan time",
  107. breakpoints: "xs sm md",
  108. style: {maxWidth: 72},
  109. sortValue: function (val) { return Number(val); }
  110. }, {
  111. sorted: true,
  112. direction: "DESC",
  113. name: "time",
  114. title: "Time",
  115. sortValue: function (val) { return Number(val.options.sortValue); }
  116. }];
  117. }
  118. function get_server() {
  119. const checked_server = rspamd.getSelector("selSrv");
  120. return (checked_server === "All SERVERS") ? "local" : checked_server;
  121. }
  122. // @upload text
  123. function scanText(data, headers) {
  124. rspamd.query("checkv2", {
  125. data: data,
  126. params: {
  127. processData: false,
  128. },
  129. method: "POST",
  130. headers: headers,
  131. success: function (neighbours_status) {
  132. function scrollTop(rows_total) {
  133. // Is there a way to get an event when all rows are loaded?
  134. rspamd.waitForRowsDisplayed("scan", rows_total, () => {
  135. $("#cleanScanHistory").removeAttr("disabled", true);
  136. $("html, body").animate({
  137. scrollTop: $("#scanResult").offset().top
  138. }, 1000);
  139. });
  140. }
  141. const json = neighbours_status[0].data;
  142. if (json.action) {
  143. rspamd.alertMessage("alert-success", "Data successfully scanned");
  144. const rows_total = $("#historyTable_scan > tbody > tr:not(.footable-detail-row)").length + 1;
  145. const o = rspamd.process_history_v2({rows: [json]}, "scan");
  146. const {items} = o;
  147. rspamd.symbols.scan.push(o.symbols[0]);
  148. if (Object.prototype.hasOwnProperty.call(rspamd.tables, "scan")) {
  149. rspamd.tables.scan.rows.load(items, true);
  150. scrollTop(rows_total);
  151. } else {
  152. rspamd.destroyTable("scan");
  153. require(["footable"], () => {
  154. // Is there a way to get an event when the table is destroyed?
  155. setTimeout(() => {
  156. rspamd.initHistoryTable(data, items, "scan", columns_v2(), true);
  157. scrollTop(rows_total);
  158. }, 200);
  159. });
  160. }
  161. } else {
  162. rspamd.alertMessage("alert-error", "Cannot scan data");
  163. }
  164. },
  165. errorMessage: "Cannot upload data",
  166. statusCode: {
  167. 404: function () {
  168. rspamd.alertMessage("alert-error", "Cannot upload data, no server found");
  169. },
  170. 500: function () {
  171. rspamd.alertMessage("alert-error", "Cannot tokenize message: no text data");
  172. },
  173. 503: function () {
  174. rspamd.alertMessage("alert-error", "Cannot tokenize message: no text data");
  175. }
  176. },
  177. server: get_server()
  178. });
  179. }
  180. function getFuzzyHashes(data) {
  181. function fillHashTable(rules) {
  182. $("#hashTable tbody").empty();
  183. for (const [rule, hashes] of Object.entries(rules)) {
  184. hashes.forEach((hash, i) => {
  185. $("#hashTable tbody").append("<tr>" +
  186. (i === 0 ? '<td rowspan="' + Object.keys(hashes).length + '">' + rule + "</td>" : "") +
  187. "<td>" + hash + "</td></tr>");
  188. });
  189. }
  190. $("#hash-card").slideDown();
  191. }
  192. rspamd.query("plugins/fuzzy/hashes?flag=" + $("#fuzzy-flag").val(), {
  193. data: data,
  194. params: {
  195. processData: false,
  196. },
  197. method: "POST",
  198. success: function (neighbours_status) {
  199. const json = neighbours_status[0].data;
  200. if (json.success) {
  201. rspamd.alertMessage("alert-success", "Message successfully processed");
  202. fillHashTable(json.hashes);
  203. } else {
  204. rspamd.alertMessage("alert-error", "Unexpected error processing message");
  205. }
  206. },
  207. server: get_server()
  208. });
  209. }
  210. rspamd.set_page_size("scan", $("#scan_page_size").val());
  211. rspamd.bindHistoryTableEventHandlers("scan", 3);
  212. $("#cleanScanHistory").off("click");
  213. $("#cleanScanHistory").on("click", (e) => {
  214. e.preventDefault();
  215. if (!confirm("Are you sure you want to clean scan history?")) { // eslint-disable-line no-alert
  216. return;
  217. }
  218. rspamd.destroyTable("scan");
  219. rspamd.symbols.scan.length = 0;
  220. $("#cleanScanHistory").attr("disabled", true);
  221. });
  222. function enable_disable_scan_btn() {
  223. $("#scan button:not(#cleanScanHistory, #scanOptionsToggle)")
  224. .prop("disabled", ($.trim($("textarea").val()).length === 0));
  225. }
  226. enable_disable_scan_btn();
  227. $("textarea").on("input", () => {
  228. enable_disable_scan_btn();
  229. });
  230. $("#scanClean").on("click", () => {
  231. $("#scan button:not(#cleanScanHistory, #scanOptionsToggle)").attr("disabled", true);
  232. $("#scanForm")[0].reset();
  233. $("#scanResult").hide();
  234. $("#scanOutput tbody").remove();
  235. $("html, body").animate({scrollTop: 0}, 1000);
  236. return false;
  237. });
  238. $(".card-close-btn").on("click", function () {
  239. $(this).closest(".card").slideUp();
  240. });
  241. $("[data-upload]").on("click", function () {
  242. const source = $(this).data("upload");
  243. const data = $("#scanMsgSource").val();
  244. let headers = {};
  245. if ($.trim(data).length > 0) {
  246. if (source === "scan") {
  247. headers = ["IP", "User", "From", "Rcpt", "Helo", "Hostname"].reduce((o, header) => {
  248. const value = $("#scan-opt-" + header.toLowerCase()).val();
  249. if (value !== "") o[header] = value;
  250. return o;
  251. }, {});
  252. if ($("#scan-opt-pass-all").prop("checked")) headers.Pass = "all";
  253. scanText(data, headers);
  254. } else if (source === "compute-fuzzy") {
  255. getFuzzyHashes(data);
  256. } else {
  257. if (source === "fuzzy") {
  258. headers = {
  259. flag: $("#fuzzyFlagText").val(),
  260. weight: $("#fuzzyWeightText").val()
  261. };
  262. }
  263. uploadText(data, source, headers);
  264. }
  265. } else {
  266. rspamd.alertMessage("alert-error", "Message source field cannot be blank");
  267. }
  268. return false;
  269. });
  270. return ui;
  271. });