forked from coder/code-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh.ts
More file actions
293 lines (264 loc) · 7.01 KB
/
ssh.ts
File metadata and controls
293 lines (264 loc) · 7.01 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
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
/**
* Provides utilities for handling SSH connections
*/
import * as net from "net";
import * as cp from "child_process";
import * as fs from "fs";
import * as path from "path";
import * as sshTypes from "ssh2";
import * as nodePty from "node-pty";
import { FileEntry, SFTPStream } from "ssh2-streams";
import { localRequire } from 'vs/server/src/node/util';
import { sanitizeProcessEnvironment } from 'vs/base/common/processes';
import { IProcessEnvironment } from 'vs/base/common/platform';
const ssh = localRequire<typeof import("ssh2")>("ssh2/lib/client");
/**
* Fills out all of the functionality of SSH using node equivalents.
*/
export function fillSSHSession(accept: () => sshTypes.Session) {
let pty: nodePty.IPty | undefined;
let activeProcess: cp.ChildProcess;
let ptyInfo: sshTypes.PseudoTtyInfo | undefined;
const env: { [key: string]: string } = {};
const session = accept();
// Run a command, stream back the data
const cmd = (command: string, channel: sshTypes.ServerChannel): void => {
if (ptyInfo) {
// Remove undefined env vars and remove VSCode / code-server env vars
const env = Object.keys(process.env).reduce((prev, k) => {
if (process.env[k] !== undefined) {
prev[k] = process.env[k] as string;
}
return prev;
}, {} as IProcessEnvironment);
sanitizeProcessEnvironment(env as IProcessEnvironment);
pty = nodePty.spawn(command, [], {
cols: ptyInfo.cols,
rows: ptyInfo.rows,
env,
});
pty.onData((d) => channel.write(d));
pty.on("exit", (exitCode) => {
channel.exit(exitCode);
channel.close();
});
channel.on("data", (d: any) => pty!.write(d));
return;
}
const proc = cp.spawn(command);
proc.stdout.on("data", (d) => channel.stdout.write(d));
proc.stderr.on("data", (d) => channel.stderr.write(d));
proc.on("exit", (exitCode) => {
channel.exit(exitCode || 0);
channel.close();
});
channel.stdin.on("data", (d: any) => proc.stdin.write(d));
channel.stdin.on("close", () => proc.stdin.end());
};
session.on("pty", (accept, _, info) => {
ptyInfo = info;
accept();
});
session.on("shell", accept => {
cmd(process.env.SHELL || "/usr/bin/env bash", accept());
});
session.on("exec", (accept, _, info) => {
cmd(info.command, accept());
});
session.on("sftp", fillSFTPStream);
session.on("signal", (accept, _, info) => {
accept();
process.kill((pty || activeProcess).pid, info.name);
});
session.on("env", (_accept, _reject, info) => {
env[info.key] = info.value;
});
session.on("window-change", (accept, reject, info) => {
if (pty) {
pty.resize(info.cols, info.rows);
accept();
} else {
reject();
}
});
}
/**
* Fills out all the functionality of SFTP using fs.
*/
function fillSFTPStream(accept: () => SFTPStream) {
const sftp = accept();
let oid = 0;
const fds: { [key: number]: boolean } = {};
const ods: { [key: number]: {
path: string
read: boolean
} } = {};
const sftpStatus = (reqID: number, err?: NodeJS.ErrnoException) => {
let code = ssh.SFTP_STATUS_CODE.OK;
if (err) {
if (err.code === "EACCES") {
code = ssh.SFTP_STATUS_CODE.PERMISSION_DENIED;
}
if (err.code === "ENOENT") {
code = ssh.SFTP_STATUS_CODE.NO_SUCH_FILE;
}
code = ssh.SFTP_STATUS_CODE.FAILURE;
}
return sftp.status(reqID, code);
};
sftp.on("OPEN", (reqID, filename) => {
fs.open(filename, "w", (err, fd) => {
if (err) {
return sftpStatus(reqID, err);
}
fds[fd] = true;
const buf = Buffer.alloc(4);
buf.writeUInt32BE(fd, 0);
return sftp.handle(reqID, buf);
});
});
sftp.on("OPENDIR", (reqID, path) => {
const buf = Buffer.alloc(4);
const id = oid++;
buf.writeUInt32BE(id, 0);
ods[id] = {
path,
read: false,
};
sftp.handle(reqID, buf);
});
sftp.on("READDIR", (reqID, handle) => {
const od = handle.readUInt32BE(0);
if (!ods[od]) {
return sftp.status(reqID, ssh.SFTP_STATUS_CODE.NO_SUCH_FILE);
}
if (ods[od].read) {
sftp.status(reqID, ssh.SFTP_STATUS_CODE.EOF);
return;
}
return fs.readdir(ods[od].path, (err, files) => {
if (err) {
return sftpStatus(reqID, err);
}
return Promise.all(files.map((f) => {
return new Promise<FileEntry>((resolve, reject) => {
const fullPath = path.join(ods[od].path, f);
fs.stat(fullPath, (err, stats) => {
if (err) {
return reject(err);
}
resolve({
filename: f,
longname: fullPath,
attrs: {
atime: stats.atimeMs,
gid: stats.gid,
mode: stats.mode,
size: stats.size,
mtime: stats.mtimeMs,
uid: stats.uid,
},
});
});
});
}))
.then((files) => {
sftp.name(reqID, files);
ods[od].read = true;
})
.catch(() => {
sftp.status(reqID, ssh.SFTP_STATUS_CODE.FAILURE);
});
});
});
sftp.on("WRITE", (reqID, handle, offset, data) => {
const fd = handle.readUInt32BE(0);
if (!fds[fd]) {
return sftp.status(reqID, ssh.SFTP_STATUS_CODE.NO_SUCH_FILE);
}
return fs.write(fd, data, offset, err => sftpStatus(reqID, err));
});
sftp.on("CLOSE", (reqID, handle) => {
const fd = handle.readUInt32BE(0);
if (!fds[fd]) {
if (ods[fd]) {
delete ods[fd];
return sftp.status(reqID, ssh.SFTP_STATUS_CODE.OK);
}
return sftp.status(reqID, ssh.SFTP_STATUS_CODE.NO_SUCH_FILE);
}
return fs.close(fd, err => sftpStatus(reqID, err));
});
sftp.on("STAT", (reqID, path) => {
fs.stat(path, (err, stats) => {
if (err) {
return sftpStatus(reqID, err);
}
return sftp.attrs(reqID, {
atime: stats.atime.getTime(),
gid: stats.gid,
mode: stats.mode,
mtime: stats.mtime.getTime(),
size: stats.size,
uid: stats.uid,
});
});
});
sftp.on("MKDIR", (reqID, path) => {
fs.mkdir(path, err => sftpStatus(reqID, err));
});
sftp.on("LSTAT", (reqID, path) => {
fs.lstat(path, (err, stats) => {
if (err) {
return sftpStatus(reqID, err);
}
return sftp.attrs(reqID, {
atime: stats.atimeMs,
gid: stats.gid,
mode: stats.mode,
mtime: stats.mtimeMs,
size: stats.size,
uid: stats.uid
});
});
});
sftp.on("REMOVE", (reqID, path) => {
fs.unlink(path, err => sftpStatus(reqID, err));
});
sftp.on("RMDIR", (reqID, path) => {
fs.rmdir(path, err => sftpStatus(reqID, err));
});
sftp.on("REALPATH", (reqID, path) => {
fs.realpath(path, (err, resolved) => {
if (err) {
return sftpStatus(reqID, err);
}
sftp.name(reqID, [{
filename: resolved,
longname: resolved,
attrs: {} as any,
}]);
return;
});
});
}
/**
* Pipes a requested port over SSH
*/
export function forwardSSHPort(
accept: () => sshTypes.ServerChannel,
reject: () => boolean,
info: sshTypes.TcpipRequestInfo,
) {
const fwdSocket = net.createConnection(info.destPort, info.destIP);
fwdSocket.on('error', () => reject());
fwdSocket.on('connect', () => {
const channel = accept();
channel.pipe(fwdSocket);
channel.on('close', () => fwdSocket.end());
fwdSocket.pipe(channel);
fwdSocket.on('close', () => channel.close());
fwdSocket.on('error', () => channel.end());
fwdSocket.on('end', () => channel.end());
});
}