From 32a9f2eb12b184c43ff423bed6460e86cb5a6185 Mon Sep 17 00:00:00 2001 From: Mattia Codato Date: Mon, 16 Oct 2023 11:35:03 +0200 Subject: [PATCH] Fixes for core-beta theme refs #30 --- docker_challenges/__init__.py | 5 +- docker_challenges/assets/view.html | 9 -- docker_challenges/assets/view.js | 134 +++++++++++++---------------- 3 files changed, 63 insertions(+), 85 deletions(-) diff --git a/docker_challenges/__init__.py b/docker_challenges/__init__.py index 190ef35..a23df88 100644 --- a/docker_challenges/__init__.py +++ b/docker_challenges/__init__.py @@ -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 diff --git a/docker_challenges/assets/view.html b/docker_challenges/assets/view.html index 7688b57..029874e 100644 --- a/docker_challenges/assets/view.html +++ b/docker_challenges/assets/view.html @@ -15,12 +15,3 @@ {% endblock %} -{% block input %} - - -{% endblock %} -{% block submit %} - -{% endblock %} diff --git a/docker_challenges/assets/view.js b/docker_challenges/assets/view.js index 64a401d..674ad25 100644 --- a/docker_challenges/assets/view.js +++ b/docker_challenges/assets/view.js @@ -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() {} @@ -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() {} @@ -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 + '
'; - }) - $('#docker_container').html('
Docker Container Information:
' + data + '
'); - 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(' Revert'); - } - }, 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 + '
'; + }) + CTFd.lib.$('#docker_container').html('
Docker Container Information:
' + data + '
'); + 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(' Revert'); + } + }, 1000); + return false; + }; + }); + }).catch(() => { + ezal("Attention!", "Error"); + }); + }).catch(() => { + // TODO + ezal("Attention!", "Error"); }); - }); }; function start_container(container) { - $('#docker_container').html('
'); - $.get("/api/v1/container", { 'name': container }, function(result) { + CTFd.lib.$('#docker_container').html('
'); + 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 = - '"; - -function ezal(args) { - var res = modal.format(args.title, args.body); - var obj = $(res); - var 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 = + '
' + + '
' + title + '
' + + '

' + body + '

' + + '
'; + + CTFd.lib.$("#docker_container").html(content); } \ No newline at end of file