Skip to content

Commit dbbf66c

Browse files
author
Mattia Codato
committed
Fixes for core-beta theme
refs #30
1 parent acac655 commit dbbf66c

File tree

3 files changed

+64
-86
lines changed

3 files changed

+64
-86
lines changed

docker_challenges/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def get_repositories(docker, tags=False, repos=False):
258258
r = do_request(docker, '/images/json?all=1')
259259
result = list()
260260
for i in r.json():
261-
if not i['RepoTags'] == None:
261+
if i['RepoTags']:
262262
if not i['RepoTags'][0].split(':')[0] == '<none>':
263263
if repos:
264264
if not i['RepoTags'][0].split(':')[0] in repos:
@@ -276,13 +276,14 @@ def get_unavailable_ports(docker):
276276
for i in r.json():
277277
if not i['Ports'] == []:
278278
for p in i['Ports']:
279-
result.append(p['PublicPort'])
279+
if 'PublicPort' in p:
280+
result.append(p['PublicPort'])
280281
return result
281282

282283

283284
def get_required_ports(docker, image):
284285
r = do_request(docker, f'/images/{image}/json?all=1')
285-
result = r.json()['ContainerConfig']['ExposedPorts'].keys()
286+
result = r.json()['Config']['ExposedPorts'].keys()
286287
return result
287288

288289

docker_challenges/assets/view.html

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,3 @@
1515
</span>
1616
</div>
1717
{% endblock %}
18-
{% block input %}
19-
<input id="challenge-id" class="challenge-id" type="hidden" value="{{ challenge.id }}">
20-
<input id="challenge-input" class="challenge-input" type="text" name="answer" placeholder="Flag" />
21-
{% endblock %}
22-
{% block submit %}
23-
<button id="challenge-submit" class="challenge-submit" type="submit">
24-
Submit
25-
</button>
26-
{% endblock %}

docker_challenges/assets/view.js

Lines changed: 60 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
CTFd._internal.challenge.data = undefined
22

3-
CTFd._internal.challenge.renderer = CTFd.lib.markdown();
3+
CTFd._internal.challenge.data = undefined;
4+
5+
CTFd._internal.challenge.renderer = null;
46

57

68
CTFd._internal.challenge.preRender = function() {}
@@ -10,6 +12,12 @@ CTFd._internal.challenge.render = function(markdown) {
1012
return CTFd._internal.challenge.renderer.render(markdown)
1113
}
1214

15+
String.prototype.format = function () {
16+
const args = arguments;
17+
return this.replace(/{([0-9]+)}/g, function (match, index) {
18+
return typeof args[index] == 'undefined' ? match : args[index];
19+
});
20+
};
1321

1422
CTFd._internal.challenge.postRender = function() {}
1523

@@ -41,85 +49,63 @@ CTFd._internal.challenge.submit = function(preview) {
4149
};
4250

