-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
282 lines (231 loc) · 9.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { SMTPServer } from "smtp-server";
import { simpleParser } from "mailparser";
import { v4 as uuidv4 } from "uuid";
import express from "express";
import path from "path";
import fs from "fs/promises";
import config from "./config.json" assert { type: "json" };
import sanitize from "sanitize-filename";
import { pathExists } from "path-exists";
import { generateUsername } from "unique-username-generator";
import cors from "cors";
function log(message) {
console.log(message);
}
function timestampToMinutes(timestamp) {
return timestamp / 1000 / 60;
}
setInterval(async () => {
try {
const currentTime = timestampToMinutes(Date.now());
const addressDirectories = await fs.readdir(config.mailDirectory);
for (const address of addressDirectories) {
const addressDirectory = path.join(config.mailDirectory, address);
const manifestFile = path.join(addressDirectory, "manifest.json");
const manifest = JSON.parse((await fs.readFile(manifestFile)).toString());
const newManifest = [];
for (const storedMail of manifest) {
if (currentTime - timestampToMinutes(storedMail.timestamp) < config.deletionTimeout) {
newManifest.push(storedMail);
continue;
}
const storedMailDirectory = path.join(addressDirectory, storedMail.id);
await fs.rm(storedMailDirectory, { recursive: true });
}
if (newManifest.length === 0) {
await fs.rm(addressDirectory, { recursive: true });
} else {
await fs.writeFile(manifestFile, JSON.stringify(newManifest));
}
}
} catch (err) {
log(err);
}
}, 30 * 1000);
const smtpServer = new SMTPServer({
authOptional: true,
maxAllowedUnauthenticatedCommands: Infinity,
disabledCommands: ["STARTTLS"],
secure: false,
async onData(stream, session, callback) {
try {
const id = uuidv4();
const parsedEmail = await simpleParser(stream);
const toAddress = parsedEmail.to?.value?.[0].address;
if (!toAddress) {
throw "Sender address missing";
}
const addressDirectory = path.join("mails", sanitize(toAddress));
await fs.mkdir(addressDirectory, { recursive: true });
const manifestFilePath = path.join(addressDirectory, "manifest.json");
let manifest;
try {
manifest = JSON.parse((await fs.readFile(manifestFilePath)).toString());
} catch (err) {
manifest = [];
}
manifest.unshift({ id, timestamp: Date.now() });
await fs.writeFile(manifestFilePath, JSON.stringify(manifest));
const mailDirectory = path.join(addressDirectory, id);
await fs.mkdir(mailDirectory);
const attachmentsDirectory = path.join(mailDirectory, "attachments");
await fs.mkdir(attachmentsDirectory);
const attachments = [];
let savedAttachmentsSize = 0;
for (const a of parsedEmail.attachments) {
const attachmentId = uuidv4();
const attachmentName = a.filename;
const attachment = {
id: attachmentId,
fileName: attachmentName
};
savedAttachmentsSize += a.size;
if (savedAttachmentsSize <= config.limitations.maxAttachmentsSize * 1_000_000) {
const attachmentPath = path.join(attachmentsDirectory, attachmentId);
await fs.writeFile(attachmentPath, a.content);
attachment.present = true;
} else {
attachment.present = false;
}
attachments.push(attachment);
}
const mailData = {
id,
from: {
address: parsedEmail.from?.value?.[0]?.address?.substring(0, config.limitations.maxOthersChars),
name: parsedEmail.from?.value?.[0].name?.substring(0, config.limitations.maxOthersChars)
},
to: {
address: parsedEmail.to?.value?.[0].address?.substring(0, config.limitations.maxOthersChars),
name: parsedEmail.to?.value?.[0].name?.substring(0, config.limitations.maxOthersChars)
},
subject: parsedEmail.subject?.substring(0, config.limitations.maxTitleChars),
date: parsedEmail.date?.toISOString().substring(0, config.limitations.maxOthersChars),
body: {
text: parsedEmail.text?.substring(0, config.limitations.maxBodyChars) || "",
html: parsedEmail.html ? parsedEmail.html.substring(0, config.limitations.maxBodyChars) : ""
},
headers: new Map(
Array.from(parsedEmail.headers, ([key, value]) => [
key.substring(0, config.limitations.maxOthersChars),
value.toString().substring(0, config.limitations.maxOthersChars)
])
),
attachments
}
const mailDataFile = path.join(mailDirectory, "mail.json");
await fs.writeFile(mailDataFile, JSON.stringify(mailData));
} catch (err) {
log(err);
}
return callback();
}
});
smtpServer.listen(config.smtpServer.port, () => {
log(`[SMTP server] SMTP server running on port ${config.smtpServer.port}`);
});
const webServer = express();
webServer.use(express.static("client/public"));
webServer.get("/api/v1/contactEmail", cors(), (req, res) => {
res.json({ email: config.contactEmail });
});
webServer.get("/api/v1/randomUsername", cors(), (req, res) => {
const username = generateUsername("", 5, 20);
res.json({ username });
});
webServer.get("/api/v1/domains", cors(), (req, res) => {
res.json(config.domains);
});
webServer.get("/api/v1/mail/:address", cors(), async (req, res) => {
try {
let limit = 10;
if (req.query.limit && !isNaN(parseInt(req.query.limit)) && parseInt(req.query.limit) <= 10) {
limit = parseInt(req.query.limit);
}
const mailDirectory = path.join(config.mailDirectory, sanitize(req.params.address));
const manifestFile = path.join(mailDirectory, "manifest.json");
const manifest = JSON.parse((await fs.readFile(manifestFile)).toString()).slice(0, limit);
const responseData = [];
for (const m of manifest) {
const mailFile = path.join(mailDirectory, m.id, "mail.json");
const mail = JSON.parse((await fs.readFile(mailFile)).toString());
responseData.push(mail);
}
res.json(responseData);
} catch (err) {
res.json([]);
}
});
webServer.get("/api/v1/mail/:address/:mailId", cors(), async (req, res) => {
try {
const mailFile = path.join(config.mailDirectory, sanitize(req.params.address), sanitize(req.params.mailId), "mail.json");
const mail = JSON.parse((await fs.readFile(mailFile)).toString());
res.json(mail);
} catch (err) {
res.status(404);
res.end();
}
});
webServer.get("/api/v1/mail/:address/:mailId/attachments", cors(), async (req, res) => {
try {
const mailFile = path.join(config.mailDirectory, sanitize(req.params.address), sanitize(req.params.mailId), "mail.json");
const mail = JSON.parse((await fs.readFile(mailFile)).toString());
res.json(mail.attachments);
} catch (err) {
res.status(404);
res.end();
}
});
webServer.get("/api/v1/mail/:address/:mailId/attachments/:attachmentId", cors(), async (req, res) => {
try {
const mailDirectory = path.join(config.mailDirectory, sanitize(req.params.address), sanitize(req.params.mailId));
const mailFile = path.join(mailDirectory, "mail.json");
const mail = JSON.parse((await fs.readFile(mailFile)).toString());
const attachment = mail.attachments.find(a => a.id === req.params.attachmentId);
if (!attachment || !attachment.present) {
throw "Attachment not found";
}
const attachmentFile = path.join(mailDirectory, "attachments", attachment.id);
if (!(await pathExists(attachmentFile))) {
throw "Attachment not found";
}
res.download(attachmentFile, attachment.fileName);
} catch (err) {
res.status(404);
res.end();
}
});
webServer.delete("/api/v1/mail/:address/:mailId", cors(), async (req, res) => {
try {
const addressDirectory = path.join(config.mailDirectory, sanitize(req.params.address));
const mailDirectory = path.join(addressDirectory, sanitize(req.params.mailId));
const manifestFile = path.join(addressDirectory, "manifest.json");
const manifest = JSON.parse((await fs.readFile(manifestFile)).toString())
.filter(m => m.id !== req.params.mailId);
if (manifest.length === 0) {
await fs.rm(addressDirectory, { recursive: true });
} else {
await fs.writeFile(manifestFile, JSON.stringify(manifest));
await fs.rm(mailDirectory, { recursive: true });
}
res.sendStatus(204);
res.end();
} catch (err) {
res.status(404);
res.end();
}
});
webServer.delete("/api/v1/mail/:address", cors(), async (req, res) => {
try {
const mailDirectory = path.join(config.mailDirectory, sanitize(req.params.address));
await fs.rm(mailDirectory, { recursive: true });
res.sendStatus(204);
res.end();
} catch (err) {
res.status(404);
res.end();
}
});
webServer.listen(config.webServer.port, () => {
log(`[Web server] Web server running on port ${config.webServer.port}`);
});