-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
193 lines (175 loc) · 6.15 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* global $:false Handlebars:false marked:false */
$.fn.serializeJson = function() {
var obj = {};
$.each(this.serializeArray(), function() {
obj[this.name] = this.value;
});
return obj;
};
$.fn.isVisible = function() {
return $.expr.filters.visible(this[0]);
};
$(function(){
var storageKey = 'yehd2015-ctf';
var questionList = [];
var userInfo = {};
loadQuestionList()
.then(filterQuestionList)
.then(formSettings)
.then(loadUserInfo);
function loadQuestionList() {
return fetch('./questionList.json')
.then(function(res) { return res.json(); })
.then(function(json) {
questionList = json.map(function(f) { return f.split('-'); })
.map(function(a) { return a.concat(a.splice(2).join('-')); })
.map(function(a) { return {
category: a[0],
point: parseInt(a[1], 10),
name: a[2],
question: a.join('-')
}; });
return questionList;
})
.then(function(list) {
var $category = $('#category');
var source = $('template', $category).html();
var template = Handlebars.compile(source);
var categoryList =
list.map(function(i) { return i.category; })
.filter(function (x, i, self) { return self.indexOf(x) === i; })
.sort();
categoryList.forEach(function(category) {
var $button = $(template({ category: category }));
$category.append($button);
});
$('a', $category).on('click', filterQuestionList);
return list;
})
.then(function(list) {
var $qListContainer = $('main#qList');
var source = $('template', $qListContainer).html();
var template = Handlebars.compile(source);
list.forEach(function(qInfo) {
var $panel = $(template(qInfo));
$('a.toggle-details', $panel).on('click', toggleDetails);
$qListContainer.append($panel);
});
});
}
function filterQuestionList(ev) {
var category = '';
if (!ev) category = location.hash.substr(1);
else category = ev.currentTarget.href.split('#')[1];
category = category.replace(/(^\s*|\s*$)/g, '');
if (!category.match(/^[\w\d]+$/)) {
$('#qList .panel').show(500);
$('#category li').removeClass('active');
$('#category li[data-category="All"]').addClass('active');
} else {
$('#qList .panel[data-category="' + category + '"]').show(500);
$('#qList .panel[data-category!="' + category + '"]').hide(500);
$('#category li').removeClass('active');
$('#category li[data-category="' + category + '"]').addClass('active');
}
}
function formSettings() {
$('form').on('submit', function(ev) {
ev.preventDefault();
});
$('#qList .panel form').on('submit', submitFlag);
$('#login button').on('click', login);
$('#logout button').on('click', logout);
}
function submitFlag(ev) {
var $target = $(ev.currentTarget);
var $parent = $target.parent();
$('.alert', $parent).hide(250);
var flag = $('input', $target).val().replace(/(^\s*|\s*$)/g, '');
$('input', $target).val('');
var question = $target.data('question');
return fetch('./' + question + '/flag.sha3-512')
.then(function(res) {
if (res.ok) {
return res.text();
} else {
$('.alert-warning', $parent).text(res.status).show(250);
}
})
.then(function(hash) {
hash = hash.replace(/(^\s*|\s*$)/g, '');
var submitted = sha3_512(flag).replace(/(^\s*|\s*$)/g, '');
if (hash !== submitted) {
$('.alert-warning', $parent).text('Flag is invalid.').show(250);
} else {
var message;
if (userInfo.submit.filter(function(s) { return s.question === question; }).length) {
message = 'Already submitted.';
} else {
message = 'Success!!';
var score = parseInt(question.split('-')[1], 10);
userInfo.score = parseInt(userInfo.score, 10) + score;
userInfo.submit.push({ question: question, score: score });
}
$('.alert-info', $parent).text(message).show(250);
}
})
.then(loadSubmitted);
}
function loadUserInfo() {
try {
userInfo = JSON.parse(localStorage.getItem(storageKey));
} catch (_e) {
userInfo = {};
}
if (
!userInfo || userInfo.constructor.name !== 'Object' ||
!userInfo.submit || userInfo.submit.constructor.name !== 'Array' ||
!userInfo.score || userInfo.score.constructor.name !== 'Number'
) {
userInfo = { score: 0, submit: [] };
}
loadSubmitted();
}
function loadSubmitted() {
var score = parseInt(userInfo.score || 0, 10);
$('#userStatus').text('Your Score: ' + score + ' pts');
$('#login').hide(0);
$('.panel .label-info').hide(0);
(userInfo.submit || []).forEach(function(sInfo) {
$('.panel[data-question="' + sInfo.question + '"]')
.find('.label-info').show(0);
});
localStorage.setItem(storageKey, JSON.stringify(userInfo))
}
function toggleDetails(ev) {
ev.preventDefault();
var $target = $(ev.currentTarget);
var question = $target.data('question');
var $details = $('.panel[data-question="' + question + '"] .details');
if (!$details.isVisible()) {
fetch('/' + question + '/README.md')
.then(function(res) { return res.text(); })
.then(function(md) {
var renderer = new marked.Renderer();
(function() {
var link = renderer.link.bind(renderer);
renderer.link = function(href, title, text) {
if (!href.match(/^http/)) href = '/' + question + '/' + href;
return link(href, title, text);
};
var image = renderer.image.bind(renderer);
renderer.image = function(href, title, text) {
if (!href.match(/^http/)) href = '/' + question + '/' + href;
return image(href, title, text);
};
})();
$details.html(marked(md + '\n\n----', { renderer: renderer }));
$('a', $details).attr({ target: '_blank' });
$details.show(500);
});
} else {
$details.hide(500);
}
}
});