defender.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <!--
  2. Copyright (C) 2019 Nicola Murino
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as published
  5. by the Free Software Foundation, version 3.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU Affero General Public License for more details.
  10. You should have received a copy of the GNU Affero General Public License
  11. along with this program. If not, see <https://www.gnu.org/licenses/>.
  12. -->
  13. {{template "base" .}}
  14. {{define "title"}}{{.Title}}{{end}}
  15. {{define "extra_css"}}
  16. <link href="{{.StaticURL}}/vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet">
  17. <link href="{{.StaticURL}}/vendor/datatables/buttons.bootstrap4.min.css" rel="stylesheet">
  18. <link href="{{.StaticURL}}/vendor/datatables/fixedHeader.bootstrap4.min.css" rel="stylesheet">
  19. <link href="{{.StaticURL}}/vendor/datatables/responsive.bootstrap4.min.css" rel="stylesheet">
  20. <link href="{{.StaticURL}}/vendor/datatables/select.bootstrap4.min.css" rel="stylesheet">
  21. {{end}}
  22. {{define "page_body"}}
  23. <div id="errorMsg" class="alert alert-warning fade show" style="display: none;" role="alert">
  24. <span id="errorTxt"></span>
  25. <button type="button" class="close" aria-label="Close" onclick="dismissErrorMsg();">
  26. <span aria-hidden="true">&times;</span>
  27. </button>
  28. </div>
  29. <script type="text/javascript">
  30. function dismissErrorMsg(){
  31. $('#errorMsg').hide();
  32. }
  33. </script>
  34. <div class="card shadow mb-4">
  35. <div class="card-header py-3">
  36. <h6 class="m-0 font-weight-bold text-primary">View and manage auto blocklist</h6>
  37. </div>
  38. <div class="card-body">
  39. <div class="table-responsive">
  40. <table class="table table-hover nowrap" id="dataTable" width="100%" cellspacing="0">
  41. <thead>
  42. <tr>
  43. <th>ID</th>
  44. <th>IP</th>
  45. <th>Ban time</th>
  46. <th>Score</th>
  47. </tr>
  48. </thead>
  49. </table>
  50. </div>
  51. </div>
  52. </div>
  53. {{end}}
  54. {{define "dialog"}}
  55. <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel"
  56. aria-hidden="true">
  57. <div class="modal-dialog" role="document">
  58. <div class="modal-content">
  59. <div class="modal-header">
  60. <h5 class="modal-title" id="deleteModalLabel">
  61. Confirmation required
  62. </h5>
  63. <button class="close" type="button" data-dismiss="modal" aria-label="Close">
  64. <span aria-hidden="true">&times;</span>
  65. </button>
  66. </div>
  67. <div class="modal-body">Do you want to remove the selected entry?</div>
  68. <div class="modal-footer">
  69. <button class="btn btn-secondary" type="button" data-dismiss="modal">
  70. Cancel
  71. </button>
  72. <a class="btn btn-warning" href="#" onclick="deleteAction()">
  73. Delete
  74. </a>
  75. </div>
  76. </div>
  77. </div>
  78. </div>
  79. {{end}}
  80. {{define "extra_js"}}
  81. <script src="{{.StaticURL}}/vendor/datatables/jquery.dataTables.min.js"></script>
  82. <script src="{{.StaticURL}}/vendor/datatables/dataTables.bootstrap4.min.js"></script>
  83. <script src="{{.StaticURL}}/vendor/datatables/dataTables.buttons.min.js"></script>
  84. <script src="{{.StaticURL}}/vendor/datatables/buttons.bootstrap4.min.js"></script>
  85. <script src="{{.StaticURL}}/vendor/datatables/dataTables.fixedHeader.min.js"></script>
  86. <script src="{{.StaticURL}}/vendor/datatables/dataTables.responsive.min.js"></script>
  87. <script src="{{.StaticURL}}/vendor/datatables/responsive.bootstrap4.min.js"></script>
  88. <script src="{{.StaticURL}}/vendor/datatables/dataTables.select.min.js"></script>
  89. <script type="text/javascript">
  90. function deleteAction() {
  91. let table = $('#dataTable').DataTable();
  92. table.button('delete:name').enable(false);
  93. let id = table.row({ selected: true }).data()["id"];
  94. let path = '{{.DefenderHostsURL}}' + "/" + fixedEncodeURIComponent(id);
  95. $('#deleteModal').modal('hide');
  96. $('#errorMsg').hide();
  97. $.ajax({
  98. url: path,
  99. type: 'DELETE',
  100. dataType: 'json',
  101. headers: {'X-CSRF-TOKEN' : '{{.CSRFToken}}'},
  102. timeout: 15000,
  103. success: function (result) {
  104. window.location.href = '{{.DefenderURL}}';
  105. },
  106. error: function ($xhr, textStatus, errorThrown) {
  107. let txt = "Unable to delete the selected entry";
  108. if ($xhr) {
  109. let json = $xhr.responseJSON;
  110. if (json) {
  111. if (json.message){
  112. txt += ": " + json.message;
  113. } else {
  114. txt += ": " + json.error;
  115. }
  116. }
  117. }
  118. $('#errorTxt').text(txt);
  119. $('#errorMsg').show();
  120. }
  121. });
  122. }
  123. $(document).ready(function () {
  124. $.fn.dataTable.ext.buttons.refresh = {
  125. text: '<i class="fas fa-sync-alt"></i>',
  126. name: 'refresh',
  127. titleAttr: "Refresh",
  128. action: function (e, dt, node, config) {
  129. location.reload();
  130. }
  131. };
  132. $.fn.dataTable.ext.buttons.delete = {
  133. text: '<i class="fas fa-trash"></i>',
  134. name: 'delete',
  135. titleAttr: "Delete",
  136. action: function (e, dt, node, config) {
  137. $('#deleteModal').modal('show');
  138. },
  139. enabled: false
  140. };
  141. let table = $('#dataTable').DataTable({
  142. "ajax": {
  143. "url": "{{.DefenderHostsURL}}",
  144. "dataSrc": "",
  145. "error": function ($xhr, textStatus, errorThrown) {
  146. $(".dataTables_processing").hide();
  147. let txt = "Failed to get auto blocklist";
  148. if ($xhr) {
  149. let json = $xhr.responseJSON;
  150. if (json) {
  151. if (json.message){
  152. txt += ": " + json.message;
  153. } else {
  154. txt += ": " + json.error;
  155. }
  156. }
  157. }
  158. $('#errorTxt').text(txt);
  159. $('#errorMsg').show();
  160. }
  161. },
  162. "deferRender": true,
  163. "processing": true,
  164. "columns": [
  165. { "data": "id" },
  166. { "data": "ip" },
  167. {
  168. "data": "ban_time",
  169. "defaultContent": ""
  170. },
  171. {
  172. "data": "score",
  173. "defaultContent": ""
  174. }
  175. ],
  176. "select": {
  177. "style": "single",
  178. "blurable": true
  179. },
  180. "buttons": [],
  181. "lengthChange": false,
  182. "columnDefs": [
  183. {
  184. "targets": [0],
  185. "visible": false,
  186. "searchable": false
  187. },
  188. ],
  189. "scrollX": false,
  190. "scrollY": false,
  191. "responsive": true,
  192. "language": {
  193. "loadingRecords": "",
  194. "emptyTable": "No records found"
  195. },
  196. "initComplete": function (settings, json) {
  197. {{if .LoggedAdmin.HasPermission "manage_defender"}}
  198. table.button().add(0, 'delete');
  199. {{end}}
  200. table.button().add(0, 'pageLength');
  201. table.button().add(0, 'refresh');
  202. table.buttons().container().appendTo('.col-md-6:eq(0)', table.table().container());
  203. },
  204. "order": [[2, 'desc'],[3,'desc']]
  205. });
  206. new $.fn.dataTable.FixedHeader(table);
  207. $.fn.dataTable.ext.errMode = 'none';
  208. {{if .LoggedAdmin.HasPermission "manage_defender"}}
  209. table.on('select deselect', function () {
  210. let selectedRows = table.rows({ selected: true }).count();
  211. table.button('delete:name').enable(selectedRows == 1);
  212. });
  213. {{end}}
  214. });
  215. </script>
  216. {{end}}