Skip to content

Commit

Permalink
CTFd 3.6.x integration (#16)
Browse files Browse the repository at this point in the history
* adjust model and assets to fit CTFd 3.6
* router config should be checked before resetting
* view.js fix and 404 message fix

reference: CTFd/core-beta#58
  • Loading branch information
frankli0324 authored Jan 4, 2024
1 parent 7b54b36 commit 24c22cb
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 52 deletions.
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@


def load(app):
app.config['RESTX_ERROR_404_HELP'] = False
# upgrade()
plugin_name = __name__.split('.')[-1]
set_config('whale:plugin_name', plugin_name)
Expand Down
15 changes: 1 addition & 14 deletions assets/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,6 @@
{% block description %}
{{ challenge.html }}
<div class="row text-center pb-3">
<div id="whale-panel" style="width: 100%;">
<button type="button" class="btn btn-primary" onclick="window.challenge.boot()">Boot a instance</button>
</div>
<div id="whale-panel" style="width: 100%;"></div>
</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 %}
46 changes: 22 additions & 24 deletions assets/view.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
CTFd._internal.challenge.data = undefined

CTFd._internal.challenge.renderer = CTFd.lib.markdown();
CTFd._internal.challenge.renderer = null;

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

CTFd._internal.challenge.render = function (markdown) {
return CTFd._internal.challenge.renderer.render(markdown)
}
CTFd._internal.challenge.render = null;

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

if ($ === undefined) $ = CTFd.lib.$;
if (window.$ === undefined) window.$ = CTFd.lib.$;

