Skip to content

Commit

Permalink
Fixes for core-beta theme
Browse files Browse the repository at this point in the history
refs #30
  • Loading branch information
Mattia Codato committed Oct 16, 2023
1 parent acac655 commit 32a9f2e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 85 deletions.
5 changes: 3 additions & 2 deletions docker_challenges/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,14 @@ def get_unavailable_ports(docker):
for i in r.json():
if not i['Ports'] == []:
for p in i['Ports']:
result.append(p['PublicPort'])
if 'PublicPort' in p:
result.append(p['PublicPort'])
return result


def get_required_ports(docker, image):
r = do_request(docker, f'/images/{image}/json?all=1')
result = r.json()['ContainerConfig']['ExposedPorts'].keys()
result = r.json()['Config']['ExposedPorts'].keys()
return result


Expand Down
9 changes: 0 additions & 9 deletions docker_challenges/assets/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,3 @@
</span>
</div>
{% endblock %}
{% block input %}
<input id="challenge-id" class="challenge-id" type="hidden" value="{{ challenge.id }}">
<input id="challenge-input" class="challenge-input" type="text" name="answer" placeholder="Flag" />
{% endblock %}
{% block submit %}
<button id="challenge-submit" class="challenge-submit" type="submit">
Submit
</button>
{% endblock %}
134 changes: 60 additions & 74 deletions docker_challenges/assets/view.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
CTFd._internal.challenge.data = undefined

CTFd._internal.challenge.renderer = CTFd.lib.markdown();
CTFd._internal.challenge.data = undefined;

CTFd._internal.challenge.renderer = null;


CTFd._internal.challenge.preRender = function() {}
Expand All @@ -10,6 +12,12 @@ CTFd._internal.challenge.render = function(markdown) {
return CTFd._internal.challenge.renderer.render(markdown)
}

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

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

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

function get_docker_status(container) {
$.get("/api/v1/docker_status", function(result) {
$.each(result['data'], function(i, item) {
if (item.docker_image == container) {
var ports = String(item.ports).split(',');
var data = '';
$.each(ports, function(x, port) {
port = String(port)
data = data + 'Host: ' + item.host + ' Port: ' + port + '<br />';
})
$('#docker_container').html('<pre>Docker Container Information:<br />' + data + '<div class="mt-2" id="' + String(item.instance_id).substring(0,10) + '_revert_container"></div>');
var countDownDate = new Date(parseInt(item.revert_time) * 1000).getTime();
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
if (seconds < 10) {
seconds = "0" + seconds
}
$("#" + String(item.instance_id).substring(0,10) + "_revert_container").html('Next Revert Available in ' + minutes + ':' + seconds);
if (distance < 0) {
clearInterval(x);
$("#" + 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>');
}
}, 1000);
return false;
};
CTFd.fetch("/api/v1/docker_status")
.then((data) => {
data.json().then((result) => {
CTFd.lib.$.each(result['data'], function(i, item) {
if (item.docker_image === container) {
var ports = String(item.ports).split(',');
var data = '';
CTFd.lib.$.each(ports, function(x, port) {
port = String(port)
data = data + 'Host: ' + item.host + ' Port: ' + port + '<br />';
})
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>');
var countDownDate = new Date(parseInt(item.revert_time) * 1000).getTime();
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
if (seconds < 10) {
seconds = "0" + seconds
}
CTFd.lib.$("#" + String(item.instance_id).substring(0,10) + "_revert_container").html('Next Revert Available in ' + minutes + ':' + seconds);
if (distance < 0) {
clearInterval(x);
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>');
}
}, 1000);
return false;
};
});
}).catch(() => {
ezal("Attention!", "Error");
});
}).catch(() => {
// TODO
ezal("Attention!", "Error");
});
});
};

function start_container(container) {
$('#docker_container').html('<div class="text-center"><i class="fas fa-circle-notch fa-spin fa-1x"></i></div>');
$.get("/api/v1/container", { 'name': container }, function(result) {
CTFd.lib.$('#docker_container').html('<div class="text-center"><i class="fas fa-circle-notch fa-spin fa-1x"></i></div>');
CTFd.fetch("/api/v1/container?name=" + container )
.then((data) => {
console.log(data);
get_docker_status(container);
}).catch(() => {
ezal("Attention!", "You can only revert a container once per 5 minutes! Please be patient.");
get_docker_status(container);
})
.fail(function(jqxhr, settings, ex) {
ezal({
title: "Attention!",
body: "You can only revert a container once per 5 minutes! Please be patient.",
button: "Got it!"
});
$(get_docker_status(container));
});
}

var modal =
'<div class="modal fade" tabindex="-1" role="dialog">' +
' <div class="modal-dialog" role="document">' +
' <div class="modal-content">' +
' <div class="modal-header">' +
' <h5 class="modal-title">{0}</h5>' +
' <button type="button" class="close" data-dismiss="modal" aria-label="Close">' +
' <span aria-hidden="true">&times;</span>' +
" </button>" +
" </div>" +
' <div class="modal-body">' +
" <p>{1}</p>" +
" </div>" +
' <div class="modal-footer">' +
" </div>" +
" </div>" +
" </div>" +
"</div>";

function ezal(args) {
var res = modal.format(args.title, args.body);
var obj = $(res);
var button = '<button type="button" class="btn btn-primary" data-dismiss="modal">{0}</button>'.format(
args.button
);
obj.find(".modal-footer").append(button);
$("main").append(obj);

obj.modal("show");

$(obj).on("hidden.bs.modal", function(e) {
$(this).modal("dispose");
});

return obj;
function ezal(title, body) {
const content =
'<div>' +
'<h5>' + title + '</h5>' +
'<p>' + body + '</p>' +
'</div>';

CTFd.lib.$("#docker_container").html(content);
}

0 comments on commit 32a9f2e

Please sign in to comment.