-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhosts_handler.js
More file actions
72 lines (70 loc) · 2.04 KB
/
hosts_handler.js
File metadata and controls
72 lines (70 loc) · 2.04 KB
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
const pty = require('node-pty')
const SSH_CMD = process.platform === 'win32' ? 'ssh.exe' : 'ssh'
class HostsHandler {
constructor(ws) {
this.ws = ws
this.ws.onmessage = this.onMessage.bind(this)
}
onMessage(ev) {
let [cmd, data] = JSON.parse(ev.data)
if (cmd === 'connect') {
this.doPtyConnect(data)
} else if (cmd === 'typedone') {
this.typedOne(data)
} else if (cmd === 'typedall') {
this.typedAll(data)
} else if (cmd === 'resizeterm') {
this.resizeTerm(data)
}
}
doPtyConnect(hosts) {
let self = this
this.ptys = []
let i = 0
this.send('starting', hosts.length)
for (let hst of hosts) {
this.send('title', [i, 'Connecting '+hst+'...'])
let hostPty = pty.spawn(SSH_CMD, [hst], {
name: 'xterm-256color',
cols: 80,
rows: 24,
cwd: process.env.HOME,
env: process.env
})
hostPty.__id = i
hostPty.on('data', function (data) {
self.send('ptydata', [hostPty.__id, data])
})
hostPty.on('exit', function (code, signal) {
hostPty.__exited = true
self.send('ptyexit', [hostPty.__id, code, signal])
})
this.ptys.push(hostPty)
i += 1
}
}
typedOne(data) {
let [idx, str] = data
if (this.ptys[idx].__exited !== true) {
this.ptys[idx].write(str)
}
}
typedAll(data) {
let str = data
for (let hostPty of this.ptys) {
if (hostPty.__exited !== true) {
hostPty.write(str)
}
}
}
resizeTerm(data) {
let [idx, sz] = data
if (this.ptys[idx].__exited !== true) {
this.ptys[idx].resize(sz.cols, sz.rows)
}
}
send(cmd, data) {
this.ws.send(JSON.stringify([cmd, data]))
}
}
module.exports = HostsHandler