mirror of
https://github.com/NuSkooler/enigma-bbs.git
synced 2025-06-07 13:15:28 +02:00
Events system, first pass
This commit is contained in:
parent
d6f5429ce8
commit
002b0e941e
5 changed files with 92 additions and 32 deletions
|
@ -11,6 +11,7 @@ const logger = require('./logger.js');
|
||||||
const database = require('./database.js');
|
const database = require('./database.js');
|
||||||
const clientConns = require('./client_connections.js');
|
const clientConns = require('./client_connections.js');
|
||||||
const resolvePath = require('./misc_util.js').resolvePath;
|
const resolvePath = require('./misc_util.js').resolvePath;
|
||||||
|
const events = require('./events.js');
|
||||||
|
|
||||||
// deps
|
// deps
|
||||||
const async = require('async');
|
const async = require('async');
|
||||||
|
@ -84,9 +85,11 @@ function main() {
|
||||||
}
|
}
|
||||||
return callback(err);
|
return callback(err);
|
||||||
});
|
});
|
||||||
},
|
}
|
||||||
],
|
],
|
||||||
function complete(err) {
|
function complete(err) {
|
||||||
|
events.registerModules();
|
||||||
|
|
||||||
// note this is escaped:
|
// note this is escaped:
|
||||||
fs.readFile(paths.join(__dirname, '../misc/startup_banner.asc'), 'utf8', (err, banner) => {
|
fs.readFile(paths.join(__dirname, '../misc/startup_banner.asc'), 'utf8', (err, banner) => {
|
||||||
console.info(ENIGMA_COPYRIGHT);
|
console.info(ENIGMA_COPYRIGHT);
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
// ENiGMA½
|
// ENiGMA½
|
||||||
const ansi = require('./ansi_term.js');
|
const ansi = require('./ansi_term.js');
|
||||||
|
const events = require('./events.js');
|
||||||
|
|
||||||
// deps
|
// deps
|
||||||
const async = require('async');
|
const async = require('async');
|
||||||
|
@ -175,6 +176,9 @@ function connectEntry(client, nextMenu) {
|
||||||
//
|
//
|
||||||
displayBanner(term);
|
displayBanner(term);
|
||||||
|
|
||||||
|
// fire event
|
||||||
|
events.emit('codes.l33t.enigma.system.connect', {'client': client});
|
||||||
|
|
||||||
setTimeout( () => {
|
setTimeout( () => {
|
||||||
return client.menuStack.goto(nextMenu);
|
return client.menuStack.goto(nextMenu);
|
||||||
}, 500);
|
}, 500);
|
||||||
|
|
47
core/events.js
Normal file
47
core/events.js
Normal 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");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -25,9 +25,11 @@ const ModuleInfo = exports.moduleInfo = {
|
||||||
function WebSocketClient(ws, req, serverType) {
|
function WebSocketClient(ws, req, serverType) {
|
||||||
|
|
||||||
Object.defineProperty(this, 'isSecure', {
|
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
|
// This bridge makes accessible various calls that client sub classes
|
||||||
// want to access on I/O socket
|
// want to access on I/O socket
|
||||||
|
@ -47,7 +49,8 @@ function WebSocketClient(ws, req, serverType) {
|
||||||
}
|
}
|
||||||
|
|
||||||
get remoteAddress() {
|
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);
|
}(ws);
|
||||||
|
|
||||||
|
@ -56,7 +59,8 @@ function WebSocketClient(ws, req, serverType) {
|
||||||
});
|
});
|
||||||
|
|
||||||
ws.on('close', () => {
|
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"
|
// If the config allows it, look for 'x-forwarded-proto' as "https"
|
||||||
// to override |isSecure|
|
// to override |isSecure|
|
||||||
//
|
//
|
||||||
if(true === _.get(Config, 'loginServers.webSocket.secureProxy') &&
|
if(true === _.get(Config, 'loginServers.webSocket.proxied') &&
|
||||||
'https' === req.headers['x-forwarded-proto'])
|
'https' === req.headers['x-forwarded-proto'])
|
||||||
{
|
{
|
||||||
Log.debug(`Assuming secure connection due to X-Forwarded-Proto of ${req.headers['x-forwarded-proto']}`);
|
Log.debug(`Assuming secure connection due to X-Forwarded-Proto of "${req.headers['x-forwarded-proto']}"`);
|
||||||
this.secureProxyConnection = true;
|
this.proxied = true;
|
||||||
|
} else {
|
||||||
|
this.proxied = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// start handshake process
|
// start handshake process
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue