-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
360 lines (286 loc) · 12.3 KB
/
index.js
File metadata and controls
360 lines (286 loc) · 12.3 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
const fs = require("fs");
const express = require("express");
const cookieParser = require('cookie-parser');
const app = express();
const crypto = require("crypto");
const sqlite3 = require("better-sqlite3");
const config = require("./config.js");
const path = require("path");
if (!fs.existsSync(config.data_location)) {
fs.mkdirSync(config.data_location);
}
const db = new sqlite3(path.join(config.data_location, "meta.sqlite"));
db.pragma('journal_mode = WAL');
db.prepare(`CREATE TABLE IF NOT EXISTS stashes (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
access_value TEXT NOT NULL,
session TEXT NOT NULL,
expires BIGINT
)`).run();
db.prepare(`CREATE TABLE IF NOT EXISTS files (
id TEXT PRIMARY KEY,
stash_id TEXT NOT NULL,
name_type TEXT NOT NULL,
name_type_iv VARCHAR(32) NOT NULL,
data_iv VARCHAR(32) NOT NULL
)`).run();
if (config.server_port_tcp && config.server_port_tcp.enabled) {
if (config.server_tls && config.server_tls.enabled) {
require("https").createServer({
key: fs.readFileSync(config.server_tls.private_key),
cert: fs.readFileSync(config.server_tls.certificate)
}, app).listen(config.server_port_tcp.port, config.server_port_tcp.host, () => {
console.log(`HTTPS server is listening on port ${config.server_port_tcp.port}`)
});
} else {
require("http").createServer(app).listen(config.server_port_tcp.port, config.server_port_tcp.host, () => {
console.log(`HTTP server is listening on port ${config.server_port_tcp.port}`)
});
}
}
else if (config.server_unix_socket_tcp && config.server_unix_socket_tcp.enabled) {
if (fs.existsSync(config.server_unix_socket_tcp.path)) fs.rmSync(config.server_unix_socket_tcp.path);
app.listen(config.server_unix_socket_tcp.path, (err) => {
if (err) throw err;
fs.chmodSync(config.server_unix_socket_tcp.path, config.server_unix_socket_tcp.permissions);
console.log(`Unix socket HTTP server is listening on path '${config.server_unix_socket_tcp.path}'`);
});
}
else throw new Error("Both unix socket and HTTP server options cannot be disabled!");
app.use(cookieParser(config.cookie_secret), express.json({ strict: true }));
const stashDirectoryPath = path.join(config.data_location, "stashes");
if (!fs.existsSync(stashDirectoryPath)) {
fs.mkdirSync(stashDirectoryPath);
}
const accounts = config.reverse_proxy_auth_mode === true ? null
: Object.entries(config.admin_accounts).map(e => ({
username: e[0],
password: e[1],
token: crypto.randomBytes(150).toString("base64")
}));
const uploadSessions = {};
// todo - add caching
function fetchStash(id) {
return db.prepare("SELECT * FROM stashes WHERE id=?").get(id);
}
function deleteStash(id) {
fs.rmSync(path.join(stashDirectoryPath, id), { recursive: true, force: true });
db.prepare("DELETE FROM stashes WHERE id=?").run(id);
db.prepare("DELETE FROM files WHERE stash_id=?").run(id);
const sessions = Object.entries(uploadSessions);
for (let i = 0; i < sessions.length; i++) {
const [id, session] = sessions[i];
if (session.stash_id === id) {
delete uploadSessions[i];
}
}
}
// Upload session clearing.
setInterval(() => {
const sessions = Object.entries(uploadSessions);
for (let i = 0; i < sessions.length; i++) {
const [id, data] = sessions[i];
if (Date.now() >= data.expires) {
delete uploadSessions[id];
}
}
}, 20000);
// Stash clearing.
setInterval(() => {
const stashes = db.prepare("SELECT id FROM stashes WHERE expires NOT NULL AND ? >= expires").all(Date.now());
for (let i = 0; i < stashes.length; i++) {
deleteStash(stashes[i].id);
}
}, 60000);
async function auth(req, res, next) {
if (config.reverse_proxy_auth_mode === true) {
if (!!req.headers["x-webauth-proxied"]) return next();
else return res.status(401).send();
}
if (req.signedCookies["auth"]) {
const acc = accounts.find(a => a.token === req.signedCookies["auth"]);
if (acc) {
req.auth = acc;
return next();
} else res.clearCookie("auth");
}
if (req.method !== "GET") return res.status(401).send();
res.sendFile(__dirname + "/web-contextual/auth.html");
}
async function stashAuth(req, res, next) {
const id = req.params["stash_id"];
let stash = fetchStash(id);
if (stash && stash.expires && Date.now() > stash.expires) {
deleteStash(id);
stash = null;
}
if (req.method === "GET") {
if (!stash) {
res.clearCookie("stash-access");
return res.redirect(`/stash/${id}`);
}
// todo - redirect to stash sign in page
const cookie = req.signedCookies["stash-access"];
if (!cookie) return res.redirect(`/stash/${id}`);
if (cookie !== stash.session) {
res.clearCookie("stash-access");
return res.redirect(`/stash/${id}`);
}
} else {
if (!stash) return res.status(404).send();
const header = req.headers["x-stash-access"];
if (!header || header !== stash.session) return res.status(401).send();
}
req.stash = stash;
next();
}
app.get("/stash/new", auth, (req, res) => {
res.sendFile(__dirname + "/web/new.html");
});
app.get("/auth/popup", (req, res) => {
return res.sendFile(__dirname + "/web-contextual/auth-popup.html");
});
app.get("/stash/:stash_id", (req, res) => {
const stash = fetchStash(req.params["stash_id"]);
if (stash) {
const cookie = req.signedCookies["stash-access"];
if (cookie && stash.session === cookie) return res.redirect(`/stash/${stash.id}/view`);
res.status(200).sendFile(__dirname + "/web/stash-login.html");
} else res.status(404).sendFile(__dirname + "/web-contextual/stash-404.html");
});
if (config.reverse_proxy_auth_mode === false) {
let accountAuthAttempts = 0;
app.post("/api/auth", (req, res) => {
if (!req.body.username || !req.body.password) return res.status(400).send();
if (accountAuthAttempts > 10) return res.status(429).send();
accountAuthAttempts++;
const acc = accounts.find(a => a.username === req.body.username);
if (!acc || acc.password !== req.body.password) return res.status(401).send();
res.status(201).cookie("auth", acc.token, { signed: true, maxAge: 6.048e+8, secure: true }).send();
});
setInterval(() => {
accountAuthAttempts = 0;
}, 120000);
}
app.get("/api/is_rp_auth", (req, res) => {
res.status(200).send(config.reverse_proxy_auth_mode === true ? "1" : "0");
});
app.post("/api/admin/stash/create", auth, (req, res) => {
if (!req.body.access_value || typeof req.body.access_value !== "string" || req.body.access_value.length > 40 || req.body.access_value.length < 10) {
return res.status(400).send();
}
if (!req.body.name || typeof req.body.name !== "string" || req.body.name.length > 50 || req.body.name.trim().length < 3 || !(/^[0-9a-zA-Z\s]+$/).test(req.body.name)) {
return res.status(400).send();
}
req.body.name = req.body.name.trim();
if (req.body.date && (typeof req.body.date !== "string" || isNaN(req.body.date))) {
return res.status(400).send();
}
const data = {
id: crypto.randomUUID(),
name: req.body.name,
access_value: req.body.access_value,
expires: req.body.date ? parseInt(req.body.date) : null,
session: crypto.randomBytes(100).toString("base64")
};
db.prepare("INSERT INTO stashes(id,name,access_value,expires,session) VALUES(?,?,?,?,?)").run(data.id, data.name, data.access_value, data.expires, data.session);
fs.mkdirSync(path.join(stashDirectoryPath, data.id));
res.status(200).json({ id: data.id, session: data.session });
});
app.post("/api/admin/stash/:stash_id/upload", auth, stashAuth, (req, res) => {
const id = req.params["stash_id"];
if (!req.body.nameType || typeof req.body.nameType !== "object" || typeof req.body.nameType.iv !== "string" || req.body.nameType.iv.length !== 32 || typeof req.body.nameType.ciphertext !== "string") {
return res.status(400).send();
}
if (!req.body.data || typeof req.body.data !== "object" || typeof req.body.data.iv !== "string" || req.body.data.iv.length !== 32 || typeof req.body.data.size !== "number" || req.body.data.size < 0) {
return res.status(400).send();
}
const sid = crypto.randomUUID();
uploadSessions[sid] = req.body;
uploadSessions[sid].expires = Date.now() + 30000;
uploadSessions[sid].stash_id = id;
res.status(202).json({ session_id: sid });
});
app.put("/api/admin/stash/:stash_id/upload/:sid", auth, stashAuth, (req, res) => {
const id = req.params["stash_id"];
const sid = req.params["sid"];
const session = uploadSessions[sid];
if (!session || session.stash_id !== id) return res.status(404).send();
session.expires = Infinity;
const fileLocation = path.join(stashDirectoryPath, id, sid);
const writeStream = fs.createWriteStream(fileLocation, { encoding: "binary" });
let totalWritten = 0;
req.on("data", chunk => {
totalWritten += chunk.byteLength;
if (totalWritten > session.data.size) {
writeStream.end();
res.status(400).send();
req.socket.destroy();
fs.rmSync(fileLocation);
delete uploadSessions[sid];
return;
}
writeStream.write(chunk);
});
req.on("end", () => {
if (req.socket.destroyed) return;
writeStream.end();
if (totalWritten !== session.data.size) {
res.status(400).send();
fs.rmSync(fileLocation);
delete uploadSessions[sid];
return;
}
db.prepare("INSERT INTO files(id,stash_id,name_type,name_type_iv,data_iv) VALUES(?,?,?,?,?)").run(sid, id, session.nameType.ciphertext, session.nameType.iv, session.data.iv);
delete uploadSessions[sid];
res.status(201).send();
});
});
app.get("/api/stash/:stash_id/files", stashAuth, (req, res) => {
const files = db.prepare("SELECT * FROM files WHERE stash_id=?").all(req.stash.id);
res.status(200).json({ files: files.map(f => ({ id: f.id, nameType: { iv: f.name_type_iv, ciphertext: f.name_type }, data: { iv: f.data_iv } })) });
});
app.delete("/api/admin/stash/:stash_id", auth, stashAuth, (req, res) => {
deleteStash(req.stash.id);
res.status(201).send();
});
let stashAuthAttempts = 0;
app.post("/api/stash/:stash_id/auth", (req, res) => {
if (!req.body.access_value || typeof req.body.access_value !== "string") return res.status(400).send();
if (stashAuthAttempts > 15) return res.status(429).send();
stashAuthAttempts++;
const stash = fetchStash(req.params["stash_id"]);
if (!stash) return res.status(404).send();
if (stash.access_value !== req.body.access_value) return res.status(401).send();
res.status(200)
.cookie("stash-access", stash.session, { signed: true, secure: true, path: `/stash/${stash.id}` })
.cookie("stash-access", stash.session, { signed: true, secure: true, path: `/api/stash/${stash.id}` })
.json({ session: stash.session });
});
setInterval(() => {
stashAuthAttempts = 0;
}, 60000);
app.get("/api/stash/:stash_id/info", stashAuth, (req, res) => {
return res.status(200).json({ id: req.stash.id, name: req.stash.name, expires: req.stash.expires });
});
app.get("/stash/:stash_id/view", stashAuth, (req, res) => {
res.sendFile(__dirname + "/web/stash-view.html")
});
app.get("/", (req, res) => {
res.sendFile(__dirname + "/web/index.html")
});
app.get("/favicon.ico", (req, res) => {
res.sendFile(__dirname + "/web/favicon.ico");
});
app.get("/api/stash/:stash_id/files/:file_id/download", stashAuth, (req, res) => {
const fileID = req.params["file_id"];
const fileData = db.prepare("SELECT id FROM files WHERE stash_id=? AND id=?").get(req.stash.id, fileID);
if (!fileData) return res.status(404).send();
const fileStat = fs.statSync(path.join(stashDirectoryPath, req.stash.id, fileID));
res.setHeader('Content-Type', 'application/octet-stream')
.setHeader('Content-Length', fileStat.size)
.status(200);
const fileStream = fs.createReadStream(path.join(stashDirectoryPath, req.stash.id, fileID));
fileStream.pipe(res);
});