-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelnet.js
146 lines (117 loc) · 4.1 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
/*
* Original: https://github.com/sonicdoe/teletype
* Date: 26.02.17
* Change: Made the TELNET_EOL constant variable through the options
* Edited by: Platzhalter
* */
'use strict'
const net = require('net')
// “The Telnet protocol defines the sequence CR LF to mean "end-of-line".”
// See https://tools.ietf.org/html/rfc1123#page-21.
let TELNET_EOL = '\r\n'
class Teletype {
constructor (host, port, options) {
if(options.eol) TELNET_EOL = options.eol
if (!port) port = 23
options = Object.assign({}, { timeout: false }, options)
if (typeof host !== 'string') {
throw new TypeError('host must be a string.')
}
if (typeof port !== 'number') {
throw new TypeError('port must be a number.')
}
if (typeof options.timeout !== 'number' && options.timeout !== false) {
throw new TypeError('options.timeout must be a number or false.')
}
this.host = host
this.port = port
this.timeout = options.timeout
}
_lazyConnect () {
return new Promise((resolve, reject) => {
let timeout
if (this._client && !this._client.connecting) {
return resolve(this._client)
}
if (!this._client) {
this._client = net.connect({
host: this.host,
port: this.port
})
if (this.timeout) {
timeout = setTimeout(() => {
this._client.destroy()
reject(errorTimedOut('Could not connect in time.'))
}, this.timeout)
}
// “The TELNET protocol is based upon the notion of a virtual teletype,
// employing a 7-bit ASCII character set.”
// See https://tools.ietf.org/html/rfc206#page-2.
this._client.setEncoding('ascii')
}
this._client.once('error', (err) => {
if (timeout) clearTimeout(timeout)
reject(err)
})
this._client.once('connect', () => {
this._client.removeListener('error', reject)
if (timeout) clearTimeout(timeout)
resolve(this._client)
})
})
}
exec (command, match) {
if (typeof command !== 'string') {
return Promise.reject(new TypeError('command must be a string.'))
}
return this._lazyConnect().then(client => {
let promise
if (match) {
promise = this.readUntil(match)
}
client.write(command + TELNET_EOL)
if (match) return promise
})
}
readUntil (match) {
if (!(match instanceof RegExp)) {
return Promise.reject(new TypeError('match must be a RegExp.'))
}
return this._lazyConnect().then(client => {
return new Promise((resolve, reject) => {
let timeout
const onData = data => {
const lines = data.split(TELNET_EOL)
for (const line of lines) {
if (match.test(line)) {
resolve(line)
client.removeListener('data', onData)
if (timeout) clearTimeout(timeout)
break
}
}
}
client.on('data', onData)
if (this.timeout) {
timeout = setTimeout(() => {
reject(errorTimedOut('Did not receive matching data in time.'))
}, this.timeout)
}
})
})
}
close () {
return this._lazyConnect().then(client => {
client.end()
client.destroy()
})
}
}
function errorTimedOut (message) {
const err = new Error(message)
err.code = 'ETIMEDOUT'
return err
}
module.exports = (host, port, options) => {
return new Teletype(host, port, options)
}