aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authormarzavec <admin@marzavec.com>2019-03-19 07:36:21 +0100
committermarzavec <admin@marzavec.com>2019-03-19 07:36:21 +0100
commitc634e03cb553e21158fddc6d4221a54aa799de79 (patch)
tree74f303d4ab65b08c7ec316fb713146cc99024c00 /server
parentMerge pull request #56 from paulgreg/manifest (diff)
downloadhackchat-c634e03cb553e21158fddc6d4221a54aa799de79.tar.gz
hackchat-c634e03cb553e21158fddc6d4221a54aa799de79.zip
refactoring 1 of 2
Diffstat (limited to 'server')
-rw-r--r--server/main.js27
-rw-r--r--server/package-lock.json21
-rw-r--r--server/package.json1
-rw-r--r--server/src/commands/admin/addmod.js2
-rw-r--r--server/src/commands/admin/reload.js2
-rw-r--r--server/src/commands/admin/saveconfig.js2
-rw-r--r--server/src/commands/core/chat.js2
-rw-r--r--server/src/commands/core/invite.js2
-rw-r--r--server/src/commands/core/join.js2
-rw-r--r--server/src/commands/core/morestats.js16
-rw-r--r--server/src/commands/core/stats.js2
-rw-r--r--server/src/commands/mod/ban.js2
-rw-r--r--server/src/commands/mod/kick.js2
-rw-r--r--server/src/commands/mod/unban.js2
-rw-r--r--server/src/managers/config.js240
-rw-r--r--server/src/managers/index.js6
-rw-r--r--server/src/scripts/configLib/SetupWizard.js107
-rw-r--r--server/src/scripts/configure.js14
-rw-r--r--server/src/scripts/setupSchema/Banner.js28
-rw-r--r--server/src/scripts/setupSchema/Footer.js7
-rw-r--r--server/src/scripts/setupSchema/Questions.js55
-rw-r--r--server/src/serverLib/CommandManager.js (renamed from server/src/managers/commands.js)5
-rw-r--r--server/src/serverLib/ConfigManager.js89
-rw-r--r--server/src/serverLib/CoreApp.js66
-rw-r--r--server/src/serverLib/ImportsManager.js (renamed from server/src/managers/imports-manager.js)29
-rw-r--r--server/src/serverLib/MainServer.js (renamed from server/src/core/server.js)8
-rw-r--r--server/src/serverLib/RateLimiter.js (renamed from server/src/core/rateLimiter.js)6
-rw-r--r--server/src/serverLib/StatsManager.js (renamed from server/src/managers/stats.js)10
-rw-r--r--server/src/serverLib/index.js8
29 files changed, 404 insertions, 359 deletions
diff --git a/server/main.js b/server/main.js
index 2e66cc1..70042e8 100644
--- a/server/main.js
+++ b/server/main.js
@@ -9,26 +9,7 @@
'use strict';
-// import required classes
-const Managers = require('./src/managers');
-const wsServer = require('./src/core/server');
-
-// initialize core reference
-const core = {};
-
-// load and initialize main manager classes
-core.managers = {};
-core.managers.dynamicImports = global.dynamicImports = new Managers.ImportsManager(core, __dirname);
-core.managers.dynamicImports.init();
-
-const configManager = core.managers.config = new Managers.Config(core, __dirname, core.managers.dynamicImports);
-core.config = configManager.loadSync();
-
-const commands = core.commands = new Managers.CommandManager(core);
-commands.loadCommands();
-
-const stats = core.managers.stats = new Managers.Stats(core);
-stats.set('start-time', process.hrtime());
-
-// initialize and start the server
-const server = new wsServer(core);
+// import and initialize the core application
+const CoreApp = require('./src/serverLib/CoreApp');
+const coreApp = new CoreApp();
+coreApp.init();
diff --git a/server/package-lock.json b/server/package-lock.json
index b23f7f6..27205bc 100644
--- a/server/package-lock.json
+++ b/server/package-lock.json
@@ -1,6 +1,6 @@
{
"name": "hack.chat-v2",
- "version": "2.1.0",
+ "version": "2.1.9",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@@ -27,11 +27,6 @@
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c="
},
- "bindings": {
- "version": "1.2.1",
- "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.2.1.tgz",
- "integrity": "sha1-FK1hE4EtLTfXLme0ystLtyZQXxE="
- },
"brace-expansion": {
"version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
@@ -89,15 +84,6 @@
"resolved": "https://registry.npmjs.org/dateformat/-/dateformat-3.0.3.tgz",
"integrity": "sha512-jyCETtSl3VMZMWeRo7iY1FL19ges1t55hMo5yaam4Jrsm5EPL89UQkoQRyiI+Yf4k8r2ZpdngkV8hr1lIdjb3Q=="
},
- "deasync": {
- "version": "0.1.14",
- "resolved": "https://registry.npmjs.org/deasync/-/deasync-0.1.14.tgz",
- "integrity": "sha512-wN8sIuEqIwyQh72AG7oY6YQODCxIp1eXzEZlZznBuwDF8Q03Tdy9QNp1BNZXeadXoklNrw+Ip1fch+KXo/+ASw==",
- "requires": {
- "bindings": "~1.2.1",
- "node-addon-api": "^1.6.0"
- }
- },
"deep-equal": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-0.2.2.tgz",
@@ -234,11 +220,6 @@
"resolved": "https://registry.npmjs.org/ncp/-/ncp-1.0.1.tgz",
"integrity": "sha1-0VNn5cuHQyuhF9K/gP30Wuz7QkY="
},
- "node-addon-api": {
- "version": "1.6.2",
- "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-1.6.2.tgz",
- "integrity": "sha512-479Bjw9nTE5DdBSZZWprFryHGjUaQC31y1wHo19We/k0BZlrmhqQitWoUL0cD8+scljCbIUL+E58oRDEakdGGA=="
- },
"once": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
diff --git a/server/package.json b/server/package.json
index 8595033..51a149d 100644
--- a/server/package.json
+++ b/server/package.json
@@ -21,7 +21,6 @@
"chalk": "^2.4.2",
"common-tags": "^1.8.0",
"dateformat": "^3.0.3",
- "deasync": "^0.1.14",
"didyoumean2": "^2.0.2",
"fs-extra": "^7.0.1",
"prompt": "^1.0.0",
diff --git a/server/src/commands/admin/addmod.js b/server/src/commands/admin/addmod.js
index c4fcdd5..a30b175 100644
--- a/server/src/commands/admin/addmod.js
+++ b/server/src/commands/admin/addmod.js
@@ -10,7 +10,7 @@ exports.run = async (core, server, socket, data) => {
}
// add new trip to config
- core.config.mods.push({ trip: data.trip }); // purposely not using `config.set()` to avoid auto-save
+ core.config.mods.push({ trip: data.trip });
// find targets current connections
let newMod = server.findSockets({ trip: data.trip });
diff --git a/server/src/commands/admin/reload.js b/server/src/commands/admin/reload.js
index 947f9d9..27be19c 100644
--- a/server/src/commands/admin/reload.js
+++ b/server/src/commands/admin/reload.js
@@ -10,7 +10,7 @@ exports.run = async (core, server, socket, data) => {
}
// do command reload and store results
- let loadResult = core.managers.dynamicImports.reloadDirCache('src/commands');
+ let loadResult = core.dynamicImports.reloadDirCache('src/commands');
loadResult += core.commands.loadCommands();
// clear and rebuild all module hooks
diff --git a/server/src/commands/admin/saveconfig.js b/server/src/commands/admin/saveconfig.js
index b8a769b..a95a39a 100644
--- a/server/src/commands/admin/saveconfig.js
+++ b/server/src/commands/admin/saveconfig.js
@@ -10,7 +10,7 @@ exports.run = async (core, server, socket, data) => {
}
// attempt save, notify of failure
- if (!core.managers.config.save()) {
+ if (!core.configManager.save()) {
return server.reply({
cmd: 'warn',
text: 'Failed to save config, check logs.'
diff --git a/server/src/commands/core/chat.js b/server/src/commands/core/chat.js
index 3455453..bb9584e 100644
--- a/server/src/commands/core/chat.js
+++ b/server/src/commands/core/chat.js
@@ -57,7 +57,7 @@ exports.run = async (core, server, socket, data) => {
server.broadcast( payload, { channel: socket.channel});
// stats are fun
- core.managers.stats.increment('messages-sent');
+ core.stats.increment('messages-sent');
};
// module hook functions
diff --git a/server/src/commands/core/invite.js b/server/src/commands/core/invite.js
index 8374309..046e47a 100644
--- a/server/src/commands/core/invite.js
+++ b/server/src/commands/core/invite.js
@@ -59,7 +59,7 @@ exports.run = async (core, server, socket, data) => {
}, socket);
// stats are fun
- core.managers.stats.increment('invites-sent');
+ core.stats.increment('invites-sent');
};
// module meta
diff --git a/server/src/commands/core/join.js b/server/src/commands/core/join.js
index 8b6a971..ce900db 100644
--- a/server/src/commands/core/join.js
+++ b/server/src/commands/core/join.js
@@ -135,7 +135,7 @@ exports.run = async (core, server, socket, data) => {
}, socket);
// stats are fun
- core.managers.stats.increment('users-joined');
+ core.stats.increment('users-joined');
};
// module meta
diff --git a/server/src/commands/core/morestats.js b/server/src/commands/core/morestats.js
index 69740a9..a71729c 100644
--- a/server/src/commands/core/morestats.js
+++ b/server/src/commands/core/morestats.js
@@ -43,17 +43,17 @@ exports.run = async (core, server, socket, data) => {
cmd: 'info',
text: stripIndents`current-connections: ${uniqueClientCount}
current-channels: ${uniqueChannels}
- users-joined: ${(core.managers.stats.get('users-joined') || 0)}
- invites-sent: ${(core.managers.stats.get('invites-sent') || 0)}
- messages-sent: ${(core.managers.stats.get('messages-sent') || 0)}
- users-banned: ${(core.managers.stats.get('users-banned') || 0)}
- users-kicked: ${(core.managers.stats.get('users-kicked') || 0)}
- stats-requested: ${(core.managers.stats.get('stats-requested') || 0)}
- server-uptime: ${formatTime(process.hrtime(core.managers.stats.get('start-time')))}`
+ users-joined: ${(core.stats.get('users-joined') || 0)}
+ invites-sent: ${(core.stats.get('invites-sent') || 0)}
+ messages-sent: ${(core.stats.get('messages-sent') || 0)}
+ users-banned: ${(core.stats.get('users-banned') || 0)}
+ users-kicked: ${(core.stats.get('users-kicked') || 0)}
+ stats-requested: ${(core.stats.get('stats-requested') || 0)}
+ server-uptime: ${formatTime(process.hrtime(core.stats.get('start-time')))}`
}, socket);
// stats are fun
- core.managers.stats.increment('stats-requested');
+ core.stats.increment('stats-requested');
};
// module hook functions
diff --git a/server/src/commands/core/stats.js b/server/src/commands/core/stats.js
index b271bb1..ff4b1ef 100644
--- a/server/src/commands/core/stats.js
+++ b/server/src/commands/core/stats.js
@@ -27,7 +27,7 @@ exports.run = async (core, server, socket, data) => {
}, socket);
// stats are fun
- core.managers.stats.increment('stats-requested');
+ core.stats.increment('stats-requested');
};
// module meta
diff --git a/server/src/commands/mod/ban.js b/server/src/commands/mod/ban.js
index 8236136..93c536b 100644
--- a/server/src/commands/mod/ban.js
+++ b/server/src/commands/mod/ban.js
@@ -56,7 +56,7 @@ exports.run = async (core, server, socket, data) => {
badClient.terminate();
// stats are fun
- core.managers.stats.increment('users-banned');
+ core.stats.increment('users-banned');
};
// module meta
diff --git a/server/src/commands/mod/kick.js b/server/src/commands/mod/kick.js
index 0e8ee0a..808defa 100644
--- a/server/src/commands/mod/kick.js
+++ b/server/src/commands/mod/kick.js
@@ -69,7 +69,7 @@ exports.run = async (core, server, socket, data) => {
}, { channel: socket.channel, uType: 'user' });
// stats are fun
- core.managers.stats.increment('users-kicked', kicked.length);
+ core.stats.increment('users-kicked', kicked.length);
};
// module meta
diff --git a/server/src/commands/mod/unban.js b/server/src/commands/mod/unban.js
index 0cd8ca7..71be9bb 100644
--- a/server/src/commands/mod/unban.js
+++ b/server/src/commands/mod/unban.js
@@ -49,7 +49,7 @@ exports.run = async (core, server, socket, data) => {
}, { uType: 'mod' });
// stats are fun
- core.managers.stats.decrement('users-banned');
+ core.stats.decrement('users-banned');
};
// module meta
diff --git a/server/src/managers/config.js b/server/src/managers/config.js
deleted file mode 100644
index 7b49aa8..0000000
--- a/server/src/managers/config.js
+++ /dev/null
@@ -1,240 +0,0 @@
-/**
- * Server configuration manager, handling loading, creation, parsing and saving
- * of the main config.json file
- *
- * Version: v2.0.0
- * Developer: Marzavec ( https://github.com/marzavec )
- * License: WTFPL ( http://www.wtfpl.net/txt/copying/ )
- *
- */
-
-const stripIndents = require('common-tags').stripIndents;
-const dateFormat = require('dateformat');
-const chalk = require('chalk');
-const fse = require('fs-extra');
-const prompt = require('prompt');
-const path = require('path');
-const deSync = require('deasync');
-
-// For hashing the admin's password into a trip.
-const crypto = require('crypto');
-
-const hash = (password) => {
- let sha = crypto.createHash('sha256');
- sha.update(password);
- return sha.digest('base64').substr(0, 6);
-};
-
-class ConfigManager {
- /**
- * Create a `ConfigManager` instance for (re)loading classes and config
- *
- * @param {Object} core reference to the global core object
- * @param {String} base executing directory name; __dirname
- * @param {Object} dynamicImports dynamic import engine reference
- */
- constructor (core, base, dynamicImports) {
- this._core = core;
- this._base = base;
-
- this._configPath = path.resolve(base, 'config/config.json');
-
- this._dynamicImports = dynamicImports;
- }
-
- /**
- * Pulls both core config questions along with any optional config questions,
- * used in building the initial config.json or re-building it.
- *
- * @param {Object} currentConfig an object containing current server settings, if any
- * @param {Object} optionalConfigs optional (non-core) module config
- */
- getQuestions (currentConfig, optionalConfigs) {
- let salt = null; // this is so it can be accessed from adminTrip.
-
- // core server setup questions
- const questions = {
- properties: {
- tripSalt: {
- type: 'string',
- required: !currentConfig.tripSalt,
- default: currentConfig.tripSalt,
- hidden: true,
- replace: '*',
- before: value => {
- salt = value;
- return salt;
- }
- },
- adminName: {
- pattern: /^"?[a-zA-Z0-9_]+"?$/,
- type: 'string',
- message: 'Nicks can only contain letters, numbers and underscores',
- required: !currentConfig.adminName,
- default: currentConfig.adminName,
- before: value => value.replace(/"/g, '')
- },
- adminTrip: {
- type: 'string',
- required: !currentConfig.adminTrip,
- default: currentConfig.adminTrip,
- hidden: true,
- replace: '*',
- description: 'adminPass',
- before: value => hash(value + salt)
- },
- websocketPort: {
- type: 'number',
- required: !currentConfig.websocketPort,
- default: currentConfig.websocketPort || 6060
- }
- }
- };
-
- // non-core server setup questions, for future plugin support
- Object.keys(optionalConfigs).forEach(configName => {
- const config = optionalConfigs[configName];
- const question = config.getQuestion(currentConfig, configName);
-
- if (!question) {
- return;
- }
-
- question.description = (question.description || configName) + ' (Optional)';
- questions.properties[configName] = question;
- });
-
- return questions;
- }
-
- /**
- * `load` function overload, only blocking
- *
- */
- loadSync () {
- let conf = {};
- conf = this.load();
-
- // websocketport is the last core config question, wait until it's been populated
- while(conf === null || typeof conf.websocketPort === 'undefined') {
- deSync.sleep(100);
- }
-
- return conf;
- }
-
- /**
- * (Re)builds the config.json (main server config), or loads the config into mem
- * if rebuilding, process will exit- this is to allow a process manager to take over
- *
- * @param {Boolean} reconfiguring set to true by `scripts/configure.js`, will exit if true
- */
- load (reconfiguring = false) {
- if (reconfiguring || !fse.existsSync(this._configPath)) {
- // gotta have that sexy console
- console.log(stripIndents`
- ${chalk.magenta('°º¤ø,¸¸,ø¤º°`°º¤ø,¸,ø¤°º¤ø,¸¸,ø¤º°`°º¤ø,¸°º¤ø,¸¸,ø¤º°`°º¤ø')}
- ${chalk.gray('--------------(') + chalk.white(' HackChat Setup Wizard v1.0 ') + chalk.gray(')--------------')}
- ${chalk.magenta('°º¤ø,¸¸,ø¤º°`°º¤ø,¸,ø¤°º¤ø,¸¸,ø¤º°`°º¤ø,¸°º¤ø,¸¸,ø¤º°`°º¤ø')}
-
- For advanced setup, see the documentation at:
- ${chalk.green('https://github.com/hack-chat/main/tree/master/documentation')}
-
- ${chalk.white('Note:')} ${chalk.green('npm/yarn run config')} will re-run this utility.
-
- You will now be asked for the following:
- - ${chalk.magenta(' Salt')}, the salt for username trip
- - ${chalk.magenta('Admin Name')}, the initial admin username
- - ${chalk.magenta('Admin Pass')}, the initial admin password
- - ${chalk.magenta(' Port')}, the port for the websocket
- \u200b
- `);
-
- let currentConfig = this._config || {};
- if (reconfiguring && fse.existsSync(this._configPath)) {
- this._backup();
- currentConfig = fse.readJSONSync(this._configPath);
- }
-
- prompt.get(this.getQuestions(currentConfig, this._dynamicImports.optionalConfigs), (err, res) => {
- if (typeof res.mods === 'undefined') {
- res.mods = [];
- }
-
- if (err) {
- console.error(err);
- process.exit(666); // SPOOKY!
- }
-
- try {
- fse.outputJsonSync(this._configPath, res);
- } catch (e) {
- console.error(`Couldn't write config to ${this._configPath}\n${e.stack}`);
- if (!reconfiguring) {
- process.exit(666); // SPOOKY!
- }
- }
-
- console.log('Config generated! You may now start the server normally.')
-
- process.exit(reconfiguring ? 0 : 42);
- });
-
- return null;
- }
-
- this._config = fse.readJSONSync(this._configPath);
-
- return this._config;
- }
-
- /**
- * Creates backup of current config into _configPath
- *
- */
- _backup () {
- const backupPath = `${this._configPath}.${dateFormat('dd-mm-yy-HH-MM-ss')}.bak`;
- fse.copySync(this._configPath, backupPath);
-
- return backupPath;
- }
-
- /**
- * First makes a backup of the current `config.json`, then writes current config
- * to disk
- *
- */
- save () {
- const backupPath = this._backup();
-
- if (!fse.existsSync(this._configPath)){
- fse.mkdirSync(this._configPath);
- }
-
- try {
- fse.writeJSONSync(this._configPath, this._config);
- fse.removeSync(backupPath);
-
- return true;
- } catch (err) {
- console.log(`Failed to save config file: ${err}`);
-
- return false;
- }
- }
-
- /**
- * Updates current config[`key`] with `value` then writes changes to disk
- *
- * @param {*} key arbitrary configuration key
- * @param {*} value new value to change `key` to
- */
- set (key, value) {
- const realKey = `${key}`;
- this._config[realKey] = value;
-
- this.save();
- }
-}
-
-module.exports = ConfigManager;
diff --git a/server/src/managers/index.js b/server/src/managers/index.js
deleted file mode 100644
index 2fac8fb..0000000
--- a/server/src/managers/index.js
+++ /dev/null
@@ -1,6 +0,0 @@
-module.exports = {
- CommandManager: require('./commands'),
- Config: require('./config'),
- ImportsManager: require('./imports-manager'),
- Stats: require('./stats')
-};
diff --git a/server/src/scripts/configLib/SetupWizard.js b/server/src/scripts/configLib/SetupWizard.js
new file mode 100644
index 0000000..339878d
--- /dev/null
+++ b/server/src/scripts/configLib/SetupWizard.js
@@ -0,0 +1,107 @@
+/**
+ * Server setup wizard, quick server setup and all that jazz. . .
+ *
+ * Version: v2.0.0
+ * Developer: Marzavec ( https://github.com/marzavec )
+ * License: WTFPL ( http://www.wtfpl.net/txt/copying/ )
+ *
+ */
+
+const fse = require('fs-extra');
+const prompt = require('prompt');
+const path = require('path');
+
+class SetupWizard {
+ /**
+ * Create a `SetupWizard` instance for initializing the server's config.json
+ *
+ * @param {Object} serverConfig reference to the server config class
+ */
+ constructor (serverConfig) {
+ this.serverConfig = serverConfig;
+ }
+
+ /**
+ * Roll a d20 and begin the wizarding process
+ *
+ */
+ async start () {
+ // load the current config to use as defaults, if available
+ let currentConfig = await this.serverConfig.load() || {};
+
+ // auto generate the salt if not currrently created
+ currentConfig.tripSalt = currentConfig.tripSalt ||
+ [...Array(Math.floor(Math.random()*1024)+1024)].map(i=>(~~(Math.random()*36)).toString(36)).join('');
+
+ // load the setup questions & set their defaults
+ let questions = require('../setupSchema/Questions.js');
+ questions.properties = this.setQuestionDefaults(questions.properties, currentConfig);
+
+ // force password re-entry
+ questions.properties.adminTrip.default = '';
+ questions.properties.adminTrip.required = true;
+
+ // output the packages setup banner
+ require('../setupSchema/Banner.js');
+
+ // let's start playing 20 questions
+ prompt.start();
+ prompt.get(questions, (err, result) => this.finalize(err, result));
+ }
+
+ /**
+ * Compares the currently loaded config with the stock questions, adds a default
+ * and required option to the question
+ *
+ * @param {Object} questions the set of questions from /setupSchema
+ * @param {Object} currentConfig the current server options
+ */
+ setQuestionDefaults (questions, currentConfig) {
+ Object.keys(questions).forEach(qName => {
+ if (typeof currentConfig[qName] !== 'undefined') {
+ questions[qName].default = currentConfig[qName];
+ questions[qName].required = false;
+ } else {
+ questions[qName].required = true;
+ }
+ });
+
+ return questions;
+ }
+
+ /**
+ * Looks like all the questions have been answered, check for errors or save
+ * the new config file
+ *
+ * @param {Object} err any errors generated by Prompt
+ * @param {Object} result the answers / new config setup
+ */
+ async finalize (err, result) {
+ // output errors and die if needed
+ if (err) {
+ console.error(err);
+ process.exit(0);
+ }
+
+ // initialize default mods config
+ if (typeof result.mods === 'undefined') {
+ result.mods = [];
+ }
+
+ // finally create the actual JSON file
+ try {
+ this.serverConfig.config = result;
+ await this.serverConfig.save();
+ } catch (e) {
+ console.error(`Couldn't write config to ${this.serverConfig.configPath}
+ ${e.stack}`);
+ }
+
+ // output the packages final notice before quitting
+ require('../setupSchema/Footer.js');
+
+ process.exit(0);
+ }
+}
+
+module.exports = SetupWizard;
diff --git a/server/src/scripts/configure.js b/server/src/scripts/configure.js
index 0ecd858..d7f2bf2 100644
--- a/server/src/scripts/configure.js
+++ b/server/src/scripts/configure.js
@@ -1,5 +1,5 @@
/**
- * Server configuration script, used reconfiguring server options
+ * Server configuration script, to (re)configure server options
*
* Version: v2.0.0
* Developer: Marzavec ( https://github.com/marzavec )
@@ -11,13 +11,11 @@
// import required classes
const path = require('path');
-const ImportsManager = require('../managers/imports-manager');
-const ConfigManager = require('../managers/config');
+const ConfigManager = require('../serverLib/ConfigManager');
+const SetupWizard = require('./configLib/SetupWizard');
// import and initialize configManager & dependencies
-const importManager = new ImportsManager(null, path.join(__dirname, '../..'));
-importManager.init();
-const configManager = new ConfigManager(null, path.join(__dirname, '../..'), importManager);
+const serverConfig = new ConfigManager(path.join(__dirname, '../..'));
+const setup = new SetupWizard(serverConfig);
-// execute config load with `reconfiguring` flag set to true
-configManager.load(true);
+setup.start();
diff --git a/server/src/scripts/setupSchema/Banner.js b/server/src/scripts/setupSchema/Banner.js
new file mode 100644
index 0000000..823f0fe
--- /dev/null
+++ b/server/src/scripts/setupSchema/Banner.js
@@ -0,0 +1,28 @@
+/**
+ * This script will be run before the package starts asking for the config data,
+ * used to output a simple guide for the coming questions, or to spam some sexy
+ * ascii art at the user.
+ *
+ */
+
+const stripIndents = require('common-tags').stripIndents;
+const chalk = require('chalk');
+
+// gotta have that sexy console
+console.log(stripIndents`
+ ${chalk.magenta('°º¤ø,¸¸,ø¤º°`°º¤ø,¸,ø¤°º¤ø,¸¸,ø¤º°`°º¤ø,¸°º¤ø,¸¸,ø¤º°`°º¤ø')}
+ ${chalk.gray('--------------(') + chalk.white(' HackChat Setup Wizard v2.0 ') + chalk.gray(')--------------')}
+ ${chalk.magenta('°º¤ø,¸¸,ø¤º°`°º¤ø,¸,ø¤°º¤ø,¸¸,ø¤º°`°º¤ø,¸°º¤ø,¸¸,ø¤º°`°º¤ø')}
+
+ For advanced setup, see the documentation at:
+ ${chalk.green('https://github.com/hack-chat/main/tree/master/documentation')}
+
+ ${chalk.white('Note:')} ${chalk.green('npm/yarn run config')} will re-run this utility.
+
+ You will now be asked for the following:
+ - ${chalk.magenta(' Salt')}, the salt for username trip
+ - ${chalk.magenta('Admin Name')}, the initial admin username
+ - ${chalk.magenta('Admin Pass')}, the initial admin password
+ - ${chalk.magenta(' Port')}, the port for the websocket
+ \u200b
+`);
diff --git a/server/src/scripts/setupSchema/Footer.js b/server/src/scripts/setupSchema/Footer.js
new file mode 100644
index 0000000..d2ee14f
--- /dev/null
+++ b/server/src/scripts/setupSchema/Footer.js
@@ -0,0 +1,7 @@
+/**
+ * This script will be run once all questions have finished and no errors have
+ * occured. You can congratulate the user on their fine choice in software usage
+ *
+ */
+
+console.log('Config generated! You may now start the server normally.');
diff --git a/server/src/scripts/setupSchema/Questions.js b/server/src/scripts/setupSchema/Questions.js
new file mode 100644
index 0000000..f84d32f
--- /dev/null
+++ b/server/src/scripts/setupSchema/Questions.js
@@ -0,0 +1,55 @@
+/**
+ * This object contains Prompt ( https://www.npmjs.com/package/prompt ) style
+ * questions that the SetupWizard will require an answer to. Questions are asked
+ * in the order they are specified here.
+ *
+ * The resulting config.json file will be used by the server, accessed by the
+ * name specified. IE, a valid use is; config.adminName
+ *
+ */
+
+const Questions = {
+ properties: {
+ tripSalt: {
+ description: 'Salt (leave as default)',
+ type: 'string',
+ hidden: true,
+ replace: '*',
+ before: value => {
+ salt = value;
+ return value;
+ }
+ },
+
+ adminName: {
+ description: 'Admin Nickname',
+ pattern: /^"?[a-zA-Z0-9_]+"?$/,
+ type: 'string',
+ message: 'Nicks can only contain letters, numbers and underscores',
+ before: value => value.replace(/"/g, '')
+ },
+
+ adminTrip: {
+ type: 'string',
+ hidden: true,
+ replace: '*',
+ description: 'Admin Password',
+ message: 'You must enter or re-enter a password',
+ before: value => {
+ const crypto = require('crypto');
+ let sha = crypto.createHash('sha256');
+ sha.update(value + salt);
+ return sha.digest('base64').substr(0, 6);
+ }
+ },
+
+ websocketPort: {
+ type: 'integer',
+ message: 'The port may only be a number!',
+ description: 'Websocket Port',
+ default: '6060'
+ }
+ }
+}
+
+module.exports = Questions;
diff --git a/server/src/managers/commands.js b/server/src/serverLib/CommandManager.js
index 434a16e..71c8884 100644
--- a/server/src/managers/commands.js
+++ b/server/src/serverLib/CommandManager.js
@@ -8,7 +8,6 @@
*/
const path = require('path');
-const chalk = require('chalk');
const didYouMean = require('didyoumean2').default;
class CommandManager {
@@ -33,7 +32,7 @@ class CommandManager {
const core = this.core;
- const commandImports = core.managers.dynamicImports.getImport('src/commands');
+ const commandImports = core.dynamicImports.getImport('src/commands');
let cmdErrors = '';
Object.keys(commandImports).forEach(file => {
let command = commandImports[file];
@@ -61,7 +60,7 @@ class CommandManager {
}
if (!command.category) {
- let base = path.join(this.core.managers.dynamicImports.base, 'commands');
+ let base = path.join(this.core.dynamicImports.base, 'commands');
let category = 'Uncategorized';
if (file.indexOf(path.sep) > -1) {
diff --git a/server/src/serverLib/ConfigManager.js b/server/src/serverLib/ConfigManager.js
new file mode 100644
index 0000000..ebec050
--- /dev/null
+++ b/server/src/serverLib/ConfigManager.js
@@ -0,0 +1,89 @@
+/**
+ * Server configuration manager, handling loading, creation, parsing and saving
+ * of the main config.json file
+ *
+ * Version: v2.0.0
+ * Developer: Marzavec ( https://github.com/marzavec )
+ * License: WTFPL ( http://www.wtfpl.net/txt/copying/ )
+ *
+ */
+const dateFormat = require('dateformat');
+const fse = require('fs-extra');
+const path = require('path');
+
+class ConfigManager {
+ /**
+ * Create a `ConfigManager` instance for managing application settings
+ *
+ * @param {String} base executing directory name; __dirname
+ */
+ constructor (base = __dirname) {
+ this.configPath = path.resolve(base, 'config/config.json');
+
+ if (!fse.existsSync(this.configPath)){
+ fse.ensureFileSync(this.configPath);
+ }
+ }
+
+ /**
+ * (Re)builds the config.json (main server config), or loads the config into mem
+ * if rebuilding, process will exit- this is to allow a process manager to take over
+ *
+ * @param {Boolean} reconfiguring set to true by `scripts/configure.js`, will exit if true
+ */
+ async load () {
+ try {
+ this.config = fse.readJsonSync(this.configPath);
+ } catch (e) {
+ return false;
+ }
+
+ return this.config;
+ }
+
+ /**
+ * Creates backup of current config into configPath
+ *
+ */
+ async backup () {
+ const backupPath = `${this.configPath}.${dateFormat('dd-mm-yy-HH-MM-ss')}.bak`;
+ fse.copySync(this.configPath, backupPath);
+
+ return backupPath;
+ }
+
+ /**
+ * First makes a backup of the current `config.json`, then writes current config
+ * to disk
+ *
+ */
+ async save () {
+ const backupPath = await this.backup();
+
+ try {
+ fse.writeJSONSync(this.configPath, this.config);
+ fse.removeSync(backupPath);
+
+ return true;
+ } catch (err) {
+ console.log(`Failed to save config file: ${err}`);
+
+ return false;
+ }
+ }
+
+ /**
+ * Updates current config[`key`] with `value` then writes changes to disk
+ *
+ * @param {*} key arbitrary configuration key
+ * @param {*} value new value to change `key` to
+ */
+ async set (key, value) {
+ const realKey = `${key}`;
+ this.config[realKey] = value;
+
+ return await this.save();
+ }
+}
+
+module.exports = ConfigManager;
diff --git a/server/src/serverLib/CoreApp.js b/server/src/serverLib/CoreApp.js
new file mode 100644
index 0000000..012ae44
--- /dev/null
+++ b/server/src/serverLib/CoreApp.js
@@ -0,0 +1,66 @@
+/**
+ * The core / global reference object
+ *
+ * Version: v2.0.0
+ * Developer: Marzavec ( https://github.com/marzavec )
+ * License: WTFPL ( http://www.wtfpl.net/txt/copying/ )
+ *
+ */
+
+const path = require('path');
+const {
+ CommandManager,
+ ConfigManager,
+ ImportsManager,
+ MainServer,
+ StatsManager
+} = require('./');
+
+class CoreApp {
+ /**
+ * Create the main core instance.
+ */
+ constructor () {
+
+ }
+
+ async init () {
+ await this.buildConfigManager();
+
+ this.buildImportManager();
+ this.buildCommandsManager();
+ this.buildStatsManager();
+ this.buildMainServer();
+ }
+
+ async buildConfigManager () {
+ this.configManager = new ConfigManager(path.join(__dirname, '../..'));
+ this.config = await this.configManager.load();
+
+ if (this.config === false) {
+ console.error('Missing config.json, have you run: npm run config');
+ process.exit(0);
+ }
+ }
+
+ buildImportManager () {
+ this.dynamicImports = new ImportsManager(path.join(__dirname, '../..'));
+ this.dynamicImports.init();
+ }
+
+ buildCommandsManager () {
+ this.commands = new CommandManager(this);
+ this.commands.loadCommands();
+ }
+
+ buildStatsManager () {
+ this.stats = new StatsManager(this);
+ this.stats.set('start-time', process.hrtime());
+ }
+
+ buildMainServer () {
+ this.server = new MainServer(this);
+ }
+}
+
+module.exports = CoreApp;
diff --git a/server/src/managers/imports-manager.js b/server/src/serverLib/ImportsManager.js
index d8b2144..a049d5c 100644
--- a/server/src/managers/imports-manager.js
+++ b/server/src/serverLib/ImportsManager.js
@@ -14,24 +14,12 @@ class ImportsManager {
/**
* Create a `ImportsManager` instance for (re)loading classes and config
*
- * @param {Object} core reference to the global core object
* @param {String} base executing directory name; __dirname
*/
- constructor (core, base) {
- this._core = core;
+ constructor (base) {
this._base = base;
this._imports = {};
- this._optionalConfigs = {};
- }
-
- /**
- * Pull core reference
- *
- * @type {Object} readonly
- */
- get core () {
- return this._core;
}
/**
@@ -44,15 +32,6 @@ class ImportsManager {
}
/**
- * Pull optional (none-core) config options
- *
- * @type {Object}
- */
- get optionalConfigs () {
- return Object.assign({}, this._optionalConfigs);
- }
-
- /**
* Initialize this class and start loading target directories
*
*/
@@ -89,12 +68,6 @@ class ImportsManager {
return errorText;
}
- if (imported.configs) {
- imported.configs.forEach(config => {
- this._optionalConfigs[config.name] = config;
- });
- }
-
if (!this._imports[dirName]) {
this._imports[dirName] = {};
}
diff --git a/server/src/core/server.js b/server/src/serverLib/MainServer.js
index 4132b55..628d8ab 100644
--- a/server/src/core/server.js
+++ b/server/src/serverLib/MainServer.js
@@ -12,10 +12,10 @@ const socketReady = require('ws').OPEN;
const crypto = require('crypto');
const ipSalt = [...Array(Math.floor(Math.random()*128)+128)].map(i=>(~~(Math.random()*36)).toString(36)).join('');
const internalCmdKey = [...Array(Math.floor(Math.random()*128)+128)].map(i=>(~~(Math.random()*36)).toString(36)).join('');
-const Police = require('./rateLimiter');
+const RateLimiter = require('./RateLimiter');
const pulseSpeed = 16000; // ping all clients every X ms
-class server extends wsServer {
+class MainServer extends wsServer {
/**
* Create a HackChat server instance.
*
@@ -26,7 +26,7 @@ class server extends wsServer {
this._core = core;
this._hooks = {};
- this._police = new Police();
+ this._police = new RateLimiter();
this._cmdBlacklist = {};
this._cmdKey = internalCmdKey;
@@ -431,4 +431,4 @@ class server extends wsServer {
}
}
-module.exports = server;
+module.exports = MainServer;
diff --git a/server/src/core/rateLimiter.js b/server/src/serverLib/RateLimiter.js
index 0c2a384..87a1f3a 100644
--- a/server/src/core/rateLimiter.js
+++ b/server/src/serverLib/RateLimiter.js
@@ -8,7 +8,7 @@
*
*/
-class Police {
+class RateLimiter {
/**
* Create a ratelimiter instance.
*/
@@ -94,10 +94,10 @@ class Police {
if (typeof this._hashes[id] !== 'undefined') {
id = this._hashes[id];
}
-
+
let record = this.search(id);
record.arrested = false;
}
}
-module.exports = Police;
+module.exports = RateLimiter;
diff --git a/server/src/managers/stats.js b/server/src/serverLib/StatsManager.js
index 20f1ae3..4ec7ddf 100644
--- a/server/src/managers/stats.js
+++ b/server/src/serverLib/StatsManager.js
@@ -7,13 +7,13 @@
*
*/
-class Stats {
+class StatsManager {
/**
* Create a stats instance.
*
*/
constructor () {
- this._stats = {};
+ this.data = {};
}
/**
@@ -22,7 +22,7 @@ class Stats {
* @param {String} key Reference to the arbitrary store name
*/
get (key) {
- return this._stats[key];
+ return this.data[key];
}
/**
@@ -32,7 +32,7 @@ class Stats {
* @param {Number} value New value for `key`
*/
set (key, value) {
- this._stats[key] = value;
+ this.data[key] = value;
}
/**
@@ -56,4 +56,4 @@ class Stats {
}
}
-module.exports = Stats;
+module.exports = StatsManager;
diff --git a/server/src/serverLib/index.js b/server/src/serverLib/index.js
new file mode 100644
index 0000000..4583de6
--- /dev/null
+++ b/server/src/serverLib/index.js
@@ -0,0 +1,8 @@
+module.exports = {
+ CommandManager: require('./CommandManager'),
+ ConfigManager: require('./ConfigManager'),
+ ImportsManager: require('./ImportsManager'),
+ MainServer: require('./MainServer'),
+ RateLimiter: require('./RateLimiter'),
+ StatsManager: require('./StatsManager')
+};