This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
repl.js
executable file
·238 lines (200 loc) · 4.8 KB
/
repl.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
#!/usr/bin/env node
/* eslint no-mixed-operators: "off" */
require('colors');
const brokenConsole = require('console');
const repl = require('repl');
const events = require('events');
const fs = require('fs');
const path = require('path');
const WebSocket = require('ws');
const History = require('repl.history');
const ee = new events.EventEmitter();
const wss = new WebSocket.Server({ port: 8100 });
const historyPath = path.resolve(__dirname, '.shell_history');
//This is needed to update the output to the console so the writing in start.js shows
console.log('');
function sendMsg (cmd, ws, args = []) {
ws.send(JSON.stringify({
cmd,
args
}));
}
ee.on('error', function (message) {
console.error('ERROR:', message.slice(0, 2));
});
function loadConfig() {
try {
fs.statSync("config.json"); // test existence
return JSON.parse(fs.readFileSync("config.json", "utf-8"));
} catch(e) {
var config = { // default config
};
fs.writeFileSync("config.json", JSON.stringify(config), "utf-8");
return config;
}
}
const fns = {
evalfile: {
response: 'evald',
help: 'evalfile <filename>',
helptxt: 'Evals code read from file',
setup: function (args, callback) {
try {
var filepath = path.resolve(__dirname, args[0]);
fs.statSync(filepath);
return [fs.readFileSync(filepath).toString()];
} catch (e) {
return callback(null, 'invalid file ' + e.message);
}
}
},
quit: {
help: 'quit',
helptxt: 'Close REPL and shut off server',
noSend: true,
setup(args, callback) {
process.exit();
}
}
};
function showHelp (callback) {
for (let k in fns) {
let out = `${k.bold}: ${fns[k].helptxt}`;
if (fns[k].help) {
out += ` (${fns[k].help})`.dim;
}
console.log(out);
}
console.log();
return callback();
}
let _; // last value reg
let isJavascript = false;
function defaultHandler (saveVal, callback) {
return function (response) {
if (saveVal) {
_ = response;
}
return callback(null, response);
};
}
function handle (input, context, filename, callback) {
let tmp = input.replace(/\n$/, '');
let saveVal = false;
let args = tmp.trimLeft().split(' ');
let cmd = args.shift();
if (cmd === 'help') {
return showHelp(callback);
}
let fn = fns[cmd];
if (!fn) {
return callback(null, 'unknown cmd');
}
if (
fn.args !== undefined && fn.args !== args.length ||
fn.minArgs !== undefined && fn.minArgs > args.length ||
fn.maxArgs !== undefined && args.length > fn.maxArgs
) {
return callback(null, fn.help);
}
if (fn.setup) {
args = fn.setup(args, callback);
if (!args) {
return;
}
}
if (!fn.noSend) {
var handle = fn.handler ? fn.handler(args, callback) : defaultHandler(saveVal, callback);
ee.once(fn.response, handle);
sendMsg(cmd, args);
if (fn.wait === false) {
ee.removeListener(fn.response, handle);
return callback();
}
}
}
function complete (line) {
var args = line.split(' ');
var cmd = args.shift();
if (args.length === 0) {
return [cmd.length ? Object.keys(fns).map((name) => name + ' ').filter((fn) => fn.startsWith(cmd)) : Object.keys(fns), line];
} else {
if (fns[cmd] && fns[cmd].complete) {
return fns[cmd].complete(line);
} else {
return [[], line];
}
}
}
const r = repl.start({
prompt: '',
eval: handle,
completer: complete
});
History(r, historyPath);
// looks for autorun var [bool]
// in config.json
function checkAutoRun(){
var result = false;
try{
var tmp = loadConfig().autorunScript;
if (typeof tmp === "boolean"){
result = tmp;
} else {
console.log("tried to load non-bool input");
}
} catch (e){
console.log("failed to load config.json");
}
return result;
}
// tries to load the js file speified in config.json
function loadScript(reqScript){
var script = "";
var scriptPath = "";
try{
if (reqScript in loadConfig().scripts) {
scriptPath = loadConfig().scripts[reqScript].scriptPath;
} else {
return undefined;
}
script = fns.evalfile.setup([scriptPath], (obj, output) => {
throw output;
});
script += `alert(\"Success!\");\n`
} catch (e){
console.log(e);
process.exit(1);
}
return script;
}
wss.on('connection', function (ws) {
ws.on('message', function(data) {
data = JSON.parse(data);
const type = data.type;
const response = data.response;
if(type == "identification") {
var u8 = new Uint8Array(0x18);
var u32 = new Uint32Array(u8.buffer);
for(var i = 0; i < data.serial.length; i++) {
u32[i] = data.serial[i];
}
var serial = String.fromCharCode.apply(null, u8);
ws.serial = serial;
ws.fwVersion = data.version;
if(checkAutoRun()){
const reqScript = data.script;
script = loadScript(reqScript);
if (script != undefined) {
sendMsg("evalfile", ws, [script]);
}
ws.terminate();
}
} else {
ee.emit(type, response, ws.serial);
}
});
});
r.on('exit', () => {
process.exit();
});