2015-12-06 22:39:24 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-12-03 02:33:07 +00:00
|
|
|
const { MenuModule } = require('./menu_module.js');
|
|
|
|
const { resetScreen } = require('./ansi_term.js');
|
|
|
|
const { Errors } = require('./enig_error.js');
|
2019-01-19 05:09:10 +00:00
|
|
|
const {
|
|
|
|
trackDoorRunBegin,
|
|
|
|
trackDoorRunEnd
|
|
|
|
} = require('./door_util.js');
|
2015-12-06 22:39:24 +00:00
|
|
|
|
2018-12-03 02:33:07 +00:00
|
|
|
// deps
|
2018-06-23 03:26:46 +00:00
|
|
|
const async = require('async');
|
|
|
|
const http = require('http');
|
|
|
|
const net = require('net');
|
|
|
|
const crypto = require('crypto');
|
2015-12-06 22:39:24 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
const packageJson = require('../package.json');
|
2015-12-06 22:39:24 +00:00
|
|
|
|
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
Expected configuration block:
|
|
|
|
|
|
|
|
{
|
|
|
|
module: bbs_link
|
|
|
|
...
|
|
|
|
config: {
|
|
|
|
sysCode: XXXXX
|
|
|
|
authCode: XXXXX
|
|
|
|
schemeCode: XXXX
|
|
|
|
door: lord
|
|
|
|
|
|
|
|
// default hoss: games.bbslink.net
|
|
|
|
host: games.bbslink.net
|
|
|
|
|
|
|
|
// defualt port: 23
|
|
|
|
port: 23
|
|
|
|
}
|
|
|
|
}
|
2015-12-06 22:39:24 +00:00
|
|
|
*/
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: BUG: When a client disconnects, it's not handled very well -- the log is spammed with tons of errors
|
|
|
|
// :TODO: ENH: Support nodeMax and tooManyArt
|
2015-12-06 22:39:24 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2018-06-23 03:26:46 +00:00
|
|
|
name : 'BBSLink',
|
|
|
|
desc : 'BBSLink Access Module',
|
|
|
|
author : 'NuSkooler',
|
2015-12-06 22:39:24 +00:00
|
|
|
};
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
exports.getModule = class BBSLinkModule extends MenuModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
this.config = options.menuConfig.config;
|
|
|
|
this.config.host = this.config.host || 'games.bbslink.net';
|
|
|
|
this.config.port = this.config.port || 23;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
initSequence() {
|
|
|
|
let token;
|
|
|
|
let randomKey;
|
|
|
|
let clientTerminated;
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function validateConfig(callback) {
|
2018-12-03 02:33:07 +00:00
|
|
|
return self.validateConfigFields(
|
|
|
|
{
|
|
|
|
host : 'string',
|
|
|
|
sysCode : 'string',
|
|
|
|
authCode : 'string',
|
|
|
|
schemeCode : 'string',
|
|
|
|
door : 'string',
|
|
|
|
port : 'number',
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function acquireToken(callback) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Acquire an authentication token
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
crypto.randomBytes(16, function rand(ex, buf) {
|
|
|
|
if(ex) {
|
|
|
|
callback(ex);
|
|
|
|
} else {
|
|
|
|
randomKey = buf.toString('base64').substr(0, 6);
|
|
|
|
self.simpleHttpRequest('/token.php?key=' + randomKey, null, function resp(err, body) {
|
|
|
|
if(err) {
|
|
|
|
callback(err);
|
|
|
|
} else {
|
|
|
|
token = body.trim();
|
|
|
|
self.client.log.trace( { token : token }, 'BBSLink token');
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function authenticateToken(callback) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Authenticate the token we acquired previously
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2019-01-07 00:42:07 +00:00
|
|
|
const headers = {
|
2018-06-23 03:26:46 +00:00
|
|
|
'X-User' : self.client.user.userId.toString(),
|
|
|
|
'X-System' : self.config.sysCode,
|
|
|
|
'X-Auth' : crypto.createHash('md5').update(self.config.authCode + token).digest('hex'),
|
|
|
|
'X-Code' : crypto.createHash('md5').update(self.config.schemeCode + token).digest('hex'),
|
|
|
|
'X-Rows' : self.client.term.termHeight.toString(),
|
|
|
|
'X-Key' : randomKey,
|
|
|
|
'X-Door' : self.config.door,
|
|
|
|
'X-Token' : token,
|
|
|
|
'X-Type' : 'enigma-bbs',
|
|
|
|
'X-Version' : packageJson.version,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self.simpleHttpRequest('/auth.php?key=' + randomKey, headers, function resp(err, body) {
|
|
|
|
var status = body.trim();
|
|
|
|
|
|
|
|
if('complete' === status) {
|
2018-12-03 02:33:07 +00:00
|
|
|
return callback(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-12-03 02:33:07 +00:00
|
|
|
return callback(Errors.AccessDenied(`Bad authentication status: ${status}`));
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function createTelnetBridge(callback) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Authentication with BBSLink successful. Now, we need to create a telnet
|
|
|
|
// bridge from us to them
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2019-01-07 00:42:07 +00:00
|
|
|
const connectOpts = {
|
2018-06-23 03:26:46 +00:00
|
|
|
port : self.config.port,
|
|
|
|
host : self.config.host,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2019-01-07 00:42:07 +00:00
|
|
|
let clientTerminated;
|
2019-07-12 04:49:15 +00:00
|
|
|
let dataOut;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
self.client.term.write(resetScreen());
|
2019-07-12 04:49:15 +00:00
|
|
|
self.client.term.write(` Connecting to ${self.config.host}, please wait...\n`);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2019-01-19 05:09:10 +00:00
|
|
|
const doorTracking = trackDoorRunBegin(self.client, `bbslink_${self.config.door}`);
|
2019-01-07 00:42:07 +00:00
|
|
|
|
|
|
|
const bridgeConnection = net.createConnection(connectOpts, function connected() {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.client.log.info(connectOpts, 'BBSLink bridge connection established');
|
|
|
|
|
2019-07-12 04:49:15 +00:00
|
|
|
dataOut = (data) => {
|
|
|
|
return bridgeConnection.write(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.client.term.output.on('data', dataOut);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
self.client.once('end', function clientEnd() {
|
|
|
|
self.client.log.info('Connection ended. Terminating BBSLink connection');
|
|
|
|
clientTerminated = true;
|
2019-07-12 04:49:15 +00:00
|
|
|
bridgeConnection.end();
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-07-12 04:49:15 +00:00
|
|
|
const restore = () => {
|
|
|
|
if(dataOut && self.client.term.output) {
|
|
|
|
self.client.term.output.removeListener('data', dataOut);
|
|
|
|
dataOut = null;
|
|
|
|
}
|
2019-01-07 00:42:07 +00:00
|
|
|
|
2019-01-19 05:09:10 +00:00
|
|
|
trackDoorRunEnd(doorTracking);
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bridgeConnection.on('data', function incomingData(data) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// pass along
|
|
|
|
// :TODO: just pipe this as well
|
2018-06-22 05:15:04 +00:00
|
|
|
self.client.term.rawWrite(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
bridgeConnection.on('end', function connectionEnd() {
|
2019-07-12 04:49:15 +00:00
|
|
|
restore();
|
2018-12-03 02:33:07 +00:00
|
|
|
return callback(clientTerminated ? Errors.General('Client connection terminated') : null);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
bridgeConnection.on('error', function error(err) {
|
|
|
|
self.client.log.info('BBSLink bridge connection error: ' + err.message);
|
2019-07-12 04:49:15 +00:00
|
|
|
restore();
|
|
|
|
return callback(err);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
if(err) {
|
|
|
|
self.client.log.warn( { error : err.toString() }, 'BBSLink connection error');
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!clientTerminated) {
|
|
|
|
self.prevMenu();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
simpleHttpRequest(path, headers, cb) {
|
|
|
|
const getOpts = {
|
2018-06-23 03:26:46 +00:00
|
|
|
host : this.config.host,
|
|
|
|
path : path,
|
|
|
|
headers : headers,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const req = http.get(getOpts, function response(resp) {
|
|
|
|
let data = '';
|
|
|
|
|
|
|
|
resp.on('data', function chunk(c) {
|
|
|
|
data += c;
|
|
|
|
});
|
|
|
|
|
|
|
|
resp.on('end', function respEnd() {
|
|
|
|
cb(null, data);
|
|
|
|
req.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
req.on('error', function reqErr(err) {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
};
|