1
1
CTFd . _internal . challenge . data = undefined
2
2
3
- CTFd . _internal . challenge . renderer = CTFd . lib . markdown ( ) ;
3
+ CTFd . _internal . challenge . data = undefined ;
4
+
5
+ CTFd . _internal . challenge . renderer = null ;
4
6
5
7
6
8
CTFd . _internal . challenge . preRender = function ( ) { }
@@ -10,6 +12,12 @@ CTFd._internal.challenge.render = function(markdown) {
10
12
return CTFd . _internal . challenge . renderer . render ( markdown )
11
13
}
12
14
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
+ } ;
13
21
14
22
CTFd . _internal . challenge . postRender = function ( ) { }
15
23
@@ -41,85 +49,63 @@ CTFd._internal.challenge.submit = function(preview) {
41
49
} ;
42
50
43
51
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" ) ;
71
88
} ) ;
72
- } ) ;
73
89
} ;
74
90
75
91
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." ) ;
78
99
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 ) ) ;
87
100
} ) ;
88
101
}
89
102
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">×</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 ) ;
125
111
}
0 commit comments