-
Notifications
You must be signed in to change notification settings - Fork 106
/
telnet.js
291 lines (240 loc) · 8.98 KB
/
telnet.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// ENiGMA½
const LoginServerModule = require('../../login_server_module');
const { Client } = require('../../client');
const Config = require('../../config').get;
const { log: Log } = require('../../logger');
const { Errors } = require('../../enig_error');
// deps
const net = require('net');
const {
TelnetSocket,
TelnetSpec: { Options, Commands },
} = require('telnet-socket');
const { inherits } = require('util');
const ModuleInfo = (exports.moduleInfo = {
name: 'Telnet',
desc: 'Telnet Server v2',
author: 'NuSkooler',
isSecure: false,
packageName: 'codes.l33t.enigma.telnet.server.v2',
});
class TelnetClient {
constructor(socket) {
Client.apply(this, socket, socket);
this.socket = new TelnetSocket(socket);
this.setInputOutput(this.socket, this.socket);
//
// Wait up to 3s to hear about from our terminal type request
// then go ahead and move on...
//
setTimeout(() => {
this._clientReady();
}, 3000);
this.dataHandler = function (data) {
this.emit('data', data);
}.bind(this);
this.socket.on('data', this.dataHandler);
this.socket.on('error', err => {
this._logDebug({ error: err.message }, 'Socket error');
return this.emit('end');
});
this.socket.on('end', () => {
this.emit('end');
});
this.socket.on('command error', (command, err) => {
this._logDebug({ command, error: err.message }, 'Command error');
});
this.socket.on('DO', command => {
switch (command.option) {
// We've already stated we WILL do the following via
// the banner - some terminals will ask over and over
// if we respond to a DO with a WILL, so just don't
// do anything...
case Options.SGA:
case Options.ECHO:
case Options.TRANSMIT_BINARY:
break;
default:
return this.socket.command(Commands.WONT, command.option);
}
});
this.socket.on('DONT', command => {
this._logTrace(command, 'DONT');
});
this.socket.on('WILL', command => {
switch (command.option) {
case Options.TTYPE:
return this.socket.sb.send.ttype();
case Options.NEW_ENVIRON:
return this.socket.sb.send.new_environ([
'ROWS',
'COLUMNS',
'TERM',
'TERM_PROGRAM',
]);
default:
break;
}
});
this.socket.on('WONT', command => {
return this._logTrace(command, 'WONT');
});
this.socket.on('SB', command => {
switch (command.option) {
case Options.TTYPE:
this.setTermType(command.optionData.ttype);
return this._clientReady();
case Options.NEW_ENVIRON:
{
this._logDebug(
{
vars: command.optionData.vars,
uservars: command.optionData.uservars,
},
'New environment received'
);
// get a value from vars with fallback of user vars
const getValue = name => {
return (
command.optionData.vars &&
(command.optionData.vars.find(nv => nv.name === name) ||
command.optionData.uservars.find(
nv => nv.name === name
))
);
};
if ('unknown' === this.term.termType) {
// allow from vars or user vars
const term = getValue('TERM') || getValue('TERM_PROGRAM');
if (term) {
this.setTermType(term.value);
}
}
if (0 === this.term.termHeight || 0 === this.term.termWidth) {
const updateTermSize = what => {
const value = parseInt(getValue(what));
if (value) {
this.term[
what === 'ROWS' ? 'termHeight' : 'termWidth'
] = value;
this._logDebug(
{ [what]: value, source: 'NEW-ENVIRON' },
'Window size updated'
);
}
};
updateTermSize('ROWS');
updateTermSize('COLUMNS');
}
}
break;
case Options.NAWS:
{
const { width, height } = command.optionData;
this.term.termWidth = width;
this.term.termHeight = height;
if (width) {
this.term.env.COLUMNS = width;
}
if (height) {
this.term.env.ROWS = height;
}
this._logDebug(
{ width, height, source: 'NAWS' },
'Windows size updated'
);
}
break;
default:
return this._logTrace(command, 'SB');
}
});
this.socket.on('IP', command => {
this._logDebug(command, 'Interrupt Process (IP) - Ending session');
return this.disconnect();
});
this.socket.on('AYT', command => {
this.socket.write('\b');
return this._logTrace(command, 'Are You There (AYT) - Replied');
});
}
get dataPassthrough() {
return this.socket.passthrough;
}
set dataPassthrough(passthrough) {
this.socket.passthrough = passthrough;
}
disconnect() {
try {
return this.socket.rawSocket.end();
} catch (e) {
// ignored
}
}
banner() {
this.socket.dont.echo(); // don't echo characters
this.socket.will.echo(); // ...we'll echo them back
this.socket.will.sga();
this.socket.do.sga();
this.socket.do.transmit_binary();
this.socket.will.transmit_binary();
this.socket.do.ttype();
this.socket.do.naws();
this.socket.do.new_environ();
}
_logTrace(info, msg) {
if (Config().loginServers.telnet.traceConnections) {
const log = this.log || Log;
return log.trace(info, `Telnet: ${msg}`);
}
}
_logDebug(info, msg) {
const log = this.log || Log;
return log.debug(info, `Telnet: ${msg}`);
}
_clientReady() {
if (this.clientReadyHandled) {
return; // already processed
}
this.clientReadyHandled = true;
this.emit('ready', { firstMenu: Config().loginServers.telnet.firstMenu });
}
}
inherits(TelnetClient, Client);
exports.getModule = class TelnetServerModule extends LoginServerModule {
constructor() {
super();
}
createServer(cb) {
this.server = net.createServer(socket => {
const client = new TelnetClient(socket);
client.banner(); // start negotiations
this.handleNewClient(client, socket, ModuleInfo);
});
this.server.on('error', err => {
Log.info({ error: err.message }, 'Telnet server error');
});
return cb(null);
}
listen(cb) {
const config = Config();
const port = parseInt(config.loginServers.telnet.port);
if (isNaN(port)) {
Log.error(
{ server: ModuleInfo.name, port: config.loginServers.telnet.port },
'Cannot load server (invalid port)'
);
return cb(Errors.Invalid(`Invalid port: ${config.loginServers.telnet.port}`));
}
this.server.listen(port, config.loginServers.telnet.address, err => {
if (!err) {
Log.info(
{ server: ModuleInfo.name, port: port },
'Listening for connections'
);
}
return cb(err);
});
}
};
exports.TelnetClient = TelnetClient; // WebSockets is a wrapper on top of this