function loadInfo() {
var challenge_id = $('#challenge-id').val();
var challenge_id = CTFd._internal.challenge.data.id;
var url = "/api/v1/plugins/ctfd-whale/container?challenge_id=" + challenge_id;

var params = {};
Expand Down Expand Up @@ -44,9 +42,9 @@ function loadInfo() {
window.t = undefined;
}
if (response.success) response = response.data;
else CTFd.ui.ezq.ezAlert({
else CTFd._functions.events.eventAlert({
title: "Fail",
body: response.message,
html: response.message,
button: "OK"
});
if (response.remaining_time === undefined) {
Expand Down Expand Up @@ -101,7 +99,7 @@ function loadInfo() {
};

CTFd._internal.challenge.destroy = function () {
var challenge_id = $('#challenge-id').val();
var challenge_id = CTFd._internal.challenge.data.id;
var url = "/api/v1/plugins/ctfd-whale/container?challenge_id=" + challenge_id;

$('#whale-button-destroy')[0].innerHTML = "Waiting...";
Expand Down Expand Up @@ -130,25 +128,25 @@ CTFd._internal.challenge.destroy = function () {
}).then(function (response) {
if (response.success) {
loadInfo();
CTFd.ui.ezq.ezAlert({
CTFd._functions.events.eventAlert({
title: "Success",
body: "Your instance has been destroyed!",
html: "Your instance has been destroyed!",
button: "OK"
});
} else {
$('#whale-button-destroy')[0].innerHTML = "Destroy this instance";
$('#whale-button-destroy')[0].disabled = false;
CTFd.ui.ezq.ezAlert({
CTFd._functions.events.eventAlert({
title: "Fail",
body: response.message,
html: response.message,
button: "OK"
});
}
});
};

CTFd._internal.challenge.renew = function () {
var challenge_id = $('#challenge-id').val();
var challenge_id = CTFd._internal.challenge.data.id;
var url = "/api/v1/plugins/ctfd-whale/container?challenge_id=" + challenge_id;

$('#whale-button-renew')[0].innerHTML = "Waiting...";
Expand Down Expand Up @@ -177,25 +175,25 @@ CTFd._internal.challenge.renew = function () {
}).then(function (response) {
if (response.success) {
loadInfo();
CTFd.ui.ezq.ezAlert({
CTFd._functions.events.eventAlert({
title: "Success",
body: "Your instance has been renewed!",
html: "Your instance has been renewed!",
button: "OK"
});
} else {
$('#whale-button-renew')[0].innerHTML = "Renew this instance";
$('#whale-button-renew')[0].disabled = false;
CTFd.ui.ezq.ezAlert({
CTFd._functions.events.eventAlert({
title: "Fail",
body: response.message,
html: response.message,
button: "OK"
});
}
});
};

CTFd._internal.challenge.boot = function () {
var challenge_id = $('#challenge-id').val();
var challenge_id = CTFd._internal.challenge.data.id;
var url = "/api/v1/plugins/ctfd-whale/container?challenge_id=" + challenge_id;

$('#whale-button-boot')[0].innerHTML = "Waiting...";
Expand Down Expand Up @@ -224,17 +222,17 @@ CTFd._internal.challenge.boot = function () {
}).then(function (response) {
if (response.success) {
loadInfo();
CTFd.ui.ezq.ezAlert({
CTFd._functions.events.eventAlert({
title: "Success",
body: "Your instance has been deployed!",
html: "Your instance has been deployed!",
button: "OK"
});
} else {
$('#whale-button-boot')[0].innerHTML = "Launch an instance";
$('#whale-button-boot')[0].disabled = false;
CTFd.ui.ezq.ezAlert({
CTFd._functions.events.eventAlert({
title: "Fail",
body: response.message,
html: response.message,
button: "OK"
});
}
Expand All @@ -243,7 +241,7 @@ CTFd._internal.challenge.boot = function () {


CTFd._internal.challenge.submit = function (preview) {
var challenge_id = $('#challenge-id').val();
var challenge_id = CTFd._internal.challenge.data.id;
var submission = $('#challenge-input').val()

var body = {
Expand Down
18 changes: 8 additions & 10 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from jinja2 import Template

from CTFd.utils import get_config
from CTFd.models import db, Challenges
from CTFd.models import db
from CTFd.plugins.dynamic_challenges import DynamicChallenge


class WhaleConfig(db.Model):
Expand Down Expand Up @@ -34,14 +35,12 @@ def __repr__(self):
return "<WhaleRedirectTemplate {0}>".format(self.key)


class DynamicDockerChallenge(Challenges):
class DynamicDockerChallenge(DynamicChallenge):
__mapper_args__ = {"polymorphic_identity": "dynamic_docker"}
id = db.Column(None, db.ForeignKey("challenges.id",
ondelete="CASCADE"), primary_key=True)
id = db.Column(
db.Integer, db.ForeignKey("dynamic_challenge.id", ondelete="CASCADE"), primary_key=True
)

initial = db.Column(db.Integer, default=0)
minimum = db.Column(db.Integer, default=0)
decay = db.Column(db.Integer, default=0)
memory_limit = db.Column(db.Text, default="128m")
cpu_limit = db.Column(db.Float, default=0.5)
dynamic_score = db.Column(db.Integer, default=0)
Expand All @@ -51,16 +50,15 @@ class DynamicDockerChallenge(Challenges):
redirect_port = db.Column(db.Integer, default=0)

def __init__(self, *args, **kwargs):
kwargs["initial"] = kwargs["value"]
super(DynamicDockerChallenge, self).__init__(**kwargs)
self.initial = kwargs["value"]


class WhaleContainer(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
user_id = db.Column(None, db.ForeignKey("users.id"))
challenge_id = db.Column(None, db.ForeignKey("challenges.id"))
start_time = db.Column(db.DateTime, nullable=False,
default=datetime.utcnow)
start_time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
renew_count = db.Column(db.Integer, nullable=False, default=0)
status = db.Column(db.Integer, default=1)
uuid = db.Column(db.String(256))
Expand Down
2 changes: 1 addition & 1 deletion templates/config/frp.router.config.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
value !=None %}value="{{ value }}" {% endif %}>
</div>
{% endfor %}
{% set frpc_template = get_config("whale:frp_config_template") %}
{% set frpc_template = get_config("whale:frp_config_template", "") %}
<div class="form-group">
<label for="frp-config-template">
Frpc config template
Expand Down
7 changes: 5 additions & 2 deletions utils/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from CTFd.utils import get_config

from .docker import get_docker_client
from .routers import Router
from .routers import Router, _routers


class WhaleChecks:
Expand Down Expand Up @@ -32,7 +32,10 @@ def check_docker_api():

@staticmethod
def check_frp_connection():
ok, msg = Router.check_availability()
router_conftype = get_config("whale:router_type", "frp")
if router_conftype not in _routers:
return "invalid router type: " + router_conftype
ok, msg = _routers[router_conftype]().check_availability()
if not ok:
return msg

Expand Down
2 changes: 1 addition & 1 deletion utils/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def setup_default_configs():
'docker_timeout': '3600',
'frp_api_url': 'http://frpc:7400',
'frp_http_port': '8080',
'frp_http_domain_suffix': '127.0.0.1.xip.io',
'frp_http_domain_suffix': '127.0.0.1.nip.io',
'frp_direct_port_maximum': '10100',
'frp_direct_port_minimum': '10000',
'template_http_subdomain': '{{ container.uuid }}',
Expand Down

0 comments on commit 24c22cb

Please sign in to comment.