-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh.js
More file actions
188 lines (162 loc) · 5.61 KB
/
Copy pathssh.js
File metadata and controls
188 lines (162 loc) · 5.61 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
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
const ssh2 = require('ssh2'),
figlet = require('figlet'),
path = require('path'),
fs = require('fs'),
readline = require('readline'),
origWrite = process.stdout.write,
origErr = process.stderr.write;
class SshIntegration extends shim {
constructor(config, onMessage) {
super(config.commandPrefix);
this._onMessage = onMessage;
this._config = config;
this._threads = {};
}
sendMessage(message, thread_id) {
const thread = this._threads[thread_id];
if (!thread) {
throw new Error('Invalid thread');
}
for (let connection of thread) {
connection.write(message + '\n');
}
}
getUsers(thread_id) {
const thread = this._threads[thread_id];
if (!thread) {
throw new Error('Invalid thread');
}
const thread0 = thread[0];
const users = {};
users[thread0.username] = {
name: thread0.username,
id: thread0.username,
email: `${thread0.username}@unknown.org`
};
return users;
}
_onAuth(ctx) {
ctx.accept();
// Workaround to the username not avalible problem
ctx._stream.username = ctx.username;
}
_onReady() {
console.debug('Client authenticated.');
}
_onEnd(ctx) {
console.debug('Client disconnected');
}
_onSession(accept, reject) {
const session = accept();
session.once('exec', (accept, reject, info) => {
reject();
});
session.on('pty', (accept, reject, info) => {
accept();
});
session.on('shell', (accept, reject) => {
const stream = accept();
const username = stream._client._sshstream.username;
const id = `ssh_${username}`;
const rl = readline.createInterface({
input: stream.stdin,
output: stream.stdout,
prompt: exports.config.prompt || exports.platform.packageInfo.name.toProperCase() + '~$ ',
terminal: true
});
rl.prompt();
rl.on('line', line => {
if (line === 'exit') {
rl.close();
return;
}
const event = shim.createEvent(id, id, username, line);
this._onMessage(this, event);
rl.prompt();
});
rl.on('close', () => {
const ind = this._threads[id].indexOf(rl);
this._threads[id].splice(ind, 1);
if (this._threads[id].length === 0) {
delete this._threads[id];
}
stream.exit(0);
stream.end();
stream.close();
});
rl.username = username;
if (!this._threads[id]) {
this._threads[id] = [];
}
this._threads[id].push(rl);
});
}
start() {
let hk = this._config.hostKey;
if (!hk) {
const hd = path.resolve(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE);
hk = path.join(hd, '.ssh/id_rsa');
}
try {
hk = fs.readFileSync(hk);
}
catch(e) {
console.error('Host keys need to be generated and provided before running this integration.');
throw e;
}
const banner = figlet.textSync(exports.platform.packageInfo.name.toProperCase()) +
'\n ' + exports.platform.packageInfo.version + '\n------------------------------------';
this._sshServer = new ssh2.Server({
hostKeys: [hk],
banner: banner,
ident: `${exports.platform.packageInfo.name}/${exports.platform.packageInfo.version} (${exports.__descriptor.name}/${exports.__descriptor.version}, like ssh2js)`
}, client => {
client.on('authentication', this._onAuth.bind(this));
client.on('ready', this._onReady.bind(this));
client.on('session', this._onSession.bind(this));
client.on('end', this._onEnd.bind(this));
}).listen(this._config.listenPort || 44, this._config.listenAddress || '0.0.0.0', err => {
if (err) {
throw new Error(err);
}
});
const self = this;
process.stdout.write = function (data) {
origWrite.apply(this, arguments);
for (let thread in self._threads) {
for (let connection of self._threads[thread]) {
connection.write(data);
}
}
};
process.stderr.write = function (data) {
origWrite.apply(this, arguments);
for (let thread in self._threads) {
for (let connection of self._threads[thread]) {
connection.write(data);
}
}
};
}
stop() {
process.stdout.write = origWrite;
process.stderr.write = origErr;
for (let thread of Object.keys(this._threads)) {
for (let connection of this._threads[thread]) {
connection.close();
}
}
this._sshServer.close();
this._sshServer = null;
}
};
let serverInstance = null;
exports.getApi = () => serverInstance;
exports.start = callback => {
serverInstance = new SshIntegration(exports.config, callback);
serverInstance.start();
};
exports.stop = () => {
serverInstance.stop();
serverInstance = null;
};