emailengine-app 2.84.0 → 2.84.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/.dockerignore +6 -0
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +35 -0
- package/Dockerfile +2 -2
- package/bin/emailengine.js +15 -0
- package/data/google-crawlers.json +1 -1
- package/docker-compose.yml +1 -1
- package/lib/account/own-account-key.js +42 -0
- package/lib/account-assignment.js +1 -33
- package/lib/account.js +32 -19
- package/lib/add-trackers.js +11 -4
- package/lib/api-routes/account-routes.js +67 -14
- package/lib/api-routes/gateway-routes.js +9 -3
- package/lib/api-routes/message-routes.js +7 -1
- package/lib/api-routes/mint-bounds.js +11 -0
- package/lib/api-routes/route-helpers.js +125 -3
- package/lib/api-routes/submit-routes.js +4 -2
- package/lib/auth-token.js +23 -10
- package/lib/bounce-detect.js +17 -5
- package/lib/consts.js +34 -0
- package/lib/create-imap-client.js +41 -2
- package/lib/email-client/base-client.js +20 -10
- package/lib/email-client/gmail-client.js +21 -7
- package/lib/email-client/imap/mailbox.js +10 -4
- package/lib/email-client/imap/response-metrics.js +75 -0
- package/lib/email-client/imap-client.js +5 -2
- package/lib/email-client/message-builder.js +7 -2
- package/lib/email-client/outlook/graph-api.js +17 -2
- package/lib/email-client/outlook-client.js +6 -3
- package/lib/email-client/smtp-pool-manager.js +6 -0
- package/lib/encrypt.js +9 -0
- package/lib/export.js +3 -1
- package/lib/get-raw-email.js +4 -2
- package/lib/imap-proxy-auth.js +14 -1
- package/lib/imapproxy/imap-core/lib/commands/authenticate-plain.js +1 -0
- package/lib/imapproxy/imap-core/lib/commands/login.js +1 -0
- package/lib/imapproxy/imap-core/lib/imap-command.js +86 -43
- package/lib/imapproxy/imap-core/lib/imap-stream.js +58 -13
- package/lib/imapproxy/imap-core/lib/indexer/indexer.js +3 -1
- package/lib/mcp/listen.js +10 -1
- package/lib/oauth/external-account-config.js +3 -1
- package/lib/oauth/gmail.js +21 -48
- package/lib/oauth/mail-ru.js +5 -38
- package/lib/oauth/outlook.js +26 -44
- package/lib/pbkdf2-phc.js +18 -1
- package/lib/privileged-settings.js +6 -3
- package/lib/redis-scan-delete.js +15 -1
- package/lib/schemas.js +78 -32
- package/lib/settings.js +13 -1
- package/lib/smtp-auth.js +23 -0
- package/lib/tools.js +245 -56
- package/lib/ui-routes/account-routes.js +16 -37
- package/lib/ui-routes/admin-config-routes.js +10 -38
- package/lib/ui-routes/admin-entities-routes.js +22 -24
- package/lib/ui-routes/auth-routes.js +47 -24
- package/lib/ui-routes/connection-security-form.js +74 -0
- package/lib/ui-routes/form-lists.js +46 -0
- package/lib/ui-routes/mcp-consent-routes.js +5 -1
- package/lib/ui-routes/oauth-config-routes.js +9 -18
- package/lib/ui-routes/sign-in-helpers.js +59 -0
- package/lib/ui-routes/unsubscribe-routes.js +6 -3
- package/lib/utils/map-concurrent.js +36 -0
- package/lib/utils/network.js +28 -0
- package/lib/utils/provider-id.js +27 -0
- package/lib/utils/require-tls.js +33 -0
- package/lib/web-safe-html.js +5 -6
- package/lib/webhook-penalty.js +75 -0
- package/lib/webhook-routing.js +22 -2
- package/package.json +21 -21
- package/sbom.json +1 -1
- package/server.js +74 -38
- package/static/js/ace/ace.js +1 -1
- package/static/js/ace/mode-handlebars.js +1 -1
- package/static/js/ace/mode-html.js +1 -1
- package/static/js/ace/mode-javascript.js +1 -1
- package/static/js/ace/mode-markdown.js +1 -1
- package/static/js/ace/worker-html.js +1 -1
- package/static/js/ace/worker-javascript.js +1 -1
- package/static/js/ace/worker-json.js +1 -1
- package/static/js/ee-client.js +5 -0
- package/static/licenses.html +23 -23
- package/translations/messages.pot +66 -66
- package/views/account/security.hbs +10 -0
- package/views/accounts/account.hbs +11 -11
- package/views/accounts/edit.hbs +6 -2
- package/views/partials/gateway_form.hbs +3 -1
- package/workers/api.js +43 -3
- package/workers/webhooks.js +39 -3
- package/config/test.toml +0 -46
- package/install.sh +0 -428
- package/setup-production.sh +0 -215
- package/systemd/emailengine.service +0 -97
- package/systemd/nginx-proxy.conf +0 -77
package/server.js
CHANGED
|
@@ -20,19 +20,23 @@
|
|
|
20
20
|
// same way. Set before anything can require bullmq; worker threads share this environment.
|
|
21
21
|
process.env.MSGPACKR_NATIVE_ACCELERATION_DISABLED = 'true';
|
|
22
22
|
|
|
23
|
-
//
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
// Attempt to change working directory to script location
|
|
23
|
+
// Attempt to change working directory to script location. Before anything reads a file by a
|
|
24
|
+
// relative path: `.env` below and the `config/` directory wild-config loads (including executable
|
|
25
|
+
// `.js` configuration files, run with the real require) are both resolved against the working
|
|
26
|
+
// directory, and the directory the operator happened to start from is not where this instance's
|
|
27
|
+
// configuration lives
|
|
30
28
|
try {
|
|
31
29
|
process.chdir(__dirname);
|
|
32
30
|
} catch (err) {
|
|
33
31
|
// ignore - may fail in some containerized environments
|
|
34
32
|
}
|
|
35
33
|
|
|
34
|
+
// Load environment variables if not already loaded
|
|
35
|
+
if (!process.env.EE_ENV_LOADED) {
|
|
36
|
+
require('dotenv').config({ quiet: true });
|
|
37
|
+
process.env.EE_ENV_LOADED = 'true';
|
|
38
|
+
}
|
|
39
|
+
|
|
36
40
|
// Set process title for easier identification in process lists
|
|
37
41
|
process.title = 'emailengine';
|
|
38
42
|
|
|
@@ -146,10 +150,10 @@ const {
|
|
|
146
150
|
releaseWorkerAccounts,
|
|
147
151
|
requeueFailedAccounts,
|
|
148
152
|
forgetAccount,
|
|
149
|
-
mapConcurrent,
|
|
150
153
|
shouldRequeueFailedAssignment,
|
|
151
154
|
planRebalance
|
|
152
155
|
} = require('./lib/account-assignment');
|
|
156
|
+
const { mapConcurrent } = require('./lib/utils/map-concurrent');
|
|
153
157
|
const { RespawnTracker } = require('./lib/respawn-backoff');
|
|
154
158
|
|
|
155
159
|
const msgpack = require('./lib/msgpack');
|
|
@@ -793,6 +797,7 @@ let workerAssigned = new WeakMap(); // Map of worker -> Set of accounts
|
|
|
793
797
|
let onlineWorkers = new WeakSet(); // Set of workers that are online
|
|
794
798
|
let reassignmentTimer = null; // Timer for failsafe reassignment
|
|
795
799
|
let reassignmentPending = false; // Flag to track if reassignment is pending
|
|
800
|
+
const REASSIGNMENT_FAILSAFE_DELAY = 10 * 1000; // How long an exited IMAP worker gets to restart before its accounts move
|
|
796
801
|
let assignRetryCount = 0; // Counter for safety-net retries in assignAccounts
|
|
797
802
|
const MAX_ASSIGN_RETRIES = 3; // Max consecutive safety-net retries before giving up
|
|
798
803
|
|
|
@@ -1155,7 +1160,7 @@ let spawnWorker = async (type, opts, slot) => {
|
|
|
1155
1160
|
|
|
1156
1161
|
// A fresh spawn opens its own slot. Counting the crash streak per type made every short-lived
|
|
1157
1162
|
// exit of ANY IMAP worker double the next respawn delay of all of them, so unrelated exits
|
|
1158
|
-
// pushed a restart past the
|
|
1163
|
+
// pushed a restart past the failsafe reassignment (REASSIGNMENT_FAILSAFE_DELAY).
|
|
1159
1164
|
if (!slot) {
|
|
1160
1165
|
slot = `${type}:${++spawnSlotCounter}`;
|
|
1161
1166
|
}
|
|
@@ -1297,24 +1302,8 @@ let spawnWorker = async (type, opts, slot) => {
|
|
|
1297
1302
|
if (unassigned && unassigned.size > 0) {
|
|
1298
1303
|
reassignmentPending = true;
|
|
1299
1304
|
|
|
1300
|
-
//
|
|
1301
|
-
|
|
1302
|
-
clearTimeout(reassignmentTimer);
|
|
1303
|
-
}
|
|
1304
|
-
|
|
1305
|
-
// Set a failsafe timer - if worker doesn't restart in 10 seconds, reassign anyway
|
|
1306
|
-
reassignmentTimer = setTimeout(() => {
|
|
1307
|
-
if (reassignmentPending && unassigned && unassigned.size > 0) {
|
|
1308
|
-
logger.warn({
|
|
1309
|
-
msg: 'Failsafe reassignment triggered - worker restart timeout',
|
|
1310
|
-
unassignedAccounts: unassigned.size,
|
|
1311
|
-
currentWorkers: availableIMAPWorkers.size,
|
|
1312
|
-
expectedWorkers: config.workers.imap
|
|
1313
|
-
});
|
|
1314
|
-
reassignmentPending = false;
|
|
1315
|
-
assignAccounts().catch(err => logger.error({ msg: 'Unable to reassign accounts (failsafe)', err }));
|
|
1316
|
-
}
|
|
1317
|
-
}, 10000); // 10 second timeout
|
|
1305
|
+
// If the worker does not restart in time, its accounts go to the others anyway
|
|
1306
|
+
armReassignmentFailsafe();
|
|
1318
1307
|
|
|
1319
1308
|
logger[exitLevel]({
|
|
1320
1309
|
msg: 'Worker exited, waiting for restart before reassignment',
|
|
@@ -1658,6 +1647,11 @@ let spawnWorker = async (type, opts, slot) => {
|
|
|
1658
1647
|
expectedWorkers: config.workers.imap,
|
|
1659
1648
|
unassignedAccounts: unassigned.size
|
|
1660
1649
|
});
|
|
1650
|
+
// The failsafe found no worker at all and stopped; give the rest
|
|
1651
|
+
// of the fleet the same time to come back from here
|
|
1652
|
+
if (!reassignmentTimer) {
|
|
1653
|
+
armReassignmentFailsafe();
|
|
1654
|
+
}
|
|
1661
1655
|
}
|
|
1662
1656
|
} else if (imapInitialWorkersLoaded) {
|
|
1663
1657
|
// The failsafe timer already gave this worker's accounts to the
|
|
@@ -1791,6 +1785,43 @@ async function unassignFromWorker(worker, account) {
|
|
|
1791
1785
|
}
|
|
1792
1786
|
}
|
|
1793
1787
|
|
|
1788
|
+
/**
|
|
1789
|
+
* Starts (or restarts) the failsafe timer that gives the accounts an exited IMAP worker left behind
|
|
1790
|
+
* to the remaining workers when it does not come back in time
|
|
1791
|
+
*/
|
|
1792
|
+
function armReassignmentFailsafe() {
|
|
1793
|
+
clearTimeout(reassignmentTimer);
|
|
1794
|
+
|
|
1795
|
+
reassignmentTimer = setTimeout(() => {
|
|
1796
|
+
reassignmentTimer = null;
|
|
1797
|
+
if (!reassignmentPending || !unassigned || !unassigned.size) {
|
|
1798
|
+
return;
|
|
1799
|
+
}
|
|
1800
|
+
|
|
1801
|
+
if (!availableIMAPWorkers.size) {
|
|
1802
|
+
// Every worker exited at once (the clean exit after a Redis reconnection) and none is
|
|
1803
|
+
// back yet. A pass now would find no worker and clear the pending flag that makes the
|
|
1804
|
+
// returning workers pick the accounts up, stranding them until a process restart. Keep
|
|
1805
|
+
// it pending instead; the first worker to come back arms this timer again
|
|
1806
|
+
logger.warn({
|
|
1807
|
+
msg: 'Failsafe reassignment postponed - no IMAP worker is available yet',
|
|
1808
|
+
unassignedAccounts: unassigned.size,
|
|
1809
|
+
expectedWorkers: config.workers.imap
|
|
1810
|
+
});
|
|
1811
|
+
return;
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
logger.warn({
|
|
1815
|
+
msg: 'Failsafe reassignment triggered - worker restart timeout',
|
|
1816
|
+
unassignedAccounts: unassigned.size,
|
|
1817
|
+
currentWorkers: availableIMAPWorkers.size,
|
|
1818
|
+
expectedWorkers: config.workers.imap
|
|
1819
|
+
});
|
|
1820
|
+
reassignmentPending = false;
|
|
1821
|
+
assignAccounts().catch(err => logger.error({ msg: 'Unable to reassign accounts (failsafe)', err }));
|
|
1822
|
+
}, REASSIGNMENT_FAILSAFE_DELAY);
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1794
1825
|
/**
|
|
1795
1826
|
* Moves a fair share of accounts onto an IMAP worker that became ready while the rest of the fleet
|
|
1796
1827
|
* already served every account. That is the normal outcome of a restart slower than the failsafe
|
|
@@ -1809,9 +1840,6 @@ async function rebalanceOnto(worker) {
|
|
|
1809
1840
|
}
|
|
1810
1841
|
|
|
1811
1842
|
let moves = planRebalance({ workerAssigned, availableWorkers: availableIMAPWorkers }, worker);
|
|
1812
|
-
if (!moves.length) {
|
|
1813
|
-
return;
|
|
1814
|
-
}
|
|
1815
1843
|
|
|
1816
1844
|
assigning = true;
|
|
1817
1845
|
let moved = 0;
|
|
@@ -1843,8 +1871,9 @@ async function rebalanceOnto(worker) {
|
|
|
1843
1871
|
logger.info({ msg: 'Rebalancing accounts onto a restarted IMAP worker', threadId: worker.threadId, accounts: moved });
|
|
1844
1872
|
}
|
|
1845
1873
|
// Also run when nothing moved: an account added while this held the assignment lock found the
|
|
1846
|
-
// pass refused and is waiting in unassigned
|
|
1847
|
-
// picks the least loaded
|
|
1874
|
+
// pass refused and is waiting in unassigned, or nothing was there to move because every worker
|
|
1875
|
+
// had exited and the accounts are all waiting. The pass picks the least loaded worker, which
|
|
1876
|
+
// is the newcomer.
|
|
1848
1877
|
if (moved || unassigned.size) {
|
|
1849
1878
|
await assignAccounts();
|
|
1850
1879
|
}
|
|
@@ -2523,10 +2552,15 @@ const openAiRequestOpts = () => ({
|
|
|
2523
2552
|
|
|
2524
2553
|
const aiValueSet = value => value !== undefined && value !== null && value !== '';
|
|
2525
2554
|
|
|
2555
|
+
const aiUrlValue = value => (value === undefined || value === null ? '' : String(value).trim());
|
|
2556
|
+
|
|
2526
2557
|
/**
|
|
2527
2558
|
* Options for one request to the AI provider. The configuration page's test sends every field as
|
|
2528
2559
|
* its form shows it, so the test runs with what the page shows; the sync path sends none and gets
|
|
2529
|
-
* the stored settings. The key is the exception: the form leaves it blank to keep the stored one
|
|
2560
|
+
* the stored settings. The key is the exception: the form leaves it blank to keep the stored one,
|
|
2561
|
+
* and a request made with the stored key goes to the stored URL, since a write-only secret must
|
|
2562
|
+
* not be extractable by pointing a request that falls back to it at a server of one's own. The
|
|
2563
|
+
* rule is here rather than on the page's routes so every request the form can make is under it
|
|
2530
2564
|
* @param {Object} data - The command's data
|
|
2531
2565
|
* @returns {Promise<{openAiAPIKey: string, requestOpts: Object}>}
|
|
2532
2566
|
*/
|
|
@@ -2538,6 +2572,9 @@ async function aiRequestOptions(data) {
|
|
|
2538
2572
|
if (!openAiAPIKey) {
|
|
2539
2573
|
throw new Error(`OpenAI API key is not configured`);
|
|
2540
2574
|
}
|
|
2575
|
+
if (data.options && !data.options.openAiAPIKey && aiUrlValue(data.options.openAiAPIUrl) !== aiUrlValue(stored.openAiAPIUrl)) {
|
|
2576
|
+
throw new Error('Enter the API key to use a different API URL');
|
|
2577
|
+
}
|
|
2541
2578
|
|
|
2542
2579
|
const requestOpts = openAiRequestOpts();
|
|
2543
2580
|
requestOpts.gptModel = aiValueSet(values.openAiModel) ? values.openAiModel : openAiDefaultModel;
|
|
@@ -3429,11 +3466,10 @@ const startApplication = async () => {
|
|
|
3429
3466
|
await settings.set('enableApiProxy', HAS_API_PROXY_SET ? API_PROXY : true);
|
|
3430
3467
|
}
|
|
3431
3468
|
|
|
3432
|
-
// Service settings
|
|
3433
|
-
|
|
3434
|
-
|
|
3435
|
-
|
|
3436
|
-
}
|
|
3469
|
+
// Service settings. HSETNX rather than read-then-write: a stored secret that does not decrypt
|
|
3470
|
+
// (the wrong EENGINE_SECRET) reads as null, and overwriting it would destroy a value the
|
|
3471
|
+
// rotation CLI can still recover once the right secret is back
|
|
3472
|
+
await settings.setIfMissing('serviceSecret', crypto.randomBytes(16).toString('hex'));
|
|
3437
3473
|
|
|
3438
3474
|
let existingQueueKeep = await settings.get('queueKeep');
|
|
3439
3475
|
if (existingQueueKeep === null) {
|