2020-10-12 20:09:53 +08:00
|
|
|
/* jshint esversion: 6 */
|
|
|
|
/* globals process */
|
|
|
|
|
|
|
|
const Crypto = require('crypto');
|
|
|
|
const WriteQueue = require("./write-queue");
|
|
|
|
const BatchRead = require("./batch-read");
|
|
|
|
|
|
|
|
const Keys = require("./keys");
|
|
|
|
const Core = require("./commands/core");
|
|
|
|
|
|
|
|
const Quota = require("./commands/quota");
|
|
|
|
const Util = require("./common-util");
|
2021-06-09 21:13:31 +08:00
|
|
|
const Package = require("../package.json");
|
2020-10-12 20:09:53 +08:00
|
|
|
|
2021-06-08 22:54:30 +08:00
|
|
|
var canonicalizeOrigin = function (s) {
|
|
|
|
if (typeof(s) === 'undefined') { return; }
|
|
|
|
return (s || '').trim().replace(/\/+$/, '');
|
|
|
|
};
|
2020-10-12 20:09:53 +08:00
|
|
|
|
2021-10-19 16:52:06 +08:00
|
|
|
var isValidPort = function (p) {
|
|
|
|
return typeof(p) === 'number' && p < 65535;
|
|
|
|
};
|
|
|
|
|
2021-06-08 22:54:30 +08:00
|
|
|
module.exports.create = function (config) {
|
2020-10-12 20:09:53 +08:00
|
|
|
const Env = {
|
2021-06-09 21:13:31 +08:00
|
|
|
version: Package.version,
|
2021-06-22 18:46:32 +08:00
|
|
|
installMethod: config.installMethod || undefined,
|
2021-06-09 21:13:31 +08:00
|
|
|
|
2021-06-08 22:54:30 +08:00
|
|
|
httpUnsafeOrigin: canonicalizeOrigin(config.httpUnsafeOrigin),
|
|
|
|
httpSafeOrigin: canonicalizeOrigin(config.httpSafeOrigin),
|
|
|
|
removeDonateButton: config.removeDonateButton,
|
2021-10-19 16:52:06 +08:00
|
|
|
httpPort: isValidPort(config.httpPort)? config.httpPort: 3000,
|
|
|
|
httpAddress: typeof(config.httpAddress) === 'string'? config.httpAddress: '127.0.0.1',
|
|
|
|
websocketPath: config.externalWebsocketURL,
|
2021-06-08 22:54:30 +08:00
|
|
|
|
2021-01-08 17:19:04 +08:00
|
|
|
OFFLINE_MODE: false,
|
2020-10-12 20:09:53 +08:00
|
|
|
FRESH_KEY: '',
|
|
|
|
FRESH_MODE: true,
|
|
|
|
DEV_MODE: false,
|
|
|
|
configCache: {},
|
2021-03-12 19:46:11 +08:00
|
|
|
broadcastCache: {},
|
2020-10-12 20:09:53 +08:00
|
|
|
flushCache: function () {
|
|
|
|
Env.configCache = {};
|
2021-03-12 19:46:11 +08:00
|
|
|
Env.broadcastCache = {};
|
2020-10-12 20:09:53 +08:00
|
|
|
Env.FRESH_KEY = +new Date();
|
|
|
|
if (!(Env.DEV_MODE || Env.FRESH_MODE)) { Env.FRESH_MODE = true; }
|
|
|
|
if (!Env.Log) { return; }
|
|
|
|
Env.Log.info("UPDATING_FRESH_KEY", Env.FRESH_KEY);
|
|
|
|
},
|
|
|
|
|
|
|
|
Log: undefined,
|
|
|
|
// store
|
|
|
|
id: Crypto.randomBytes(8).toString('hex'),
|
|
|
|
|
|
|
|
launchTime: +new Date(),
|
|
|
|
|
|
|
|
inactiveTime: config.inactiveTime,
|
|
|
|
archiveRetentionTime: config.archiveRetentionTime,
|
|
|
|
accountRetentionTime: config.accountRetentionTime,
|
|
|
|
|
|
|
|
// TODO implement mutability
|
|
|
|
adminEmail: config.adminEmail,
|
|
|
|
supportMailbox: config.supportMailboxPublicKey,
|
|
|
|
|
|
|
|
metadata_cache: {},
|
|
|
|
channel_cache: {},
|
2020-12-08 19:04:28 +08:00
|
|
|
cache_checks: {},
|
|
|
|
|
2020-10-12 20:09:53 +08:00
|
|
|
queueStorage: WriteQueue(),
|
|
|
|
queueDeletes: WriteQueue(),
|
|
|
|
queueValidation: WriteQueue(),
|
2020-10-27 15:17:15 +08:00
|
|
|
queueMetadata: WriteQueue(),
|
2020-10-12 20:09:53 +08:00
|
|
|
|
|
|
|
batchIndexReads: BatchRead("HK_GET_INDEX"),
|
|
|
|
batchMetadata: BatchRead('GET_METADATA'),
|
|
|
|
batchRegisteredUsers: BatchRead("GET_REGISTERED_USERS"),
|
|
|
|
batchDiskUsage: BatchRead('GET_DISK_USAGE'),
|
|
|
|
batchUserPins: BatchRead('LOAD_USER_PINS'),
|
|
|
|
batchTotalSize: BatchRead('GET_TOTAL_SIZE'),
|
|
|
|
batchAccountQuery: BatchRead("QUERY_ACCOUNT_SERVER"),
|
|
|
|
|
|
|
|
intervals: {},
|
|
|
|
maxUploadSize: config.maxUploadSize || (20 * 1024 * 1024),
|
|
|
|
premiumUploadSize: false, // overridden below...
|
|
|
|
Sessions: {},
|
|
|
|
paths: {},
|
|
|
|
//msgStore: config.store,
|
|
|
|
|
2021-03-17 00:00:23 +08:00
|
|
|
// /api/broadcast
|
2021-03-12 19:46:11 +08:00
|
|
|
lastBroadcastHash: '',
|
2021-03-17 00:00:23 +08:00
|
|
|
surveyURL: undefined,
|
2021-03-30 23:41:12 +08:00
|
|
|
maintenance: undefined,
|
2021-03-12 19:46:11 +08:00
|
|
|
|
2020-10-12 20:09:53 +08:00
|
|
|
netfluxUsers: {},
|
|
|
|
|
|
|
|
pinStore: undefined,
|
|
|
|
|
|
|
|
limits: {},
|
|
|
|
admins: [],
|
|
|
|
WARN: function (e, output) { // TODO deprecate this
|
|
|
|
if (!Env.Log) { return; }
|
|
|
|
if (e && output) {
|
|
|
|
Env.Log.warn(e, {
|
|
|
|
output: output,
|
|
|
|
message: String(e),
|
|
|
|
stack: new Error(e).stack,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-05-05 14:37:45 +08:00
|
|
|
/* FIXME restrictRegistration is initialized as false and then overridden by admin decree
|
|
|
|
There is a narrow window in which someone could register before the server updates this value.
|
|
|
|
See also the cached 'restrictRegistration' value in server.js#serveConfig
|
|
|
|
*/
|
2021-04-26 21:01:33 +08:00
|
|
|
restrictRegistration: false,
|
2020-10-12 20:09:53 +08:00
|
|
|
allowSubscriptions: config.allowSubscriptions === true,
|
|
|
|
blockDailyCheck: config.blockDailyCheck === true,
|
|
|
|
|
2021-06-08 22:54:30 +08:00
|
|
|
consentToContact: false,
|
|
|
|
listMyInstance: false,
|
|
|
|
provideAggregateStatistics: false,
|
2021-06-10 23:26:12 +08:00
|
|
|
updateAvailable: undefined,
|
2021-06-08 22:54:30 +08:00
|
|
|
|
2020-10-12 20:09:53 +08:00
|
|
|
myDomain: config.myDomain,
|
|
|
|
mySubdomain: config.mySubdomain, // only exists for the accounts integration
|
|
|
|
customLimits: {},
|
|
|
|
// FIXME this attribute isn't in the default conf
|
|
|
|
// but it is referenced in Quota
|
|
|
|
domain: config.domain,
|
|
|
|
|
|
|
|
maxWorkers: config.maxWorkers,
|
|
|
|
disableIntegratedTasks: config.disableIntegratedTasks || false,
|
2021-08-16 21:02:34 +08:00
|
|
|
disableIntegratedEviction: typeof(config.disableIntegratedEviction) === 'undefined'? true: config.disableIntegratedEviction, // XXX 4.11.0 false,
|
2020-10-12 20:09:53 +08:00
|
|
|
lastEviction: +new Date(),
|
2020-10-15 15:41:09 +08:00
|
|
|
evictionReport: {},
|
2020-12-08 13:14:46 +08:00
|
|
|
commandTimers: {},
|
2020-10-12 20:09:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
(function () {
|
2020-10-12 21:49:57 +08:00
|
|
|
// mode can be FRESH (default), DEV, or PACKAGE
|
2020-10-12 20:09:53 +08:00
|
|
|
if (process.env.PACKAGE) {
|
|
|
|
// `PACKAGE=1 node server` uses the version string from package.json as the cache string
|
2020-10-12 21:49:57 +08:00
|
|
|
//console.log("PACKAGE MODE ENABLED");
|
2020-10-12 20:09:53 +08:00
|
|
|
Env.FRESH_MODE = false;
|
|
|
|
Env.DEV_MODE = false;
|
|
|
|
} else if (process.env.DEV) {
|
|
|
|
// `DEV=1 node server` will use a random cache string on every page reload
|
2020-10-12 21:49:57 +08:00
|
|
|
//console.log("DEV MODE ENABLED");
|
2020-10-12 20:09:53 +08:00
|
|
|
Env.FRESH_MODE = false;
|
|
|
|
Env.DEV_MODE = true;
|
|
|
|
} else {
|
|
|
|
// `FRESH=1 node server` will set a random cache string when the server is launched
|
|
|
|
// and use it for the process lifetime or until it is reset from the admin panel
|
2020-10-12 21:49:57 +08:00
|
|
|
//console.log("FRESH MODE ENABLED");
|
2020-10-12 20:09:53 +08:00
|
|
|
Env.FRESH_KEY = +new Date();
|
|
|
|
}
|
2021-01-08 17:19:04 +08:00
|
|
|
|
|
|
|
// Offline mode is mostly for development. It lets us test clientside cache and offline support
|
|
|
|
if (process.env.OFFLINE) { Env.OFFLINE_MODE = true; }
|
2020-10-12 20:09:53 +08:00
|
|
|
}());
|
|
|
|
|
2020-12-08 19:04:28 +08:00
|
|
|
Env.checkCache = function (channel) {
|
|
|
|
var f = Env.cache_checks[channel] || Util.throttle(function () {
|
2020-12-08 19:20:37 +08:00
|
|
|
delete Env.cache_checks[channel];
|
2020-12-08 19:04:28 +08:00
|
|
|
if (Env.channel_cache[channel]) { return; }
|
|
|
|
delete Env.metadata_cache[channel];
|
|
|
|
}, 30000);
|
|
|
|
f();
|
|
|
|
};
|
2020-10-12 20:09:53 +08:00
|
|
|
|
|
|
|
(function () {
|
|
|
|
var custom = config.customLimits;
|
|
|
|
if (!custom) { return; }
|
|
|
|
|
|
|
|
var stored = Env.customLimits;
|
|
|
|
|
|
|
|
Object.keys(custom).forEach(function (k) {
|
|
|
|
var unsafeKey = Keys.canonicalize(k);
|
|
|
|
|
|
|
|
if (!unsafeKey) {
|
|
|
|
console.log("INVALID_CUSTOM_LIMIT_ID", {
|
|
|
|
message: "A custom quota upgrade was provided via your config with an invalid identifier. It will be ignored.",
|
|
|
|
key: k,
|
|
|
|
value: custom[k],
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stored[unsafeKey]) {
|
|
|
|
console.log("INVALID_CUSTOM_LIMIT_DUPLICATED", {
|
|
|
|
message: "A duplicated custom quota upgrade was provided via your config which would have overridden an existing value. It will be ignored.",
|
|
|
|
key: k,
|
|
|
|
value: custom[k],
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Quota.isValidLimit(custom[k])) {
|
|
|
|
console.log("INVALID_CUSTOM_LIMIT_VALUE", {
|
|
|
|
message: "A custom quota upgrade was provided via your config with an invalid value. It will be ignored.",
|
|
|
|
key: k,
|
|
|
|
value: custom[k],
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var limit = stored[unsafeKey] = Util.clone(custom[k]);
|
|
|
|
limit.origin = 'config';
|
|
|
|
});
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
var pes = config.premiumUploadSize;
|
|
|
|
if (!isNaN(pes) && pes >= Env.maxUploadSize) {
|
|
|
|
Env.premiumUploadSize = pes;
|
|
|
|
}
|
|
|
|
}());
|
|
|
|
|
|
|
|
var paths = Env.paths;
|
|
|
|
|
|
|
|
var keyOrDefaultString = function (key, def) {
|
|
|
|
return typeof(config[key]) === 'string'? config[key]: def;
|
|
|
|
};
|
|
|
|
|
|
|
|
paths.pin = keyOrDefaultString('pinPath', './pins');
|
|
|
|
paths.block = keyOrDefaultString('blockPath', './block');
|
|
|
|
paths.data = keyOrDefaultString('filePath', './datastore');
|
|
|
|
paths.staging = keyOrDefaultString('blobStagingPath', './blobstage');
|
|
|
|
paths.blob = keyOrDefaultString('blobPath', './blob');
|
|
|
|
paths.decree = keyOrDefaultString('decreePath', './data/');
|
|
|
|
paths.archive = keyOrDefaultString('archivePath', './data/archive');
|
|
|
|
paths.task = keyOrDefaultString('taskPath', './tasks');
|
|
|
|
|
|
|
|
Env.defaultStorageLimit = typeof(config.defaultStorageLimit) === 'number' && config.defaultStorageLimit >= 0?
|
|
|
|
config.defaultStorageLimit:
|
|
|
|
Core.DEFAULT_LIMIT;
|
|
|
|
|
|
|
|
try {
|
|
|
|
Env.admins = (config.adminKeys || []).map(function (k) {
|
|
|
|
try {
|
|
|
|
return Keys.canonicalize(k);
|
|
|
|
} catch (err) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}).filter(Boolean);
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Can't parse admin keys. Please update or fix your config.js file!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Env;
|
|
|
|
};
|