4351
function get_docker_status(container) {
44-
$.get("/api/v1/docker_status", function(result) {
45-
$.each(result['data'], function(i, item) {
46-
if (item.docker_image == container) {
47-
var ports = String(item.ports).split(',');
48-
var data = '';
49-
$.each(ports, function(x, port) {
50-
port = String(port)
51-
data = data + 'Host: ' + item.host + ' Port: ' + port + '<br />';
52-
})
53-
$('#docker_container').html('<pre>Docker Container Information:<br />' + data + '<div class="mt-2" id="' + String(item.instance_id).substring(0,10) + '_revert_container"></div>');
54-
var countDownDate = new Date(parseInt(item.revert_time) * 1000).getTime();
55-
var x = setInterval(function() {
56-
var now = new Date().getTime();
57-
var distance = countDownDate - now;
58-
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
59-
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
60-
if (seconds < 10) {
61-
seconds = "0" + seconds
62-
}
63-
$("#" + String(item.instance_id).substring(0,10) + "_revert_container").html('Next Revert Available in ' + minutes + ':' + seconds);
64-
if (distance < 0) {
65-
clearInterval(x);
66-
$("#" + String(item.instance_id).substring(0,10) + "_revert_container").html('<a onclick="start_container(\'' + item.docker_image + '\');" class=\'btn btn-dark\'><small style=\'color:white;\'><i class="fas fa-redo"></i> Revert</small></a>');
67-
}
68-
}, 1000);
69-
return false;
70-
};
52+
CTFd.fetch("/api/v1/docker_status")
53+
.then((data) => {
54+
data.json().then((result) => {
55+
CTFd.lib.$.each(result['data'], function(i, item) {
56+
if (item.docker_image === container) {
57+
var ports = String(item.ports).split(',');
58+
var data = '';
59+
CTFd.lib.$.each(ports, function(x, port) {
60+
port = String(port)
61+
data = data + 'Host: ' + item.host + ' Port: ' + port + '<br />';
62+
})
63+
CTFd.lib.$('#docker_container').html('<pre>Docker Container Information:<br />' + data + '<div class="mt-2" id="' + String(item.instance_id).substring(0,10) + '_revert_container"></div>');
64+
var countDownDate = new Date(parseInt(item.revert_time) * 1000).getTime();
65+
var x = setInterval(function() {
66+
var now = new Date().getTime();
67+
var distance = countDownDate - now;
68+
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
69+
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
70+
if (seconds < 10) {
71+
seconds = "0" + seconds
72+
}
73+
CTFd.lib.$("#" + String(item.instance_id).substring(0,10) + "_revert_container").html('Next Revert Available in ' + minutes + ':' + seconds);
74+
if (distance < 0) {
75+
clearInterval(x);
76+
CTFd.lib.$("#" + String(item.instance_id).substring(0,10) + "_revert_container").html('<a onclick="start_container(\'' + item.docker_image + '\');" class=\'btn btn-dark\'><small style=\'color:white;\'><i class="fas fa-redo"></i> Revert</small></a>');
77+
}
78+
}, 1000);
79+
return false;
80+
};
81+
});
82+
}).catch(() => {
83+
ezal("Attention!", "Error");
84+
});
85+
}).catch(() => {
86+
// TODO
87+
ezal("Attention!", "Error");
7188
});
72-
});
7389
};
7490

7591
function start_container(container) {
76-
$('#docker_container').html('<div class="text-center"><i class="fas fa-circle-notch fa-spin fa-1x"></i></div>');
77-
$.get("/api/v1/container", { 'name': container }, function(result) {
92+
CTFd.lib.$('#docker_container').html('<div class="text-center"><i class="fas fa-circle-notch fa-spin fa-1x"></i></div>');
93+
CTFd.fetch("/api/v1/container?name=" + container )
94+
.then((data) => {
95+
console.log(data);
96+
get_docker_status(container);
97+
}).catch(() => {
98+
ezal("Attention!", "You can only revert a container once per 5 minutes! Please be patient.");
7899
get_docker_status(container);
79-
})
80-
.fail(function(jqxhr, settings, ex) {
81-
ezal({
82-
title: "Attention!",
83-
body: "You can only revert a container once per 5 minutes! Please be patient.",
84-
button: "Got it!"
85-
});
86-
$(get_docker_status(container));
87100
});
88101
}
89102

90-
var modal =
91-
'<div class="modal fade" tabindex="-1" role="dialog">' +
92-
' <div class="modal-dialog" role="document">' +
93-
' <div class="modal-content">' +
94-
' <div class="modal-header">' +
95-
' <h5 class="modal-title">{0}</h5>' +
96-
' <button type="button" class="close" data-dismiss="modal" aria-label="Close">' +
97-
' <span aria-hidden="true">&times;</span>' +
98-
" </button>" +
99-
" </div>" +
100-
' <div class="modal-body">' +
101-
" <p>{1}</p>" +
102-
" </div>" +
103-
' <div class="modal-footer">' +
104-
" </div>" +
105-
" </div>" +
106-
" </div>" +
107-
"</div>";
108-
109-
function ezal(args) {
110-
var res = modal.format(args.title, args.body);
111-
var obj = $(res);
112-
var button = '<button type="button" class="btn btn-primary" data-dismiss="modal">{0}</button>'.format(
113-
args.button
114-
);
115-
obj.find(".modal-footer").append(button);
116-
$("main").append(obj);
117-
118-
obj.modal("show");
119-
120-
$(obj).on("hidden.bs.modal", function(e) {
121-
$(this).modal("dispose");
122-
});
123-
124-
return obj;
103+
function ezal(title, body) {
104+
const content =
105+
'<div>' +
106+
'<h5>' + title + '</h5>' +
107+
'<p>' + body + '</p>' +
108+
'</div>';
109+
110+
CTFd.lib.$("#docker_container").html(content);
125111
}

0 commit comments

Comments
 (0)