github.com/lunarobliq/gophish@v0.8.1-0.20230523153303-93511002234d/static/js/src/app/webhooks.js (about)

     1  let webhooks = [];
     2  
     3  const dismiss = () => {
     4      $("#name").val("");
     5      $("#url").val("");
     6      $("#secret").val("");
     7      $("#is_active").prop("checked", false);
     8      $("#flashes").empty();
     9  };
    10  
    11  const saveWebhook = (id) => {
    12      let wh = {
    13          name: $("#name").val(),
    14          url: $("#url").val(),
    15          secret: $("#secret").val(),
    16          is_active: $("#is_active").is(":checked"),
    17      };
    18      if (id != -1) {
    19          wh.id = parseInt(id);
    20          api.webhookId.put(wh)
    21              .success(function(data) {
    22                  dismiss();
    23                  load();
    24                  $("#modal").modal("hide");
    25                  successFlash(`Webhook "${escapeHtml(wh.name)}" has been updated successfully!`);
    26              })
    27              .error(function(data) {
    28                  modalError(data.responseJSON.message)
    29              })
    30      } else {
    31          api.webhooks.post(wh)
    32              .success(function(data) {
    33                  load();
    34                  dismiss();
    35                  $("#modal").modal("hide");
    36                  successFlash(`Webhook "${escapeHtml(wh.name)}" has been created successfully!`);
    37              })
    38              .error(function(data) {
    39                  modalError(data.responseJSON.message)
    40              })
    41      }
    42  };
    43  
    44  const load = () => {
    45      $("#webhookTable").hide();
    46      $("#loading").show();
    47      api.webhooks.get()
    48          .success((whs) => {
    49              webhooks = whs;
    50              $("#loading").hide()
    51              $("#webhookTable").show()
    52              let webhookTable = $("#webhookTable").DataTable({
    53                  destroy: true,
    54                  columnDefs: [{
    55                      orderable: false,
    56                      targets: "no-sort"
    57                  }]
    58              });
    59              webhookTable.clear();
    60              $.each(webhooks, (i, webhook) => {
    61                  webhookTable.row.add([
    62                      escapeHtml(webhook.name),
    63                      escapeHtml(webhook.url),
    64                      escapeHtml(webhook.is_active),
    65                      `
    66                        <div class="pull-right">
    67                          <button class="btn btn-primary ping_button" data-webhook-id="${webhook.id}">
    68                            Ping
    69                          </button>
    70                          <button class="btn btn-primary edit_button" data-toggle="modal" data-backdrop="static" data-target="#modal" data-webhook-id="${webhook.id}">
    71                            <i class="fa fa-pencil"></i>
    72                          </button>
    73                          <button class="btn btn-danger delete_button" data-webhook-id="${webhook.id}">
    74                            <i class="fa fa-trash-o"></i>
    75                          </button>
    76                        </div>
    77                      `
    78                  ]).draw()
    79              })
    80          })
    81          .error(() => {
    82              errorFlash("Error fetching webhooks")
    83          })
    84  };
    85  
    86  const editWebhook = (id) => {
    87      $("#modalSubmit").unbind("click").click(() => {
    88          saveWebhook(id);
    89      });
    90      if (id !== -1) {
    91          $("#webhookModalLabel").text("Edit Webhook")
    92          api.webhookId.get(id)
    93            .success(function(wh) {
    94                $("#name").val(wh.name);
    95                $("#url").val(wh.url);
    96                $("#secret").val(wh.secret);
    97                $("#is_active").prop("checked", wh.is_active);
    98            })
    99            .error(function () {
   100                errorFlash("Error fetching webhook")
   101            });
   102      } else {
   103          $("#webhookModalLabel").text("New Webhook")
   104      }
   105  };
   106  
   107  const deleteWebhook = (id) => {
   108      var wh = webhooks.find(x => x.id == id);
   109      if (!wh) {
   110          return;
   111      }
   112      Swal.fire({
   113          title: "Are you sure?",
   114          text: `This will delete the webhook '${escapeHtml(wh.name)}'`,
   115          type: "warning",
   116          animation: false,
   117          showCancelButton: true,
   118          confirmButtonText: "Delete",
   119          confirmButtonColor: "#428bca",
   120          reverseButtons: true,
   121          allowOutsideClick: false,
   122          preConfirm: function () {
   123              return new Promise((resolve, reject) => {
   124                  api.webhookId.delete(id)
   125                      .success((msg) => {
   126                          resolve()
   127                      })
   128                      .error((data) => {
   129                          reject(data.responseJSON.message)
   130                      })
   131              })
   132              .catch(error => {
   133                  Swal.showValidationMessage(error)
   134                })
   135          }
   136      }).then(function(result) {
   137          if (result.value) {
   138              Swal.fire(
   139                  "Webhook Deleted!",
   140                  `The webhook has been deleted!`,
   141                  "success"
   142              );
   143          }
   144          $("button:contains('OK')").on("click", function() {
   145              location.reload();
   146          })
   147      })
   148  };
   149  
   150  const pingUrl = (btn, whId) => {
   151      dismiss();
   152      btn.disabled = true;
   153      api.webhookId.ping(whId)
   154          .success(function(wh) {
   155              btn.disabled = false;
   156              successFlash(`Ping of "${escapeHtml(wh.name)}" webhook succeeded.`);
   157          })
   158          .error(function(data) {
   159              btn.disabled = false;
   160              var wh = webhooks.find(x => x.id == whId);
   161              if (!wh) {
   162                  return
   163              }
   164              errorFlash(`Ping of "${escapeHtml(wh.name)}" webhook failed: "${escapeHtml(data.responseJSON.message)}"`)
   165          });
   166  };
   167  
   168  $(document).ready(function() {
   169      load();
   170      $("#modal").on("hide.bs.modal", function() {
   171          dismiss();
   172      });
   173      $("#new_button").on("click", function() {
   174          editWebhook(-1);
   175      });
   176      $("#webhookTable").on("click", ".edit_button", function(e) {
   177          editWebhook($(this).attr("data-webhook-id"));
   178      });
   179      $("#webhookTable").on("click", ".delete_button", function(e) {
   180          deleteWebhook($(this).attr("data-webhook-id"));
   181      });
   182      $("#webhookTable").on("click", ".ping_button", function(e) {
   183          pingUrl(e.currentTarget, e.currentTarget.dataset.webhookId);
   184      });
   185  });