2017-05-26 14:25:41 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const Config = require('../../config.js').get;
|
|
|
|
const TelnetClient = require('./telnet.js').TelnetClient;
|
|
|
|
const Log = require('../../logger.js').log;
|
|
|
|
const LoginServerModule = require('../../login_server_module.js');
|
2018-12-27 09:46:16 +00:00
|
|
|
const { Errors } = require('../../enig_error.js');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// deps
|
|
|
|
const _ = require('lodash');
|
|
|
|
const WebSocketServer = require('ws').Server;
|
|
|
|
const http = require('http');
|
|
|
|
const https = require('https');
|
|
|
|
const fs = require('graceful-fs');
|
|
|
|
const Writable = require('stream');
|
2018-12-27 09:46:16 +00:00
|
|
|
const forEachSeries = require('async/forEachSeries');
|
2017-05-26 14:25:41 +00:00
|
|
|
|
|
|
|
const ModuleInfo = exports.moduleInfo = {
|
2018-06-23 03:26:46 +00:00
|
|
|
name : 'WebSocket',
|
|
|
|
desc : 'WebSocket Server',
|
|
|
|
author : 'NuSkooler',
|
|
|
|
packageName : 'codes.l33t.enigma.websocket.server',
|
2017-05-26 14:25:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function WebSocketClient(ws, req, serverType) {
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
Object.defineProperty(this, 'isSecure', {
|
|
|
|
get : () => ('secure' === serverType || true === this.proxied) ? true : false,
|
|
|
|
});
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
this.dataHandler = function(data) {
|
2018-07-08 17:23:56 +00:00
|
|
|
if(self.pipedDest) {
|
|
|
|
self.pipedDest.write(data);
|
|
|
|
} else {
|
|
|
|
self.socketBridge.emit('data', data);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// This bridge makes accessible various calls that client sub classes
|
|
|
|
// want to access on I/O socket
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
this.socketBridge = new class SocketBridge extends Writable {
|
|
|
|
constructor(ws) {
|
|
|
|
super();
|
|
|
|
this.ws = ws;
|
|
|
|
}
|
|
|
|
|
|
|
|
end() {
|
|
|
|
return ws.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
write(data, cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
cb = cb || ( () => { /* eat it up */} ); // handle data writes after close
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
return this.ws.send(data, { binary : true }, cb);
|
|
|
|
}
|
|
|
|
|
2018-07-08 17:23:56 +00:00
|
|
|
pipe(dest) {
|
|
|
|
Log.trace('WebSocket SocketBridge pipe()');
|
|
|
|
self.pipedDest = dest;
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
unpipe() {
|
|
|
|
Log.trace('WebSocket SocketBridge unpipe()');
|
2018-07-08 17:23:56 +00:00
|
|
|
self.pipedDest = null;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resume() {
|
|
|
|
Log.trace('WebSocket SocketBridge resume()');
|
|
|
|
}
|
|
|
|
|
|
|
|
get remoteAddress() {
|
2018-06-23 03:26:46 +00:00
|
|
|
// Support X-Forwarded-For and X-Real-IP headers for proxied connections
|
2018-06-22 05:15:04 +00:00
|
|
|
return (self.proxied && (req.headers['x-forwarded-for'] || req.headers['x-real-ip'])) || req.connection.remoteAddress;
|
|
|
|
}
|
|
|
|
}(ws);
|
|
|
|
|
|
|
|
ws.on('message', this.dataHandler);
|
|
|
|
|
|
|
|
ws.on('close', () => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// we'll remove client connection which will in turn end() via our SocketBridge above
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.emit('end');
|
|
|
|
});
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Montior connection status with ping/pong
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
ws.on('pong', () => {
|
|
|
|
Log.trace(`Pong from ${this.socketBridge.remoteAddress}`);
|
|
|
|
ws.isConnectionAlive = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
TelnetClient.call(this, this.socketBridge, this.socketBridge);
|
|
|
|
|
|
|
|
Log.trace( { headers : req.headers }, 'WebSocket connection headers' );
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// If the config allows it, look for 'x-forwarded-proto' as "https"
|
|
|
|
// to override |isSecure|
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(true === _.get(Config(), 'loginServers.webSocket.proxied') &&
|
2018-06-23 03:26:46 +00:00
|
|
|
'https' === req.headers['x-forwarded-proto'])
|
2018-06-22 05:15:04 +00:00
|
|
|
{
|
|
|
|
Log.debug(`Assuming secure connection due to X-Forwarded-Proto of "${req.headers['x-forwarded-proto']}"`);
|
|
|
|
this.proxied = true;
|
|
|
|
} else {
|
|
|
|
this.proxied = false;
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// start handshake process
|
2018-06-22 05:15:04 +00:00
|
|
|
this.banner();
|
2017-05-26 14:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require('util').inherits(WebSocketClient, TelnetClient);
|
|
|
|
|
|
|
|
const WSS_SERVER_TYPES = [ 'insecure', 'secure' ];
|
|
|
|
|
|
|
|
exports.getModule = class WebSocketLoginServer extends LoginServerModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:19:26 +00:00
|
|
|
createServer(cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// We will actually create up to two servers:
|
|
|
|
// * insecure websocket (ws://)
|
|
|
|
// * secure (tls) websocket (wss://)
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const config = _.get(Config(), 'loginServers.webSocket');
|
|
|
|
if(!_.isObject(config)) {
|
2018-12-27 09:19:26 +00:00
|
|
|
return cb(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
const wsPort = _.get(config, 'ws.port');
|
|
|
|
const wssPort = _.get(config, 'wss.port');
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
if(true === _.get(config, 'ws.enabled') && _.isNumber(wsPort)) {
|
|
|
|
const httpServer = http.createServer( (req, resp) => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// dummy handler
|
2018-06-22 05:15:04 +00:00
|
|
|
resp.writeHead(200);
|
|
|
|
return resp.end('ENiGMA½ BBS WebSocket Server!');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.insecure = {
|
2018-06-23 03:26:46 +00:00
|
|
|
httpServer : httpServer,
|
|
|
|
wsServer : new WebSocketServer( { server : httpServer } ),
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_.isObject(config, 'wss') && true === _.get(config, 'wss.enabled') && _.isNumber(wssPort)) {
|
|
|
|
const httpServer = https.createServer({
|
2018-06-23 03:26:46 +00:00
|
|
|
key : fs.readFileSync(config.wss.keyPem),
|
|
|
|
cert : fs.readFileSync(config.wss.certPem),
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.secure = {
|
2018-06-23 03:26:46 +00:00
|
|
|
httpServer : httpServer,
|
|
|
|
wsServer : new WebSocketServer( { server : httpServer } ),
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
2018-12-27 09:19:26 +00:00
|
|
|
|
|
|
|
return cb(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 09:46:16 +00:00
|
|
|
listen(cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Send pings every 30s
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
setInterval( () => {
|
|
|
|
WSS_SERVER_TYPES.forEach(serverType => {
|
|
|
|
if(this[serverType]) {
|
|
|
|
this[serverType].wsServer.clients.forEach(ws => {
|
|
|
|
if(false === ws.isConnectionAlive) {
|
|
|
|
Log.debug('WebSocket connection seems inactive. Terminating.');
|
|
|
|
return ws.terminate();
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
ws.isConnectionAlive = false; // pong will reset this
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
Log.trace('Ping to remote WebSocket client');
|
2018-12-15 06:21:33 +00:00
|
|
|
try {
|
|
|
|
ws.ping('', false); // false=don't mask
|
|
|
|
} catch(e) { // don't barf on closing state
|
|
|
|
/* nothing */
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 30000);
|
|
|
|
|
2018-12-27 09:46:16 +00:00
|
|
|
forEachSeries(WSS_SERVER_TYPES, (serverType, nextServerType) => {
|
|
|
|
const server = this[serverType];
|
|
|
|
if(!server) {
|
|
|
|
return nextServerType(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
const serverName = `${ModuleInfo.name} (${serverType})`;
|
2019-04-10 02:25:37 +00:00
|
|
|
const conf = _.get(Config(), [ 'loginServers', 'webSocket', 'secure' === serverType ? 'wss' : 'ws' ] );
|
|
|
|
const confPort = conf.port;
|
2018-12-27 09:46:16 +00:00
|
|
|
const port = parseInt(confPort);
|
|
|
|
|
|
|
|
if(isNaN(port)) {
|
|
|
|
Log.error( { server : serverName, port : confPort }, 'Cannot load server (invalid port)' );
|
|
|
|
return nextServerType(Errors.Invalid(`Invalid port: ${confPort}`));
|
|
|
|
}
|
|
|
|
|
2019-04-10 02:25:37 +00:00
|
|
|
server.httpServer.listen(port, conf.address, err => {
|
2018-12-27 09:46:16 +00:00
|
|
|
if(err) {
|
|
|
|
return nextServerType(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
server.wsServer.on('connection', (ws, req) => {
|
|
|
|
const webSocketClient = new WebSocketClient(ws, req, serverType);
|
|
|
|
this.handleNewClient(webSocketClient, webSocketClient.socketBridge, ModuleInfo);
|
|
|
|
});
|
|
|
|
|
|
|
|
Log.info( { server : serverName, port : port }, 'Listening for connections' );
|
|
|
|
return nextServerType(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
cb(err);
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
webSocketConnection(conn) {
|
|
|
|
const webSocketClient = new WebSocketClient(conn);
|
|
|
|
this.handleNewClient(webSocketClient, webSocketClient.socketShim, ModuleInfo);
|
|
|
|
}
|
2017-05-26 14:25:41 +00:00
|
|
|
};
|