forked from bitfocus/companion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telnet.js
314 lines (251 loc) · 6.94 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* This file is part of the Companion project
* Copyright (c) 2018 Bitfocus AS
* Authors: William Viker <[email protected]>, Håkon Nessjøen <[email protected]>
*
* This program is free software.
* You should have received a copy of the MIT licence as well as the Bitfocus
* Individual Contributor License Agreement for companion along with
* this program.
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial activities involving the Companion software without
* disclosing the source code of your own applications.
*
*/
var debug = require('debug')('lib/telnet')
var Stream = require('stream')
var Transform = Stream.Transform
var EventEmitter = require('events').EventEmitter
var inherits = require('util').inherits
var net = require('net')
var tcp_sockets = []
const NULL = 0
const DATA = 0
const SE = 240
const SB = 250
const WILL = 251
const WONT = 252
const DO = 253
const DONT = 254
const IAC = 255
var reverse = {
240: 'SE',
250: 'SB',
251: 'WILL',
252: 'WONT',
253: 'DO',
254: 'DONT',
255: 'IAC',
}
const STATUS_UNKNOWN = null
const STATUS_OK = 0
const STATUS_WARNING = 1
const STATUS_ERROR = 2
// Private functions
function new_status(self, status, message) {
if (self.status != status) {
self.status = status
self.emit('status_change', status, message)
}
}
function tcp_reconnect(self) {
delete self.try_timer
new_status(self, STATUS_WARNING, 'Connecting')
self.failed_attempts++
debug('Reconnecting to ' + self.host + ':' + self.port + ', retry ' + self.failed_attempts)
self.connect()
}
function telnetStreamer(host, port, options) {
var self = this
if (!(this instanceof telnetStreamer)) return new telnetStreamer(options)
self.destroyed = false
self._options = options = options || {}
self.ts = new telnetStream(options)
self.status = undefined
self.host = host
self.port = port
self.options = options || {}
self.trying = false
self.try_timer = undefined
self.failed_attempts = 0
self.connected = false
if (self.options.reconnect_interval === undefined) {
self.options.reconnect_interval = 2000
}
if (self.options.reconnect === undefined) {
self.options.reconnect = true
}
self.ts = new telnetStream()
self.socket = new net.Socket()
self.socket.setKeepAlive(true)
self.socket.setNoDelay(true)
self.socket.pipe(self.ts)
debug('new telnet instance for sending to ' + host)
self.socket.on('error', function (err) {
self.trying = false
self.connected = false
if (self.options.reconnect) {
if (self.try_timer !== undefined) {
clearTimeout(self.try_timer)
}
self.try_timer = setTimeout(tcp_reconnect, self.options.reconnect_interval, self)
}
// status levels: null = unknown, 0 = ok, 1 = warning, 2 = error
debug('error', err.message)
new_status(self, STATUS_ERROR, err.message)
self.emit.apply(self, ['error'].concat(Array.from(arguments)))
})
self.socket.on('connect', function () {
self.failed_attempts = 0
self.connected = true
self.trying = false
new_status(self, STATUS_OK)
self.emit('connect', self.socket)
})
self.socket.on('end', function () {
debug('Disconnected')
self.connected = false
new_status(self, STATUS_ERROR, 'Disconnected')
if (!self.trying && self.options.reconnect) {
if (self.try_timer !== undefined) {
clearTimeout(self.try_timer)
}
self.try_timer = setTimeout(tcp_reconnect, self.options.reconnect_interval, self)
}
self.emit('end')
})
self.ts.on('iac', self.emit.bind(self, 'iac'))
self.ts.on('sb', self.emit.bind(self, 'sb'))
self.ts.on('data', self.emit.bind(self, 'data'))
self.ts.on('drain', self.emit.bind(self, 'drain'))
tcp_sockets.push(self.socket)
debug(tcp_sockets.length + ' TCP sockets in use (+1)')
// Let caller install event handlers first
setImmediate(self.connect.bind(self))
setTimeout(() => {
if (!self.destroyed && !self.listenerCount('error')) {
// The socket is active and has no listeners. Log an error for the module devs!
console.error(`Danger: Telnet client for ${self.host}:${self.port} is missing an error handler!`)
}
}, 5000)
return self
}
inherits(telnetStreamer, EventEmitter)
telnetStreamer.prototype.write = telnetStreamer.prototype.send = function (message, cb) {
var self = this
if (self.connected) {
debug('sending ' + (message !== undefined ? message.length : 'undefined') + ' bytes to', self.host, self.port)
self.socket.write(message, function (error) {
if (error) {
new_status(self, STATUS_ERROR, error.message)
self.emit.apply(self, ['error'].concat(Array.from(arguments)))
if (typeof cb == 'function') {
cb(error)
}
return
}
if (typeof cb == 'function') {
cb()
}
})
return true
} else {
debug('Tried to send, but not connected')
return false
}
}
telnetStreamer.prototype.connect = function () {
var self = this
if (self.trying) {
return
}
try {
debug('new conn')
self.trying = true
self.socket.connect(self.port, self.host)
} catch (e) {
console.log('TELNET: ', e)
}
}
telnetStreamer.prototype.destroy = function () {
var self = this
self.destroyed = true
if (self.try_timer !== undefined) {
clearTimeout(self.try_timer)
}
if (tcp_sockets.indexOf(self.socket) !== -1) {
tcp_sockets.splice(tcp_sockets.indexOf(self.socket), 1)
debug(tcp_sockets.length + ' telnet sockets in use (-1)')
}
self.socket.removeAllListeners()
self.ts.removeAllListeners()
self.removeAllListeners()
self.ts.destroy()
self.socket.destroy()
}
/*
* TelnetStream
*/
function telnetStream(options) {
this._options = options = options || {}
this.buffer = Buffer.from('')
this.subbuffer = Buffer.from('')
this.state = DATA
debug('new telnetStreamer(', options, ')')
Transform.call(this, options)
}
inherits(telnetStream, Transform)
telnetStream.prototype._transform = function _transform(obj, encoding, callback) {
for (var i = 0; i < obj.length; ++i) {
this._handleByte(obj[i])
}
var data = this._getData()
if (data.length) {
this.push(data)
}
callback()
}
telnetStream.prototype._handleByte = function (byte) {
var self = this
if (self.state === DATA) {
if (byte === IAC) {
self.state = IAC
return
}
self.buffer = Buffer.concat([self.buffer, Buffer.from([byte])])
} else if (self.state === IAC) {
switch (byte) {
case SB:
case WILL:
case WONT:
case DO:
case DONT:
self.state = byte
break
default:
self.state = DATA
break
}
} else if (self.state >= WILL && self.state <= DONT) {
self.emit('iac', reverse[self.state], byte)
self.state = DATA
return
} else if (self.state === SB) {
if (byte === SE) {
self.emit('sb', self.subbuffer)
self.state = DATA
self.subbuffer = Buffer.from('')
return
}
self.subbuffer = Buffer.concat([self.subbuffer, Buffer.from([byte])])
}
}
telnetStream.prototype._getData = function () {
var self = this
var buff = self.buffer
self.buffer = Buffer.from('')
return buff
}
module.exports = telnetStreamer