Events system, first pass

This commit is contained in:
Josh M. McKee 2017-06-10 17:29:00 -07:00
parent d6f5429ce8
commit 002b0e941e
5 changed files with 92 additions and 32 deletions

View File

@ -11,6 +11,7 @@ const logger = require('./logger.js');
const database = require('./database.js');
const clientConns = require('./client_connections.js');
const resolvePath = require('./misc_util.js').resolvePath;
const events = require('./events.js');
// deps
const async = require('async');
@ -84,9 +85,11 @@ function main() {
}
return callback(err);
});
},
}
],
function complete(err) {
events.registerModules();
// note this is escaped:
fs.readFile(paths.join(__dirname, '../misc/startup_banner.asc'), 'utf8', (err, banner) => {
console.info(ENIGMA_COPYRIGHT);

View File

@ -3,6 +3,7 @@
// ENiGMA½
const ansi = require('./ansi_term.js');
const events = require('./events.js');
// deps
const async = require('async');
@ -175,6 +176,9 @@ function connectEntry(client, nextMenu) {
//
displayBanner(term);
// fire event
events.emit('codes.l33t.enigma.system.connect', {'client': client});
setTimeout( () => {
return client.menuStack.goto(nextMenu);
}, 500);

47
core/events.js Normal file
View File

@ -0,0 +1,47 @@
/* jslint node: true */
'use strict';
const Config = require('./config.js');
const fs = require('fs');
const path = require('path');
const events = require('events');
const logger = require('./logger.js');
var eventEmitter = new events.EventEmitter();
var self = module.exports = {
emit: function(eventName, args) {
logger.log.debug("Emit "+eventName);
eventEmitter.emit(eventName, args);
},
on: function(eventName, listener) {
logger.log.debug("Register listener for "+eventName);
eventEmitter.on(eventName, listener);
},
remove: function(eventName, listener) {
logger.log.debug("Remove listener for "+eventName);
eventEmitter.removeListener(eventName, listener);
},
registerModules: function() {
var mods = fs.readdirSync(Config.config.paths.mods);
mods.forEach(function(item) {
var modPath = Config.config.paths.mods+item;
if (item.substr(item.length-3) != '.js') {
modPath += path.sep+item+'.js';
}
if (fs.existsSync(modPath)) {
var module = require(modPath);
if (module.registerEvents !== undefined) {
logger.log.debug(modPath+" calling registerEvents function");
module.registerEvents();
} else {
logger.log.debug(modPath+" has no registerEvents function");
}
} else {
logger.log.debug(modPath+" - file not found");
}
});
}
}

View File

@ -25,9 +25,11 @@ const ModuleInfo = exports.moduleInfo = {
function WebSocketClient(ws, req, serverType) {
Object.defineProperty(this, 'isSecure', {
get : () => ('secure' === serverType || true === this.secureProxyConnection) ? true : false,
get : () => ('secure' === serverType || true === this.proxied) ? true : false,
});
const self = this;
//
// This bridge makes accessible various calls that client sub classes
// want to access on I/O socket
@ -47,7 +49,8 @@ function WebSocketClient(ws, req, serverType) {
}
get remoteAddress() {
return req.connection.remoteAddress;
// Support X-Forwarded-For and X-Real-IP headers for proxied connections
return (self.proxied && (req.headers['x-forwarded-for'] || req.headers['x-real-ip'])) || req.connection.remoteAddress;
}
}(ws);
@ -56,7 +59,8 @@ function WebSocketClient(ws, req, serverType) {
});
ws.on('close', () => {
this.end();
// we'll remove client connection which will in turn end() via our SocketBridge above
return this.emit('end');
});
//
@ -75,11 +79,13 @@ function WebSocketClient(ws, req, serverType) {
// If the config allows it, look for 'x-forwarded-proto' as "https"
// to override |isSecure|
//
if(true === _.get(Config, 'loginServers.webSocket.secureProxy') &&
if(true === _.get(Config, 'loginServers.webSocket.proxied') &&
'https' === req.headers['x-forwarded-proto'])
{
Log.debug(`Assuming secure connection due to X-Forwarded-Proto of ${req.headers['x-forwarded-proto']}`);
this.secureProxyConnection = true;
Log.debug(`Assuming secure connection due to X-Forwarded-Proto of "${req.headers['x-forwarded-proto']}"`);
this.proxied = true;
} else {
this.proxied = false;
}
// start handshake process