-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
267 lines (240 loc) · 7.73 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
var util = util || {};
util.toArray = function(list) {
return Array.prototype.slice.call(list || [], 0);
};
var Terminal =
Terminal ||
function(cmdLineContainer, outputContainer) {
window.URL = window.URL || window.webkitURL;
window.requestFileSystem =
window.requestFileSystem || window.webkitRequestFileSystem;
var cmdLine_ = document.querySelector(cmdLineContainer);
var output_ = document.querySelector(outputContainer);
const CMDS_ = [
"cat",
"cd",
"clear",
"clear",
"date",
"echo",
"email_me",
"help",
"ls",
"pwd",
"social",
"sudo",
"uname",
"whoami",
"wget"
];
var fs_ = null;
var cwd_ = null;
var history_ = [];
var histpos_ = 0;
var histtemp_ = 0;
window.addEventListener(
"click",
function(e) {
cmdLine_.focus();
},
false
);
cmdLine_.addEventListener("click", inputTextClick_, false);
cmdLine_.addEventListener("keydown", historyHandler_, true);
cmdLine_.addEventListener("keydown", processNewCommand_, false);
//
function inputTextClick_(e) {
this.value = this.value;
}
//
function historyHandler_(e) {
if (history_.length) {
if (e.keyCode == 38 || e.keyCode == 40) {
if (history_[histpos_]) {
history_[histpos_] = this.value;
} else {
histtemp_ = this.value;
}
}
if (e.keyCode == 38) {
// up
histpos_--;
if (histpos_ < 0) {
histpos_ = 0;
}
} else if (e.keyCode == 40) {
// down
histpos_++;
if (histpos_ > history_.length) {
histpos_ = history_.length;
}
}
if (e.keyCode == 38 || e.keyCode == 40) {
this.value = history_[histpos_] ? history_[histpos_] : histtemp_;
this.value = this.value; // Sets cursor to end of input.
}
}
}
//
function processNewCommand_(e) {
if (e.keyCode == 9) {
// tab
e.preventDefault();
// Implement tab suggest.
} else if (e.keyCode == 13) {
// enter
// Save shell history.
if (this.value) {
history_[history_.length] = this.value;
histpos_ = history_.length;
}
// Duplicate current input and append to output section.
var line = this.parentNode.parentNode.cloneNode(true);
line.removeAttribute("id");
line.classList.add("line");
var input = line.querySelector("input.cmdline");
input.autofocus = false;
input.readOnly = true;
output_.appendChild(line);
if (this.value && this.value.trim()) {
var args = this.value.split(" ").filter(function(val, i) {
return val;
});
var cmd = args[0].toLowerCase();
args = args.splice(1); // Remove cmd from arg list.
}
execCMD(cmd, args);
window.scrollTo(0, getDocHeight_());
this.value = ""; // Clear/setup line for next input.
}
}
//
function formatColumns_(entries) {
var maxName = entries[0].name;
util.toArray(entries).forEach(function(entry, i) {
if (entry.name.length > maxName.length) {
maxName = entry.name;
}
});
var height =
entries.length <= 3 ? "height: " + entries.length * 15 + "px;" : "";
// 12px monospace font yields ~7px screen width.
var colWidth = maxName.length * 7;
return [
'<div class="ls-files" style="-webkit-column-width:',
colWidth,
"px;",
height,
'">'
];
}
//
function output(html) {
output_.insertAdjacentHTML("beforeEnd", "<p>" + html + "</p>");
}
// Cross-browser impl to get document's height.
function getDocHeight_() {
var d = document;
return Math.max(
Math.max(d.body.scrollHeight, d.documentElement.scrollHeight),
Math.max(d.body.offsetHeight, d.documentElement.offsetHeight),
Math.max(d.body.clientHeight, d.documentElement.clientHeight)
);
}
function execCMD(cmd, args) {
switch (cmd) {
case "cat":
var url = args.join(" ");
if (!url) {
output("Usage: " + cmd + " filename");
output(
"Example: " +
cmd +
" cat resume.txt"
);
break;
}
$.get(url, function(data) {
var encodedStr = data.replace(
/[\u00A0-\u9999<>\&]/gim,
function(i) {
return "&#" + i.charCodeAt(0) + ";";
}
);
output("<pre>" + encodedStr + "</pre>");
});
break;
case "clear":
output_.innerHTML = "";
this.value = "";
return;
case "date":
output(new Date());
break;
case "echo":
output(args.join(" "));
break;
case "help":
output('<div class="ls-files">' + CMDS_.join("<br>") + "</div>");
break;
case "uname":
output(navigator.appVersion);
break;
case "sudo":
output('<img src="https://i.imgur.com/ZpHQiGz.png"')
output("i'm sorry dave i can't allow you to do that.");
break;
case "email_me":
window.open(
"mailto:[email protected]?subject=Resume&body= Great Resume"
);
break;
case "whoami":
var result = '<img src="' + codehelper_ip["Flag"] + '"><br><br>';
for (var prop in codehelper_ip)
result += prop + ": " + codehelper_ip[prop] + "<br>";
output(result);
break;
case "ls":
output("resume.txt welcome.txt user_flag.txt")
break;
case "cd":
output("command disabled")
break;
case "pwd":
output("/users/dedsec")
break;
case "wget":
output("Usage: " + cmd + " filename");
output("Example: " + cmd + " resume.txt");
break;
default:
if (cmd) {
output(cmd + ": command not found");
}
}
}
//
return {
init: function() {
output(
'<img align="left" src="https://i.imgur.com/HElQzF0.png" width="100" height="100"'
);
output(
"<br>" +
"<br>" +
"<h2>DEDSEC</h2><p>" +
'</p><p>Last logon: ' + new Date() +
'</p><p>Enter "help" for more information.</p>'
);
},
output: output
};
};
$(function() {
console.log("o.o");
$(".prompt").html("[dedsec@lulz189] $ ");
// Initialize a new terminal object
var term = new Terminal("#input-line .cmdline", "#wrapper output");
term.init();
});