-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·405 lines (392 loc) · 16.2 KB
/
app.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/env node
const formidable = require('formidable'),
mustache = require('mustache'),
lmdb = require('node-lmdb'),
http = require('http'),
fs = require('fs'),
process = require('process'),
path = require('path');
//const BIND_IP = '0.0.0.0';
const BIND_IP = '::';
const BIND_PORT = 8080;
const INDEX_HTML_TPL = fs.readFileSync(`${__dirname}/index.mustache`).toString();
const SIMPLE_RESPONSIVE_CSS = fs.readFileSync(`${__dirname}/simpleresponsive.css`).toString();
const INDEX_JS = fs.readFileSync(`${__dirname}/index.js`).toString();
const TAIL_F_HTML_TPL = fs.readFileSync(`${__dirname}/tail_f.mustache`).toString();
function parseHttpReqRange(rangeLine, fileSize) {
// if multiple ranges requested, we only support the first range
let [, rangeStart, rangeEnd] = rangeLine.trim().match(/bytes=(\d+)?-(\d+)?/);
rangeStart = parseInt(rangeStart);
rangeEnd = parseInt(rangeEnd);
if (isNaN(rangeStart)) {
if (isNaN(rangeEnd)) {
return [NaN, NaN];
}
// something like Range: -500
if (rangeEnd >= fileSize) {
return [NaN, NaN];
}
return [fileSize - rangeEnd, fileSize - 1];
}
if (rangeStart >= fileSize) {
return [NaN, NaN];
}
if (isNaN(rangeEnd)) {
// something like Range: 999-
return [rangeStart, fileSize - 1];
}
// something like Range:600-700
if (rangeEnd >= fileSize) {
return [NaN, NaN];
}
return [rangeStart, rangeEnd];
}
function mkStatPromise(dirPath, fileName) {
return new Promise((resolve) => {
const fileRelPath = path.join(dirPath, fileName);
fs.lstat(fileRelPath, (err, stat) => {
if (err != null) {
console.warn(`directory: stat(${fileRelPath}) failed: ${err.message}`);
resolve({
'name': fileName,
'size': 0,
});
} else {
const outStat = {
'name': fileName,
'href': encodeURI(path.join('/', dirPath, fileName)),
'mtime': stat.mtime.toLocaleString('sv', { timeZoneName: 'short' }),
'size': stat.size,
}
if (stat.isDirectory()) {
outStat.name = `📁${fileName}/`;
} else if (stat.isSymbolicLink()) {
outStat.name = `↪${fileName}`;
}
const lowercaseName = fileName.toLowerCase();
if (lowercaseName.endsWith('.log') || lowercaseName.endsWith('.txt')) {
outStat.extraHref = {
text: "tail -f",
link: `${outStat.href}?tail_f=1`,
};
} else if (lowercaseName == "data.mdb") {
outStat.extraHref = {
text: "lmdb",
link: `${encodeURI(path.join('/', dirPath))}?lmdb=1`,
};
}
resolve(outStat);
}
})
});
}
async function parseUriToFileStat(uri) {
const file = {};
uri = new URL(uri, 'scheme://host');
file.decodedUrlPath = decodeURI(uri.pathname);
file.isTailFPage = (uri.searchParams.has("tail_f"));
file.isLmdbPage = (uri.searchParams.has("lmdb"));
// now: decodedUrlPath possible values(tailing slash is not always carried): '/', '/foo', '/foo/', '/bar.txt'
if (file.decodedUrlPath.endsWith('/')) {
// make sure there is no trailing slash, resulting like: '', '/foo', '/bar.txt'
file.decodedUrlPath = file.decodedUrlPath.substring(0, file.decodedUrlPath.length - 1);
}
file.relPath = path.normalize(path.join('.', file.decodedUrlPath));
// now: file.relPath possible values: '.', 'foo', 'bar.txt'
// path.resolve(file.relPath) possible values(tailing slash is never carried): '/cwd', '/cwd/foo', '/cwd/bar.txt'
if (path.resolve(file.relPath) != path.join(path.resolve(process.cwd()), file.decodedUrlPath)) {
return [null, {
code: 403,
header: { 'Content-Type': "text/plain; charset=UTF-8" },
message: "Not allowed to break the jail",
}];
}
let [err, fileStat] = await new Promise((resolve) => {
fs.lstat(file.relPath, (err, stat) => { resolve([err, stat]); });
});
file.stat = fileStat;
if (err == null) {
if (file.stat.isSymbolicLink()) {
[err, file.linkStr] = await new Promise(
(resolve) => fs.readlink(file.relPath, (err, linkStr) => { resolve([err, linkStr]); }));
} else {
err = await new Promise((resolve) => fs.access(file.relPath, fs.R_OK, (err) => { resolve(err); }));
}
}
if (err) {
return [null, {
code: 404,
header: { 'Content-Type': "text/plain; charset=UTF-8" },
message: err.message,
}];
}
return [file, null];
}
function filterHandleDatestampReq(req, rsp) {
if (req.method.toLowerCase() != 'get') {
return false;
}
if (req.url != "/?stamp=now") {
return false;
}
rsp.writeHead(200, { 'Content-Type': "text/plain; charset=UTF-8" });
rsp.end((new Date).toISOString());
return true;
}
function listLmdbKeys(dbPath) {
const outList = [];
try {
const env = new lmdb.Env();
env.open({
path: dbPath,
readOnly: true,
});
const dbi = env.openDbi({ name: null });
const txn = env.beginTxn({ readOnly: true });
const cursor = new lmdb.Cursor(txn, dbi, { keyIsBuffer: true });
let key = cursor.goToFirst();
while (key) {
outList.push({
'name': key.toString('utf8'),
'size': 0,
});
key = cursor.goToNext();
}
cursor.close();
txn.commit();
dbi.close();
env.close();
} catch (err) {
console.error(`listLmdbKeys(${dbPath}): ${err.message}`);
}
return outList;
}
const httpServer = http.createServer();
httpServer.on('request', async function (req, rsp) {
if (filterHandleDatestampReq(req, rsp)) return;
const [file, errRsp] = await parseUriToFileStat(req.url);
if (errRsp) {
rsp.writeHead(errRsp.code, errRsp.header);
rsp.end(errRsp.message);
return;
}
if (req.method.toLowerCase() == 'get') {
// "GET" is used for directory listing and file downloading
if (file.stat.isDirectory()) {
if (file.isLmdbPage) {
const index_html = mustache.render(INDEX_HTML_TPL, {
'data': {
'simpleResponsiveCSS': SIMPLE_RESPONSIVE_CSS,
'indexJS': INDEX_JS,
'title': `lmdb: ${file.decodedUrlPath}/`,
'stringifiedFileList': JSON.stringify(listLmdbKeys(file.relPath)),
}
});
rsp.writeHead(200, { 'Content-Type': "text/html; charset=UTF-8" });
rsp.end(index_html, 'utf-8');
return;
}
const [err, subFileNameList] = await new Promise((resolve) => {
fs.readdir(file.relPath, (err, list) => { resolve([err, list]); });
});
if (err) {
rsp.writeHead(404, { 'Content-Type': "text/plain; charset=UTF-8" });
rsp.end(err.message);
return;
}
let stat_promises = [];
for (const subFileName of subFileNameList) {
stat_promises.push(mkStatPromise(file.relPath, subFileName));
}
const subFileStats = await Promise.all(stat_promises);
const index_html = mustache.render(INDEX_HTML_TPL, {
'data': {
'simpleResponsiveCSS': SIMPLE_RESPONSIVE_CSS,
'indexJS': INDEX_JS,
'upload': true,
'title': `${file.decodedUrlPath}/`,
'stringifiedFileList': JSON.stringify(subFileStats),
}
});
rsp.writeHead(200, { 'Content-Type': "text/html; charset=UTF-8" });
rsp.end(index_html, 'utf-8');
} else if (file.stat.isFile()) {
if (file.isTailFPage) {
rsp.writeHead(200, { 'Content-Type': "text/html; charset=UTF-8" });
rsp.end(mustache.render(TAIL_F_HTML_TPL, {
"data": {
'title': file.decodedUrlPath,
'pathWebsocket': JSON.stringify(encodeURI(path.join('/', file.relPath))),
}
}), 'utf-8');
return;
}
let fileStream = null;
if (req.headers.range) {
const [rangeStart, rangeEnd] = parseHttpReqRange(req.headers.range, file.stat.size);
if (isNaN(rangeStart)) {
rsp.writeHead(416, { 'Content-Type': "text/plain; charset=UTF-8" });
rsp.end("Range Not Satisfiable");
return;
}
fileStream = fs.createReadStream(file.relPath, { start: rangeStart, end: rangeEnd });
rsp.writeHead(206, {
'Content-Type': 'application/octet-stream',
'Content-Range': `bytes ${rangeStart}-${rangeEnd}/${file.stat.size}`,
'Accept-Ranges': 'bytes',
'Content-Length': `${rangeEnd - rangeStart + 1}`,
});
console.info(`download: streaming ${file.relPath} range ${rangeStart}-${rangeEnd} to downloader...`);
} else {
fileStream = fs.createReadStream(file.relPath);
rsp.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Length': file.stat.size,
});
console.info(`download: streaming ${file.relPath} to downloader...`);
}
fileStream.pipe(rsp);
// rsp.end() will be called when fileStream emits "end" event
fileStream.on('error', (err) => {
console.error(`download: error occurs during piping ${file.relPath} to http response: ${err.message}`);
fileStream.close();
rsp.end();
});
fileStream.on('close', () => {
console.info(`download: streaming ${file.relPath} to downloader finished`);
});
} else if (file.stat.isSymbolicLink()) {
rsp.writeHead(200, { 'Content-Type': "text/html; charset=UTF-8" });
rsp.end(file.linkStr, 'utf-8');
} else {
rsp.writeHead(403, { 'Content-Type': "text/plain; charset=UTF-8" });
rsp.end(`${file.relPath} is neither directory nor regular file.`);
return;
}
} else if (req.method.toLowerCase() == "post") {
// "POST" is used for file uploading
if (!file.stat.isDirectory()) {
rsp.writeHead(403, { 'Content-Type': "text/plain; charset=UTF-8" });
rsp.end(`File can only be uploaded to a directory, but ${file.relPath} is not.`);
return;
}
let form = new formidable.IncomingForm({
'keepExtensions ': true,
'maxFileSize': 100 * 1024 * 1024 * 1024
});
form.encoding = 'utf-8';
// use resolved path as uploadDir, otherwise '.' will accidently trigger formidable's attack detection
form.uploadDir = path.resolve(file.relPath);
console.log(`upload: ${file.relPath}/ receiving a new upload...`);
const [err, upfile] = await new Promise((resolve) => {
form.parse(req, function (err, fields, upfile) {
resolve([err, upfile]);
});
});
if (err) {
console.error(`upload: ${file.relPath}/ failed: ${err.message}`);
rsp.writeHead(500, { 'content-type': 'text/plain; charset=UTF-8' });
rsp.end(err.message);
return;
}
if (upfile.up === undefined) {
rsp.writeHead(403, { 'content-type': 'text/plain; charset=UTF-8' });
rsp.end("No file selected yet to be uploaded");
return;
}
// rename file back to original name
const upfileTargetPath = path.normalize(path.join(file.relPath, upfile.up.originalFilename));
if (path.dirname(path.resolve(upfile.up.filepath)) != path.dirname(path.resolve(upfileTargetPath))) {
rsp.writeHead(403, { 'Content-Type': "text/plain; charset=UTF-8" });
rsp.end("Not allowed to break the jail");
return;
}
await new Promise((resolve) => {
fs.rename(upfile.up.filepath, upfileTargetPath, (err) => {
resolve(null);
})
});
rsp.writeHead(200, { 'content-type': 'text/plain; charset=UTF-8' });
rsp.end(`${upfileTargetPath} uploaded.\nAll glory be to https://github.com/node-formidable/formidable`);
console.log(`upload: finished ${JSON.stringify(upfile, null, 2)}`);
} else {
rsp.writeHead(403);
rsp.end();
return;
}
});
httpServer.on('upgrade', async function (req, socket) {
if (req.headers['upgrade'] !== 'websocket') {
socket.end("HTTP/1.1 403 Forbidden\r\n\r\n");
return;
}
const [file, errRsp] = await parseUriToFileStat(req.url);
if (errRsp || !file.stat.isFile()) {
socket.end(`HTTP/1.1 403 Forbidden\r\n\r\n`);
return;
}
// Sec-WebSocket-Accept is calculated and responsed for proving this is not a cache
const secWebsocketAccept = require('crypto')
.createHash('SHA1')
.update(req.headers['sec-websocket-key'] + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
.digest('base64');
socket.write(
"HTTP/1.1 101 Web Socket Protocol Handshake\r\n" +
"Upgrade: WebSocket\r\n" +
"Connection: Upgrade\r\n" +
"Sec-WebSocket-Accept: " + secWebsocketAccept + "\r\n" +
"\r\n");
// websocket is ready, now wrap file updating data into websocket frames to send to client.
const tail = require('child_process').spawn("tail", ["-f", file.relPath]);
console.log(`launch tail -f ${file.relPath} process ${tail.pid} for websocket connection`);
tail.on('error', () => {
console.log(`close websocket for tail -f ${file.relPath} process ${tail.pid} failed to spawn`);
socket.end();
});
tail.on('exit', () => {
tail.exited = true;
if (!socket.destroyed) {
console.log(`close websocket for lost tail -f ${file.relPath} process ${tail.pid}`);
socket.end();
}
});
socket.on('data', () => {
// have to register 'data' event for 'end' event to work.
// we will not parse any incoming frames from client.
});
socket.on('end', () => {
socket.destroy();
if (!tail.exited) {
console.log(`kill tail -f ${file.relPath} process ${tail.pid} for closed websocket`);
tail.kill();
}
});
tail.stdout.on('data', (stdoutChunk) => {
// websocket frame: https://www.rfc-editor.org/rfc/rfc6455
let buffer = null;
let payloadOffset = 0;
if (stdoutChunk.length < 126) {
buffer = Buffer.alloc(2);
buffer.writeUInt8(stdoutChunk.length, 1);
payloadOffset = 2;
} else if (stdoutChunk.length <= 65536) {
buffer = Buffer.alloc(2 + 2);
buffer.writeUInt8(126, 1);
buffer.writeUInt16BE(stdoutChunk.length, 2);
payloadOffset = 4;
} else {
buffer = Buffer.alloc(2 + 8);
buffer.writeUInt8(127, 1);
buffer.writeBigUint64BE(stdoutChunk.length, 2);
payloadOffset = 10;
}
// Write out the first byte, using opcode `2` to indicate that the message
// payload is binary(although most of the time it is pure text)
buffer.writeUInt8(0b10000010, 0);
buffer = Buffer.concat([buffer, stdoutChunk]);
// might return false indicating underlying buffer is full, but it's ok to lose some frames
socket.write(buffer);
});
});
console.log(`starting http server at ${BIND_IP}:${BIND_PORT}...`);
httpServer.listen(BIND_PORT, BIND_IP);