2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
2016-07-25 16:47:30 +00:00
|
|
|
/* eslint-disable no-console */
|
2014-10-17 02:21:06 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
//var SegfaultHandler = require('segfault-handler');
|
|
|
|
//SegfaultHandler.registerHandler('enigma-bbs-segfault.log');
|
|
|
|
|
2018-06-23 02:48:36 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const conf = require('./config.js');
|
|
|
|
const logger = require('./logger.js');
|
|
|
|
const database = require('./database.js');
|
|
|
|
const resolvePath = require('./misc_util.js').resolvePath;
|
2018-11-24 05:18:15 +00:00
|
|
|
const UserProps = require('./user_property.js');
|
2018-11-26 02:05:16 +00:00
|
|
|
const SysProps = require('./system_property.js');
|
2018-11-26 03:13:48 +00:00
|
|
|
const SysLogKeys = require('./system_log.js');
|
2020-11-26 22:53:21 +00:00
|
|
|
const UserLogNames = require('./user_log_name');
|
2018-06-23 02:48:36 +00:00
|
|
|
|
|
|
|
// deps
|
|
|
|
const async = require('async');
|
|
|
|
const util = require('util');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const mkdirs = require('fs-extra').mkdirs;
|
|
|
|
const fs = require('graceful-fs');
|
|
|
|
const paths = require('path');
|
2018-11-26 03:13:48 +00:00
|
|
|
const moment = require('moment');
|
2018-06-23 02:48:36 +00:00
|
|
|
|
|
|
|
// our main entry point
|
|
|
|
exports.main = main;
|
|
|
|
|
|
|
|
// object with various services we want to de-init/shutdown cleanly if possible
|
2016-06-21 02:39:20 +00:00
|
|
|
const initServices = {};
|
|
|
|
|
2018-06-23 02:48:36 +00:00
|
|
|
// only include bbs.js once @ startup; this should be fine
|
2018-04-11 01:53:04 +00:00
|
|
|
const COPYRIGHT = fs.readFileSync(paths.join(__dirname, '../LICENSE.TXT'), 'utf8').split(/\r?\n/g)[0];
|
|
|
|
|
2018-06-23 02:48:36 +00:00
|
|
|
const FULL_COPYRIGHT = `ENiGMA½ ${COPYRIGHT}`;
|
2017-06-11 00:29:00 +00:00
|
|
|
const HELP =
|
2018-04-11 01:53:04 +00:00
|
|
|
`${FULL_COPYRIGHT}
|
2017-02-10 04:08:23 +00:00
|
|
|
usage: main.js <args>
|
2017-11-25 22:45:19 +00:00
|
|
|
eg : main.js --config /enigma_install_path/config/
|
2017-02-10 04:08:23 +00:00
|
|
|
|
|
|
|
valid args:
|
|
|
|
--version : display version
|
|
|
|
--help : displays this help
|
2017-11-25 22:45:19 +00:00
|
|
|
--config PATH : override default config path
|
2017-02-10 04:08:23 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
function printHelpAndExit() {
|
2018-06-22 05:15:04 +00:00
|
|
|
console.info(HELP);
|
|
|
|
process.exit();
|
2017-02-10 04:08:23 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 02:53:14 +00:00
|
|
|
function printVersionAndExit() {
|
|
|
|
console.info(require('../package.json').version);
|
|
|
|
}
|
|
|
|
|
2017-05-11 03:29:04 +00:00
|
|
|
function main() {
|
2020-06-19 03:29:24 +00:00
|
|
|
let errorDisplayed = false;
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function processArgs(callback) {
|
|
|
|
const argv = require('minimist')(process.argv.slice(2));
|
2017-02-10 04:08:23 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(argv.help) {
|
2018-11-15 02:53:14 +00:00
|
|
|
return printHelpAndExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(argv.version) {
|
|
|
|
return printVersionAndExit();
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2015-04-19 08:13:13 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const configOverridePath = argv.config;
|
2017-02-10 04:08:23 +00:00
|
|
|
|
2020-06-12 03:16:15 +00:00
|
|
|
return callback(null, configOverridePath || conf.Config.getDefaultPath(), _.isString(configOverridePath));
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function initConfig(configPath, configPathSupplied, callback) {
|
|
|
|
const configFile = configPath + 'config.hjson';
|
2015-04-19 08:13:13 +00:00
|
|
|
|
2020-06-12 03:16:15 +00:00
|
|
|
conf.Config.create(resolvePath(configFile), err => {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 02:48:36 +00:00
|
|
|
// If the user supplied a path and we can't read/parse it
|
|
|
|
// then it's a fatal error
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(err) {
|
|
|
|
if('ENOENT' === err.code) {
|
|
|
|
if(configPathSupplied) {
|
|
|
|
console.error('Configuration file does not exist: ' + configFile);
|
|
|
|
} else {
|
2018-06-23 02:48:36 +00:00
|
|
|
configPathSupplied = null; // make non-fatal; we'll go with defaults
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-06-19 03:29:24 +00:00
|
|
|
errorDisplayed = true;
|
|
|
|
console.error(`Configuration error: ${err.message}`); // eslint-disable-line no-console
|
|
|
|
if (err.hint) {
|
|
|
|
console.error(`Hint: ${err.hint}`);
|
|
|
|
}
|
|
|
|
if (err.configPath) {
|
|
|
|
console.error(`Note: ${err.configPath}`);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-03 01:32:27 +00:00
|
|
|
return callback(err);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function initSystem(callback) {
|
|
|
|
initialize(function init(err) {
|
|
|
|
if(err) {
|
|
|
|
console.error('Error initializing: ' + util.inspect(err));
|
|
|
|
}
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
2018-07-03 01:32:27 +00:00
|
|
|
if(!err) {
|
|
|
|
// note this is escaped:
|
|
|
|
fs.readFile(paths.join(__dirname, '../misc/startup_banner.asc'), 'utf8', (err, banner) => {
|
|
|
|
console.info(FULL_COPYRIGHT);
|
|
|
|
if(!err) {
|
|
|
|
console.info(banner);
|
|
|
|
}
|
|
|
|
console.info('System started!');
|
|
|
|
});
|
|
|
|
}
|
2016-09-29 03:54:25 +00:00
|
|
|
|
2020-06-19 03:29:24 +00:00
|
|
|
if(err && !errorDisplayed) {
|
2018-06-22 05:15:04 +00:00
|
|
|
console.error('Error initializing: ' + util.inspect(err));
|
2020-07-05 21:32:10 +00:00
|
|
|
return process.exit();
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2015-04-19 08:13:13 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 02:39:20 +00:00
|
|
|
function shutdownSystem() {
|
2018-06-22 05:15:04 +00:00
|
|
|
const msg = 'Process interrupted. Shutting down...';
|
|
|
|
console.info(msg);
|
|
|
|
logger.log.info(msg);
|
2016-06-21 02:39:20 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function closeConnections(callback) {
|
|
|
|
const ClientConns = require('./client_connections.js');
|
|
|
|
const activeConnections = ClientConns.getActiveConnections();
|
|
|
|
let i = activeConnections.length;
|
|
|
|
while(i--) {
|
|
|
|
const activeTerm = activeConnections[i].term;
|
|
|
|
if(activeTerm) {
|
|
|
|
activeTerm.write('\n\nServer is shutting down NOW! Disconnecting...\n\n');
|
|
|
|
}
|
|
|
|
ClientConns.removeClient(activeConnections[i]);
|
|
|
|
}
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function stopListeningServers(callback) {
|
|
|
|
return require('./listening_server.js').shutdown( () => {
|
2018-06-23 02:48:36 +00:00
|
|
|
return callback(null); // ignore err
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function stopEventScheduler(callback) {
|
|
|
|
if(initServices.eventScheduler) {
|
|
|
|
return initServices.eventScheduler.shutdown( () => {
|
2018-06-23 02:48:36 +00:00
|
|
|
return callback(null); // ignore err
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function stopFileAreaWeb(callback) {
|
|
|
|
require('./file_area_web.js').startup( () => {
|
2018-06-23 02:48:36 +00:00
|
|
|
return callback(null); // ignore err
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function stopMsgNetwork(callback) {
|
|
|
|
require('./msg_network.js').shutdown(callback);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
() => {
|
|
|
|
console.info('Goodbye!');
|
|
|
|
return process.exit();
|
|
|
|
}
|
|
|
|
);
|
2016-06-21 02:39:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 11:30:20 +00:00
|
|
|
function initialize(cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function createMissingDirectories(callback) {
|
2020-06-08 03:21:31 +00:00
|
|
|
const Config = conf.get();
|
|
|
|
async.each(Object.keys(Config.paths), function entry(pathKey, next) {
|
|
|
|
mkdirs(Config.paths[pathKey], function dirCreated(err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(err) {
|
2020-06-08 03:21:31 +00:00
|
|
|
console.error('Could not create path: ' + Config.paths[pathKey] + ': ' + err.toString());
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
return next(err);
|
|
|
|
});
|
|
|
|
}, function dirCreationComplete(err) {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function basicInit(callback) {
|
|
|
|
logger.init();
|
|
|
|
logger.log.info(
|
2020-05-14 01:04:11 +00:00
|
|
|
{
|
|
|
|
version : require('../package.json').version,
|
|
|
|
nodeVersion : process.version,
|
|
|
|
},
|
|
|
|
'**** ENiGMA½ Bulletin Board System Starting Up! ****'
|
|
|
|
);
|
2014-10-29 11:30:20 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
process.on('SIGINT', shutdownSystem);
|
2016-09-14 03:44:35 +00:00
|
|
|
|
2020-11-24 00:24:40 +00:00
|
|
|
require('@breejs/later').date.localTime(); // use local times for later.js/scheduling
|
2017-06-11 00:29:00 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function initDatabases(callback) {
|
|
|
|
return database.initializeDatabases(callback);
|
|
|
|
},
|
|
|
|
function initMimeTypes(callback) {
|
|
|
|
return require('./mime_util.js').startup(callback);
|
|
|
|
},
|
|
|
|
function initStatLog(callback) {
|
|
|
|
return require('./stat_log.js').init(callback);
|
|
|
|
},
|
2020-06-17 06:10:51 +00:00
|
|
|
function initMenusAndThemes(callback) {
|
|
|
|
const { ThemeManager } = require('./theme');
|
|
|
|
return ThemeManager.create(callback);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function loadSysOpInformation(callback) {
|
|
|
|
//
|
2018-11-25 03:02:19 +00:00
|
|
|
// Copy over some +op information from the user DB -> system properties.
|
2018-06-23 02:48:36 +00:00
|
|
|
// * Makes this accessible for MCI codes, easy non-blocking access, etc.
|
|
|
|
// * We do this every time as the op is free to change this information just
|
|
|
|
// like any other user
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const User = require('./user.js');
|
2016-07-28 03:44:27 +00:00
|
|
|
|
2020-11-27 02:51:00 +00:00
|
|
|
// :TODO: use User.getUserInfo() for this!
|
|
|
|
|
2018-11-24 16:39:53 +00:00
|
|
|
const propLoadOpts = {
|
|
|
|
names : [
|
|
|
|
UserProps.RealName, UserProps.Sex, UserProps.EmailAddress,
|
|
|
|
UserProps.Location, UserProps.Affiliations,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getOpUserName(next) {
|
2020-11-27 02:51:00 +00:00
|
|
|
return User.getUserName(User.RootUserID, next);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function getOpProps(opUserName, next) {
|
|
|
|
User.loadProperties(User.RootUserID, propLoadOpts, (err, opProps) => {
|
2018-11-24 16:39:53 +00:00
|
|
|
return next(err, opUserName, opProps);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
2018-11-24 16:39:53 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
2018-11-24 16:39:53 +00:00
|
|
|
(err, opUserName, opProps) => {
|
2018-06-22 05:15:04 +00:00
|
|
|
const StatLog = require('./stat_log.js');
|
2015-08-03 00:27:05 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(err) {
|
2018-11-24 16:39:53 +00:00
|
|
|
propLoadOpts.names.concat('username').forEach(v => {
|
2018-11-26 02:05:16 +00:00
|
|
|
StatLog.setNonPersistentSystemStat(`sysop_${v}`, 'N/A');
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
opProps.username = opUserName;
|
2016-07-28 03:44:27 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
_.each(opProps, (v, k) => {
|
2018-11-26 02:05:16 +00:00
|
|
|
StatLog.setNonPersistentSystemStat(`sysop_${k}`, v);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
2016-07-28 03:44:27 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2020-11-27 02:51:00 +00:00
|
|
|
function initSystemLogStats(callback) {
|
2018-11-26 03:13:48 +00:00
|
|
|
const StatLog = require('./stat_log.js');
|
2020-11-27 02:51:00 +00:00
|
|
|
|
2018-11-26 03:13:48 +00:00
|
|
|
const filter = {
|
|
|
|
logName : SysLogKeys.UserLoginHistory,
|
|
|
|
resultType : 'count',
|
|
|
|
date : moment(),
|
|
|
|
};
|
|
|
|
|
|
|
|
StatLog.findSystemLogEntries(filter, (err, callsToday) => {
|
|
|
|
if(!err) {
|
|
|
|
StatLog.setNonPersistentSystemStat(SysProps.LoginsToday, callsToday);
|
|
|
|
}
|
|
|
|
return callback(null);
|
|
|
|
});
|
|
|
|
},
|
2020-11-27 02:51:00 +00:00
|
|
|
function initUserLogStats(callback) {
|
2020-11-26 22:53:21 +00:00
|
|
|
const StatLog = require('./stat_log');
|
|
|
|
|
|
|
|
const entries = [
|
|
|
|
[ UserLogNames.UlFiles, [ SysProps.FileUlTodayCount, 'count' ] ],
|
|
|
|
[ UserLogNames.UlFileBytes, [ SysProps.FileUlTodayBytes, 'obj' ] ],
|
|
|
|
[ UserLogNames.DlFiles, [ SysProps.FileDlTodayCount, 'count' ] ],
|
|
|
|
[ UserLogNames.DlFileBytes, [ SysProps.FileDlTodayBytes, 'obj' ] ],
|
|
|
|
];
|
|
|
|
|
|
|
|
async.each(entries, (entry, nextEntry) => {
|
|
|
|
const [ logName, [sysPropName, resultType] ] = entry;
|
|
|
|
|
|
|
|
const filter = {
|
|
|
|
logName,
|
|
|
|
resultType,
|
|
|
|
date : moment(),
|
|
|
|
};
|
|
|
|
|
|
|
|
filter.logName = logName;
|
|
|
|
|
|
|
|
StatLog.findUserLogEntries(filter, (err, stat) => {
|
|
|
|
if (!err) {
|
|
|
|
if (resultType === 'obj') {
|
|
|
|
stat = stat.reduce( (bytes, entry) => bytes + parseInt(entry.log_value) || 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
StatLog.setNonPersistentSystemStat(sysPropName, stat);
|
|
|
|
}
|
|
|
|
return nextEntry(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
return callback(null);
|
|
|
|
});
|
|
|
|
},
|
2020-11-27 02:51:00 +00:00
|
|
|
function initLastLogin(callback) {
|
|
|
|
const StatLog = require('./stat_log');
|
|
|
|
StatLog.getSystemLogEntries(SysLogKeys.UserLoginHistory, 'timestamp_desc', 1, (err, lastLogin) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
let loginObj;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
loginObj = JSON.parse(lastLogin[0].log_value);
|
|
|
|
loginObj.timestamp = moment(lastLogin[0].timestamp);
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// For live stats we want to resolve user ID -> name, etc.
|
|
|
|
const User = require('./user');
|
|
|
|
User.getUserInfo(loginObj.userId, (err, props) => {
|
|
|
|
const stat = Object.assign({}, props, loginObj);
|
|
|
|
StatLog.setNonPersistentSystemStat(SysProps.LastLogin, stat);
|
|
|
|
return callback(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2022-05-02 01:58:00 +00:00
|
|
|
function initUserCount(callback) {
|
|
|
|
const User = require('./user.js');
|
|
|
|
User.getUserCount((err, count) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const StatLog = require('./stat_log');
|
|
|
|
StatLog.setNonPersistentSystemStat(SysProps.TotalUserCount, count);
|
|
|
|
return callback(null);
|
|
|
|
});
|
|
|
|
},
|
2018-11-28 04:21:00 +00:00
|
|
|
function initMessageStats(callback) {
|
|
|
|
return require('./message_area.js').startup(callback);
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
function initMCI(callback) {
|
|
|
|
return require('./predefined_mci.js').init(callback);
|
|
|
|
},
|
|
|
|
function readyMessageNetworkSupport(callback) {
|
|
|
|
return require('./msg_network.js').startup(callback);
|
|
|
|
},
|
|
|
|
function readyEvents(callback) {
|
|
|
|
return require('./events.js').startup(callback);
|
|
|
|
},
|
2018-07-21 20:32:06 +00:00
|
|
|
function genericModulesInit(callback) {
|
|
|
|
return require('./module_util.js').initializeModules(callback);
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
function listenConnections(callback) {
|
|
|
|
return require('./listening_server.js').startup(callback);
|
|
|
|
},
|
|
|
|
function readyFileBaseArea(callback) {
|
|
|
|
return require('./file_base_area.js').startup(callback);
|
|
|
|
},
|
|
|
|
function readyFileAreaWeb(callback) {
|
|
|
|
return require('./file_area_web.js').startup(callback);
|
|
|
|
},
|
|
|
|
function readyPasswordReset(callback) {
|
|
|
|
const WebPasswordReset = require('./web_password_reset.js').WebPasswordReset;
|
|
|
|
return WebPasswordReset.startup(callback);
|
|
|
|
},
|
2019-06-13 03:57:45 +00:00
|
|
|
function ready2FA_OTPRegister(callback) {
|
|
|
|
const User2FA_OTPWebRegister = require('./user_2fa_otp_web_register.js');
|
|
|
|
return User2FA_OTPWebRegister.startup(callback);
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
function readyEventScheduler(callback) {
|
|
|
|
const EventSchedulerModule = require('./event_scheduler.js').EventSchedulerModule;
|
|
|
|
EventSchedulerModule.loadAndStart( (err, modInst) => {
|
|
|
|
initServices.eventScheduler = modInst;
|
|
|
|
return callback(err);
|
|
|
|
});
|
2018-07-21 20:32:06 +00:00
|
|
|
},
|
|
|
|
function listenUserEventsForStatLog(callback) {
|
|
|
|
return require('./stat_log.js').initUserEvents(callback);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
function onComplete(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
2014-10-29 11:30:20 +00:00
|
|
|
}
|