mirror of
https://github.com/hack-chat/main.git
synced 2024-03-22 13:20:33 +08:00
refactoring 1 of 2
This commit is contained in:
parent
8ceb0d7363
commit
c634e03cb5
10
CHANGELOG.md
10
CHANGELOG.md
@ -3,6 +3,16 @@ All notable changes to this project will be documented in this file.
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [2.1.9 pre 2.2] - 2019-03-18
|
||||
### Changed
|
||||
- Configuration script setup, making it more portable/sane
|
||||
- Refactored naming scheme and entry point
|
||||
|
||||
### Removed
|
||||
- Configuration setup from `./serverLib/ConfigManager`
|
||||
- Unused feature allowing command modules to add to the configuration/setup process
|
||||
- `deasync` dependency
|
||||
|
||||
## [2.1.9] - 2019-02-21
|
||||
### Added
|
||||
- `./server/src/commands/core/emote.js` module to provide action text
|
||||
|
@ -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();
|
||||
|
21
server/package-lock.json
generated
21
server/package-lock.json
generated
@ -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",
|
||||
|
@ -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",
|
||||
|
@ -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 });
|
||||
|
@ -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
|
||||
|
@ -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.'
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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;
|
@ -1,6 +0,0 @@
|
||||
module.exports = {
|
||||
CommandManager: require('./commands'),
|
||||
Config: require('./config'),
|
||||
ImportsManager: require('./imports-manager'),
|
||||
Stats: require('./stats')
|
||||
};
|
107
server/src/scripts/configLib/SetupWizard.js
Normal file
107
server/src/scripts/configLib/SetupWizard.js
Normal file
@ -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;
|
@ -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();
|
||||
|
28
server/src/scripts/setupSchema/Banner.js
Normal file
28
server/src/scripts/setupSchema/Banner.js
Normal file
@ -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
|
||||
`);
|
7
server/src/scripts/setupSchema/Footer.js
Normal file
7
server/src/scripts/setupSchema/Footer.js
Normal file
@ -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.');
|
55
server/src/scripts/setupSchema/Questions.js
Normal file
55
server/src/scripts/setupSchema/Questions.js
Normal file
@ -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;
|
@ -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) {
|
89
server/src/serverLib/ConfigManager.js
Normal file
89
server/src/serverLib/ConfigManager.js
Normal file
@ -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;
|
66
server/src/serverLib/CoreApp.js
Normal file
66
server/src/serverLib/CoreApp.js
Normal file
@ -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;
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -43,15 +31,6 @@ class ImportsManager {
|
||||
return this._base;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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] = {};
|
||||
}
|
@ -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;
|
@ -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;
|
@ -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;
|
8
server/src/serverLib/index.js
Normal file
8
server/src/serverLib/index.js
Normal file
@ -0,0 +1,8 @@
|
||||
module.exports = {
|
||||
CommandManager: require('./CommandManager'),
|
||||
ConfigManager: require('./ConfigManager'),
|
||||
ImportsManager: require('./ImportsManager'),
|
||||
MainServer: require('./MainServer'),
|
||||
RateLimiter: require('./RateLimiter'),
|
||||
StatsManager: require('./StatsManager')
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user