emailengine-app 2.74.0 → 2.74.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/data/google-crawlers.json +1 -1
- package/lib/account.js +23 -45
- package/lib/api-routes/changes-routes.js +51 -0
- package/lib/api-routes/index.js +160 -0
- package/lib/consts.js +15 -0
- package/lib/email-client/gmail/send-request.js +69 -0
- package/lib/email-client/gmail-client.js +4 -41
- package/lib/email-client/imap/detach-client.js +27 -0
- package/lib/email-client/imap/listing-diff.js +165 -0
- package/lib/email-client/imap/mailbox.js +32 -13
- package/lib/email-client/imap/subconnection.js +2 -4
- package/lib/email-client/imap/sync-operations.js +5 -3
- package/lib/email-client/imap-client.js +50 -84
- package/lib/email-client/outlook/send-request.js +48 -0
- package/lib/email-client/outlook-client.js +5 -14
- package/lib/export.js +97 -21
- package/lib/imapproxy/imap-core/lib/imap-connection.js +1 -1
- package/lib/logger.js +14 -6
- package/lib/rate-limit.js +33 -11
- package/lib/response-stream.js +138 -0
- package/lib/schemas.js +4 -2
- package/lib/sentry.js +8 -0
- package/lib/smtp-message-processor.js +131 -0
- package/lib/tools.js +28 -5
- package/lib/ui-routes/internals-routes.js +167 -67
- package/lib/ui-routes/route-helpers.js +9 -6
- package/package.json +9 -9
- package/sbom.json +1 -1
- package/static/css/flyonui.css +1 -1
- package/static/licenses.html +28 -18
- package/translations/messages.pot +67 -67
- package/views/config/ai.hbs +3 -1
- package/views/internals/index.hbs +63 -29
- package/views/layout/app.hbs +22 -0
- package/views/partials/document_store_header.hbs +7 -4
- package/workers/api.js +20 -216
- package/workers/export.js +29 -15
- package/workers/imap.js +47 -10
- package/workers/smtp.js +50 -91
package/workers/api.js
CHANGED
|
@@ -61,7 +61,7 @@ const HapiSwagger = require('hapi-swagger');
|
|
|
61
61
|
const pathlib = require('path');
|
|
62
62
|
|
|
63
63
|
const crypto = require('crypto');
|
|
64
|
-
const {
|
|
64
|
+
const { registeredPublishers, openChangeStream } = require('../lib/response-stream');
|
|
65
65
|
const { oauth2Apps, OAUTH_PROVIDERS } = require('../lib/oauth2-apps');
|
|
66
66
|
|
|
67
67
|
const handlebars = require('handlebars');
|
|
@@ -101,27 +101,10 @@ const {
|
|
|
101
101
|
NONCE_BYTES
|
|
102
102
|
} = consts;
|
|
103
103
|
|
|
104
|
-
const
|
|
105
|
-
const chatRoutes = require('../lib/api-routes/chat-routes');
|
|
104
|
+
const registerApiRoutes = require('../lib/api-routes');
|
|
106
105
|
const bullBoardRoutes = require('../lib/api-routes/bull-board-routes');
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
const exportRoutes = require('../lib/api-routes/export-routes');
|
|
110
|
-
const pubsubRoutes = require('../lib/api-routes/pubsub-routes');
|
|
111
|
-
const tokenRoutes = require('../lib/api-routes/token-routes');
|
|
112
|
-
const mailboxRoutes = require('../lib/api-routes/mailbox-routes');
|
|
113
|
-
const settingsRoutes = require('../lib/api-routes/settings-routes');
|
|
114
|
-
const statsRoutes = require('../lib/api-routes/stats-routes');
|
|
115
|
-
const licenseRoutes = require('../lib/api-routes/license-routes');
|
|
116
|
-
const outboxRoutes = require('../lib/api-routes/outbox-routes');
|
|
117
|
-
const webhookRouteRoutes = require('../lib/api-routes/webhook-route-routes');
|
|
118
|
-
const oauth2AppRoutes = require('../lib/api-routes/oauth2-app-routes');
|
|
119
|
-
const gatewayRoutes = require('../lib/api-routes/gateway-routes');
|
|
120
|
-
const deliveryTestRoutes = require('../lib/api-routes/delivery-test-routes');
|
|
121
|
-
const blocklistRoutes = require('../lib/api-routes/blocklist-routes');
|
|
122
|
-
const submitRoutes = require('../lib/api-routes/submit-routes');
|
|
123
|
-
|
|
124
|
-
const { imapSchema, smtpSchema, oauth2Schema, accountIdSchema, headerTimeoutSchema, errorResponses } = require('../lib/schemas');
|
|
106
|
+
|
|
107
|
+
const { imapSchema, smtpSchema, oauth2Schema, accountIdSchema, headerTimeoutSchema } = require('../lib/schemas');
|
|
125
108
|
|
|
126
109
|
const OAuth2ProviderSchema = Joi.string()
|
|
127
110
|
.valid(...Object.keys(OAUTH_PROVIDERS))
|
|
@@ -279,73 +262,6 @@ logger.info({
|
|
|
279
262
|
|
|
280
263
|
const TRACKER_IMAGE = Buffer.from('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', 'base64');
|
|
281
264
|
|
|
282
|
-
let registeredPublishers = new Set();
|
|
283
|
-
|
|
284
|
-
class ResponseStream extends Transform {
|
|
285
|
-
constructor() {
|
|
286
|
-
super();
|
|
287
|
-
registeredPublishers.add(this);
|
|
288
|
-
this.periodicKeepAliveTimer = false;
|
|
289
|
-
this.updateTimer();
|
|
290
|
-
|
|
291
|
-
this._finalized = false;
|
|
292
|
-
|
|
293
|
-
// Ensure cleanup on all stream end scenarios
|
|
294
|
-
this.on('error', () => this.finalize());
|
|
295
|
-
this.once('close', () => this.finalize());
|
|
296
|
-
this.once('end', () => this.finalize());
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
updateTimer() {
|
|
300
|
-
clearTimeout(this.periodicKeepAliveTimer);
|
|
301
|
-
this.periodicKeepAliveTimer = setTimeout(() => {
|
|
302
|
-
if (this._finalized || this.destroyed) return;
|
|
303
|
-
this.write(': still here\n\n');
|
|
304
|
-
if (this._compressor) {
|
|
305
|
-
this._compressor.flush();
|
|
306
|
-
}
|
|
307
|
-
this.updateTimer();
|
|
308
|
-
}, 90 * 1000);
|
|
309
|
-
this.periodicKeepAliveTimer.unref();
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
setCompressor(compressor) {
|
|
313
|
-
this._compressor = compressor;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
sendMessage(payload) {
|
|
317
|
-
if (this._finalized || this.destroyed) return;
|
|
318
|
-
let sendData = JSON.stringify(payload);
|
|
319
|
-
this.write('event: message\ndata:' + sendData + '\n\n');
|
|
320
|
-
if (this._compressor) {
|
|
321
|
-
this._compressor.flush();
|
|
322
|
-
}
|
|
323
|
-
this.updateTimer();
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
finalize() {
|
|
327
|
-
if (this._finalized) return; // Prevent double cleanup
|
|
328
|
-
this._finalized = true;
|
|
329
|
-
|
|
330
|
-
clearTimeout(this.periodicKeepAliveTimer);
|
|
331
|
-
registeredPublishers.delete(this);
|
|
332
|
-
|
|
333
|
-
if (!this.destroyed) {
|
|
334
|
-
this.destroy();
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
_transform(data, encoding, done) {
|
|
339
|
-
this.push(data);
|
|
340
|
-
done();
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
_flush(done) {
|
|
344
|
-
this.finalize();
|
|
345
|
-
done();
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
|
|
349
265
|
let callQueue = new Map();
|
|
350
266
|
let mids = 0;
|
|
351
267
|
|
|
@@ -2779,124 +2695,26 @@ Include your token in requests using one of these methods:
|
|
|
2779
2695
|
}
|
|
2780
2696
|
});
|
|
2781
2697
|
|
|
2782
|
-
// setup
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
// setup "chat with email" routes (deprecated Document Store feature; only when enabled)
|
|
2786
|
-
if (documentStoreFeatureEnabled) {
|
|
2787
|
-
await chatRoutes({ server, call, CORS_CONFIG });
|
|
2788
|
-
}
|
|
2789
|
-
|
|
2790
|
-
// setup account CRUD routes
|
|
2791
|
-
await accountRoutes({
|
|
2698
|
+
// setup every /v1 route module (wiring lives in lib/api-routes/index.js so the route table
|
|
2699
|
+
// can be captured without booting this worker - see test/api-routes-table-test.js)
|
|
2700
|
+
await registerApiRoutes({
|
|
2792
2701
|
server,
|
|
2793
2702
|
call,
|
|
2703
|
+
notify,
|
|
2794
2704
|
documentsQueue,
|
|
2795
|
-
|
|
2796
|
-
imapSchema,
|
|
2797
|
-
smtpSchema,
|
|
2798
|
-
CORS_CONFIG,
|
|
2799
|
-
AccountTypeSchema,
|
|
2800
|
-
OAuth2ProviderSchema,
|
|
2801
|
-
metrics
|
|
2802
|
-
});
|
|
2803
|
-
|
|
2804
|
-
// setup message routes
|
|
2805
|
-
await messageRoutes({
|
|
2806
|
-
server,
|
|
2807
|
-
call,
|
|
2705
|
+
metrics,
|
|
2808
2706
|
CORS_CONFIG,
|
|
2707
|
+
FLAG_SORT_ORDER,
|
|
2708
|
+
SMTP_TEST_HOST,
|
|
2809
2709
|
MAX_ATTACHMENT_SIZE,
|
|
2810
2710
|
MAX_BODY_SIZE,
|
|
2811
2711
|
MAX_PAYLOAD_TIMEOUT,
|
|
2812
|
-
documentStoreFeatureEnabled
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
CORS_CONFIG
|
|
2819
|
-
});
|
|
2820
|
-
|
|
2821
|
-
// setup Pub/Sub status route
|
|
2822
|
-
await pubsubRoutes({ server, CORS_CONFIG });
|
|
2823
|
-
|
|
2824
|
-
// setup access token routes
|
|
2825
|
-
await tokenRoutes({ server, call, CORS_CONFIG });
|
|
2826
|
-
|
|
2827
|
-
// setup mailbox routes
|
|
2828
|
-
await mailboxRoutes({ server, call, CORS_CONFIG, FLAG_SORT_ORDER });
|
|
2829
|
-
|
|
2830
|
-
// setup settings routes
|
|
2831
|
-
await settingsRoutes({ server, notify, CORS_CONFIG });
|
|
2832
|
-
|
|
2833
|
-
// setup stats route
|
|
2834
|
-
await statsRoutes({ server, call, CORS_CONFIG });
|
|
2835
|
-
|
|
2836
|
-
// setup license routes
|
|
2837
|
-
await licenseRoutes({ server, call, CORS_CONFIG });
|
|
2838
|
-
|
|
2839
|
-
// setup outbox routes
|
|
2840
|
-
await outboxRoutes({ server, CORS_CONFIG });
|
|
2841
|
-
|
|
2842
|
-
// setup webhook route management routes
|
|
2843
|
-
await webhookRouteRoutes({ server, CORS_CONFIG });
|
|
2844
|
-
|
|
2845
|
-
// setup OAuth2 application routes
|
|
2846
|
-
await oauth2AppRoutes({ server, call, CORS_CONFIG, OAuth2ProviderSchema });
|
|
2847
|
-
|
|
2848
|
-
// setup SMTP gateway routes
|
|
2849
|
-
await gatewayRoutes({ server, call, CORS_CONFIG });
|
|
2850
|
-
|
|
2851
|
-
// setup delivery test routes
|
|
2852
|
-
await deliveryTestRoutes({ server, call, CORS_CONFIG, SMTP_TEST_HOST });
|
|
2853
|
-
|
|
2854
|
-
// setup blocklist routes
|
|
2855
|
-
await blocklistRoutes({ server, call, CORS_CONFIG });
|
|
2856
|
-
|
|
2857
|
-
// setup message submit route
|
|
2858
|
-
await submitRoutes({ server, call, CORS_CONFIG, MAX_ATTACHMENT_SIZE, MAX_BODY_SIZE, MAX_PAYLOAD_TIMEOUT });
|
|
2859
|
-
|
|
2860
|
-
server.route({
|
|
2861
|
-
method: 'GET',
|
|
2862
|
-
path: '/v1/changes',
|
|
2863
|
-
|
|
2864
|
-
async handler(request, h) {
|
|
2865
|
-
request.app.stream = new ResponseStream();
|
|
2866
|
-
finished(request.app.stream, err => request.app.stream.finalize(err));
|
|
2867
|
-
setImmediate(() => {
|
|
2868
|
-
try {
|
|
2869
|
-
request.app.stream.write(`: EmailEngine v${packageData.version}\n\n`);
|
|
2870
|
-
} catch (err) {
|
|
2871
|
-
// ignore
|
|
2872
|
-
}
|
|
2873
|
-
});
|
|
2874
|
-
return h
|
|
2875
|
-
.response(request.app.stream)
|
|
2876
|
-
.header('X-Accel-Buffering', 'no')
|
|
2877
|
-
.header('Connection', 'keep-alive')
|
|
2878
|
-
.header('Cache-Control', 'no-cache')
|
|
2879
|
-
.type('text/event-stream');
|
|
2880
|
-
},
|
|
2881
|
-
|
|
2882
|
-
options: {
|
|
2883
|
-
description: 'Stream state changes',
|
|
2884
|
-
notes: 'Stream account state changes as an EventSource',
|
|
2885
|
-
tags: ['api', 'Account'],
|
|
2886
|
-
|
|
2887
|
-
plugins: {
|
|
2888
|
-
'hapi-swagger': {
|
|
2889
|
-
produces: ['text/event-stream'],
|
|
2890
|
-
responses: errorResponses(401, 403, 429, 500)
|
|
2891
|
-
}
|
|
2892
|
-
},
|
|
2893
|
-
|
|
2894
|
-
auth: {
|
|
2895
|
-
strategy: 'api-token',
|
|
2896
|
-
mode: 'required'
|
|
2897
|
-
},
|
|
2898
|
-
cors: CORS_CONFIG
|
|
2899
|
-
}
|
|
2712
|
+
documentStoreFeatureEnabled,
|
|
2713
|
+
oauth2Schema,
|
|
2714
|
+
imapSchema,
|
|
2715
|
+
smtpSchema,
|
|
2716
|
+
AccountTypeSchema,
|
|
2717
|
+
OAuth2ProviderSchema
|
|
2900
2718
|
});
|
|
2901
2719
|
|
|
2902
2720
|
// Web UI routes
|
|
@@ -3266,23 +3084,9 @@ Include your token in requests using one of these methods:
|
|
|
3266
3084
|
method: 'GET',
|
|
3267
3085
|
path: '/admin/changes',
|
|
3268
3086
|
|
|
3269
|
-
|
|
3270
|
-
|
|
3271
|
-
|
|
3272
|
-
setImmediate(() => {
|
|
3273
|
-
try {
|
|
3274
|
-
request.app.stream.write(`: EmailEngine v${packageData.version}\n\n`);
|
|
3275
|
-
} catch (err) {
|
|
3276
|
-
// ignore
|
|
3277
|
-
}
|
|
3278
|
-
});
|
|
3279
|
-
return h
|
|
3280
|
-
.response(request.app.stream)
|
|
3281
|
-
.header('X-Accel-Buffering', 'no')
|
|
3282
|
-
.header('Connection', 'keep-alive')
|
|
3283
|
-
.header('Cache-Control', 'no-cache')
|
|
3284
|
-
.type('text/event-stream');
|
|
3285
|
-
}
|
|
3087
|
+
// Same feed as /v1/changes (lib/api-routes/changes-routes.js), opened through the shared
|
|
3088
|
+
// helper so the two can not drift apart on framing or on stream cleanup.
|
|
3089
|
+
handler: openChangeStream
|
|
3286
3090
|
});
|
|
3287
3091
|
|
|
3288
3092
|
// setup web UI
|
package/workers/export.js
CHANGED
|
@@ -20,7 +20,18 @@ const {
|
|
|
20
20
|
const { getDuration, readEnvValue, threadStats, maybeReloadHttpProxyAgent } = require('../lib/tools');
|
|
21
21
|
const { webhooks: Webhooks } = require('../lib/webhooks');
|
|
22
22
|
const settings = require('../lib/settings');
|
|
23
|
-
const {
|
|
23
|
+
const {
|
|
24
|
+
Export,
|
|
25
|
+
isTransientError,
|
|
26
|
+
isSkippableError,
|
|
27
|
+
isFolderMissingError,
|
|
28
|
+
isRetryableError,
|
|
29
|
+
resolveExportLimit,
|
|
30
|
+
isExportLimitReached,
|
|
31
|
+
parseActiveEntry,
|
|
32
|
+
getExportKey,
|
|
33
|
+
ACTIVE_EXPORTS_KEY
|
|
34
|
+
} = require('../lib/export');
|
|
24
35
|
|
|
25
36
|
const { initSentry } = require('../lib/sentry');
|
|
26
37
|
initSentry('export');
|
|
@@ -174,7 +185,7 @@ async function indexMessages(job, exportData) {
|
|
|
174
185
|
|
|
175
186
|
await Export.update(account, exportId, { foldersTotal: foldersToProcess.length });
|
|
176
187
|
|
|
177
|
-
const maxMessages =
|
|
188
|
+
const maxMessages = resolveExportLimit(await settings.get('exportMaxMessages'), DEFAULT_EXPORT_MAX_MESSAGES);
|
|
178
189
|
|
|
179
190
|
logger.info({ msg: 'Starting export indexing', account, exportId, foldersToProcess: foldersToProcess.length, maxMessages: maxMessages || 'unlimited' });
|
|
180
191
|
|
|
@@ -249,7 +260,7 @@ async function indexMessages(job, exportData) {
|
|
|
249
260
|
// The folder indexed successfully (failures throw above); count it toward progress.
|
|
250
261
|
await Export.update(account, exportId, { foldersScanned: i + 1 });
|
|
251
262
|
|
|
252
|
-
if (
|
|
263
|
+
if (isExportLimitReached(totalIndexed, maxMessages)) {
|
|
253
264
|
truncated = true;
|
|
254
265
|
logger.warn({
|
|
255
266
|
msg: 'Export indexing truncated: message limit reached',
|
|
@@ -354,8 +365,8 @@ async function exportMessages(job, exportData) {
|
|
|
354
365
|
const rawMaxBytes = Number(exportData.maxBytes);
|
|
355
366
|
// Preserve an explicit 0 ("unlimited" per the API contract); only fall back when unset/invalid.
|
|
356
367
|
const maxBytes = Number.isFinite(rawMaxBytes) ? rawMaxBytes : 5 * 1024 * 1024;
|
|
357
|
-
const maxMessageSize = (await settings.get('exportMaxMessageSize')
|
|
358
|
-
const maxExportSize =
|
|
368
|
+
const maxMessageSize = resolveExportLimit(await settings.get('exportMaxMessageSize'), DEFAULT_EXPORT_MAX_MESSAGE_SIZE);
|
|
369
|
+
const maxExportSize = resolveExportLimit(await settings.get('exportMaxSize'), DEFAULT_EXPORT_MAX_SIZE);
|
|
359
370
|
const isEncrypted = exportData.isEncrypted === '1';
|
|
360
371
|
|
|
361
372
|
const accountObject = new Account({
|
|
@@ -576,7 +587,7 @@ async function exportMessages(job, exportData) {
|
|
|
576
587
|
}
|
|
577
588
|
} else if (result && result.data) {
|
|
578
589
|
await processMessage(result.data, entry);
|
|
579
|
-
if (
|
|
590
|
+
if (isExportLimitReached(totalBytesWritten, maxExportSize)) {
|
|
580
591
|
sizeLimitReached = true;
|
|
581
592
|
break;
|
|
582
593
|
}
|
|
@@ -661,7 +672,7 @@ async function exportMessages(job, exportData) {
|
|
|
661
672
|
|
|
662
673
|
if (message) {
|
|
663
674
|
await processMessage(message, entry);
|
|
664
|
-
if (
|
|
675
|
+
if (isExportLimitReached(totalBytesWritten, maxExportSize)) {
|
|
665
676
|
sizeLimitReached = true;
|
|
666
677
|
break;
|
|
667
678
|
}
|
|
@@ -883,16 +894,19 @@ function onCommand(command) {
|
|
|
883
894
|
setInterval(
|
|
884
895
|
async () => {
|
|
885
896
|
try {
|
|
886
|
-
|
|
897
|
+
// The set key, the entry encoding and the export key all come from lib/export.js.
|
|
898
|
+
// Hand-building them here meant this cleanup and the limiter that writes the
|
|
899
|
+
// entries could drift apart: entries added under one encoding and removed under
|
|
900
|
+
// another leave the active set growing forever, and once it reaches the global
|
|
901
|
+
// limit every export in the deployment is refused with 429.
|
|
902
|
+
const activeExports = await redis.smembers(ACTIVE_EXPORTS_KEY);
|
|
887
903
|
for (const entry of activeExports) {
|
|
888
|
-
const
|
|
889
|
-
if (
|
|
890
|
-
const
|
|
891
|
-
const entryExportId = entry.substring(separatorIndex + 1);
|
|
892
|
-
const exists = await redis.exists(`${REDIS_PREFIX}exp:${entryAccount}:${entryExportId}`);
|
|
904
|
+
const parsed = parseActiveEntry(entry);
|
|
905
|
+
if (!parsed) continue;
|
|
906
|
+
const exists = await redis.exists(getExportKey(parsed.account, parsed.exportId));
|
|
893
907
|
if (!exists) {
|
|
894
|
-
await redis.srem(
|
|
895
|
-
logger.info({ msg: 'Cleaned stale active set entry', account:
|
|
908
|
+
await redis.srem(ACTIVE_EXPORTS_KEY, entry);
|
|
909
|
+
logger.info({ msg: 'Cleaned stale active set entry', account: parsed.account, exportId: parsed.exportId });
|
|
896
910
|
}
|
|
897
911
|
}
|
|
898
912
|
} catch (err) {
|
package/workers/imap.js
CHANGED
|
@@ -49,6 +49,9 @@ const DEFAULT_STATES = {
|
|
|
49
49
|
disconnected: 0
|
|
50
50
|
};
|
|
51
51
|
|
|
52
|
+
// Minimum interval between reports about failed account log writes, see getAccountLogger()
|
|
53
|
+
const LOG_FAILURE_REPORT_INTERVAL = 60 * 1000;
|
|
54
|
+
|
|
52
55
|
const NO_ACTIVE_HANDLER_RESP_ERR = new Error('No active handler for requested account. Try again later.');
|
|
53
56
|
NO_ACTIVE_HANDLER_RESP_ERR.statusCode = 503;
|
|
54
57
|
NO_ACTIVE_HANDLER_RESP_ERR.code = 'WorkerNotAvailable';
|
|
@@ -116,11 +119,42 @@ class ConnectionHandler {
|
|
|
116
119
|
let logKey = this.getLogKey(account);
|
|
117
120
|
let logging = await settings.getLoggingInfo(account);
|
|
118
121
|
|
|
119
|
-
|
|
122
|
+
// log() runs once per protocol line, so a persistent write failure (Redis OOM, MISCONF,
|
|
123
|
+
// READONLY) would report itself on every single line. Report the first failure, then at
|
|
124
|
+
// most one summary per interval, otherwise a failing account log floods the main log.
|
|
125
|
+
let lastFailureReport = 0;
|
|
126
|
+
let suppressedFailures = 0;
|
|
127
|
+
let reportFailure = (msg, err) => {
|
|
128
|
+
let now = Date.now();
|
|
129
|
+
if (now - lastFailureReport < LOG_FAILURE_REPORT_INTERVAL) {
|
|
130
|
+
suppressedFailures++;
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// ioredis attaches the failed command to the error, and the arguments of these
|
|
135
|
+
// writes include the encoded log entry itself (protocol traces, and even raw
|
|
136
|
+
// credentials when EENGINE_LOG_RAW is set), so such errors are summarized instead
|
|
137
|
+
// of being handed to the log serializer, which copies all enumerable properties
|
|
138
|
+
let reported = err && (err.command || err.previousErrors) ? { name: err.name, code: err.code, message: err.message } : err;
|
|
139
|
+
|
|
140
|
+
let payload = { msg, account, err: reported };
|
|
141
|
+
if (suppressedFailures) {
|
|
142
|
+
payload.suppressed = suppressedFailures;
|
|
143
|
+
}
|
|
144
|
+
logger.error(payload);
|
|
145
|
+
|
|
146
|
+
lastFailureReport = now;
|
|
147
|
+
suppressedFailures = 0;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// Members are referenced by name instead of through `this` as this object is passed
|
|
151
|
+
// around to client classes that might call log() detached from its receiver
|
|
152
|
+
let accountLogger = {
|
|
120
153
|
enabled: logging.enabled,
|
|
121
154
|
maxLogLines: logging.maxLogLines,
|
|
155
|
+
|
|
122
156
|
log(entry) {
|
|
123
|
-
if (!
|
|
157
|
+
if (!accountLogger.maxLogLines || !accountLogger.enabled) {
|
|
124
158
|
return;
|
|
125
159
|
}
|
|
126
160
|
|
|
@@ -134,19 +168,22 @@ class ConnectionHandler {
|
|
|
134
168
|
redis
|
|
135
169
|
.multi()
|
|
136
170
|
.rpush(logKey, logRow)
|
|
137
|
-
.ltrim(logKey, -
|
|
171
|
+
.ltrim(logKey, -accountLogger.maxLogLines, -1)
|
|
138
172
|
.exec()
|
|
139
|
-
.catch(err =>
|
|
173
|
+
.catch(err => reportFailure('Failed to update log entries', err));
|
|
140
174
|
} catch (err) {
|
|
141
|
-
|
|
175
|
+
reportFailure('Failed to encode log entry', err);
|
|
142
176
|
}
|
|
143
177
|
},
|
|
178
|
+
|
|
144
179
|
async reload() {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
180
|
+
let updated = await settings.getLoggingInfo(account);
|
|
181
|
+
accountLogger.enabled = updated.enabled;
|
|
182
|
+
accountLogger.maxLogLines = updated.maxLogLines;
|
|
148
183
|
}
|
|
149
184
|
};
|
|
185
|
+
|
|
186
|
+
return accountLogger;
|
|
150
187
|
}
|
|
151
188
|
|
|
152
189
|
async assignConnection(account, runIndex, initOpts) {
|
|
@@ -264,7 +301,7 @@ class ConnectionHandler {
|
|
|
264
301
|
|
|
265
302
|
if (accountData.state) {
|
|
266
303
|
await redis.hSetExists(accountObject.connection.getAccountKey(), 'state', accountData.state);
|
|
267
|
-
await emitChangeEvent(
|
|
304
|
+
await emitChangeEvent(logger, account, 'state', accountData.state);
|
|
268
305
|
}
|
|
269
306
|
|
|
270
307
|
// do not wait before returning as it may take forever
|
|
@@ -299,7 +336,7 @@ class ConnectionHandler {
|
|
|
299
336
|
let state = 'connecting';
|
|
300
337
|
await redis.hSetExists(accountObject.connection.getAccountKey(), 'state', state);
|
|
301
338
|
accountObject.connection.state = state;
|
|
302
|
-
await emitChangeEvent(
|
|
339
|
+
await emitChangeEvent(logger, account, 'state', state);
|
|
303
340
|
await accountObject.connection.reconnect(true);
|
|
304
341
|
}
|
|
305
342
|
}
|
package/workers/smtp.js
CHANGED
|
@@ -17,8 +17,7 @@ const util = require('util');
|
|
|
17
17
|
const { redis } = require('../lib/db');
|
|
18
18
|
const { Account } = require('../lib/account');
|
|
19
19
|
const getSecret = require('../lib/get-secret');
|
|
20
|
-
const {
|
|
21
|
-
const { HeadersRewriter } = require('../lib/headers-rewriter');
|
|
20
|
+
const { collectMessage } = require('../lib/smtp-message-processor');
|
|
22
21
|
const settings = require('../lib/settings');
|
|
23
22
|
|
|
24
23
|
const { encrypt, decrypt } = require('../lib/encrypt');
|
|
@@ -113,31 +112,6 @@ for (let level of ['trace', 'debug', 'info', 'warn', 'error', 'fatal']) {
|
|
|
113
112
|
// so onAuth caches the Account for later processing steps.
|
|
114
113
|
const onAuth = createSmtpAuthHandler({ accountCache: ACCOUNT_CACHE, call });
|
|
115
114
|
|
|
116
|
-
function processMessage(stream, session, meta) {
|
|
117
|
-
meta = meta || {};
|
|
118
|
-
const splitter = new Splitter();
|
|
119
|
-
const joiner = new Joiner();
|
|
120
|
-
|
|
121
|
-
const headersRewriter = new HeadersRewriter(async headers => {
|
|
122
|
-
let requestedAccount = headers.getFirst('x-ee-account');
|
|
123
|
-
headers.remove('x-ee-account');
|
|
124
|
-
if (requestedAccount) {
|
|
125
|
-
meta.requestedAccount = requestedAccount;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
let idempotencyKey = headers.getFirst('x-ee-idempotency-key');
|
|
129
|
-
headers.remove('x-ee-idempotency-key');
|
|
130
|
-
if (idempotencyKey) {
|
|
131
|
-
meta.idempotencyKey = idempotencyKey;
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
stream.once('error', err => joiner.emit('error', err));
|
|
136
|
-
headersRewriter.once('error', err => joiner.emit('error', err));
|
|
137
|
-
|
|
138
|
-
return stream.pipe(splitter).pipe(headersRewriter).pipe(joiner);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
115
|
async function checkAccountData(session, messageMeta) {
|
|
142
116
|
let accountObject;
|
|
143
117
|
|
|
@@ -243,74 +217,59 @@ async function init() {
|
|
|
243
217
|
};
|
|
244
218
|
|
|
245
219
|
serverOptions.onData = (rawStream, session, callback) => {
|
|
246
|
-
let chunks = [];
|
|
247
|
-
let chunklen = 0;
|
|
248
|
-
|
|
249
220
|
let messageMeta = {};
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
221
|
+
|
|
222
|
+
// Collecting the message lives in lib/smtp-message-processor.js so that the control
|
|
223
|
+
// header stripping, the stream error handling and the size verdict are unit testable -
|
|
224
|
+
// this worker can not be required from a test, it boots an SMTP server on load.
|
|
225
|
+
// collectMessage() rejects on a processing error instead of leaving it unhandled, which
|
|
226
|
+
// used to kill the entire worker thread rather than the one failing submission.
|
|
227
|
+
collectMessage(rawStream, messageMeta)
|
|
228
|
+
.then(async ({ message, sizeExceeded }) => {
|
|
229
|
+
if (sizeExceeded) {
|
|
230
|
+
let err = new Error('Message exceeds fixed maximum message size');
|
|
231
|
+
err.responseCode = 552;
|
|
232
|
+
throw err;
|
|
258
233
|
}
|
|
259
|
-
}
|
|
260
|
-
});
|
|
261
234
|
|
|
262
|
-
|
|
263
|
-
let err;
|
|
264
|
-
if (stream.sizeExceeded) {
|
|
265
|
-
err = new Error('Message exceeds fixed maximum message size');
|
|
266
|
-
err.responseCode = 552;
|
|
267
|
-
return callback(err);
|
|
268
|
-
}
|
|
235
|
+
let accountObject = await checkAccountData(session, messageMeta);
|
|
269
236
|
|
|
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
|
-
event: 'smtpSubmitFail'
|
|
307
|
-
});
|
|
308
|
-
logger.error({ msg: 'Failed to submit message', account: session.user, err });
|
|
309
|
-
callback(err);
|
|
310
|
-
});
|
|
311
|
-
})
|
|
312
|
-
.catch(err => callback(err));
|
|
313
|
-
});
|
|
237
|
+
let payload = {
|
|
238
|
+
envelope: {
|
|
239
|
+
from: session.envelope.mailFrom.address,
|
|
240
|
+
to: session.envelope.rcptTo.map(entry => entry.address)
|
|
241
|
+
},
|
|
242
|
+
raw: message
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
let res = await accountObject.queueMessage(payload, {
|
|
246
|
+
source: 'smtp',
|
|
247
|
+
idempotencyKey: messageMeta.idempotencyKey
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// queued for later
|
|
251
|
+
metrics(logger, 'events', 'inc', {
|
|
252
|
+
event: 'smtpSubmitQueued'
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
logger.info({
|
|
256
|
+
msg: 'Message queued',
|
|
257
|
+
account: session.user,
|
|
258
|
+
messageId: res.messageId,
|
|
259
|
+
sendAt: res.sendAt,
|
|
260
|
+
queueId: res.queueId,
|
|
261
|
+
idempotency: res.idempotency
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
callback(null, `Message queued for delivery as ${res.queueId} (${new Date(res.sendAt).toISOString()})`);
|
|
265
|
+
})
|
|
266
|
+
.catch(err => {
|
|
267
|
+
metrics(logger, 'events', 'inc', {
|
|
268
|
+
event: 'smtpSubmitFail'
|
|
269
|
+
});
|
|
270
|
+
logger.error({ msg: 'Failed to submit message', account: session.user, err });
|
|
271
|
+
callback(err);
|
|
272
|
+
});
|
|
314
273
|
};
|
|
315
274
|
|
|
316
275
|
let tls = await settings.get('smtpServerTLSEnabled');
|