-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.html
810 lines (738 loc) · 32.6 KB
/
index.html
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
<!--
'use strict';
var fs = require('fs');
var os = require('os');
var url = require('url');
var exec = require('child_process').exec;
var pathLib = require('path');
var nativePathSep = pathLib.sep;
// http://stackoverflow.com/a/38897674
function fileSizeSI(size) {
var e = (Math.log(size) / Math.log(1e3)) | 0;
return Math.floor(size / Math.pow(1e3, e)) + ('kMGTPEZY'[e - 1] || '') + 'B';
}
// http://stackoverflow.com/a/6234804
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
// http://stackoverflow.com/a/9756789
function quoteattr(s) {
return escapeHtml(s).replace(/\r\n/g, '\n').replace(/[\r\n]/g, '\n');
}
// https://gist.github.com/ebraminio/a2e4338ce29d71156ea1703f5aaec4b6
var contentTypes = {
html: 'text/html', htm: 'text/html', shtml: 'text/html', css: 'text/css',
xml: 'text/xml', gif: 'image/gif', jpeg: 'image/jpeg', jpg: 'image/jpeg',
js: 'application/javascript', atom: 'application/atom+xml',
rss: 'application/rss+xml', mml: 'text/mathml', txt: 'text/plain',
jad: 'text/vnd.sun.j2me.app-descriptor', wml: 'text/vnd.wap.wml',
htc: 'text/x-component', png: 'image/png', tif: 'image/tiff',
tiff: 'image/tiff', wbmp: 'image/vnd.wap.wbmp', ico: 'image/x-icon',
jng: 'image/x-jng', bmp: 'image/x-ms-bmp', svg: 'image/svg+xml',
svgz: 'image/svg+xml', webp: 'image/webp', md: 'text/markdown',
woff: 'application/font-woff', jar: 'application/java-archive',
war: 'application/java-archive', ear: 'application/java-archive',
json: 'application/json', hqx: 'application/mac-binhex40',
doc: 'application/msword', pdf: 'application/pdf',
ps: 'application/postscript', eps: 'application/postscript',
ai: 'application/postscript', rtf: 'application/rtf',
m3u8: 'application/vnd.apple.mpegurl', xls: 'application/vnd.ms-excel',
eot: 'application/vnd.ms-fontobject', ppt: 'application/vnd.ms-powerpoint',
wmlc: 'application/vnd.wap.wmlc', kml: 'application/vnd.google-earth.kml+xml',
kmz: 'application/vnd.google-earth.kmz', '7z': 'application/x-7z-compressed',
cco: 'application/x-cocoa', jardiff: 'application/x-java-archive-diff',
jnlp: 'application/x-java-jnlp-file', run: 'application/x-makeself',
pl: 'application/x-perl', pm: 'application/x-perl', prc: 'application/x-pilot',
pdb: 'application/x-pilot', rar: 'application/x-rar-compressed',
rpm: 'application/x-redhat-package-manager', sea: 'application/x-sea',
swf: 'application/x-shockwave-flash', sit: 'application/x-stuffit',
tcl: 'application/x-tcl', tk: 'application/x-tcl',
der: 'application/x-x509-ca-cert', pem: 'application/x-x509-ca-cert',
crt: 'application/x-x509-ca-cert', xpi: 'application/x-xpinstall',
xhtml: 'application/xhtml+xml', xspf: 'application/xspf+xml',
zip: 'application/zip', bin: 'application/octet-stream',
exe: 'application/octet-stream', dll: 'application/octet-stream',
deb: 'application/x-debian-package', dmg: 'application/octet-stream',
iso: 'application/octet-stream', img: 'application/octet-stream',
msi: 'application/octet-stream', msp: 'application/octet-stream',
msm: 'application/octet-stream', wasm: 'application/wasm',
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
mid: 'audio/midi', midi: 'audio/midi', kar: 'audio/midi', mp3: 'audio/mpeg',
ogg: 'audio/ogg', m4a: 'audio/x-m4a',
ra: 'audio/x-realaudio', '3gpp': 'video/3gpp', '3gp': 'video/3gpp',
ts: 'video/mp2t', mp4: 'video/mp4', mpeg: 'video/mpeg', mpg: 'video/mpeg',
mov: 'video/quicktime', webm: 'video/webm', flv: 'video/x-flv',
m4v: 'video/x-m4v', mng: 'video/x-mng', asx: 'video/x-ms-asf',
asf: 'video/x-ms-asf', wmv: 'video/x-ms-wmv', avi: 'video/x-msvideo',
mkv: 'video/x-matroska', mime: 'www/mime',
tar: 'application/x-tar', tgz: 'application/x-tar-gz', gz: 'application/x-gzip'
};
var argv = process.argv;
try {
var config = require('./config.json');
argv = argv.concat(config.argv || []);
} catch (e) {}
function hasOption(option) {
return argv.indexOf(option) !== -1;
}
function getOption(option) {
var arg = argv.filter(function (arg) {
return arg.indexOf(option + '=') === 0;
})[0];
return arg ? arg.split('=')[1] : false;
}
var port = +process.env.PORT || +argv[2] || 9090;
var localOnly = hasOption('--local');
var allowDelete = hasOption('--delete');
var allowRename = hasOption('--rename');
var noCors = hasOption('--no-cors');
var noInfo = hasOption('--no-info');
var noMime = hasOption('--no-mime');
var noUpload = hasOption('--no-upload');
var noIndex = hasOption('--no-index');
var https = hasOption('--https');
var cmd = hasOption('--cmd');
var noCrossOriginIsolation = hasOption('--no-cross-origin-isolation');
var followOutsideLinks = hasOption('--follow-outside-links');
// 'wx' disallows file replace
var writeFlag = hasOption('--replace') ? 'w' : 'wx';
var userpass = getOption('--userpass');
// early convert the given username and password pair to base64
if (userpass) userpass = new Buffer(userpass).toString('base64');
var prefix = getOption('--prefix');
if (prefix && prefix[0] !== '/')
prefix = '/' + prefix;
var servedir = getOption('--servedir');
var timeout = +getOption('--timeout') || 10;
if (hasOption('-h') || hasOption('--help')) {
console.log([
'Usage: pad [PORT] [OPTION]...',
'',
'Options:',
' --local run only on localhost',
' --no-cors don\'t allow Cross-Origin Resource Sharing (CORS)',
' --no-info hide extra info pad.js gives',
' --no-mime don\'t add dangerous mime, for more safety',
' --no-upload disable uploads',
' --no-index disable index creation for folders',
' --delete allow file deletion',
' --rename allow file rename',
' --replace allow file replace',
' --cmd enable executing commands',
' --https enable https',
' -h, --help display this help and exit',
'',
' --userpass=user:pass run pad.js instance password protected',
' --prefix=/prefix expect pad.js to have the specified prefix',
' --servedir=/files serve pad.js from the specified directory',
' --timeout=10 minutes to exit if no request is issued,',
' default is 10, -1 to disable',
' --follow-outside-links add this if you like the tool follow a',
' filesystem link outside the working directory',
' and it allows creating nested files',
'',
'The PORT argument is an integer and optional, its default value is 9090',
'and can be set from environment variables also'
].join('\n'));
process.exit();
}
if (servedir) {
process.chdir(servedir);
}
var cwd = process.cwd();
if (!cwd.endsWith(nativePathSep)) cwd = cwd + nativePathSep;
var emptyBuffer = Buffer.alloc(0);
var lastActivityTimestamp = Date.now();
if (timeout !== -1) {
setInterval(function () {
if (Date.now() - lastActivityTimestamp > timeout * 60 * 1000) {
console.log('pad.js exited due to inactivity, set --timeout=-1 to disable this feature.');
process.exit();
}
}, 60 * 1000);
}
var server = (https ? function (requestListener) {
return require('https').createServer({
key: fs.readFileSync('/etc/ssl/pad.js/pad.js.key'),
cert: fs.readFileSync('/etc/ssl/pad.js/pad.js.crt')
}, requestListener);
} : require('http').createServer)(function (req, res) {
lastActivityTimestamp = Date.now();
console.log(req.connection.remoteAddress + ' ' + req.method + ' ' + req.url);
if (userpass && ((req.headers.authorization || '').split(' ')[1] || '') !== userpass) {
res.writeHead(401, { 'WWW-Authenticate': 'Basic realm="nope"' });
return res.end('HTTP Error 401 Unauthorized: Access is denied');
}
if (noCors && req.headers.origin &&
url.parse(req.headers.origin).host !== req.headers.host) {
res.writeHead(403);
return res.end('HTTP Error 403 CORS is not allowed');
}
if (noUpload && ['POST', 'PUT'].indexOf(req.method) !== -1) {
res.writeHead(405);
return res.end('HTTP Error 405 Method Not Allowed');
}
var path = req.url.split('?')[0];
try {
path = decodeURI(path);
} catch (e) {
res.writeHead(400);
return res.end('HTTP Error 400 Bad Request, error on parsing url');
}
if (prefix) {
if (path.indexOf(prefix) === 0) {
path = path.replace(prefix, '');
} else {
res.writeHead(302, { 'Location': prefix + req.url });
return res.end();
}
}
if (cmd) {
if (path.indexOf('/@cmd') === 0) {
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
return exec(decodeURI(req.url).split('/@cmd/')[1] + ' 2>&1').stdout.pipe(res);
}
}
path = path.replace(/^[\/\\]+/, '').replace(/(^|[\/\\])\.\.(?=[\/\\]|$)/g, '');
if (allowRename && req.method === 'PATCH') {
var from = path.split('@')[0];
var to = path.split('@')[1];
try {
if ((fs.realpathSync(from.replace(/[^\/\\]+$/, '')) + '/').indexOf(cwd) === 0 &&
(fs.realpathSync(to.replace(/[^\/\\]+$/, '')) + '/').indexOf(cwd) === 0) {
return fs.rename(cwd + from, cwd + to, function () {
return res.end();
});
} else {
res.writeHead(500);
return res.end('HTTP Error 500 Internal Error, error on move');
}
} catch (e) {
res.writeHead(500);
return res.end('HTTP Error 500 Internal Error, exception on move, ' + e);
}
}
try {
if (!followOutsideLinks && (fs.realpathSync(path.replace(/[^\/\\]+$/, '')) + nativePathSep).indexOf(cwd) !== 0) {
res.writeHead(404);
return res.end('HTTP Error 404 File not found, invalid path');
}
} catch (e) {
// a rare case
res.writeHead(404);
return res.end('HTTP Error 404 File not found, error');
}
if (allowDelete && req.method === 'DELETE') {
return fs.stat(cwd + path, function (err, stats) {
if (err) {
console.error(err.message);
return res.end('HTTP 404 File not found, invalid path to delete');
}
if (stats.isDirectory()) {
return fs.rmdir(cwd + path, function (err) {
if (err) {
res.writeHead(500);
return res.end('HTTP Error 500 ' + err.message);
}
res.end('OK');
});
}
return fs.unlink(cwd + path, function () {
if (err) {
res.writeHead(500);
return res.end('HTTP Error 500 ' + err.message);
}
res.end();
});
});
}
if (req.method === 'PUT') {
return fs.mkdir(cwd + path, function () {
return res.end();
});
}
if (req.method === 'POST') {
if (followOutsideLinks) {
try {
fs.mkdirSync(pathLib.dirname(cwd + path), { recursive: true });
} catch (e) {
return res.end('HTTP 404 Failed to create parent folder');
}
}
var writeStream = fs.createWriteStream(cwd + path, { flags: writeFlag });
var isInError = false;
writeStream.on('error', function (err) {
isInError = true;
console.error(err.message);
res.writeHead(409);
return res.end('HTTP Error 409 Conflict, a file with the same name exists');
});
var previousPreviousBuffer = emptyBuffer;
var previousBuffer = emptyBuffer;
var expectingHeader = true;
req.on('data', function (buffer) {
if (isInError) return;
if (expectingHeader) {
var index = buffer.indexOf('\r\n\r\n');
// Expecting stream to have enough buffer size to have '\r\n\r\n' in its first packet
if (index === -1) {
isInError = true;
console.error('Invalid request stream');
res.writeHead(400);
return res.end('HTTP Error 400 Bad request. Invalid stream format');
}
buffer = buffer.slice(index + 4);
expectingHeader = false;
}
writeStream.write(previousPreviousBuffer);
previousPreviousBuffer = previousBuffer;
previousBuffer = buffer;
});
return req.on('end', function () {
if (isInError) return;
var buffer = Buffer.concat([previousPreviousBuffer, previousBuffer]);
var end = buffer.lastIndexOf('\r\n-');
writeStream.write(buffer.slice(0, end));
writeStream.end();
console.log('"' + path + '" SAVED!');
res.writeHead(200, {
'Content-Type' : 'text/plain',
'Access-Control-Allow-Origin': '*'
});
return res.end('UPLOADED');
});
}
return fs.stat(cwd + path, function (err, stats) {
if (err) {
console.error('"' + path + '" was requested but not found.');
res.writeHead(404);
return res.end('HTTP 404 File not found');
}
if (stats.isSymbolicLink()) {
console.error('"' + path + '" a symlink was requested but got denied.');
res.writeHead(403);
return res.end('HTTP 403 Serving symlink is forbidden, at least for now');
}
if (!stats.isDirectory()) { // i.e. is a file
var contentType = contentTypes[path.replace(/^.*\.(.*)$/, "$1")];
if (contentType === undefined) // fallback mime
contentType = 'application/octet-stream';
if (contentType.indexOf('text/') === 0 ||
contentType === 'application/json')
contentType = contentType + '; charset=utf-8';
if (noMime && !/^(audio|video)\//.test(contentType))
contentType = 'application/octet-stream';
if (!noCrossOriginIsolation) {
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
}
// http://stackoverflow.com/a/28972079
if (req.headers.range) {
var parts = req.headers.range.replace(/bytes=/, "").split("-");
var start = +parts[0];
var end = parts[1] ? +parts[1] : stats.size - 1;
console.log('"' + path + '" SERVED! RANGE: ' + start + ' - ' + end);
res.writeHead(206, {
'Content-Range': 'bytes ' + start + '-' + end + '/' + stats.size,
'Accept-Ranges': 'bytes',
'Content-Length': (end - start) + 1,
'Content-Type': contentType,
'Access-Control-Allow-Origin': '*'
});
return fs.createReadStream(path, { start: start, end: end }).pipe(res);
}
console.log('"' + path + '" SERVED!');
res.writeHead(200, {
'Accept-Ranges': 'bytes',
'Content-Length': stats.size,
'Content-Type': contentType,
'Access-Control-Allow-Origin': '*'
});
return fs.createReadStream(path).pipe(res);
}
// so is a directory, create and serve directory index page
if (!req.url.match(/\/$/)) {
res.writeHead(302, { 'Location': req.url + '/' });
return res.end();
}
if (noIndex) {
res.writeHead(403);
console.error('"' + path + '" index was requested but rejected due to configs.');
return res.end('Access is denied');
}
return fs.readdir(cwd + path, function (err, dir) {
return res.end([
'<!DOCTYPE html>',
'<html>',
'<head>',
' <meta charset="utf-8">',
' <meta name="viewport" content="width=device-width, initial-scale=1">',
' <meta name="color-scheme" content="light dark">',
' <link rel="icon" href="data:,">',
' <style>',
'td:nth-child(3), th:nth-child(3) { text-align: right; }',
'th, td { padding: 0 14px 0 0; } th { padding-bottom: 3px; }',
'a, a:active { text-decoration: none; }',
'a:hover, a:focus { text-decoration: underline; color: red; }',
'body { background-color: #F5F5F5; }',
'#main, #over { background-color: white; }',
// Used instead of light-dark for better backward compatibility
'@media (prefers-color-scheme: dark) {',
' body, #main { background-color: transparent; }',
' #over { background-color: black; }',
'}',
'@media (max-width: 800px) {',
' table, tbody, tr { display: block; }',
' td:nth-child(2), td:nth-child(4), thead { display: none; }',
' td:nth-child(1), td:nth-child(3) { display: inline; }',
' td:nth-child(3) { float: right; }',
' input[type="file"] { font-size: 90%; }',
'}',
' </style>',
' <title>Index of ' + escapeHtml('/' + path) + '</title>',
'</head>',
'<body>',
(noUpload ? '' : ' <div style="float: right">' +
'<a onclick="createFolder(); return false;" href="#" title="Create a folder">📁</a> ' +
'<a onclick="noteSubmit(); return false;" href="#" title="Create a note">📝</a> ' +
'<form method="post" enctype="multipart/form-data" style="display: inline">' +
'<input type="file" name="fileToUpload" id="fileToUpload" multiple ' +
'onchange="if (window.FormData) upload(this.files); else {' +
' var form = this.parentElement;' +
' form.action = this.value.split(\'\\\\\').slice(-1)[0];' +
' form.submit();' +
'}"></form></div>'),
' <h2 style="margin-bottom: 12px;">Index of ' +
(path === '' ? '/' : '<a href="/">./</a>') +
path.split('/').map(function (x, i, arr) {
return '<a href="/' +
quoteattr(arr.slice(0, i + 1).join('/')) + '/">' +
escapeHtml(x) + '</a>';
}).join('/') + '</h2>',
' <div id="main" style="' +
' border-top: 1px solid #646464; border-bottom: 1px solid #646464;' +
' padding-top: 10px; padding-bottom: 14px;">',
' <table ellpadding="0" cellspacing="0" style="' +
'margin-left: 12px; font: 90% monospace; text-align: left;' +
'">', ' <thead>', ' <tr>',
' <th onclick="sort(1); return false;"><a href="#">Name↓</a></a></th>',
' <th onclick="sort(2); return false;"><a href="#">Last Modified:</a></th>',
' <th onclick="sort(3); return false;"><a href="#">Size:</a></th>',
' <th onclick="sort(4); return false;"><a href="#">Type:</a></th>',
' </tr>', ' </thead>', ' <tbody>',
(path === '' ? [] : ['..']).concat(dir).map(function (x) {
var fileStats;
try {
fileStats = fs.statSync(cwd + path + x);
} catch (e) {
fileStats = { size: '', mtime: 0, isDirectory: function () { return false; } };
}
var ext = x.replace(/^.*\.(.*)$/, "$1");
return {
name: x.toString(),
ext: ext,
size: fileStats.size,
date: new Date(fileStats.mtime),
isDir: fileStats.isDirectory()
};
}).sort(function (x, y) {
return x.isDir !== y.isDir
? (x.isDir < y.isDir ? 1 : -1)
: (x.name > y.name ? 1 : -1);
}).map(function (x) {
return ' <tr>\n <td>' +
(allowRename && x.name !== '..' && path === '' ?
'<a href="#" onclick="renameFile(this); return false;"' +
' title="Rename" style="color: green; line-height: 0;">✍</a> ' : '') +
(allowDelete && x.name !== '..' ?
'<a href="#" onclick="deleteFile(this); return false;" title="Delete"' +
' style="color: red">✗</a> ' : '') +
'<a href="' + quoteattr(x.name) + (x.isDir ? '/' : '') + '"' +
(allowRename && x.name !== '..' && path === '' ?
(x.isDir ? ' ondrop="dropHandler(event);" ondragover="dragOverHandler(event);"'
: ' draggable="true" ondragstart="dragStart(event);"') :
'') + '>' +
escapeHtml(x.name) + '</a>' + (x.isDir ? '/' : '') +
'</td>\n ' +
(x.name === '..' ? '<td></td>' :
'<td title="' + x.date.getTime() + '">' +
x.date.toISOString().replace('T', ' ').split('.')[0] + '</td>') +
'\n <td title="' + x.size + '">' +
(x.isDir ? '- ' : fileSizeSI(x.size)) + '</td>' +
'\n <td>' + (x.isDir ? 'Directory' : contentTypes[x.ext] || '') +
'</td>\n </tr>';
}).join('\n'),
' </tbody></table>',
' </div>', ' <div style="' +
'font: 90% monospace; color: #787878; padding-top: 4px;' +
'">' + (noInfo ? '' : os.hostname() + ', ' + os.platform() + '/' + os.arch() +
', memory: ' + fileSizeSI(os.totalmem()) + ', ') +
' pad.js.org, ' +
(noUpload ? '' : 'supports upload by drag-and-drop and copy-and-paste, ') +
'your IP is: ' +
req.connection.remoteAddress + ' ' +
(req.headers['x-forwarded-for'] || '') + '</div>' +
(cmd && path === ''
? '<br><input placeholder="Enter your command here and press enter" ' +
'style="float: right; width: 250px" ' +
'onkeypress="if (event.which === 13) location.href += \'@cmd/\' + this.value;">'
: ''), ' <script>',
'"use strict";',
'document.addEventListener("dragover", function (e) {',
' e.stopPropagation(); e.preventDefault();',
allowRename ? ' return;' : '', // don't add overlay on the case, we have other uses
' if (!document.getElementById("over"))',
' document.body.innerHTML += "<pre id=\'over\' style=\'top: 0; right: 0; ' +
'width: 100%; height: 100%; opacity: .9; position: absolute; margin: 0; ' +
'text-align: center; padding-top: 10em;' +
'\'>…drop anything here…</pre>";',
'}, false);',
'document.addEventListener("dragleave", function (e) {',
' e.stopPropagation(); e.preventDefault();',
' if (document.getElementById("over")) document.getElementById("over").remove();',
'}, false);',
'document.addEventListener("drop", function (e) {',
' e.stopPropagation(); e.preventDefault();',
' if (document.getElementById("over")) document.getElementById("over").remove();',
' handleDataTransfer(e.dataTransfer);',
'});',
'document.body.addEventListener("paste", function (e) {',
' if (e.target.nodeName !== "INPUT")', // don't interfere with input elements
' handleDataTransfer(e.clipboardData);',
'});',
'function noteSubmit(text) {',
' text = text || prompt("Enter a note:");',
' if (!text) return;',
// fakemime as we split second part for the upload extension which is okay most of the times
' upload([new Blob([text], { type: "fakemime/txt" })]);',
'}',
'function request(callback, url, method, body, onProgress) {',
' var xhr = new XMLHttpRequest();',
' xhr.open(method, url);',
' xhr.onload = function () {',
' if (xhr.readyState === xhr.DONE) {',
' if (xhr.status !== 200) alert(xhr.responseText);',
' callback(xhr.responseText, xhr.status);',
' }',
' };',
' xhr.onerror = function (err) { alert(err); callback(err); };',
' if (xhr.upload && onProgress)',
' xhr.upload.onprogress = onProgress;',
' xhr.withCredentials = true;',
' xhr.send(body);',
'}',
'function createFolder() {',
' var name = prompt("Enter folder name:");',
' if (!name) return;',
' request(function () { location.reload(); }, encodeURIComponent(name), "PUT");',
'}',
'function deleteFile(element) {',
' var name = element.parentElement.lastElementChild.textContent;',
' request(function (msg, status) {',
' if (status === 200)',
' element.parentElement.parentElement.parentElement.removeChild(element.parentElement.parentElement);',
' }, encodeURIComponent(name), "DELETE");',
'}',
'function dragStart(ev) {',
' ev.dataTransfer.setData("text/plain", ev.target.textContent);',
'}',
'function dropHandler(ev) {',
' ev.preventDefault();',
' var source = ev.dataTransfer.getData("text");',
' var target = ev.target.textContent;',
' request(function () { location.reload(); },',
' encodeURIComponent(source) +',
' "@" + encodeURIComponent(target) + "/" + encodeURIComponent(source), "PATCH");',
'}',
'function dragOverHandler(ev) {',
' ev.preventDefault();',
' ev.dataTransfer.dropEffect = "move";',
'}',
'function renameFile(element) {',
' var entry = element.parentElement.lastElementChild;',
' entry.draggable = false;', // https://stackoverflow.com/q/10317128
' var from = entry.textContent;',
' entry.contentEditable = true;',
' entry.focus();',
' entry.onkeypress = function (e) {',
' if (e.which === 13 || e.which === 27) entry.blur();',
' if (e.which !== 13) return;',
' e.preventDefault();',
' var to = entry.textContent;',
' request(function () { location.reload(); },',
' encodeURIComponent(from) + "@" + encodeURIComponent(to), "PATCH");',
' };',
' entry.onblur = function (e) {',
' entry.contentEditable = false;',
' entry.draggable = true;',
' };',
'}',
'function handleDataTransfer(dataTransfer) {',
' if (dataTransfer.files.length) upload(dataTransfer.files);',
' else if ((dataTransfer.items || [{}])[0].kind === "string")',
' dataTransfer.items[0].getAsString(noteSubmit);',
'}',
'function upload(files) {',
' document.body.innerHTML += "<pre id=\'over\' style=\'top: 0; right: 0; ' +
'width: 100%; height: 100%; opacity: .9; position: absolute; margin: 0; ' +
'text-align: center; padding-top: 10em;' +
'\'>Uploading…<br>' +
'<div style=\'margin: 0 auto; width: 200px; background-color: #DDD\'>' +
'<div id=\'bar\' style=\'height: 30px; background-color: #4CAF50\'>' +
'</div></div></pre>";',
' var jobs = Array.prototype.slice.call(files).map(function (file) {',
' return function (callback) {',
' var name = file.name;',
' if (!name || name.split(".")[0] === "image")', // image.{png,jpeg,...}
' name = Date.now() + "." + file.type.split("/")[1];',
' var formData = new FormData();',
' formData.append("blob", file);',
' document.getElementById("over").innerHTML += "<div>" + name + "…</div>";',
' request(callback, encodeURIComponent(name), "POST", formData, function (e) {',
' document.getElementById("bar").style.width = e.loaded / e.total * 100 + "%";',
' });',
' };',
' });',
' (function reactor() {',
' (jobs.shift() || function () { location.reload(); })(reactor);',
' }());',
'}',
'var sortState = 1;',
'function sort(ord) {',
' sortState = ord === sortState ? -ord : ord;',
' var p = document.getElementsByTagName("tbody")[0];',
// http://stackoverflow.com/a/39569822
' Array.prototype.slice.call(p.children)',
' .map(function (x) { return p.removeChild(x); })',
' .sort(function (x, y) {',
' x = x.getElementsByTagName("td");',
' y = y.getElementsByTagName("td");',
' var xDir = x[3].textContent === "Directory";',
' var yDir = y[3].textContent === "Directory";',
' if (xDir !== yDir) return xDir < yDir ? 1 : -1;',
' if (x[0].textContent === "../") return -1;',
' if (y[0].textContent === "../") return 1;',
' if (ord === 2 || ord === 3)',
' return +x[ord - 1].title > +y[ord - 1].title ? sortState : -sortState;',
' return x[ord - 1].textContent > y[ord - 1].textContent ? sortState : -sortState;',
' }).forEach(function (x) { p.appendChild(x); });',
' Array.prototype.slice.call(document.getElementsByTagName("th")).forEach(function (x, i) {',
' x.firstChild.textContent = x.firstChild.textContent.replace(/.$/, i === ord - 1',
' ? (sortState < 0 ? "↑" : "↓") : ":");',
' });',
'}',
' </script>', '</body>', '</html>'
].join('\n'));
});
});
});
function listen(port) {
// https://stackoverflow.com/a/19129614
var canary = require('net').createServer();
canary.once('error', function (err) {
if (err.code === 'EADDRINUSE') {
console.log('Apparently :' + port + ' is busy, let\'s try the next port');
listen(++port);
}
});
canary.once('listening', function () {
canary.close();
server.listen(port, localOnly ? '127.0.0.1' : '0.0.0.0');
console.log('pad.js is listening on ' + port +
' port of ' + (localOnly ? 'local' : 'all') + ' network interface(s)');
var ifs = os.networkInterfaces();
// http://stackoverflow.com/a/38929214
console.log(Object.keys(ifs)
.map(function (x) {
return [x, ifs[x].filter(function (x) { return x.family === 'IPv4'; })[0]];
})
.filter(function (x) { return x[1]; })
.filter(function (x) { return !localOnly || (x[1].address === '127.0.0.1'); })
.map(function (x) { return x[0] + ': ' + (https ? 'https' : 'http') + '://' +
x[1].address + ':' + port; })
.join(' - '));
});
canary.listen(port, localOnly ? '127.0.0.1' : '0.0.0.0');
}
listen(port);
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
});
/* -->
<!DOCTYPE html>
<meta name="color-scheme" content="light dark">
<link rel="icon" href="data:,">
<title>pad.js</title>
<h1>pad.js</h1>
<h2>Total fancy node.js webserver for transferring files from/to browser console and terminal</h2>
<em><a href="https://stackoverflow.com/a/44990733">What is this?</a></em> (stackoverflow)
<h3>Setup:</h3>
<pre>curl pad.js.org | node</pre>
<p>Or:</p>
<pre>curl pad.js.org | PORT=9090 node</pre>
<p>Or without curl:</p>
<pre>wget -O- pad.js.org | node</pre>
<p>You should probably use this instead if you are using Debian/Ubuntu based Linux distribution:</p>
<pre>curl pad.js.org | nodejs</pre>
<p>And try this on Windows:</p>
<pre>powershell -Command "(iwr pad.js.org).Content | node"</pre>
<p>Or this which doesn't need curl/powershell:</p>
<pre>node -e "require('http').request({host:'pad.js.org'},function(r){var t='';r.on('data',function(c){t+=c});r.on('end',function(){eval(t)})}).end();"</pre>
<p>Or this sets it up as a Docker daemon which exposes pad.js on 7070 of all your interfaces:</p>
<pre>docker run --restart=always -v /files:/files --name pad.js -d -p 7070:9090 quay.io/ebraminio/pad.js</pre>
<p>Use this if you want to have it as a terminal tool:
<u>(please install nodejs-legacy on Debian before the installation)</u></p>
<pre>npm install -g pad.js
pad [PORT]</pre>
<h3>HTTPS:</h3>
<pre>
sudo mkdir /etc/ssl/pad.js
sudo openssl genrsa -out "/etc/ssl/pad.js/pad.js.key" 2048
sudo openssl req -new -key "/etc/ssl/pad.js/pad.js.key" -out "/etc/ssl/pad.js/pad.js.csr"
sudo openssl x509 -req -days 365 -in "/etc/ssl/pad.js/pad.js.csr" -signkey "/etc/ssl/pad.js/pad.js.key" -out "/etc/ssl/pad.js/pad.js.crt"
</pre>
Then run locally installed pad.js like:
<pre>pad --https</pre>
or: (needs <a href="https://github.com/nodejs/node/pull/13012">Node.js 8</a>)
<pre>curl pad.js.org | node - --https</pre>
or: (works only on macOS, at least for now)
<pre>curl pad.js.org | node /dev/stdin --https</pre>
<h3>Show cases:</h3>
<u>download a file from the place the server is ran from:</u>
<pre>
fetch('http://127.0.0.1:9090/a.txt')
.then(x => x.text())
.then(x => console.log('File content: ' + x));
</pre>
<u>upload a text file:</u>
<pre>
var formData = new FormData();
formData.append('blob', new Blob(['STRING YOU LIKE TO SAVE']));
fetch('http://127.0.0.1:9090/a.txt', { method: 'POST', body: formData })
.then(x => x.text())
.then(console.log);
</pre>
<u>upload a file:</u>
<pre>
// Create a canvas, make 200x200 blue rectangle on it, upload its base64 encoded binary
var canvas = document.createElement('canvas'), context = canvas.getContext('2d');
canvas.width = 200; canvas.height = 200; context.fillStyle = 'blue'; context.fillRect(0, 0, 200, 200);
canvas.toBlob(function (blob) {
var formData = new FormData();
formData.append('blob', blob);
fetch('http://127.0.0.1:9090/a.png', { method: 'POST', body: formData })
.then(x => x.text())
.then(console.log);
});
</pre>
<u>upload a file from terminal:</u>
<pre>curl -F "[email protected]" http://127.0.0.1:9090/a.png</pre>
<p><a href="https://github.com/ebraminio/pad.js">Source</a></p>
<!-- */