forked from mscdex/ssh2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
336 lines (303 loc) · 9.54 KB
/
utils.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
'use strict';
const { SFTP } = require('./protocol/SFTP.js');
const MAX_CHANNEL = 2 ** 32 - 1;
function onChannelOpenFailure(self, recipient, info, cb) {
self._chanMgr.remove(recipient);
if (typeof cb !== 'function')
return;
let err;
if (info instanceof Error) {
err = info;
} else if (typeof info === 'object' && info !== null) {
err = new Error(`(SSH) Channel open failure: ${info.description}`);
err.reason = info.reason;
} else {
err = new Error(
'(SSH) Channel open failure: server closed channel unexpectedly'
);
err.reason = '';
}
cb(err);
}
function onCHANNEL_CLOSE(self, recipient, channel, err, dead) {
if (typeof channel === 'function') {
// We got CHANNEL_CLOSE instead of CHANNEL_OPEN_FAILURE when
// requesting to open a channel
onChannelOpenFailure(self, recipient, err, channel);
return;
}
if (typeof channel !== 'object' || channel === null)
return;
if (channel.incoming && channel.incoming.state === 'closed')
return;
self._chanMgr.remove(recipient);
if (channel.server && channel.constructor.name === 'Session')
return;
channel.incoming.state = 'closed';
if (channel.readable)
channel.push(null);
if (channel.server) {
if (channel.stderr.writable)
channel.stderr.end();
} else if (channel.stderr.readable) {
channel.stderr.push(null);
}
if (channel.constructor !== SFTP
&& (channel.outgoing.state === 'open'
|| channel.outgoing.state === 'eof')
&& !dead) {
channel.close();
}
if (channel.outgoing.state === 'closing')
channel.outgoing.state = 'closed';
const readState = channel._readableState;
const writeState = channel._writableState;
if (writeState && !writeState.ending && !writeState.finished && !dead)
channel.end();
// Take care of any outstanding channel requests
const chanCallbacks = channel._callbacks;
channel._callbacks = [];
for (let i = 0; i < chanCallbacks.length; ++i)
chanCallbacks[i](true);
if (channel.server) {
if (!channel.readable
|| channel.destroyed
|| (readState && readState.endEmitted)) {
channel.emit('close');
} else {
channel.once('end', () => channel.emit('close'));
}
} else {
let doClose;
switch (channel.type) {
case '[email protected]':
case 'direct-tcpip':
doClose = () => channel.emit('close');
break;
default: {
// Align more with node child processes, where the close event gets
// the same arguments as the exit event
const exit = channel._exit;
doClose = () => {
if (exit.code === null)
channel.emit('close', exit.code, exit.signal, exit.dump, exit.desc);
else
channel.emit('close', exit.code);
};
}
}
if (!channel.readable
|| channel.destroyed
|| (readState && readState.endEmitted)) {
doClose();
} else {
channel.once('end', doClose);
}
const errReadState = channel.stderr._readableState;
if (!channel.stderr.readable
|| channel.stderr.destroyed
|| (errReadState && errReadState.endEmitted)) {
channel.stderr.emit('close');
} else {
channel.stderr.once('end', () => channel.stderr.emit('close'));
}
}
}
class ChannelManager {
constructor(client) {
this._client = client;
this._channels = {};
this._cur = -1;
this._count = 0;
}
add(val) {
// Attempt to reserve an id
let id;
// Optimized paths
if (this._cur < MAX_CHANNEL) {
id = ++this._cur;
} else if (this._count === 0) {
// Revert and reset back to fast path once we no longer have any channels
// open
this._cur = 0;
id = 0;
} else {
// Slower lookup path
// This path is triggered we have opened at least MAX_CHANNEL channels
// while having at least one channel open at any given time, so we have
// to search for a free id.
const channels = this._channels;
for (let i = 0; i < MAX_CHANNEL; ++i) {
if (channels[i] === undefined) {
id = i;
break;
}
}
}
if (id === undefined)
return -1;
this._channels[id] = (val || true);
++this._count;
return id;
}
update(id, val) {
if (typeof id !== 'number' || id < 0 || id >= MAX_CHANNEL || !isFinite(id))
throw new Error(`Invalid channel id: ${id}`);
if (val && this._channels[id])
this._channels[id] = val;
}
get(id) {
if (typeof id !== 'number' || id < 0 || id >= MAX_CHANNEL || !isFinite(id))
throw new Error(`Invalid channel id: ${id}`);
return this._channels[id];
}
remove(id) {
if (typeof id !== 'number' || id < 0 || id >= MAX_CHANNEL || !isFinite(id))
throw new Error(`Invalid channel id: ${id}`);
if (this._channels[id]) {
delete this._channels[id];
if (this._count)
--this._count;
}
}
cleanup(err) {
const channels = this._channels;
this._channels = {};
this._cur = -1;
this._count = 0;
const chanIDs = Object.keys(channels);
const client = this._client;
for (let i = 0; i < chanIDs.length; ++i) {
const id = +chanIDs[i];
const channel = channels[id];
onCHANNEL_CLOSE(client, id, channel._channel || channel, err, true);
}
}
}
const isRegExp = (() => {
const toString = Object.prototype.toString;
return (val) => toString.call(val) === '[object RegExp]';
})();
function generateAlgorithmList(algoList, defaultList, supportedList) {
if (Array.isArray(algoList) && algoList.length > 0) {
// Exact list
for (let i = 0; i < algoList.length; ++i) {
if (supportedList.indexOf(algoList[i]) === -1)
throw new Error(`Unsupported algorithm: ${algoList[i]}`);
}
return algoList;
}
if (typeof algoList === 'object' && algoList !== null) {
// Operations based on the default list
const keys = Object.keys(algoList);
let list = defaultList;
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
let val = algoList[key];
switch (key) {
case 'append':
if (!Array.isArray(val))
val = [val];
if (Array.isArray(val)) {
for (let j = 0; j < val.length; ++j) {
const append = val[j];
if (typeof append === 'string') {
if (!append || list.indexOf(append) !== -1)
continue;
if (supportedList.indexOf(append) === -1)
throw new Error(`Unsupported algorithm: ${append}`);
if (list === defaultList)
list = list.slice();
list.push(append);
} else if (isRegExp(append)) {
for (let k = 0; k < supportedList.length; ++k) {
const algo = supportedList[k];
if (append.test(algo)) {
if (list.indexOf(algo) !== -1)
continue;
if (list === defaultList)
list = list.slice();
list.push(algo);
}
}
}
}
}
break;
case 'prepend':
if (!Array.isArray(val))
val = [val];
if (Array.isArray(val)) {
for (let j = val.length; j >= 0; --j) {
const prepend = val[j];
if (typeof prepend === 'string') {
if (!prepend || list.indexOf(prepend) !== -1)
continue;
if (supportedList.indexOf(prepend) === -1)
throw new Error(`Unsupported algorithm: ${prepend}`);
if (list === defaultList)
list = list.slice();
list.unshift(prepend);
} else if (isRegExp(prepend)) {
for (let k = supportedList.length; k >= 0; --k) {
const algo = supportedList[k];
if (prepend.test(algo)) {
if (list.indexOf(algo) !== -1)
continue;
if (list === defaultList)
list = list.slice();
list.unshift(algo);
}
}
}
}
}
break;
case 'remove':
if (!Array.isArray(val))
val = [val];
if (Array.isArray(val)) {
for (let j = 0; j < val.length; ++j) {
const search = val[j];
if (typeof search === 'string') {
if (!search)
continue;
const idx = list.indexOf(search);
if (idx === -1)
continue;
if (list === defaultList)
list = list.slice();
list.splice(idx, 1);
} else if (isRegExp(search)) {
for (let k = 0; k < list.length; ++k) {
if (search.test(list[k])) {
if (list === defaultList)
list = list.slice();
list.splice(k, 1);
--k;
}
}
}
}
}
break;
}
}
return list;
}
return defaultList;
}
module.exports = {
ChannelManager,
generateAlgorithmList,
onChannelOpenFailure,
onCHANNEL_CLOSE,
isWritable: (stream) => {
// XXX: hack to workaround regression in node
// See: https://github.com/nodejs/node/issues/36029
return (stream
&& stream.writable
&& stream._readableState
&& stream._readableState.ended === false);
},
};