-
Notifications
You must be signed in to change notification settings - Fork 104
/
door_party.js
159 lines (135 loc) · 5.92 KB
/
door_party.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* jslint node: true */
'use strict';
// enigma-bbs
const { MenuModule } = require('./menu_module.js');
const { resetScreen } = require('./ansi_term.js');
const { Errors } = require('./enig_error.js');
const { trackDoorRunBegin, trackDoorRunEnd } = require('./door_util.js');
// deps
const async = require('async');
const SSHClient = require('ssh2').Client;
exports.moduleInfo = {
name: 'DoorParty',
desc: 'DoorParty Access Module',
author: 'NuSkooler',
};
exports.getModule = class DoorPartyModule extends MenuModule {
constructor(options) {
super(options);
// establish defaults
this.config = options.menuConfig.config;
this.config.host = this.config.host || 'dp.throwbackbbs.com';
this.config.sshPort = this.config.sshPort || 2022;
this.config.rloginPort = this.config.rloginPort || 513;
}
initSequence() {
let clientTerminated;
const self = this;
async.series(
[
function validateConfig(callback) {
return self.validateConfigFields(
{
host: 'string',
username: 'string',
password: 'string',
bbsTag: 'string',
sshPort: 'number',
rloginPort: 'number',
},
callback
);
},
function establishSecureConnection(callback) {
self.client.term.write(resetScreen());
self.client.term.write('Connecting to DoorParty, please wait...\n');
const sshClient = new SSHClient();
let pipeRestored = false;
let pipedStream;
let doorTracking;
const restorePipe = function () {
if (pipedStream && !pipeRestored && !clientTerminated) {
self.client.term.output.unpipe(pipedStream);
self.client.term.output.resume();
if (doorTracking) {
trackDoorRunEnd(doorTracking);
doorTracking = null;
}
}
};
sshClient.on('ready', () => {
// track client termination so we can clean up early
self.client.once('end', () => {
self.client.log.info(
'Connection ended. Terminating DoorParty connection'
);
clientTerminated = true;
sshClient.end();
});
// establish tunnel for rlogin
sshClient.forwardOut(
'127.0.0.1',
self.config.sshPort,
self.config.host,
self.config.rloginPort,
(err, stream) => {
if (err) {
return callback(
Errors.General('Failed to establish tunnel')
);
}
doorTracking = trackDoorRunBegin(self.client);
//
// Send rlogin
// DoorParty wants the "server username" portion to be in the format of [BBS_TAG]USERNAME, e.g.
// [XA]nuskooler
//
const rlogin = `\x00${self.client.user.username}\x00[${self.config.bbsTag}]${self.client.user.username}\x00${self.client.term.termType}\x00`;
stream.write(rlogin);
pipedStream = stream; // :TODO: this is hacky...
self.client.term.output.pipe(stream);
stream.on('data', d => {
// :TODO: we should just pipe this...
self.client.term.rawWrite(d);
});
stream.on('end', () => {
sshClient.end();
});
stream.on('close', () => {
restorePipe();
sshClient.end();
});
}
);
});
sshClient.on('error', err => {
self.client.log.info(
`DoorParty SSH client error: ${err.message}`
);
trackDoorRunEnd(doorTracking);
});
sshClient.on('close', () => {
restorePipe();
callback(null);
});
sshClient.connect({
host: self.config.host,
port: self.config.sshPort,
username: self.config.username,
password: self.config.password,
});
// note: no explicit callback() until we're finished!
},
],
err => {
if (err) {
self.client.log.warn({ error: err.message }, 'DoorParty error');
}
// if the client is still here, go to previous
if (!clientTerminated) {
self.prevMenu();
}
}
);
}
};