Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
408 lines (360 loc) · 13.8 KB
/
Copy pathserver.js
File metadata and controls
408 lines (360 loc) · 13.8 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
// server.js — zero-dependency HTTP server: serves the UI, proxies mail.tm, owns the JSON DB.
import http from 'node:http';
import { promises as fs } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { spawn } from 'node:child_process';
import * as db from './lib/db.js';
import * as generate from './lib/generate.js';
import * as mail from './providers/index.js';
import * as browser from './lib/browser.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const PUBLIC_DIR = path.join(__dirname, 'public');
const PORT = Number(process.env.PORT) || 3000;
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const clamp = (n, lo, hi) => Math.max(lo, Math.min(hi, n));
const MIME = {
'.html': 'text/html; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
};
// ---- helpers ---------------------------------------------------------------
function sendJson(res, status, obj) {
const body = JSON.stringify(obj);
res.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8' });
res.end(body);
}
function readBody(req) {
return new Promise((resolve, reject) => {
let data = '';
req.on('data', (c) => {
data += c;
if (data.length > 5_000_000) req.destroy();
});
req.on('end', () => {
if (!data) return resolve({});
try {
resolve(JSON.parse(data));
} catch (e) {
reject(e);
}
});
req.on('error', reject);
});
}
async function serveStatic(res, pathname) {
const rel = pathname === '/' ? 'index.html' : pathname.replace(/^\/+/, '');
const filePath = path.join(PUBLIC_DIR, rel);
// Prevent path traversal outside /public.
if (!filePath.startsWith(PUBLIC_DIR)) {
res.writeHead(403).end('Forbidden');
return;
}
try {
const buf = await fs.readFile(filePath);
const type = MIME[path.extname(filePath)] || 'application/octet-stream';
res.writeHead(200, { 'Content-Type': type });
res.end(buf);
} catch {
res.writeHead(404, { 'Content-Type': 'text/plain' }).end('Not found');
}
}
// Why generation could not start, in the words of the provider you picked.
function noDomainMessage(provider) {
return provider === 'domain'
? 'Set your domain in Settings first (the catch-all domain your worker receives mail for).'
: 'No mail.tm domains are available right now. Try again shortly.';
}
// ---- API handlers ----------------------------------------------------------
async function handleGenerate(req, res, q) {
const count = clamp(parseInt(q.get('count')) || 100, 1, 500);
const style = q.get('localPart') || 'word';
const pwLen = parseInt(q.get('pwLen')) || 16;
const pwStyle = q.get('pwStyle') || 'strong';
const settings = await db.getSettings();
const provider = q.get('provider') || settings.provider || 'mail.tm';
const conf = { ...settings, provider };
const domain = q.get('domain') || '';
res.writeHead(200, {
'Content-Type': 'text/event-stream; charset=utf-8',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
'X-Accel-Buffering': 'no',
});
let aborted = false;
req.on('close', () => {
aborted = true;
});
const send = (event, data) => {
if (res.writableEnded) return;
res.write(`event: ${event}\n`);
res.write(`data: ${JSON.stringify(data)}\n\n`);
};
try {
// Some backends hand out the address themselves; for the rest, spread over
// every domain they offer instead of burning one, which is what gets a
// domain blocklisted in the first place.
const assigned = mail.assignsAddresses(conf);
const pool = assigned ? [] : domain ? [domain] : await mail.getDomains(conf);
if (!assigned && !pool.length) throw new Error(noDomainMessage(provider));
const throttleMs = provider === '5minmail'
? 5200 // 5minmail allows one new address every 5 seconds
: provider === 'domain'
? 0 // a catch-all domain accepts every address offline: nothing to rate-limit
: clamp(Number(settings.throttleMs) || 500, 150, 5000);
const existing = new Set((await db.read()).identities.map((i) => i.email.toLowerCase()));
let created = 0;
let failed = 0;
send('start', {
total: count,
domain: assigned ? provider : pool.length > 1 ? `${pool.length} domains` : pool[0],
provider,
});
for (let i = 0; i < count && !aborted; i++) {
const nickname = generate.randomNickname();
let identity = null;
let lastErr = null;
for (let attempt = 0; attempt < 6 && !aborted; attempt++) {
const wanted = assigned
? ''
: `${generate.localPart(style, nickname)}@${pool[(i + attempt) % pool.length]}`.toLowerCase();
if (wanted && existing.has(wanted)) continue;
const password = generate.randomPassword(pwLen, pwStyle);
try {
const made = await mail.createIdentity(conf, wanted, password);
if (existing.has(made.email)) continue;
identity = {
id: db.newId(),
email: made.email,
nickname,
password,
provider,
accountId: made.accountId,
expiresAt: made.expiresAt || null,
active: true,
createdAt: new Date().toISOString(),
};
existing.add(made.email);
break;
} catch (err) {
lastErr = err;
if (err.fatal) throw err; // bad token / rate limited -> retrying can't help
if (err.status === 422) continue; // taken/invalid -> new local part
await sleep(600 * (attempt + 1)); // transient -> back off and retry
}
}
if (identity) {
await db.addIdentity(identity);
created++;
send('progress', {
index: i + 1,
total: count,
created,
failed,
identity: {
id: identity.id,
email: identity.email,
nickname: identity.nickname,
password: identity.password,
createdAt: identity.createdAt,
},
});
} else {
failed++;
send('progress', {
index: i + 1,
total: count,
created,
failed,
error: lastErr ? lastErr.message : 'unknown error',
});
}
if (i < count - 1 && !aborted) await sleep(throttleMs);
}
send('done', { created, failed, aborted });
} catch (err) {
send('failed', { message: err.message });
} finally {
res.end();
}
}
async function handleInbox(res, id) {
const identity = await db.getIdentity(id);
if (!identity) return sendJson(res, 404, { error: 'Identity not found' });
try {
const messages = await mail.listMessages(await db.getSettings(), identity);
sendJson(res, 200, { email: identity.email, messages });
} catch (err) {
sendJson(res, 502, { error: `Could not open inbox: ${err.message}` });
}
}
async function handleMessage(res, id, mid) {
const identity = await db.getIdentity(id);
if (!identity) return sendJson(res, 404, { error: 'Identity not found' });
try {
const message = await mail.getMessage(await db.getSettings(), identity, mid);
sendJson(res, 200, { message });
} catch (err) {
sendJson(res, 502, { error: `Could not load message: ${err.message}` });
}
}
async function handleDelete(res, ids) {
if (!Array.isArray(ids) || !ids.length) return sendJson(res, 400, { error: 'No ids provided' });
await db.backup('pre-delete'); // snapshot before removing anything
const settings = await db.getSettings();
let serverDeleted = 0;
for (const id of ids) {
const identity = await db.getIdentity(id);
if (!identity) continue;
try {
await mail.deleteIdentity(settings, identity);
serverDeleted++;
} catch {
/* account may already be gone server-side; still remove it locally */
}
await browser.removeProfile(id); // drop this identity's saved browser data too
}
const removed = await db.removeIdentities(ids);
sendJson(res, 200, { removed, serverDeleted });
}
async function handleOpenBrowser(res, id) {
const identity = await db.getIdentity(id);
if (!identity) return sendJson(res, 404, { error: 'Identity not found' });
try {
const info = await browser.openProfile(identity.id, '');
sendJson(res, 200, { ok: true, browser: info.browser });
} catch (err) {
sendJson(res, 500, { error: err.message });
}
}
async function handleExport(res) {
const dbData = await db.read();
const body = JSON.stringify(dbData, null, 2);
res.writeHead(200, {
'Content-Type': 'application/json; charset=utf-8',
'Content-Disposition': 'attachment; filename="emails.json"',
});
res.end(body);
}
// ---- router ----------------------------------------------------------------
const server = http.createServer(async (req, res) => {
const parsed = new URL(req.url, `http://${req.headers.host}`);
const { pathname } = parsed;
const q = parsed.searchParams;
try {
if (pathname === '/api/domains' && req.method === 'GET') {
const settings = await db.getSettings();
const provider = q.get('provider') || settings.provider;
const domains = await mail.getDomains({ ...settings, provider });
return sendJson(res, 200, { domains });
}
if (pathname === '/api/identities' && req.method === 'GET') {
const data = await db.read();
return sendJson(res, 200, data);
}
if (pathname === '/api/generate' && req.method === 'GET') {
return handleGenerate(req, res, q);
}
if (pathname === '/api/inbox' && req.method === 'GET') {
return handleInbox(res, q.get('id'));
}
if (pathname === '/api/message' && req.method === 'GET') {
return handleMessage(res, q.get('id'), q.get('mid'));
}
if (pathname === '/api/delete' && req.method === 'POST') {
const body = await readBody(req);
return handleDelete(res, body.ids);
}
if (pathname === '/api/browser' && req.method === 'POST') {
const body = await readBody(req);
return handleOpenBrowser(res, body.id);
}
if (pathname === '/api/categories' && req.method === 'POST') {
const body = await readBody(req);
try {
return sendJson(res, 200, { categories: await db.upsertCategory(body) });
} catch (err) {
return sendJson(res, 400, { error: err.message });
}
}
if (pathname === '/api/categories/remove' && req.method === 'POST') {
const { id } = await readBody(req);
return sendJson(res, 200, await db.removeCategory(id));
}
if (pathname === '/api/assign' && req.method === 'POST') {
const { ids, categoryId } = await readBody(req);
if (!Array.isArray(ids) || !ids.length) return sendJson(res, 400, { error: 'No ids provided' });
try {
return sendJson(res, 200, await db.assignCategory(ids, categoryId || null));
} catch (err) {
return sendJson(res, 400, { error: err.message });
}
}
if (pathname === '/api/rename' && req.method === 'POST') {
const { id, nickname } = await readBody(req);
const clean = generate.cleanNickname(nickname);
if (!clean) return sendJson(res, 400, { error: 'A handle cannot be empty.' });
const identity = await db.renameIdentity(id, clean);
if (!identity) return sendJson(res, 404, { error: 'Identity not found' });
return sendJson(res, 200, { id, nickname: clean });
}
if (pathname === '/api/settings' && req.method === 'GET') {
return sendJson(res, 200, await db.getSettings());
}
if (pathname === '/api/settings' && req.method === 'POST') {
const body = await readBody(req);
const patch = {};
if (body.provider) patch.provider = body.provider;
if (body.throttleMs != null) patch.throttleMs = clamp(Number(body.throttleMs) || 500, 150, 5000);
if (body.theme) patch.theme = body.theme;
if (body.domain !== undefined) patch.domain = String(body.domain || '').trim().toLowerCase();
if (body.mailApi !== undefined) patch.mailApi = String(body.mailApi || '').trim();
if (body.mailToken !== undefined) patch.mailToken = String(body.mailToken || '').trim();
if (body.duckToken !== undefined) patch.duckToken = String(body.duckToken || '').trim();
return sendJson(res, 200, await db.updateSettings(patch));
}
if (pathname === '/api/settings/test' && req.method === 'POST') {
const body = await readBody(req);
try {
const detail = await mail.checkConnection({ ...(await db.getSettings()), ...body });
return sendJson(res, 200, { ok: true, detail });
} catch (err) {
return sendJson(res, 200, { ok: false, detail: err.message });
}
}
if (pathname === '/api/export' && req.method === 'GET') {
return handleExport(res);
}
if (pathname.startsWith('/api/')) {
return sendJson(res, 404, { error: 'Unknown endpoint' });
}
// Static files (UI).
return serveStatic(res, pathname);
} catch (err) {
if (!res.writableEnded) sendJson(res, 500, { error: err.message });
}
});
function openBrowser(url) {
try {
if (process.platform === 'win32') {
spawn('cmd', ['/c', 'start', '""', url], { detached: true, stdio: 'ignore' }).unref();
} else if (process.platform === 'darwin') {
spawn('open', [url], { detached: true, stdio: 'ignore' }).unref();
} else {
spawn('xdg-open', [url], { detached: true, stdio: 'ignore' }).unref();
}
} catch {
/* opening a browser is a nicety; ignore failures */
}
}
await db.ensure();
server.listen(PORT, () => {
const url = `http://localhost:${PORT}`;
console.log(`\n Bulbox running at ${url}`);
console.log(` Database file: ${db.dbPath()}`);
console.log(` Press Ctrl+C to stop.\n`);
if (process.env.NO_OPEN !== '1') openBrowser(url);
});