emailengine-app 2.87.1 → 2.88.0
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/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +31 -0
- package/ENVIRONMENT.md +26 -23
- package/LICENSE_EMAILENGINE.txt +20 -12
- package/app.json +3 -3
- package/bin/emailengine.js +7 -11
- package/data/google-crawlers.json +1 -1
- package/data/public_suffix_list.dat +48 -10
- package/lib/account.js +69 -49
- package/lib/admin-password.js +85 -0
- package/lib/api-routes/license-routes.js +9 -0
- package/lib/api-token-auth.js +21 -5
- package/lib/calendar-event.js +15 -3
- package/lib/consts.js +5 -0
- package/lib/db.js +1 -0
- package/lib/dbsc/index.js +292 -0
- package/lib/dbsc/jwt.js +157 -0
- package/lib/dbsc/protocol.js +132 -0
- package/lib/dbsc/store.js +136 -0
- package/lib/email-client/base-client.js +36 -15
- package/lib/email-client/gmail-client.js +20 -6
- package/lib/email-client/imap/connection-errors.js +30 -1
- package/lib/email-client/imap/session-expiry.js +83 -0
- package/lib/email-client/imap-client.js +12 -2
- package/lib/email-client/smtp-pool-manager.js +66 -8
- package/lib/export.js +147 -22
- package/lib/graph-notifications.js +60 -0
- package/lib/imap-proxy-auth.js +52 -18
- package/lib/imapproxy/imap-core/lib/commands/id.js +4 -2
- package/lib/imapproxy/imap-core/lib/imap-command.js +44 -4
- package/lib/imapproxy/imap-server.js +10 -1
- package/lib/imapproxy/proxy-handoff.js +100 -5
- package/lib/license-subscription.js +238 -0
- package/lib/oauth/gmail.js +18 -0
- package/lib/oauth/outlook-identity.js +54 -12
- package/lib/prepared-password.js +38 -0
- package/lib/privileged-settings.js +6 -0
- package/lib/queue-browser/index.js +6 -6
- package/lib/queue-job-actions.js +59 -4
- package/lib/read-env-value.js +46 -21
- package/lib/redis-url.js +23 -0
- package/lib/response-stream.js +69 -1
- package/lib/revocation-events.js +79 -0
- package/lib/routes-ui.js +4 -1
- package/lib/schemas.js +26 -2
- package/lib/session-default.js +13 -3
- package/lib/settings.js +47 -24
- package/lib/sso.js +23 -0
- package/lib/submit-penalty.js +24 -0
- package/lib/submit-processor.js +10 -2
- package/lib/tokens.js +6 -0
- package/lib/tools.js +86 -15
- package/lib/ui-routes/admin-config-routes.js +30 -281
- package/lib/ui-routes/auth-routes.js +17 -64
- package/lib/ui-routes/dbsc-routes.js +43 -0
- package/lib/ui-routes/license-routes.js +326 -0
- package/lib/web-safe-html.js +29 -0
- package/lib/webhook-delivery.js +21 -20
- package/lib/webhook-penalty.js +18 -17
- package/lib/webhooks.js +18 -0
- package/package.json +18 -18
- package/sbom.json +294 -294
- package/scan.js +2 -0
- package/server.js +253 -80
- package/static/css/flyonui.css +2 -2
- package/static/js/ui.js +11 -4
- package/static/licenses.html +27 -27
- package/translations/messages.pot +17 -17
- package/views/config/license.hbs +43 -16
- package/views/config/security.hbs +37 -0
- package/views/partials/side_menu.hbs +1 -1
- package/workers/api.js +128 -109
- package/workers/imap.js +4 -6
package/scan.js
CHANGED
package/server.js
CHANGED
|
@@ -40,6 +40,16 @@ if (!process.env.EE_ENV_LOADED) {
|
|
|
40
40
|
// Set process title for easier identification in process lists
|
|
41
41
|
process.title = 'emailengine';
|
|
42
42
|
|
|
43
|
+
// Every EENGINE_*_FILE is read now: a file the operator named and the process can not read stops
|
|
44
|
+
// the boot here, once and by name, rather than resolving to an empty value (which every reader
|
|
45
|
+
// takes for "not configured") or crashing each worker thread that reads it later.
|
|
46
|
+
try {
|
|
47
|
+
require('./lib/read-env-value').resolveEnvFiles();
|
|
48
|
+
} catch (err) {
|
|
49
|
+
console.error(err.message);
|
|
50
|
+
process.exit(1);
|
|
51
|
+
}
|
|
52
|
+
|
|
43
53
|
// EmailEngine and its dependencies need Node 20 or newer. Checked before anything is required, so an
|
|
44
54
|
// older runtime gets this line instead of a syntax error from a dependency. The check used to be a
|
|
45
55
|
// structuredClone probe, which only caught Node 16 and older.
|
|
@@ -55,6 +65,7 @@ if (Number(process.versions.node.split('.')[0]) < 20) {
|
|
|
55
65
|
// their own but not the Node they run on, and an instance that works today must not stop at its next
|
|
56
66
|
// self-upgrade over a minor version it may never exercise.
|
|
57
67
|
const { accountDataKey } = require('./lib/redis-keys');
|
|
68
|
+
const { importPreparedPassword } = require('./lib/prepared-password');
|
|
58
69
|
const { MIN_SUPPORTED_NODE, isBelowSupportedNode } = require('./lib/node-version');
|
|
59
70
|
if (isBelowSupportedNode(process.versions.node)) {
|
|
60
71
|
console.error(
|
|
@@ -149,8 +160,18 @@ const { settingsSchema } = require('./lib/schemas');
|
|
|
149
160
|
const settings = require('./lib/settings');
|
|
150
161
|
const { oauth2Apps } = require('./lib/oauth2-apps');
|
|
151
162
|
const { backfillAuthFailureDisabled } = require('./lib/account/auth-failure-backfill');
|
|
163
|
+
const { backfillExportIndex } = require('./lib/export');
|
|
152
164
|
const { sweepBounceStore } = require('./lib/account/bounce-store-sweep');
|
|
153
165
|
const { attachBeacon, persistBeaconMarkers } = require('./lib/license-beacon');
|
|
166
|
+
const {
|
|
167
|
+
decide,
|
|
168
|
+
startupPlan,
|
|
169
|
+
expiredSuspension,
|
|
170
|
+
checkInterval,
|
|
171
|
+
subscriptionView,
|
|
172
|
+
parseSuspension,
|
|
173
|
+
isSubscriptionLicense
|
|
174
|
+
} = require('./lib/license-subscription');
|
|
154
175
|
const tokens = require('./lib/tokens');
|
|
155
176
|
|
|
156
177
|
const { checkRateLimit } = require('./lib/rate-limit');
|
|
@@ -231,11 +252,6 @@ const EENGINE_TIMEOUT = getDuration(readEnvValue('EENGINE_TIMEOUT') || config.se
|
|
|
231
252
|
// Size limits
|
|
232
253
|
const DEFAULT_MAX_ATTACHMENT_SIZE = 5 * 1024 * 1024; // 5MB
|
|
233
254
|
|
|
234
|
-
// License check intervals
|
|
235
|
-
const SUBSCRIPTION_CHECK_TIMEOUT = 1 * 24 * 60 * 60 * 1000; // 24 hours
|
|
236
|
-
const SUBSCRIPTION_RECHECK_TIMEOUT = 1 * 60 * 60 * 1000; // 1 hour
|
|
237
|
-
const SUBSCRIPTION_ALLOW_DELAY = 28 * 24 * 60 * 60 * 1000; // 28 days grace period
|
|
238
|
-
|
|
239
255
|
// Delay between account connection setups (to avoid overwhelming the system)
|
|
240
256
|
const CONNECTION_SETUP_DELAY = getDuration(readEnvValue('EENGINE_CONNECTION_SETUP_DELAY') || config.service.setupDelay) || 0;
|
|
241
257
|
|
|
@@ -333,7 +349,6 @@ WORKER_DIED_RESP_ERR.code = 'WorkerNotAvailable';
|
|
|
333
349
|
// Update check intervals
|
|
334
350
|
const UPGRADE_CHECK_TIMEOUT = 1 * 24 * 3600 * 1000; // 24 hours
|
|
335
351
|
const LICENSE_CHECK_TIMEOUT = 20 * 60 * 1000; // 20 minutes
|
|
336
|
-
const MAX_LICENSE_CHECK_DELAY = 30 * 24 * 60 * 60 * 1000; // 30 days
|
|
337
352
|
|
|
338
353
|
/**
|
|
339
354
|
* License information object
|
|
@@ -2147,28 +2162,128 @@ async function assignAccounts() {
|
|
|
2147
2162
|
// License checking state
|
|
2148
2163
|
let licenseCheckTimer = false;
|
|
2149
2164
|
let checkingLicense = false;
|
|
2165
|
+
// set by startApplication: the stored subscription state asks for an online check at startup
|
|
2166
|
+
let startupLicenseCheck = false;
|
|
2167
|
+
|
|
2168
|
+
// The worker types that run only under a valid license; everything but the API server
|
|
2169
|
+
const LICENSED_WORKER_TYPES = ['imap', 'submit', 'smtp', 'webhooks', 'imapProxy'];
|
|
2170
|
+
|
|
2171
|
+
// The stored subscription deadline (`subexp`, ms) as last written by this thread, so the license
|
|
2172
|
+
// command answering every request does not read Redis for it
|
|
2173
|
+
let subscriptionDeadline = null;
|
|
2174
|
+
|
|
2175
|
+
/**
|
|
2176
|
+
* Records a subscription license as suspended, or no longer suspended. The workers follow at the
|
|
2177
|
+
* end of the next license check (licenseCheckHandler)
|
|
2178
|
+
* @param {Object|null} suspension - the subsusp record, or null to lift a suspension
|
|
2179
|
+
* @returns {void}
|
|
2180
|
+
*/
|
|
2181
|
+
const setSuspension = suspension => {
|
|
2182
|
+
if (suspension) {
|
|
2183
|
+
licenseInfo.active = false;
|
|
2184
|
+
licenseInfo.suspension = suspension;
|
|
2185
|
+
} else {
|
|
2186
|
+
licenseInfo.active = true;
|
|
2187
|
+
delete licenseInfo.suspension;
|
|
2188
|
+
}
|
|
2189
|
+
};
|
|
2190
|
+
|
|
2191
|
+
/**
|
|
2192
|
+
* Applies one decision of lib/license-subscription.js to the stored state and to licenseInfo
|
|
2193
|
+
* @param {Object} decision - decide() output
|
|
2194
|
+
* @param {Object} data - the license server's answer
|
|
2195
|
+
* @returns {Promise<void>}
|
|
2196
|
+
*/
|
|
2197
|
+
const applySubscriptionDecision = async (decision, data) => {
|
|
2198
|
+
const settingsKey = `${REDIS_PREFIX}settings`;
|
|
2199
|
+
|
|
2200
|
+
if (decision.removeLicense) {
|
|
2201
|
+
// what a license server that names no deadline and does not ask to keep the key gets
|
|
2202
|
+
logger.warn({ msg: 'License validation failed', license: redactLicense(licenseInfo.details), data });
|
|
2203
|
+
await settings.removeLicense();
|
|
2204
|
+
subscriptionDeadline = null;
|
|
2205
|
+
licenseInfo.active = false;
|
|
2206
|
+
licenseInfo.details = false;
|
|
2207
|
+
delete licenseInfo.suspension;
|
|
2208
|
+
licenseInfo.type = packageData.license;
|
|
2209
|
+
return;
|
|
2210
|
+
}
|
|
2211
|
+
|
|
2212
|
+
let multi = redis.multi();
|
|
2213
|
+
if (decision.clear) {
|
|
2214
|
+
multi = multi.hdel(settingsKey, 'subexp', 'subsusp');
|
|
2215
|
+
subscriptionDeadline = null;
|
|
2216
|
+
} else if (decision.resume) {
|
|
2217
|
+
multi = multi.hdel(settingsKey, 'subsusp');
|
|
2218
|
+
}
|
|
2219
|
+
if (decision.subexp) {
|
|
2220
|
+
multi = multi.hset(settingsKey, 'subexp', decision.subexp.toString());
|
|
2221
|
+
subscriptionDeadline = decision.subexp;
|
|
2222
|
+
}
|
|
2223
|
+
if (decision.suspend) {
|
|
2224
|
+
multi = multi.hset(settingsKey, 'subsusp', JSON.stringify(decision.suspend));
|
|
2225
|
+
}
|
|
2226
|
+
if (decision.nextCheck) {
|
|
2227
|
+
multi = multi.hset(settingsKey, 'ks', new Date(decision.nextCheck).getTime().toString(16));
|
|
2228
|
+
}
|
|
2229
|
+
await multi.exec();
|
|
2230
|
+
|
|
2231
|
+
if (decision.resume && licenseInfo.suspension) {
|
|
2232
|
+
logger.info({
|
|
2233
|
+
msg: decision.clear ? 'Subscription in good standing again, resuming' : 'Subscription inside its payment window again, resuming',
|
|
2234
|
+
license: redactLicense(licenseInfo.details)
|
|
2235
|
+
});
|
|
2236
|
+
setSuspension(null);
|
|
2237
|
+
}
|
|
2238
|
+
|
|
2239
|
+
if (decision.suspend) {
|
|
2240
|
+
if (!licenseInfo.suspension) {
|
|
2241
|
+
logger.warn({ msg: 'Subscription not in good standing, suspending until it is', license: redactLicense(licenseInfo.details), data });
|
|
2242
|
+
}
|
|
2243
|
+
setSuspension(decision.suspend);
|
|
2244
|
+
}
|
|
2245
|
+
};
|
|
2246
|
+
|
|
2247
|
+
/**
|
|
2248
|
+
* Tells the API workers to run their prechecks again, so the REST API follows a suspension or a
|
|
2249
|
+
* resume at once instead of at the next ten-minute tick
|
|
2250
|
+
* @returns {void}
|
|
2251
|
+
*/
|
|
2252
|
+
const notifyApiLicenseState = () => {
|
|
2253
|
+
for (let worker of workers.get('api') || []) {
|
|
2254
|
+
try {
|
|
2255
|
+
postMessage(worker, { cmd: 'licenseState' }, true);
|
|
2256
|
+
} catch (err) {
|
|
2257
|
+
logger.error({ msg: 'Unable to notify worker about the license state', worker: worker.threadId, err });
|
|
2258
|
+
}
|
|
2259
|
+
}
|
|
2260
|
+
};
|
|
2150
2261
|
|
|
2151
2262
|
/**
|
|
2152
2263
|
* Check and validate license status
|
|
2153
2264
|
* @param {Object} opts - Options
|
|
2154
|
-
* @param {number} opts.subscriptionCheckTimeout -
|
|
2155
|
-
* @
|
|
2265
|
+
* @param {number} opts.subscriptionCheckTimeout - Minimum time since the last online check
|
|
2266
|
+
* @param {boolean} opts.force - Check online now, whatever the schedule says
|
|
2267
|
+
* @returns {Promise<Object>} { outcome }: ok, refused, error, unreachable, skipped or busy
|
|
2156
2268
|
*/
|
|
2157
2269
|
let licenseCheckHandler = async opts => {
|
|
2158
2270
|
if (checkingLicense) {
|
|
2159
|
-
return;
|
|
2271
|
+
return { outcome: 'busy' };
|
|
2160
2272
|
}
|
|
2161
2273
|
checkingLicense = true;
|
|
2162
2274
|
clearTimeout(licenseCheckTimer);
|
|
2163
2275
|
|
|
2276
|
+
let outcome = 'skipped';
|
|
2277
|
+
let wasActive = licenseInfo.active;
|
|
2278
|
+
|
|
2164
2279
|
try {
|
|
2165
2280
|
opts = opts || {};
|
|
2166
|
-
let { subscriptionCheckTimeout } = opts;
|
|
2167
2281
|
let now = Date.now();
|
|
2168
|
-
|
|
2282
|
+
let [subexp, subsusp, kv, ks] = await redis.hmget(`${REDIS_PREFIX}settings`, 'subexp', 'subsusp', 'kv', 'ks');
|
|
2283
|
+
// daily, and hourly while the subscription is not in good standing
|
|
2284
|
+
let subscriptionCheckTimeout = opts.subscriptionCheckTimeout || checkInterval({ subexp, subsusp });
|
|
2169
2285
|
|
|
2170
2286
|
// Check if we need to validate the license
|
|
2171
|
-
let kv = await redis.hget(`${REDIS_PREFIX}settings`, 'kv');
|
|
2172
2287
|
let checkKv = true;
|
|
2173
2288
|
// an unreadable record means checking again, which also rewrites it
|
|
2174
2289
|
let validatedVersion = kv && typeof kv === 'string' ? Buffer.from(kv, 'hex').toString() : null;
|
|
@@ -2176,7 +2291,6 @@ let licenseCheckHandler = async opts => {
|
|
|
2176
2291
|
checkKv = false;
|
|
2177
2292
|
}
|
|
2178
2293
|
|
|
2179
|
-
let ks = await redis.hget(`${REDIS_PREFIX}settings`, 'ks');
|
|
2180
2294
|
if (ks && typeof ks === 'string') {
|
|
2181
2295
|
let ksDate = new Date(parseInt(ks, 16));
|
|
2182
2296
|
if (ksDate < now) {
|
|
@@ -2187,18 +2301,36 @@ let licenseCheckHandler = async opts => {
|
|
|
2187
2301
|
}
|
|
2188
2302
|
|
|
2189
2303
|
// Clean up for lifetime licenses
|
|
2190
|
-
if (licenseInfo.details.lt) {
|
|
2191
|
-
await redis.hdel(`${REDIS_PREFIX}settings`, 'subexp');
|
|
2304
|
+
if (licenseInfo.details && licenseInfo.details.lt) {
|
|
2305
|
+
await redis.hdel(`${REDIS_PREFIX}settings`, 'subexp', 'subsusp');
|
|
2306
|
+
subscriptionDeadline = null;
|
|
2307
|
+
}
|
|
2308
|
+
|
|
2309
|
+
// A suspended license is still registered and still checked, which is how it resumes
|
|
2310
|
+
let subscriptionLicense = (licenseInfo.active || !!licenseInfo.suspension) && isSubscriptionLicense(licenseInfo.details);
|
|
2311
|
+
|
|
2312
|
+
// A stored deadline that has passed suspends on its own, whether or not the license server
|
|
2313
|
+
// can be reached to say so; its next answer decides what happens after that
|
|
2314
|
+
let localSuspension = subscriptionLicense && !licenseInfo.suspension ? expiredSuspension(subexp, subsusp, now) : null;
|
|
2315
|
+
if (localSuspension) {
|
|
2316
|
+
await redis.hset(`${REDIS_PREFIX}settings`, 'subsusp', JSON.stringify(localSuspension));
|
|
2317
|
+
subsusp = JSON.stringify(localSuspension);
|
|
2318
|
+
logger.warn({
|
|
2319
|
+
msg: 'Subscription deadline passed, suspending until the license server says otherwise',
|
|
2320
|
+
license: redactLicense(licenseInfo.details)
|
|
2321
|
+
});
|
|
2322
|
+
setSuspension(localSuspension);
|
|
2323
|
+
}
|
|
2324
|
+
let due = false;
|
|
2325
|
+
if (subscriptionLicense && opts.force) {
|
|
2326
|
+
await redis.hset(`${REDIS_PREFIX}settings`, 'subcheck', now.toString());
|
|
2327
|
+
due = true;
|
|
2328
|
+
} else if (subscriptionLicense && checkKv) {
|
|
2329
|
+
due = !!(await redis.hUpdateBigger(`${REDIS_PREFIX}settings`, 'subcheck', now - subscriptionCheckTimeout, now));
|
|
2192
2330
|
}
|
|
2193
2331
|
|
|
2194
2332
|
// Validate subscription license
|
|
2195
|
-
if (
|
|
2196
|
-
checkKv &&
|
|
2197
|
-
licenseInfo.active &&
|
|
2198
|
-
!(licenseInfo.details && licenseInfo.details.expires) &&
|
|
2199
|
-
!licenseInfo.details.lt &&
|
|
2200
|
-
(await redis.hUpdateBigger(`${REDIS_PREFIX}settings`, 'subcheck', now - subscriptionCheckTimeout, now))
|
|
2201
|
-
) {
|
|
2333
|
+
if (due) {
|
|
2202
2334
|
try {
|
|
2203
2335
|
let body = {
|
|
2204
2336
|
key: licenseInfo.details.key,
|
|
@@ -2228,32 +2360,11 @@ let licenseCheckHandler = async opts => {
|
|
|
2228
2360
|
|
|
2229
2361
|
let data = await res.json();
|
|
2230
2362
|
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
if (res === 2) {
|
|
2236
|
-
// Grace period expired
|
|
2237
|
-
logger.warn({ msg: 'License validation failed', license: redactLicense(licenseInfo.details), data });
|
|
2238
|
-
await settings.removeLicense();
|
|
2239
|
-
licenseInfo.active = false;
|
|
2240
|
-
licenseInfo.details = false;
|
|
2241
|
-
licenseInfo.type = packageData.license;
|
|
2242
|
-
} else {
|
|
2243
|
-
// Still in grace period - check again soon
|
|
2244
|
-
let nextCheck = now + SUBSCRIPTION_RECHECK_TIMEOUT;
|
|
2245
|
-
await redis.hset(`${REDIS_PREFIX}settings`, 'ks', new Date(nextCheck).getTime().toString(16));
|
|
2246
|
-
}
|
|
2247
|
-
}
|
|
2248
|
-
} else {
|
|
2249
|
-
// License validated successfully
|
|
2250
|
-
await redis.hdel(`${REDIS_PREFIX}settings`, 'subexp');
|
|
2363
|
+
await applySubscriptionDecision(decide({ ok: res.ok, data, now, subexp }), data);
|
|
2364
|
+
outcome = res.ok ? 'ok' : data && data.invalidate ? 'refused' : 'error';
|
|
2365
|
+
|
|
2366
|
+
if (res.ok) {
|
|
2251
2367
|
await redis.hset(`${REDIS_PREFIX}settings`, 'kv', Buffer.from(packageData.version).toString('hex'));
|
|
2252
|
-
if (data.validatedUntil) {
|
|
2253
|
-
let validatedUntil = new Date(data.validatedUntil);
|
|
2254
|
-
let nextCheck = Math.min(now + MAX_LICENSE_CHECK_DELAY, validatedUntil.getTime());
|
|
2255
|
-
await redis.hset(`${REDIS_PREFIX}settings`, 'ks', new Date(nextCheck).getTime().toString(16));
|
|
2256
|
-
}
|
|
2257
2368
|
|
|
2258
2369
|
// Persist beacon markers so the full snapshot is only resent when it changes.
|
|
2259
2370
|
if (!BEACON_DISABLED) {
|
|
@@ -2261,6 +2372,8 @@ let licenseCheckHandler = async opts => {
|
|
|
2261
2372
|
}
|
|
2262
2373
|
}
|
|
2263
2374
|
} catch (err) {
|
|
2375
|
+
// never a reason to stop: an unreachable license server leaves the stored state as it was
|
|
2376
|
+
outcome = 'unreachable';
|
|
2264
2377
|
logger.warn({ msg: 'License validation error', err });
|
|
2265
2378
|
}
|
|
2266
2379
|
}
|
|
@@ -2281,7 +2394,7 @@ let licenseCheckHandler = async opts => {
|
|
|
2281
2394
|
logger.info({ msg: 'No active license. Suspending every worker except the API server.' });
|
|
2282
2395
|
|
|
2283
2396
|
// Suspend all worker types except API
|
|
2284
|
-
for (let type of
|
|
2397
|
+
for (let type of LICENSED_WORKER_TYPES) {
|
|
2285
2398
|
suspendedWorkerTypes.add(type);
|
|
2286
2399
|
if (workers.has(type)) {
|
|
2287
2400
|
for (let worker of workers.get(type).values()) {
|
|
@@ -2317,11 +2430,16 @@ let licenseCheckHandler = async opts => {
|
|
|
2317
2430
|
// worker ready handler only reassigns after crashes (reassignmentPending)
|
|
2318
2431
|
assignAccounts().catch(err => logger.error({ msg: 'Unable to assign accounts after license activation', err }));
|
|
2319
2432
|
}
|
|
2433
|
+
if (wasActive !== licenseInfo.active) {
|
|
2434
|
+
notifyApiLicenseState();
|
|
2435
|
+
}
|
|
2320
2436
|
} finally {
|
|
2321
2437
|
checkingLicense = false;
|
|
2322
2438
|
licenseCheckTimer = setTimeout(checkActiveLicense, LICENSE_CHECK_TIMEOUT);
|
|
2323
2439
|
licenseCheckTimer.unref();
|
|
2324
2440
|
}
|
|
2441
|
+
|
|
2442
|
+
return { outcome };
|
|
2325
2443
|
};
|
|
2326
2444
|
|
|
2327
2445
|
/**
|
|
@@ -2768,24 +2886,38 @@ async function commandImapWorkerCount() {
|
|
|
2768
2886
|
return { workers: availableIMAPWorkers.size };
|
|
2769
2887
|
}
|
|
2770
2888
|
|
|
2771
|
-
async function commandCheckLicense() {
|
|
2772
|
-
//
|
|
2889
|
+
async function commandCheckLicense(worker, message) {
|
|
2890
|
+
// The license page asks on every view (60 s gate); the "Check subscription now" button forces a
|
|
2891
|
+
// check, at most once a minute per instance, so the button cannot be used to hammer the server
|
|
2892
|
+
let result = { outcome: 'skipped' };
|
|
2773
2893
|
try {
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2894
|
+
if (message && message.force) {
|
|
2895
|
+
let acquired = await redis.set(`${REDIS_PREFIX}license:recheck`, '1', 'PX', 60 * 1000, 'NX');
|
|
2896
|
+
result = acquired ? await licenseCheckHandler({ force: true }) : { outcome: 'throttled' };
|
|
2897
|
+
} else {
|
|
2898
|
+
result = await licenseCheckHandler({ subscriptionCheckTimeout: 60 * 1000 });
|
|
2899
|
+
}
|
|
2777
2900
|
} catch (err) {
|
|
2778
2901
|
// ignore
|
|
2779
2902
|
}
|
|
2780
|
-
return
|
|
2903
|
+
return Object.assign(await commandLicense(), { checkOutcome: result.outcome });
|
|
2781
2904
|
}
|
|
2782
2905
|
|
|
2783
2906
|
async function commandLicense() {
|
|
2784
|
-
// Return license info with suspension status
|
|
2907
|
+
// Return license info with suspension status and, for a subscription license that is not in
|
|
2908
|
+
// good standing, the deadline or the suspension (lib/license-subscription.js)
|
|
2909
|
+
let info = Object.assign({}, licenseInfo);
|
|
2910
|
+
delete info.suspension;
|
|
2785
2911
|
if (!licenseInfo.active && suspendedWorkerTypes.size) {
|
|
2786
|
-
|
|
2912
|
+
info.suspended = true;
|
|
2913
|
+
}
|
|
2914
|
+
if (isSubscriptionLicense(licenseInfo.details)) {
|
|
2915
|
+
let subscription = subscriptionView({ subexp: subscriptionDeadline, suspension: licenseInfo.suspension, now: Date.now() });
|
|
2916
|
+
if (subscription) {
|
|
2917
|
+
info.subscription = subscription;
|
|
2918
|
+
}
|
|
2787
2919
|
}
|
|
2788
|
-
return
|
|
2920
|
+
return info;
|
|
2789
2921
|
}
|
|
2790
2922
|
|
|
2791
2923
|
async function commandUpdateLicense(worker, message) {
|
|
@@ -2802,14 +2934,24 @@ async function commandUpdateLicense(worker, message) {
|
|
|
2802
2934
|
|
|
2803
2935
|
await setLicense(licenseData, licenseFile);
|
|
2804
2936
|
|
|
2805
|
-
licenseInfo.active = true;
|
|
2806
2937
|
licenseInfo.details = licenseData;
|
|
2807
2938
|
licenseInfo.type = 'EmailEngine License';
|
|
2808
2939
|
|
|
2809
|
-
//
|
|
2810
|
-
|
|
2940
|
+
// The same key registered again keeps a suspension (setLicense keeps the stored state), so
|
|
2941
|
+
// posting the file again does not lift one; a subscription license is checked online before
|
|
2942
|
+
// the workers start, and the answer decides
|
|
2943
|
+
let subscriptionLicense = isSubscriptionLicense(licenseData);
|
|
2944
|
+
let [subexp, subsusp] = await redis.hmget(`${REDIS_PREFIX}settings`, 'subexp', 'subsusp');
|
|
2945
|
+
subscriptionDeadline = subscriptionLicense && subexp ? Number(subexp) : null;
|
|
2946
|
+
setSuspension(subscriptionLicense ? parseSuspension(subsusp) : null);
|
|
2947
|
+
|
|
2948
|
+
if (!subscriptionLicense || (await licenseCheckHandler({ force: true })).outcome === 'busy') {
|
|
2949
|
+
// Re-enable workers
|
|
2950
|
+
checkActiveLicense();
|
|
2951
|
+
}
|
|
2952
|
+
notifyApiLicenseState();
|
|
2811
2953
|
|
|
2812
|
-
return
|
|
2954
|
+
return await commandLicense();
|
|
2813
2955
|
} catch (err) {
|
|
2814
2956
|
logger.warn({ msg: 'License update failed', source: 'API', err });
|
|
2815
2957
|
return false;
|
|
@@ -2823,7 +2965,10 @@ async function commandRemoveLicense() {
|
|
|
2823
2965
|
|
|
2824
2966
|
licenseInfo.active = false;
|
|
2825
2967
|
licenseInfo.details = false;
|
|
2968
|
+
delete licenseInfo.suspension;
|
|
2969
|
+
subscriptionDeadline = null;
|
|
2826
2970
|
licenseInfo.type = packageData.license;
|
|
2971
|
+
notifyApiLicenseState();
|
|
2827
2972
|
|
|
2828
2973
|
return licenseInfo;
|
|
2829
2974
|
} catch (err) {
|
|
@@ -3514,6 +3659,28 @@ const loadStoredLicense = async () => {
|
|
|
3514
3659
|
}
|
|
3515
3660
|
};
|
|
3516
3661
|
|
|
3662
|
+
/**
|
|
3663
|
+
* Applies the stored subscription state before any worker starts (lib/license-subscription.js): a
|
|
3664
|
+
* license suspended when EmailEngine stopped, or whose deadline ran out while it was not running,
|
|
3665
|
+
* starts suspended rather than running until the first check
|
|
3666
|
+
* @returns {Promise<boolean>} whether to check the subscription online right away
|
|
3667
|
+
*/
|
|
3668
|
+
const applyStartupSubscriptionState = async () => {
|
|
3669
|
+
let [subexp, subsusp, subcheck] = await redis.hmget(`${REDIS_PREFIX}settings`, 'subexp', 'subsusp', 'subcheck');
|
|
3670
|
+
subscriptionDeadline = subexp ? Number(subexp) : null;
|
|
3671
|
+
let plan = startupPlan({ details: licenseInfo.active && licenseInfo.details, subexp, subsusp, subcheck, now: Date.now() });
|
|
3672
|
+
|
|
3673
|
+
if (plan.suspension) {
|
|
3674
|
+
logger.warn({ msg: 'Subscription not in good standing, starting suspended', license: redactLicense(licenseInfo.details), suspension: plan.suspension });
|
|
3675
|
+
setSuspension(plan.suspension);
|
|
3676
|
+
for (let type of LICENSED_WORKER_TYPES) {
|
|
3677
|
+
suspendedWorkerTypes.add(type);
|
|
3678
|
+
}
|
|
3679
|
+
}
|
|
3680
|
+
|
|
3681
|
+
return plan.checkNow;
|
|
3682
|
+
};
|
|
3683
|
+
|
|
3517
3684
|
/**
|
|
3518
3685
|
* One-time data migrations that run before the workers spawn. None of them is fatal
|
|
3519
3686
|
* @returns {Promise<void>}
|
|
@@ -3537,6 +3704,14 @@ const runStartupMigrations = async () => {
|
|
|
3537
3704
|
logger.error({ msg: 'Failed to backfill the auth-failure disable marker', err });
|
|
3538
3705
|
}
|
|
3539
3706
|
|
|
3707
|
+
// Index the exports created before the per-account export index, so deleting an account removes
|
|
3708
|
+
// them too. Not fatal: the completion key is written last, so the next startup re-enters
|
|
3709
|
+
try {
|
|
3710
|
+
await backfillExportIndex();
|
|
3711
|
+
} catch (err) {
|
|
3712
|
+
logger.error({ msg: 'Failed to backfill the export index', err });
|
|
3713
|
+
}
|
|
3714
|
+
|
|
3540
3715
|
// One-time delete of the store behind the retired `bounces` field; not fatal, the completion key is written last
|
|
3541
3716
|
try {
|
|
3542
3717
|
await sweepBounceStore();
|
|
@@ -3727,22 +3902,7 @@ const importPreparedCredentials = async () => {
|
|
|
3727
3902
|
// Import prepared password
|
|
3728
3903
|
if (preparedPassword) {
|
|
3729
3904
|
try {
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
// Written only when the stored hash differs: passwordVersion is what every admin
|
|
3733
|
-
// session and message-browser token is checked against, so rewriting the same hash
|
|
3734
|
-
// on each boot would log every operator out at every restart
|
|
3735
|
-
if (!authData || authData.password !== preparedPassword) {
|
|
3736
|
-
authData = authData || {};
|
|
3737
|
-
authData.user = authData.user || 'admin';
|
|
3738
|
-
authData.password = preparedPassword;
|
|
3739
|
-
authData.passwordVersion = Date.now();
|
|
3740
|
-
|
|
3741
|
-
await settings.set('authData', authData);
|
|
3742
|
-
logger.debug({ msg: 'Password imported' });
|
|
3743
|
-
} else {
|
|
3744
|
-
logger.debug({ msg: 'Password already imported' });
|
|
3745
|
-
}
|
|
3905
|
+
await importPreparedPassword(preparedPassword, { settings, passkeys: require('./lib/passkeys'), logger });
|
|
3746
3906
|
} catch (err) {
|
|
3747
3907
|
logger.error({ msg: 'Password import failed', err });
|
|
3748
3908
|
}
|
|
@@ -3961,6 +4121,8 @@ const startApplication = async () => {
|
|
|
3961
4121
|
|
|
3962
4122
|
await loadStoredLicense();
|
|
3963
4123
|
|
|
4124
|
+
startupLicenseCheck = await applyStartupSubscriptionState();
|
|
4125
|
+
|
|
3964
4126
|
// Check for updates
|
|
3965
4127
|
processCheckUpgrade().catch(err => {
|
|
3966
4128
|
logger.warn({ msg: 'Update check failed', err });
|
|
@@ -3987,6 +4149,11 @@ const startApplication = async () => {
|
|
|
3987
4149
|
|
|
3988
4150
|
await startApiWorkers();
|
|
3989
4151
|
|
|
4152
|
+
if (licenseInfo.suspension) {
|
|
4153
|
+
// the API answers like an unlicensed instance from the start, not from the next precheck tick
|
|
4154
|
+
notifyApiLicenseState();
|
|
4155
|
+
}
|
|
4156
|
+
|
|
3990
4157
|
// Small delay to allow API to start
|
|
3991
4158
|
await new Promise(r => setTimeout(r, 100));
|
|
3992
4159
|
|
|
@@ -4006,9 +4173,15 @@ startApplication()
|
|
|
4006
4173
|
collectMetrics().catch(err => logger.error({ msg: 'Metrics collection failed', err }));
|
|
4007
4174
|
}, 1000).unref();
|
|
4008
4175
|
|
|
4009
|
-
// Schedule periodic checks
|
|
4010
|
-
|
|
4011
|
-
|
|
4176
|
+
// Schedule periodic checks. A monthly-plan license, or one that is not in good standing, is
|
|
4177
|
+
// checked right away (unless an hour has not passed since the last check, so a restart
|
|
4178
|
+
// loop costs one call an hour); everything else after the usual first delay
|
|
4179
|
+
if (startupLicenseCheck) {
|
|
4180
|
+
licenseCheckHandler({ force: true }).catch(err => logger.error({ msg: 'License check error', err }));
|
|
4181
|
+
} else {
|
|
4182
|
+
licenseCheckTimer = setTimeout(checkActiveLicense, LICENSE_CHECK_TIMEOUT);
|
|
4183
|
+
licenseCheckTimer.unref();
|
|
4184
|
+
}
|
|
4012
4185
|
|
|
4013
4186
|
upgradeCheckTimer = setTimeout(checkUpgrade, UPGRADE_CHECK_TIMEOUT);
|
|
4014
4187
|
upgradeCheckTimer.unref();
|