Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
1295 lines (1113 loc) · 51.5 KB
/
Copy pathserver.js
File metadata and controls
1295 lines (1113 loc) · 51.5 KB
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const express = require('express');
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
const { Transform } = require('stream');
const Database = require('better-sqlite3');
const { v4: uuidv4 } = require('uuid');
const multer = require('multer');
const cookieParser = require('cookie-parser');
const archiver = require('archiver');
const app = express();
const PORT = parseInt(process.env.PORT || '3000');
const DATA_DIR = process.env.DATA_DIR || '/data';
const FILES_DIR = path.join(DATA_DIR, 'files');
const CHUNKS_DIR = path.join(DATA_DIR, 'chunks');
const DB_PATH = path.join(DATA_DIR, 'safedrop.db');
const SESSION_TTL_MS = 8 * 60 * 60 * 1000; // 8 hours
[DATA_DIR, FILES_DIR, CHUNKS_DIR].forEach(d => fs.mkdirSync(d, { recursive: true }));
// ── STIG Password Generator ───────────────────────────────────────────────────
function _stigCore(upper, lower, digits, special) {
const all = upper + lower + digits + special;
const cls = c => upper.includes(c) ? 0 : lower.includes(c) ? 1 : digits.includes(c) ? 2 : 3;
let chars;
do {
chars = [
upper[crypto.randomInt(upper.length)], upper[crypto.randomInt(upper.length)],
lower[crypto.randomInt(lower.length)], lower[crypto.randomInt(lower.length)],
digits[crypto.randomInt(digits.length)], digits[crypto.randomInt(digits.length)],
special[crypto.randomInt(special.length)], special[crypto.randomInt(special.length)],
];
while (chars.length < 20) chars.push(all[crypto.randomInt(all.length)]);
for (let i = chars.length - 1; i > 0; i--) {
const j = crypto.randomInt(i + 1);
[chars[i], chars[j]] = [chars[j], chars[i]];
}
} while (chars.some((c, i) => i >= 3 && cls(c) === cls(chars[i-1]) && cls(c) === cls(chars[i-2]) && cls(c) === cls(chars[i-3])));
return chars.join('');
}
// Full STIG — standard character set
function generateStigPassword() {
return _stigCore(
'ABCDEFGHJKLMNPQRSTUVWXYZ',
'abcdefghjkmnpqrstuvwxyz',
'23456789',
'!@#$%^&*()-_=+[]{}|;:,.<>?'
);
}
// Simple STIG — no visually ambiguous chars (z Z y Y I i l L O o 0 1),
// special characters limited to ! and . only
function generateStigPasswordSimple() {
return _stigCore(
'ABCDEFGHJKMNPQRSTUVWX', // removed I L O Y Z
'abcdefghjkmnpqrstuvwx', // removed i l o y z
'23456789',
'!.'
);
}
// ── Database ─────────────────────────────────────────────────────────────────
const db = new Database(DB_PATH);
db.exec(`
PRAGMA journal_mode = WAL;
CREATE TABLE IF NOT EXISTS files (
id TEXT PRIMARY KEY,
original_name TEXT NOT NULL,
mime_type TEXT NOT NULL DEFAULT 'application/octet-stream',
size INTEGER NOT NULL DEFAULT 0,
password_hash TEXT,
password_salt TEXT,
expires_at INTEGER NOT NULL,
created_at INTEGER NOT NULL,
download_count INTEGER NOT NULL DEFAULT 0,
max_downloads INTEGER NOT NULL DEFAULT 0,
encrypted INTEGER NOT NULL DEFAULT 0,
iv TEXT,
auth_tag TEXT,
user_id TEXT
);
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
original_name TEXT NOT NULL,
mime_type TEXT NOT NULL DEFAULT 'application/octet-stream',
total_chunks INTEGER NOT NULL,
received_chunks TEXT NOT NULL DEFAULT '[]',
password TEXT,
expires_in INTEGER NOT NULL,
max_downloads INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL,
user_id TEXT
);
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
password_salt TEXT NOT NULL,
role TEXT NOT NULL DEFAULT 'user',
created_at INTEGER NOT NULL,
last_login INTEGER,
active INTEGER NOT NULL DEFAULT 1
);
CREATE TABLE IF NOT EXISTS auth_sessions (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
created_at INTEGER NOT NULL,
expires_at INTEGER NOT NULL,
ip TEXT
);
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS system_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
event_type TEXT NOT NULL,
message TEXT NOT NULL,
meta TEXT,
created_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS secrets (
token TEXT PRIMARY KEY,
content TEXT NOT NULL,
views_max INTEGER NOT NULL DEFAULT 1,
views_used INTEGER NOT NULL DEFAULT 0,
expires_at INTEGER NOT NULL,
created_at INTEGER NOT NULL,
created_by TEXT,
passphrase_hash TEXT,
passphrase_salt TEXT
);
CREATE TABLE IF NOT EXISTS transfer_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
file_id TEXT NOT NULL UNIQUE,
original_name TEXT NOT NULL,
size INTEGER NOT NULL DEFAULT 0,
encrypted INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL,
expires_at INTEGER NOT NULL,
max_downloads INTEGER NOT NULL DEFAULT 0,
download_count INTEGER NOT NULL DEFAULT 0,
uploader_ip TEXT,
user_id TEXT,
uploader TEXT,
is_guest INTEGER NOT NULL DEFAULT 0,
status TEXT NOT NULL DEFAULT 'active',
deleted_at INTEGER
);
`);
// ── Seed default settings ─────────────────────────────────────────────────────
{
const DEFAULT_BW = '25600'; // 200 Mbit/s in KB/s
if (!db.prepare("SELECT value FROM settings WHERE key = 'download_limit_kbps'").get())
db.prepare("INSERT INTO settings (key, value) VALUES ('download_limit_kbps', ?)").run(DEFAULT_BW);
if (!db.prepare("SELECT value FROM settings WHERE key = 'upload_limit_kbps'").get())
db.prepare("INSERT INTO settings (key, value) VALUES ('upload_limit_kbps', ?)").run(DEFAULT_BW);
}
// Migrate: add columns if they don't exist yet
const _migrations = [
'ALTER TABLE files ADD COLUMN user_id TEXT',
'ALTER TABLE sessions ADD COLUMN user_id TEXT',
'ALTER TABLE auth_sessions ADD COLUMN is_guest INTEGER NOT NULL DEFAULT 0',
'ALTER TABLE files ADD COLUMN uploader_ip TEXT',
];
for (const m of _migrations) { try { db.exec(m); } catch (_) {} }
// Seed transfer_log from existing files (one-time migration for upgrades)
try {
db.prepare(`
INSERT OR IGNORE INTO transfer_log
(file_id, original_name, size, encrypted, created_at, expires_at,
max_downloads, download_count, uploader_ip, user_id, uploader, is_guest, status)
SELECT
f.id, f.original_name, f.size, f.encrypted, f.created_at, f.expires_at,
f.max_downloads, f.download_count, f.uploader_ip, f.user_id,
COALESCE(u.username, 'Guest'),
CASE WHEN f.user_id IS NULL THEN 1 ELSE 0 END,
CASE WHEN f.expires_at < ? THEN 'expired' ELSE 'active' END
FROM files f
LEFT JOIN users u ON u.id = f.user_id
`).run(Date.now());
} catch (_) {}
// ── Settings helpers ──────────────────────────────────────────────────────────
function getSetting(key, def = '0') {
const row = db.prepare('SELECT value FROM settings WHERE key = ?').get(key);
return row ? row.value : def;
}
function setSetting(key, value) {
db.prepare('INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)').run(key, String(value));
}
function getSettingInt(key, def = 0) {
return parseInt(getSetting(key, String(def))) || def;
}
// ── System log ────────────────────────────────────────────────────────────────
function logEvent(type, message, meta = null) {
try {
db.prepare('INSERT INTO system_log (event_type, message, meta, created_at) VALUES (?, ?, ?, ?)')
.run(type, message, meta ? JSON.stringify(meta) : null, Date.now());
} catch (_) {}
}
// ── Bandwidth throttle ────────────────────────────────────────────────────────
// Download: per-request token-bucket throttle stream
function createDownloadThrottle(kbps) {
if (!kbps || kbps <= 0) return null;
const bps = kbps * 1024;
let sent = 0;
const start = Date.now();
return new Transform({
transform(chunk, _enc, cb) {
sent += chunk.length;
const elapsed = (Date.now() - start) / 1000;
const target = sent / bps; // how many seconds this should have taken
const delay = Math.max(0, (target - elapsed) * 1000);
if (delay > 5) {
setTimeout(() => { this.push(chunk); cb(); }, delay);
} else {
this.push(chunk); cb();
}
}
});
}
// Upload: global token-bucket – delays finalization of chunk handler
const _upBucket = { tokens: 0, last: Date.now() };
function waitUploadThrottle(bytes) {
const kbps = getSettingInt('upload_limit_kbps');
if (!kbps) return Promise.resolve();
const bps = kbps * 1024;
const now = Date.now();
const elapsed = (now - _upBucket.last) / 1000;
_upBucket.tokens = Math.min(bps * 2, _upBucket.tokens + elapsed * bps);
_upBucket.last = now;
_upBucket.tokens -= bytes;
if (_upBucket.tokens < 0) {
const wait = Math.min((-_upBucket.tokens / bps) * 1000, 15000);
return new Promise(r => setTimeout(r, wait));
}
return Promise.resolve();
}
// ── Bootstrap admin user ──────────────────────────────────────────────────────
{
const count = db.prepare('SELECT COUNT(*) as c FROM users').get();
if (count.c === 0) {
const pw = 'SZ7@W57PWiv25lk8';
const salt = crypto.randomBytes(32).toString('hex');
const hash = crypto.pbkdf2Sync(pw, salt, 100000, 32, 'sha256').toString('hex');
const id = uuidv4();
db.prepare(`
INSERT INTO users (id, username, password_hash, password_salt, role, created_at, active)
VALUES (?, 'admin', ?, ?, 'admin', ?, 1)
`).run(id, hash, salt, Date.now());
console.log('');
console.log('╔══════════════════════════════════════════════╗');
console.log('║ INITIAL ADMIN USER CREATED ║');
console.log('╠══════════════════════════════════════════════╣');
console.log(`║ Username: admin ║`);
console.log(`║ Password: ${pw} ║`);
console.log('╚══════════════════════════════════════════════╝');
console.log('');
}
}
// ── Middleware ────────────────────────────────────────────────────────────────
app.use(express.json({ limit: '1mb' }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// ── Auth Middleware ───────────────────────────────────────────────────────────
function requireAuth(req, res, next) {
const token = req.cookies && req.cookies.fs_session;
if (!token) return res.status(401).json({ error: 'Not logged in' });
const session = db.prepare(`
SELECT s.*,
COALESCE(u.id, s.id) AS uid,
COALESCE(u.username, 'Guest') AS uname,
COALESCE(u.role, 'guest') AS urole,
COALESCE(u.active, 1) AS uactive
FROM auth_sessions s
LEFT JOIN users u ON u.id = s.user_id
WHERE s.id = ? AND s.expires_at > ?
`).get(token, Date.now());
if (!session) {
res.clearCookie('fs_session');
return res.status(401).json({ error: 'Session expired' });
}
if (!session.is_guest && !session.uactive)
return res.status(403).json({ error: 'Account disabled' });
req.user = {
id: session.uid,
username: session.uname,
role: session.is_guest ? 'guest' : session.urole,
isGuest: !!session.is_guest
};
next();
}
function requireAdmin(req, res, next) {
requireAuth(req, res, () => {
if (req.user.role !== 'admin') return res.status(403).json({ error: 'Access denied' });
next();
});
}
// ── Multer: chunk storage ─────────────────────────────────────────────────────
const chunkStorage = multer.diskStorage({
destination(req, _file, cb) {
const sid = (req.body.sessionId || '').replace(/[^a-zA-Z0-9\-]/g, '');
if (!sid) return cb(new Error('Missing sessionId'));
const dir = path.join(CHUNKS_DIR, sid);
fs.mkdirSync(dir, { recursive: true });
cb(null, dir);
},
filename(req, _file, cb) {
const idx = parseInt(req.body.chunkIndex ?? '-1');
if (idx < 0) return cb(new Error('Invalid chunkIndex'));
cb(null, `chunk_${idx}`);
}
});
const upload = multer({
storage: chunkStorage,
limits: { fileSize: 100 * 1024 * 1024 }
});
// ── Auth Routes ───────────────────────────────────────────────────────────────
// Login
app.post('/api/auth/login', (req, res) => {
const { username, password } = req.body;
if (!username || !password) return res.status(400).json({ error: 'Username and password required' });
const user = db.prepare('SELECT * FROM users WHERE username = ? AND active = 1').get(username);
if (!user) return res.status(401).json({ error: 'Invalid credentials' });
const hash = crypto.pbkdf2Sync(password, user.password_salt, 100000, 32, 'sha256').toString('hex');
if (hash !== user.password_hash) return res.status(401).json({ error: 'Invalid credentials' });
// Delete old sessions for this user to keep it clean
db.prepare('DELETE FROM auth_sessions WHERE user_id = ? AND expires_at < ?').run(user.id, Date.now());
const token = uuidv4();
const now = Date.now();
const expires = now + SESSION_TTL_MS;
const ip = req.ip || req.connection.remoteAddress || '';
db.prepare(`
INSERT INTO auth_sessions (id, user_id, created_at, expires_at, ip)
VALUES (?, ?, ?, ?, ?)
`).run(token, user.id, now, expires, ip);
db.prepare('UPDATE users SET last_login = ? WHERE id = ?').run(now, user.id);
res.cookie('fs_session', token, {
httpOnly: true,
sameSite: 'lax',
maxAge: SESSION_TTL_MS,
secure: process.env.NODE_ENV === 'production'
});
res.json({ ok: true, username: user.username, role: user.role });
});
// Logout
app.post('/api/auth/logout', (req, res) => {
const token = req.cookies && req.cookies.fs_session;
if (token) db.prepare('DELETE FROM auth_sessions WHERE id = ?').run(token);
res.clearCookie('fs_session');
res.json({ ok: true });
});
// Me
app.get('/api/auth/me', requireAuth, (req, res) => {
res.json({ id: req.user.id, username: req.user.username, role: req.user.role, isGuest: req.user.isGuest });
});
// Guest (anonymous) session
app.post('/api/auth/guest', (req, res) => {
const token = uuidv4();
const now = Date.now();
const GUEST_TTL = 4 * 60 * 60 * 1000; // 4 hours
const ip = req.ip || req.connection.remoteAddress || '';
db.prepare(`
INSERT INTO auth_sessions (id, user_id, created_at, expires_at, ip, is_guest)
VALUES (?, '', ?, ?, ?, 1)
`).run(token, now, now + GUEST_TTL, ip);
res.cookie('fs_session', token, {
httpOnly: true,
sameSite: 'lax',
maxAge: GUEST_TTL,
secure: process.env.NODE_ENV === 'production'
});
res.json({ ok: true });
});
// Generate STIG password (authenticated users only)
// ?type=simple → no ambiguous chars, special: ! and . only
app.get('/api/password/generate', requireAuth, (req, res) => {
const pw = req.query.type === 'simple'
? generateStigPasswordSimple()
: generateStigPassword();
res.json({ password: pw });
});
// ── Upload Routes (require Auth) ──────────────────────────────────────────────
// 1. Init upload session
app.post('/api/upload/init', requireAuth, (req, res) => {
const { filename, mimeType, totalChunks, password, expiresIn, maxDownloads } = req.body;
if (!filename || !totalChunks || expiresIn == null) {
return res.status(400).json({ error: 'filename, totalChunks and expiresIn are required' });
}
// Guest restrictions: max 4h, password mandatory
if (req.user.isGuest) {
if (!password) return res.status(400).json({ error: 'Guests must set a password (required)' });
const maxGuest = 4 * 3600;
if (parseInt(expiresIn) === 0 || parseInt(expiresIn) > maxGuest) {
return res.status(400).json({ error: 'Guests can store files for a maximum of 4 hours' });
}
}
const sessionId = uuidv4();
db.prepare(`
INSERT INTO sessions (id, original_name, mime_type, total_chunks, password, expires_in, max_downloads, created_at, user_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`).run(
sessionId,
filename,
mimeType || 'application/octet-stream',
parseInt(totalChunks),
password || null,
parseInt(expiresIn),
parseInt(maxDownloads || 0),
Date.now(),
req.user.id
);
res.json({ sessionId });
});
// 2. Upload chunk (require Auth)
app.post('/api/upload/chunk', requireAuth, upload.single('chunk'), async (req, res) => {
if (!req.file) return res.status(400).json({ error: 'No chunk received' });
const { sessionId, chunkIndex } = req.body;
const session = db.prepare('SELECT * FROM sessions WHERE id = ? AND user_id = ?').get(sessionId, req.user.id);
if (!session) return res.status(404).json({ error: 'Session not found' });
// Apply upload throttle (delays response proportionally to chunk size)
await waitUploadThrottle(req.file.size);
const received = new Set(JSON.parse(session.received_chunks));
received.add(parseInt(chunkIndex));
db.prepare('UPDATE sessions SET received_chunks = ? WHERE id = ?')
.run(JSON.stringify([...received]), sessionId);
res.json({ received: received.size, total: session.total_chunks });
});
// 3. Finalize (require Auth)
app.post('/api/upload/finalize', requireAuth, async (req, res) => {
const { sessionId } = req.body;
const session = db.prepare('SELECT * FROM sessions WHERE id = ? AND user_id = ?').get(sessionId, req.user.id);
if (!session) return res.status(404).json({ error: 'Session not found' });
const received = JSON.parse(session.received_chunks);
if (received.length < session.total_chunks) {
return res.status(400).json({
error: `Incomplete: ${received.length}/${session.total_chunks} chunks received`
});
}
const fileId = uuidv4();
const sessionDir = path.join(CHUNKS_DIR, sessionId);
const filePath = path.join(FILES_DIR, fileId);
try {
await assembleChunks(sessionDir, filePath, session.total_chunks);
const { size } = fs.statSync(filePath);
let encrypted = 0, iv = null, authTag = null, passwordHash = null, passwordSalt = null;
if (session.password) {
const enc = await encryptFile(filePath, session.password);
iv = enc.iv;
authTag = enc.authTag;
passwordSalt = enc.salt;
passwordHash = crypto.createHash('sha256')
.update(session.password + passwordSalt).digest('hex');
encrypted = 1;
}
const now = Date.now();
const uploaderIp = req.ip || req.connection.remoteAddress || null;
db.prepare(`
INSERT INTO files
(id, original_name, mime_type, size, password_hash, password_salt,
expires_at, created_at, max_downloads, encrypted, iv, auth_tag, user_id, uploader_ip)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`).run(
fileId, session.original_name, session.mime_type, size,
passwordHash, passwordSalt,
session.expires_in === 0 ? 9999999999999 : now + session.expires_in * 1000, now,
session.max_downloads, encrypted, iv, authTag,
req.user.isGuest ? null : req.user.id,
uploaderIp
);
// Mirror upload into transfer_log for persistent history
db.prepare(`
INSERT OR IGNORE INTO transfer_log
(file_id, original_name, size, encrypted, created_at, expires_at,
max_downloads, download_count, uploader_ip, user_id, uploader, is_guest, status)
VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?, ?, 'active')
`).run(
fileId, session.original_name, size, encrypted,
now,
session.expires_in === 0 ? 9999999999999 : now + session.expires_in * 1000,
session.max_downloads, uploaderIp,
req.user.isGuest ? null : req.user.id,
req.user.isGuest ? 'Guest' : req.user.username,
req.user.isGuest ? 1 : 0
);
fs.rmSync(sessionDir, { recursive: true, force: true });
db.prepare('DELETE FROM sessions WHERE id = ?').run(sessionId);
res.json({ fileId, expiresAt: now + session.expires_in * 1000 });
} catch (err) {
console.error('Finalize error:', err);
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
res.status(500).json({ error: 'Processing failed' });
}
});
// ── Download Routes (public) ──────────────────────────────────────────────────
// 4. File metadata (public)
app.get('/api/info/:id', (req, res) => {
const file = db.prepare(`
SELECT id, original_name, mime_type, size, expires_at, created_at,
download_count, max_downloads, encrypted
FROM files WHERE id = ?
`).get(req.params.id);
if (!file) return res.status(404).json({ error: 'Not found' });
if (Date.now() > file.expires_at) return res.status(410).json({ error: 'Expired' });
res.json({
id: file.id,
name: file.original_name,
size: file.size,
mimeType: file.mime_type,
expiresAt: file.expires_at,
createdAt: file.created_at,
downloads: file.download_count,
maxDownloads: file.max_downloads,
passwordProtected: file.encrypted === 1
});
});
// helper: no-cache
function noCache(res) {
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, private');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
res.setHeader('Surrogate-Control', 'no-store');
res.setHeader('CDN-Cache-Control', 'no-store');
}
// 5. Download file (public)
app.get('/api/download/:id', (req, res) => {
noCache(res);
if (req.method === 'HEAD') return res.status(200).end();
const file = db.prepare('SELECT * FROM files WHERE id = ?').get(req.params.id);
if (!file) return res.status(404).json({ error: 'Not found' });
if (Date.now() > file.expires_at) return res.status(410).json({ error: 'Expired' });
const password = req.query.password;
if (file.encrypted) {
if (!password) return res.status(401).json({ error: 'Password required' });
const hash = crypto.createHash('sha256').update(password + file.password_salt).digest('hex');
if (hash !== file.password_hash) return res.status(401).json({ error: 'Wrong password' });
}
const filePath = path.join(FILES_DIR, file.id);
if (!fs.existsSync(filePath)) return res.status(404).json({ error: 'File missing' });
const result = db.prepare(`
UPDATE files
SET download_count = download_count + 1
WHERE id = ?
AND expires_at > ?
AND (max_downloads = 0 OR download_count < max_downloads)
`).run(file.id, Date.now());
if (result.changes === 0) {
return res.status(410).json({ error: 'Download limit reached' });
}
// Keep transfer_log download count in sync
db.prepare('UPDATE transfer_log SET download_count = download_count + 1 WHERE file_id = ?').run(file.id);
res.setHeader('Content-Disposition',
`attachment; filename*=UTF-8''${encodeURIComponent(file.original_name)}`);
res.setHeader('Content-Type', file.mime_type || 'application/octet-stream');
const throttle = createDownloadThrottle(getSettingInt('download_limit_kbps'));
if (file.encrypted) {
const key = crypto.pbkdf2Sync(
password, Buffer.from(file.password_salt, 'hex'), 100000, 32, 'sha256'
);
const decipher = crypto.createDecipheriv(
'aes-256-gcm', key, Buffer.from(file.iv, 'hex')
);
decipher.setAuthTag(Buffer.from(file.auth_tag, 'hex'));
const stream = fs.createReadStream(filePath);
res.on('close', () => stream.destroy());
if (throttle) {
stream.pipe(decipher).pipe(throttle).pipe(res);
decipher.on('error', () => res.destroy());
} else {
stream.pipe(decipher).pipe(res);
decipher.on('error', () => res.destroy());
}
} else {
if (!throttle) res.setHeader('Content-Length', file.size);
const stream = fs.createReadStream(filePath);
res.on('close', () => stream.destroy());
if (throttle) {
stream.pipe(throttle).pipe(res);
} else {
stream.pipe(res);
}
}
});
// Helper: append a file entry (plain or decrypted) to an archiver instance
function appendToZip(archive, file, fp, password) {
if (file.encrypted && password) {
const key = crypto.pbkdf2Sync(
password, Buffer.from(file.password_salt, 'hex'), 100000, 32, 'sha256'
);
const decipher = crypto.createDecipheriv(
'aes-256-gcm', key, Buffer.from(file.iv, 'hex')
);
decipher.setAuthTag(Buffer.from(file.auth_tag, 'hex'));
const fileStream = fs.createReadStream(fp);
decipher.on('error', err => {
console.error('ZIP decrypt error for', file.original_name, err.message);
});
archive.append(fileStream.pipe(decipher), { name: file.original_name });
} else {
archive.file(fp, { name: file.original_name });
}
}
// 5b. Bulk ZIP download – own files (user); optional password for encrypted files
app.post('/api/my/download-zip', requireAuth, (req, res) => {
const { ids, password } = req.body;
if (!Array.isArray(ids) || ids.length === 0)
return res.status(400).json({ error: 'No file IDs provided' });
const toAdd = [], skipped = [];
for (const id of ids) {
if (typeof id !== 'string' || !/^[0-9a-f-]{36}$/.test(id)) continue;
const file = db.prepare('SELECT * FROM files WHERE id = ? AND user_id = ?')
.get(id, req.user.id);
if (!file) continue;
const fp = path.join(FILES_DIR, id);
if (!fs.existsSync(fp)) continue;
if (file.encrypted) {
if (!password) { skipped.push(file.original_name); continue; }
const hash = crypto.createHash('sha256')
.update(password + file.password_salt).digest('hex');
if (hash !== file.password_hash) { skipped.push(file.original_name); continue; }
toAdd.push({ file, fp, decrypt: true });
} else {
toAdd.push({ file, fp, decrypt: false });
}
}
if (!toAdd.length)
return res.status(400).json({ error: 'No downloadable files (wrong / missing password or not found)' });
const date = new Date().toISOString().slice(0, 10);
res.setHeader('Content-Type', 'application/zip');
res.setHeader('Content-Disposition', `attachment; filename="safedrop-${date}.zip"`);
res.setHeader('X-Skipped-Files', skipped.length.toString());
const archive = archiver('zip', { zlib: { level: 6 } });
archive.on('error', err => { console.error('ZIP error:', err); try { res.destroy(); } catch (_) {} });
archive.pipe(res);
for (const { file, fp, decrypt } of toAdd)
appendToZip(archive, file, fp, decrypt ? password : null);
archive.finalize();
});
// 5c. Bulk ZIP download – any files (admin); optional password for encrypted files
app.post('/api/admin/download-zip', requireAdmin, (req, res) => {
const { ids, password } = req.body;
if (!Array.isArray(ids) || ids.length === 0)
return res.status(400).json({ error: 'No file IDs provided' });
const toAdd = [], skipped = [];
for (const id of ids) {
if (typeof id !== 'string' || !/^[0-9a-f-]{36}$/.test(id)) continue;
const file = db.prepare('SELECT * FROM files WHERE id = ?').get(id);
if (!file) continue;
const fp = path.join(FILES_DIR, id);
if (!fs.existsSync(fp)) continue;
if (file.encrypted) {
if (!password) { skipped.push(file.original_name); continue; }
const hash = crypto.createHash('sha256')
.update(password + file.password_salt).digest('hex');
if (hash !== file.password_hash) { skipped.push(file.original_name); continue; }
toAdd.push({ file, fp, decrypt: true });
} else {
toAdd.push({ file, fp, decrypt: false });
}
}
if (!toAdd.length)
return res.status(400).json({ error: 'No downloadable files (wrong / missing password or not found)' });
const date = new Date().toISOString().slice(0, 10);
res.setHeader('Content-Type', 'application/zip');
res.setHeader('Content-Disposition', `attachment; filename="safedrop-admin-${date}.zip"`);
res.setHeader('X-Skipped-Files', skipped.length.toString());
const archive = archiver('zip', { zlib: { level: 6 } });
archive.on('error', err => { console.error('ZIP error:', err); try { res.destroy(); } catch (_) {} });
archive.pipe(res);
for (const { file, fp, decrypt } of toAdd)
appendToZip(archive, file, fp, decrypt ? password : null);
archive.finalize();
});
// 5d. Image preview (public, no download-count increment)
app.get('/api/preview/:id', (req, res) => {
noCache(res);
const file = db.prepare('SELECT * FROM files WHERE id = ?').get(req.params.id);
if (!file) return res.status(404).end();
if (Date.now() > file.expires_at) return res.status(410).end();
if (file.encrypted) return res.status(403).json({ error: 'Encrypted – preview not available' });
const mime = file.mime_type || '';
if (!mime.startsWith('image/')) return res.status(415).end();
const filePath = path.join(FILES_DIR, file.id);
if (!fs.existsSync(filePath)) return res.status(404).end();
res.setHeader('Content-Type', mime);
res.setHeader('Content-Length', file.size);
fs.createReadStream(filePath).pipe(res);
});
// 6. Verify password (public)
app.post('/api/verify/:id', (req, res) => {
const file = db.prepare('SELECT * FROM files WHERE id = ?').get(req.params.id);
if (!file) return res.status(404).json({ error: 'Not found' });
if (Date.now() > file.expires_at) return res.status(410).json({ error: 'Expired' });
if (!file.encrypted) return res.json({ ok: true });
const { password } = req.body;
if (!password) return res.status(401).json({ error: 'Password required' });
const hash = crypto.createHash('sha256').update(password + file.password_salt).digest('hex');
if (hash !== file.password_hash) return res.status(401).json({ error: 'Wrong password' });
res.json({ ok: true });
});
// ── Anonymous upload log (admin) ─────────────────────────────────────────────
// Legacy anon-log (kept for backwards compat)
app.get('/api/admin/anon-log', requireAdmin, (_req, res) => {
const rows = db.prepare(`
SELECT id, original_name, size, created_at, expires_at,
download_count, max_downloads, encrypted, uploader_ip
FROM files WHERE user_id IS NULL
ORDER BY created_at DESC LIMIT 200
`).all();
res.json(rows);
});
// All-transfers protocol — reads from persistent transfer_log (includes deleted/expired)
app.get('/api/admin/transfers', requireAdmin, (_req, res) => {
const rows = db.prepare(`
SELECT file_id AS id, original_name, size, created_at, expires_at,
download_count, max_downloads, encrypted, uploader_ip,
user_id, uploader, is_guest, status, deleted_at
FROM transfer_log
ORDER BY created_at DESC
LIMIT 500
`).all();
res.json(rows);
});
// Delete anon file (admin) — also used for transfers tab
app.delete('/api/admin/anon-log/:id', requireAdmin, (req, res) => {
const { id } = req.params;
const file = db.prepare('SELECT id, original_name FROM files WHERE id = ?').get(id);
if (!file) return res.status(404).json({ error: 'Not found' });
const fp = path.join(FILES_DIR, id);
if (fs.existsSync(fp)) fs.unlinkSync(fp);
db.prepare('UPDATE transfer_log SET status = ?, deleted_at = ? WHERE file_id = ?').run('deleted', Date.now(), id);
db.prepare('DELETE FROM files WHERE id = ?').run(id);
logEvent('file_deleted_admin', `Admin "${req.user.username}" deleted file: ${file.original_name}`, { fileId: id });
res.json({ ok: true });
});
// ── Storage breakdown (admin) ─────────────────────────────────────────────────
app.get('/api/admin/storage', requireAdmin, (_req, res) => {
const rows = db.prepare(`
SELECT COALESCE(u.username, '(deleted)') AS username,
COUNT(f.id) AS file_count,
COALESCE(SUM(f.size), 0) AS used_bytes
FROM files f
LEFT JOIN users u ON u.id = f.user_id
WHERE f.expires_at > ?
GROUP BY f.user_id
ORDER BY used_bytes DESC
LIMIT 12
`).all(Date.now());
res.json(rows);
});
// ── Settings Routes ───────────────────────────────────────────────────────────
app.get('/api/admin/settings', requireAdmin, (_req, res) => {
res.json({
download_limit_kbps: getSettingInt('download_limit_kbps'),
upload_limit_kbps: getSettingInt('upload_limit_kbps')
});
});
app.put('/api/admin/settings', requireAdmin, (req, res) => {
const dl = parseInt(req.body.download_limit_kbps) || 0;
const ul = parseInt(req.body.upload_limit_kbps) || 0;
if (dl < 0 || ul < 0) return res.status(400).json({ error: 'Negative values not allowed' });
setSetting('download_limit_kbps', dl);
setSetting('upload_limit_kbps', ul);
// reset upload bucket so new setting takes effect immediately
_upBucket.tokens = 0;
_upBucket.last = Date.now();
res.json({ ok: true, download_limit_kbps: dl, upload_limit_kbps: ul });
});
// ── My Files Route ────────────────────────────────────────────────────────────
app.get('/api/my/files', requireAuth, (req, res) => {
const files = db.prepare(`
SELECT id, original_name, mime_type, size, created_at, expires_at,
download_count, max_downloads, encrypted
FROM files
WHERE user_id = ? AND expires_at > ?
ORDER BY created_at DESC
`).all(req.user.id, Date.now());
res.json(files);
});
// ── Admin Routes ──────────────────────────────────────────────────────────────
// Stats
app.get('/api/admin/stats', requireAdmin, (req, res) => {
const totalUsers = db.prepare('SELECT COUNT(*) as c FROM users').get().c;
const totalFiles = db.prepare('SELECT COUNT(*) as c FROM files').get().c;
const activeFiles = db.prepare('SELECT COUNT(*) as c FROM files WHERE expires_at > ?').get(Date.now()).c;
const totalDownloads = db.prepare('SELECT COALESCE(SUM(download_count),0) as c FROM files').get().c;
const totalStorage = db.prepare('SELECT COALESCE(SUM(size),0) as c FROM files WHERE expires_at > ?').get(Date.now()).c;
res.json({ totalUsers, totalFiles, activeFiles, totalDownloads, totalStorage });
});
// List users
app.get('/api/admin/users', requireAdmin, (_req, res) => {
const users = db.prepare(`
SELECT id, username, role, created_at, last_login, active FROM users ORDER BY created_at DESC
`).all();
res.json(users);
});
// Create user
app.post('/api/admin/users', requireAdmin, (req, res) => {
const { username } = req.body;
if (!username || username.trim().length < 2) {
return res.status(400).json({ error: 'Username must be at least 2 characters' });
}
const trimmed = username.trim().toLowerCase();
const existing = db.prepare('SELECT id FROM users WHERE username = ?').get(trimmed);
if (existing) return res.status(409).json({ error: 'Username already taken' });
const pw = generateStigPassword();
const salt = crypto.randomBytes(32).toString('hex');
const hash = crypto.pbkdf2Sync(pw, salt, 100000, 32, 'sha256').toString('hex');
const id = uuidv4();
db.prepare(`
INSERT INTO users (id, username, password_hash, password_salt, role, created_at, active)
VALUES (?, ?, ?, ?, 'user', ?, 1)
`).run(id, trimmed, hash, salt, Date.now());
res.status(201).json({ id, username: trimmed, password: pw, role: 'user' });
});
// Update user (toggle active or reset password)
app.patch('/api/admin/users/:id', requireAdmin, (req, res) => {
const { id } = req.params;
const user = db.prepare('SELECT * FROM users WHERE id = ?').get(id);
if (!user) return res.status(404).json({ error: 'User not found' });
const { action } = req.body;
if (action === 'toggleActive') {
const newActive = user.active ? 0 : 1;
db.prepare('UPDATE users SET active = ? WHERE id = ?').run(newActive, id);
if (!newActive) {
db.prepare('DELETE FROM auth_sessions WHERE user_id = ?').run(id);
}
return res.json({ ok: true, active: newActive });
}
if (action === 'resetPassword') {
const pw = generateStigPassword();
const salt = crypto.randomBytes(32).toString('hex');
const hash = crypto.pbkdf2Sync(pw, salt, 100000, 32, 'sha256').toString('hex');
db.prepare('UPDATE users SET password_hash = ?, password_salt = ? WHERE id = ?').run(hash, salt, id);
db.prepare('DELETE FROM auth_sessions WHERE user_id = ?').run(id);
return res.json({ ok: true, password: pw });
}
if (action === 'setPassword') {
const { password } = req.body;
if (!password || password.trim().length < 8)
return res.status(400).json({ error: 'Password must be at least 8 characters.' });
const salt = crypto.randomBytes(32).toString('hex');
const hash = crypto.pbkdf2Sync(password.trim(), salt, 100000, 32, 'sha256').toString('hex');
db.prepare('UPDATE users SET password_hash = ?, password_salt = ? WHERE id = ?').run(hash, salt, id);
db.prepare('DELETE FROM auth_sessions WHERE user_id = ?').run(id);
return res.json({ ok: true });
}
res.status(400).json({ error: 'Unknown action' });
});
// Delete user
app.delete('/api/admin/users/:id', requireAdmin, (req, res) => {
const { id } = req.params;
if (req.user.id === id) return res.status(400).json({ error: 'Cannot delete own account' });
const user = db.prepare('SELECT id FROM users WHERE id = ?').get(id);
if (!user) return res.status(404).json({ error: 'User not found' });
db.prepare('DELETE FROM auth_sessions WHERE user_id = ?').run(id);
db.prepare('DELETE FROM users WHERE id = ?').run(id);
res.json({ ok: true });
});
// List all files (admin)
app.get('/api/admin/files', requireAdmin, (_req, res) => {
const files = db.prepare(`
SELECT f.id, f.original_name, f.size, f.created_at, f.expires_at,
f.download_count, f.max_downloads, f.encrypted,
u.username as uploader
FROM files f