2016-10-25 03:49:45 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const conf = require('./config.js');
|
|
|
|
const logger = require('./logger.js');
|
|
|
|
const ServerModule = require('./server_module.js').ServerModule;
|
|
|
|
const clientConns = require('./client_connections.js');
|
2018-11-24 00:41:16 +00:00
|
|
|
const UserProps = require('./user_property.js');
|
2016-10-25 03:49:45 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
|
|
|
const _ = require('lodash');
|
2016-10-25 03:49:45 +00:00
|
|
|
|
|
|
|
module.exports = class LoginServerModule extends ServerModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: we need to max connections -- e.g. from config 'maxConnections'
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
prepareClient(client, cb) {
|
2019-01-31 03:42:30 +00:00
|
|
|
if(client.user.isAuthenticated()) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const theme = require('./theme.js');
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Choose initial theme before we have user context
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-11-08 01:33:07 +00:00
|
|
|
const preLoginTheme = _.get(conf.config, 'theme.preLogin');
|
|
|
|
if('*' === preLoginTheme) {
|
2018-11-24 00:41:16 +00:00
|
|
|
client.user.properties[UserProps.ThemeId] = theme.getRandomTheme() || '';
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2018-11-24 00:41:16 +00:00
|
|
|
client.user.properties[UserProps.ThemeId] = preLoginTheme;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-24 00:41:16 +00:00
|
|
|
theme.setClientTheme(client, client.user.properties[UserProps.ThemeId]);
|
2019-01-31 03:42:30 +00:00
|
|
|
return cb(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleNewClient(client, clientSock, modInfo) {
|
2019-04-09 02:14:00 +00:00
|
|
|
clientSock.on('error', err => {
|
|
|
|
logger.log.warn({ modInfo, error : err.message }, 'Client socket error');
|
|
|
|
});
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2019-01-04 04:02:57 +00:00
|
|
|
// Start tracking the client. A session ID aka client ID
|
|
|
|
// will be established in addNewClient() below.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(_.isUndefined(client.session)) {
|
|
|
|
client.session = {};
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
client.session.serverName = modInfo.name;
|
|
|
|
client.session.isSecure = _.isBoolean(client.isSecure) ? client.isSecure : (modInfo.isSecure || false);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
clientConns.addNewClient(client, clientSock);
|
|
|
|
|
|
|
|
client.on('ready', readyOptions => {
|
|
|
|
|
|
|
|
client.startIdleMonitor();
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// Go to module -- use default error handler
|
2018-06-22 05:15:04 +00:00
|
|
|
this.prepareClient(client, () => {
|
|
|
|
require('./connect.js').connectEntry(client, readyOptions.firstMenu);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('end', () => {
|
|
|
|
clientConns.removeClient(client);
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('error', err => {
|
2019-04-09 02:14:00 +00:00
|
|
|
logger.log.info({ clientId : client.session.id, error : err.message }, 'Connection error');
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
client.on('close', err => {
|
|
|
|
const logFunc = err ? logger.log.info : logger.log.debug;
|
|
|
|
logFunc( { clientId : client.session.id }, 'Connection closed');
|
|
|
|
|
|
|
|
clientConns.removeClient(client);
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('idle timeout', () => {
|
|
|
|
client.log.info('User idle timeout expired');
|
|
|
|
|
|
|
|
client.menuStack.goto('idleLogoff', err => {
|
|
|
|
if(err) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// likely just doesn't exist
|
2018-06-22 05:15:04 +00:00
|
|
|
client.term.write('\nIdle timeout expired. Goodbye!\n');
|
|
|
|
client.end();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-10-25 03:49:45 +00:00
|
|
|
};
|