forked from frankli0324/ctfd-whale
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainers.js
More file actions
122 lines (110 loc) · 3.94 KB
/
containers.js
File metadata and controls
122 lines (110 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const $ = CTFd.lib.$;
function htmlentities(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
function copyToClipboard(event, str) {
// Select element
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
$(event.target).tooltip({
title: "Copied!",
trigger: "manual"
});
$(event.target).tooltip("show");
setTimeout(function () {
$(event.target).tooltip("hide");
}, 1500);
}
$(".click-copy").click(function (e) {
copyToClipboard(e, $(this).data("copy"));
})
async function delete_container(user_id, challenge_id) {
let response = await CTFd.fetch("/api/v1/plugins/ctfd-whale/admin/container?user_id=" + user_id + "&challenge_id=" + challenge_id, {
method: "DELETE",
credentials: "same-origin",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
}
});
response = await response.json();
return response.success;
}
async function renew_container(user_id, challenge_id) {
let response = await CTFd.fetch(
"/api/v1/plugins/ctfd-whale/admin/container?user_id=" + user_id + "&challenge_id=" + challenge_id, {
method: "PATCH",
credentials: "same-origin",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
}
});
response = await response.json();
return response.success;
}
$('#containers-renew-button').click(function (e) {
let containers = $("input[data-user-id]:checked").map(function () {
return [[$(this).data("user-id"), $(this).data("challenge-id")]];
});
CTFd.ui.ezq.ezQuery({
title: "Renew Containers",
body: `Are you sure you want to renew the selected ${containers.length} container(s)?`,
success: async function () {
await Promise.all(containers.toArray().map((container) => renew_container(container[0],container[1])));
location.reload();
}
});
});
$('#containers-delete-button').click(function (e) {
let containers = $("input[data-user-id]:checked").map(function () {
return [[$(this).data("user-id"), $(this).data("challenge-id")]];
});
CTFd.ui.ezq.ezQuery({
title: "Delete Containers",
body: `Are you sure you want to delete the selected ${containers.length} container(s)?`,
success: async function () {
await Promise.all(containers.toArray().map((container) => delete_container(container[0],container[1])));
location.reload();
}
});
});
$(".delete-container").click(function (e) {
e.preventDefault();
let container_id = $(this).attr("container-id");
let user_id = $(this).attr("user-id");
let challenge_id = $(this).attr("challenge-id");
CTFd.ui.ezq.ezQuery({
title: "Destroy Container",
body: "<span>Are you sure you want to delete <strong>Container #{0}</strong>?</span>".format(
htmlentities(container_id)
),
success: async function () {
await delete_container(user_id, challenge_id);
location.reload();
}
});
});
$(".renew-container").click(function (e) {
e.preventDefault();
let container_id = $(this).attr("container-id");
let user_id = $(this).attr("user-id");
let challenge_id = $(this).attr("challenge-id");
CTFd.ui.ezq.ezQuery({
title: "Renew Container",
body: "<span>Are you sure you want to renew <strong>Container #{0}</strong>?</span>".format(
htmlentities(container_id)
),
success: async function () {
await renew_container(user_id, challenge_id);
location.reload();
},
});
});