@theia/remote 1.45.0 → 1.46.0-next.72

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/README.md +61 -61
  2. package/lib/electron-browser/remote-electron-file-dialog-service.d.ts +13 -13
  3. package/lib/electron-browser/remote-electron-file-dialog-service.js +57 -57
  4. package/lib/electron-browser/remote-frontend-contribution.d.ts +25 -25
  5. package/lib/electron-browser/remote-frontend-contribution.js +164 -164
  6. package/lib/electron-browser/remote-frontend-module.d.ts +3 -3
  7. package/lib/electron-browser/remote-frontend-module.js +42 -42
  8. package/lib/electron-browser/remote-preferences.d.ts +10 -10
  9. package/lib/electron-browser/remote-preferences.js +48 -48
  10. package/lib/electron-browser/remote-registry-contribution.d.ts +18 -18
  11. package/lib/electron-browser/remote-registry-contribution.js +74 -74
  12. package/lib/electron-browser/remote-service.d.ts +5 -5
  13. package/lib/electron-browser/remote-service.js +37 -37
  14. package/lib/electron-browser/remote-ssh-contribution.d.ts +17 -17
  15. package/lib/electron-browser/remote-ssh-contribution.js +117 -117
  16. package/lib/electron-common/remote-ssh-connection-provider.d.ts +10 -10
  17. package/lib/electron-common/remote-ssh-connection-provider.js +20 -20
  18. package/lib/electron-common/remote-status-service.d.ts +14 -14
  19. package/lib/electron-common/remote-status-service.js +20 -20
  20. package/lib/electron-node/backend-remote-service-impl.d.ts +11 -11
  21. package/lib/electron-node/backend-remote-service-impl.js +50 -50
  22. package/lib/electron-node/remote-backend-module.d.ts +4 -4
  23. package/lib/electron-node/remote-backend-module.js +74 -74
  24. package/lib/electron-node/remote-connection-service.d.ts +14 -14
  25. package/lib/electron-node/remote-connection-service.js +65 -65
  26. package/lib/electron-node/remote-connection-socket-provider.d.ts +8 -8
  27. package/lib/electron-node/remote-connection-socket-provider.js +37 -37
  28. package/lib/electron-node/remote-proxy-server-provider.d.ts +5 -5
  29. package/lib/electron-node/remote-proxy-server-provider.js +43 -43
  30. package/lib/electron-node/remote-status-service.d.ts +6 -6
  31. package/lib/electron-node/remote-status-service.js +54 -54
  32. package/lib/electron-node/remote-types.d.ts +34 -34
  33. package/lib/electron-node/remote-types.js +17 -17
  34. package/lib/electron-node/setup/app-native-dependency-contribution.d.ts +7 -7
  35. package/lib/electron-node/setup/app-native-dependency-contribution.js +57 -57
  36. package/lib/electron-node/setup/main-copy-contribution.d.ts +4 -4
  37. package/lib/electron-node/setup/main-copy-contribution.js +37 -37
  38. package/lib/electron-node/setup/remote-copy-contribution.d.ts +28 -28
  39. package/lib/electron-node/setup/remote-copy-contribution.js +78 -78
  40. package/lib/electron-node/setup/remote-copy-service.d.ts +18 -18
  41. package/lib/electron-node/setup/remote-copy-service.js +126 -126
  42. package/lib/electron-node/setup/remote-native-dependency-contribution.d.ts +34 -34
  43. package/lib/electron-node/setup/remote-native-dependency-contribution.js +34 -34
  44. package/lib/electron-node/setup/remote-native-dependency-service.d.ts +23 -23
  45. package/lib/electron-node/setup/remote-native-dependency-service.js +118 -118
  46. package/lib/electron-node/setup/remote-node-setup-service.d.ts +22 -22
  47. package/lib/electron-node/setup/remote-node-setup-service.js +132 -132
  48. package/lib/electron-node/setup/remote-setup-script-service.d.ts +40 -40
  49. package/lib/electron-node/setup/remote-setup-script-service.js +132 -132
  50. package/lib/electron-node/setup/remote-setup-service.d.ts +28 -28
  51. package/lib/electron-node/setup/remote-setup-service.js +198 -198
  52. package/lib/electron-node/ssh/remote-ssh-connection-provider.d.ts +55 -55
  53. package/lib/electron-node/ssh/remote-ssh-connection-provider.d.ts.map +1 -1
  54. package/lib/electron-node/ssh/remote-ssh-connection-provider.js +344 -342
  55. package/lib/electron-node/ssh/remote-ssh-connection-provider.js.map +1 -1
  56. package/lib/electron-node/ssh/ssh-identity-file-collector.d.ts +12 -12
  57. package/lib/electron-node/ssh/ssh-identity-file-collector.js +131 -131
  58. package/lib/package.spec.js +25 -25
  59. package/package.json +6 -7
  60. package/src/electron-browser/remote-electron-file-dialog-service.ts +47 -47
  61. package/src/electron-browser/remote-frontend-contribution.ts +145 -145
  62. package/src/electron-browser/remote-frontend-module.ts +49 -49
  63. package/src/electron-browser/remote-preferences.ts +62 -62
  64. package/src/electron-browser/remote-registry-contribution.ts +70 -70
  65. package/src/electron-browser/remote-service.ts +31 -31
  66. package/src/electron-browser/remote-ssh-contribution.ts +102 -102
  67. package/src/electron-common/remote-ssh-connection-provider.ts +29 -29
  68. package/src/electron-common/remote-status-service.ts +35 -35
  69. package/src/electron-node/backend-remote-service-impl.ts +45 -45
  70. package/src/electron-node/remote-backend-module.ts +80 -80
  71. package/src/electron-node/remote-connection-service.ts +55 -55
  72. package/src/electron-node/remote-connection-socket-provider.ts +34 -34
  73. package/src/electron-node/remote-proxy-server-provider.ts +37 -37
  74. package/src/electron-node/remote-status-service.ts +41 -41
  75. package/src/electron-node/remote-types.ts +56 -56
  76. package/src/electron-node/setup/app-native-dependency-contribution.ts +48 -48
  77. package/src/electron-node/setup/main-copy-contribution.ts +28 -28
  78. package/src/electron-node/setup/remote-copy-contribution.ts +90 -90
  79. package/src/electron-node/setup/remote-copy-service.ts +114 -114
  80. package/src/electron-node/setup/remote-native-dependency-contribution.ts +63 -63
  81. package/src/electron-node/setup/remote-native-dependency-service.ts +111 -111
  82. package/src/electron-node/setup/remote-node-setup-service.ts +123 -123
  83. package/src/electron-node/setup/remote-setup-script-service.ts +146 -146
  84. package/src/electron-node/setup/remote-setup-service.ts +197 -197
  85. package/src/electron-node/ssh/remote-ssh-connection-provider.ts +358 -356
  86. package/src/electron-node/ssh/ssh-identity-file-collector.ts +137 -137
  87. package/src/package.spec.ts +29 -29
@@ -1,343 +1,345 @@
1
- "use strict";
2
- // *****************************************************************************
3
- // Copyright (C) 2023 TypeFox and others.
4
- //
5
- // This program and the accompanying materials are made available under the
6
- // terms of the Eclipse Public License v. 2.0 which is available at
7
- // http://www.eclipse.org/legal/epl-2.0.
8
- //
9
- // This Source Code may also be made available under the following Secondary
10
- // Licenses when the conditions for such availability set forth in the Eclipse
11
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
- // with the GNU Classpath Exception which is available at
13
- // https://www.gnu.org/software/classpath/license.html.
14
- //
15
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16
- // *****************************************************************************
17
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21
- return c > 3 && r && Object.defineProperty(target, key, r), r;
22
- };
23
- var __metadata = (this && this.__metadata) || function (k, v) {
24
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25
- };
26
- Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.RemoteSSHConnection = exports.RemoteSSHConnectionProviderImpl = void 0;
28
- const ssh2 = require("ssh2");
29
- const fs = require("@theia/core/shared/fs-extra");
30
- const SftpClient = require("ssh2-sftp-client");
31
- const core_1 = require("@theia/core");
32
- const inversify_1 = require("@theia/core/shared/inversify");
33
- const remote_connection_service_1 = require("../remote-connection-service");
34
- const remote_proxy_server_provider_1 = require("../remote-proxy-server-provider");
35
- const promise_util_1 = require("@theia/core/lib/common/promise-util");
36
- const ssh_identity_file_collector_1 = require("./ssh-identity-file-collector");
37
- const remote_setup_service_1 = require("../setup/remote-setup-service");
38
- const uuid_1 = require("uuid");
39
- let RemoteSSHConnectionProviderImpl = class RemoteSSHConnectionProviderImpl {
40
- constructor() {
41
- this.passwordRetryCount = 3;
42
- this.passphraseRetryCount = 3;
43
- }
44
- async establishConnection(options) {
45
- const progress = await this.messageService.showProgress({
46
- text: 'Remote SSH'
47
- });
48
- const report = message => progress.report({ message });
49
- report('Connecting to remote system...');
50
- try {
51
- const remote = await this.establishSSHConnection(options.host, options.user);
52
- await this.remoteSetup.setup({
53
- connection: remote,
54
- report,
55
- nodeDownloadTemplate: options.nodeDownloadTemplate
56
- });
57
- const registration = this.remoteConnectionService.register(remote);
58
- const server = await this.serverProvider.getProxyServer(socket => {
59
- remote.forwardOut(socket);
60
- });
61
- remote.onDidDisconnect(() => {
62
- server.close();
63
- registration.dispose();
64
- });
65
- const localPort = server.address().port;
66
- remote.localPort = localPort;
67
- return localPort.toString();
68
- }
69
- finally {
70
- progress.cancel();
71
- }
72
- }
73
- async establishSSHConnection(host, user) {
74
- const deferred = new promise_util_1.Deferred();
75
- const sshClient = new ssh2.Client();
76
- const identityFiles = await this.identityFileCollector.gatherIdentityFiles();
77
- const sshAuthHandler = this.getAuthHandler(user, host, identityFiles);
78
- sshClient
79
- .on('ready', async () => {
80
- const connection = new RemoteSSHConnection({
81
- client: sshClient,
82
- id: (0, uuid_1.v4)(),
83
- name: host,
84
- type: 'SSH'
85
- });
86
- try {
87
- await this.testConnection(connection);
88
- deferred.resolve(connection);
89
- }
90
- catch (err) {
91
- deferred.reject(err);
92
- }
93
- }).on('end', () => {
94
- console.log(`Ended remote connection to host '${user}@${host}'`);
95
- }).on('error', err => {
96
- deferred.reject(err);
97
- }).connect({
98
- host: host,
99
- username: user,
100
- authHandler: (methodsLeft, successes, callback) => (sshAuthHandler(methodsLeft, successes, callback), undefined)
101
- });
102
- return deferred.promise;
103
- }
104
- /**
105
- * Sometimes, ssh2.exec will not execute and retrieve any data right after the `ready` event fired.
106
- * In this method, we just perform `echo hello` in a loop to ensure that the connection is really ready.
107
- * See also https://github.com/mscdex/ssh2/issues/48
108
- */
109
- async testConnection(connection) {
110
- for (let i = 0; i < 100; i++) {
111
- const result = await connection.exec('echo hello');
112
- if (result.stdout.includes('hello')) {
113
- return;
114
- }
115
- await (0, promise_util_1.timeout)(50);
116
- }
117
- throw new Error('SSH connection failed testing. Could not execute "echo"');
118
- }
119
- getAuthHandler(user, host, identityKeys) {
120
- let passwordRetryCount = this.passwordRetryCount;
121
- let keyboardRetryCount = this.passphraseRetryCount;
122
- // `false` is a valid return value, indicating that the authentication has failed
123
- const END_AUTH = false;
124
- // `null` indicates that we just want to continue with the next auth type
125
- // eslint-disable-next-line no-null/no-null
126
- const NEXT_AUTH = null;
127
- return async (methodsLeft, _partialSuccess, callback) => {
128
- if (!methodsLeft) {
129
- return callback({
130
- type: 'none',
131
- username: user,
132
- });
133
- }
134
- if (methodsLeft && methodsLeft.includes('publickey') && identityKeys.length) {
135
- const identityKey = identityKeys.shift();
136
- if (identityKey.isPrivate) {
137
- return callback({
138
- type: 'publickey',
139
- username: user,
140
- key: identityKey.parsedKey
141
- });
142
- }
143
- if (!await fs.pathExists(identityKey.filename)) {
144
- // Try next identity file
145
- return callback(NEXT_AUTH);
146
- }
147
- const keyBuffer = await fs.promises.readFile(identityKey.filename);
148
- let result = ssh2.utils.parseKey(keyBuffer); // First try without passphrase
149
- if (result instanceof Error && result.message.match(/no passphrase given/)) {
150
- let passphraseRetryCount = this.passphraseRetryCount;
151
- while (result instanceof Error && passphraseRetryCount > 0) {
152
- const passphrase = await this.quickInputService.input({
153
- title: `Enter passphrase for ${identityKey.filename}`,
154
- password: true
155
- });
156
- if (!passphrase) {
157
- break;
158
- }
159
- result = ssh2.utils.parseKey(keyBuffer, passphrase);
160
- passphraseRetryCount--;
161
- }
162
- }
163
- if (!result || result instanceof Error) {
164
- // Try next identity file
165
- return callback(NEXT_AUTH);
166
- }
167
- const key = Array.isArray(result) ? result[0] : result;
168
- return callback({
169
- type: 'publickey',
170
- username: user,
171
- key
172
- });
173
- }
174
- if (methodsLeft && methodsLeft.includes('password') && passwordRetryCount > 0) {
175
- const password = await this.quickInputService.input({
176
- title: `Enter password for ${user}@${host}`,
177
- password: true
178
- });
179
- passwordRetryCount--;
180
- return callback(password
181
- ? {
182
- type: 'password',
183
- username: user,
184
- password
185
- }
186
- : END_AUTH);
187
- }
188
- if (methodsLeft && methodsLeft.includes('keyboard-interactive') && keyboardRetryCount > 0) {
189
- return callback({
190
- type: 'keyboard-interactive',
191
- username: user,
192
- prompt: async (_name, _instructions, _instructionsLang, prompts, finish) => {
193
- const responses = [];
194
- for (const prompt of prompts) {
195
- const response = await this.quickInputService.input({
196
- title: `(${user}@${host}) ${prompt.prompt}`,
197
- password: !prompt.echo
198
- });
199
- if (response === undefined) {
200
- keyboardRetryCount = 0;
201
- break;
202
- }
203
- responses.push(response);
204
- }
205
- keyboardRetryCount--;
206
- finish(responses);
207
- }
208
- });
209
- }
210
- callback(END_AUTH);
211
- };
212
- }
213
- };
214
- __decorate([
215
- (0, inversify_1.inject)(remote_connection_service_1.RemoteConnectionService),
216
- __metadata("design:type", remote_connection_service_1.RemoteConnectionService)
217
- ], RemoteSSHConnectionProviderImpl.prototype, "remoteConnectionService", void 0);
218
- __decorate([
219
- (0, inversify_1.inject)(remote_proxy_server_provider_1.RemoteProxyServerProvider),
220
- __metadata("design:type", remote_proxy_server_provider_1.RemoteProxyServerProvider)
221
- ], RemoteSSHConnectionProviderImpl.prototype, "serverProvider", void 0);
222
- __decorate([
223
- (0, inversify_1.inject)(ssh_identity_file_collector_1.SSHIdentityFileCollector),
224
- __metadata("design:type", ssh_identity_file_collector_1.SSHIdentityFileCollector)
225
- ], RemoteSSHConnectionProviderImpl.prototype, "identityFileCollector", void 0);
226
- __decorate([
227
- (0, inversify_1.inject)(remote_setup_service_1.RemoteSetupService),
228
- __metadata("design:type", remote_setup_service_1.RemoteSetupService)
229
- ], RemoteSSHConnectionProviderImpl.prototype, "remoteSetup", void 0);
230
- __decorate([
231
- (0, inversify_1.inject)(core_1.QuickInputService),
232
- __metadata("design:type", Object)
233
- ], RemoteSSHConnectionProviderImpl.prototype, "quickInputService", void 0);
234
- __decorate([
235
- (0, inversify_1.inject)(core_1.MessageService),
236
- __metadata("design:type", core_1.MessageService)
237
- ], RemoteSSHConnectionProviderImpl.prototype, "messageService", void 0);
238
- RemoteSSHConnectionProviderImpl = __decorate([
239
- (0, inversify_1.injectable)()
240
- ], RemoteSSHConnectionProviderImpl);
241
- exports.RemoteSSHConnectionProviderImpl = RemoteSSHConnectionProviderImpl;
242
- class RemoteSSHConnection {
243
- constructor(options) {
244
- this.localPort = 0;
245
- this.remotePort = 0;
246
- this.onDidDisconnectEmitter = new core_1.Emitter();
247
- this.id = options.id;
248
- this.type = options.type;
249
- this.name = options.name;
250
- this.client = options.client;
251
- this.onDidDisconnect(() => this.dispose());
252
- this.client.on('end', () => {
253
- this.onDidDisconnectEmitter.fire();
254
- });
255
- this.sftpClientPromise = this.setupSftpClient();
256
- }
257
- get onDidDisconnect() {
258
- return this.onDidDisconnectEmitter.event;
259
- }
260
- async setupSftpClient() {
261
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
262
- const sftpClient = new SftpClient();
263
- // A hack to set the internal ssh2 client of the sftp client
264
- // That way, we don't have to create a second connection
265
- sftpClient.client = this.client;
266
- // Calling this function establishes the sftp connection on the ssh client
267
- await sftpClient.getSftpChannel();
268
- return sftpClient;
269
- }
270
- forwardOut(socket) {
271
- this.client.forwardOut(socket.localAddress, socket.localPort, '127.0.0.1', this.remotePort, (err, stream) => {
272
- if (err) {
273
- console.debug('Proxy message rejected', err);
274
- }
275
- else {
276
- stream.pipe(socket).pipe(stream);
277
- }
278
- });
279
- }
280
- async copy(localPath, remotePath) {
281
- const sftpClient = await this.sftpClientPromise;
282
- await sftpClient.put(localPath, remotePath);
283
- }
284
- exec(cmd, args, options = {}) {
285
- const deferred = new promise_util_1.Deferred();
286
- cmd = this.buildCmd(cmd, args);
287
- this.client.exec(cmd, options, (err, stream) => {
288
- if (err) {
289
- return deferred.reject(err);
290
- }
291
- let stdout = '';
292
- let stderr = '';
293
- stream.on('close', () => {
294
- deferred.resolve({ stdout, stderr });
295
- }).on('data', (data) => {
296
- stdout += data.toString();
297
- }).stderr.on('data', (data) => {
298
- stderr += data.toString();
299
- });
300
- });
301
- return deferred.promise;
302
- }
303
- execPartial(cmd, tester, args, options = {}) {
304
- const deferred = new promise_util_1.Deferred();
305
- cmd = this.buildCmd(cmd, args);
306
- this.client.exec(cmd, {
307
- ...options,
308
- // Ensure that the process on the remote ends when the connection is closed
309
- pty: true
310
- }, (err, stream) => {
311
- if (err) {
312
- return deferred.reject(err);
313
- }
314
- // in pty mode we only have an stdout stream
315
- // return stdout as stderr as well
316
- let stdout = '';
317
- stream.on('close', () => {
318
- if (deferred.state === 'unresolved') {
319
- deferred.resolve({ stdout, stderr: stdout });
320
- }
321
- }).on('data', (data) => {
322
- if (deferred.state === 'unresolved') {
323
- stdout += data.toString();
324
- if (tester(stdout, stdout)) {
325
- deferred.resolve({ stdout, stderr: stdout });
326
- }
327
- }
328
- });
329
- });
330
- return deferred.promise;
331
- }
332
- buildCmd(cmd, args) {
333
- const escapedArgs = (args === null || args === void 0 ? void 0 : args.map(arg => `"${arg.replace(/"/g, '\\"')}"`)) || [];
334
- const fullCmd = cmd + (escapedArgs.length > 0 ? (' ' + escapedArgs.join(' ')) : '');
335
- return fullCmd;
336
- }
337
- dispose() {
338
- this.client.end();
339
- this.client.destroy();
340
- }
341
- }
342
- exports.RemoteSSHConnection = RemoteSSHConnection;
1
+ "use strict";
2
+ // *****************************************************************************
3
+ // Copyright (C) 2023 TypeFox and others.
4
+ //
5
+ // This program and the accompanying materials are made available under the
6
+ // terms of the Eclipse Public License v. 2.0 which is available at
7
+ // http://www.eclipse.org/legal/epl-2.0.
8
+ //
9
+ // This Source Code may also be made available under the following Secondary
10
+ // Licenses when the conditions for such availability set forth in the Eclipse
11
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
+ // with the GNU Classpath Exception which is available at
13
+ // https://www.gnu.org/software/classpath/license.html.
14
+ //
15
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16
+ // *****************************************************************************
17
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
22
+ };
23
+ var __metadata = (this && this.__metadata) || function (k, v) {
24
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.RemoteSSHConnection = exports.RemoteSSHConnectionProviderImpl = void 0;
28
+ const ssh2 = require("ssh2");
29
+ const fs = require("@theia/core/shared/fs-extra");
30
+ const SftpClient = require("ssh2-sftp-client");
31
+ const core_1 = require("@theia/core");
32
+ const inversify_1 = require("@theia/core/shared/inversify");
33
+ const remote_connection_service_1 = require("../remote-connection-service");
34
+ const remote_proxy_server_provider_1 = require("../remote-proxy-server-provider");
35
+ const promise_util_1 = require("@theia/core/lib/common/promise-util");
36
+ const ssh_identity_file_collector_1 = require("./ssh-identity-file-collector");
37
+ const remote_setup_service_1 = require("../setup/remote-setup-service");
38
+ const uuid_1 = require("@theia/core/lib/common/uuid");
39
+ let RemoteSSHConnectionProviderImpl = class RemoteSSHConnectionProviderImpl {
40
+ constructor() {
41
+ this.passwordRetryCount = 3;
42
+ this.passphraseRetryCount = 3;
43
+ }
44
+ async establishConnection(options) {
45
+ const progress = await this.messageService.showProgress({
46
+ text: 'Remote SSH'
47
+ });
48
+ const report = message => progress.report({ message });
49
+ report('Connecting to remote system...');
50
+ try {
51
+ const remote = await this.establishSSHConnection(options.host, options.user);
52
+ await this.remoteSetup.setup({
53
+ connection: remote,
54
+ report,
55
+ nodeDownloadTemplate: options.nodeDownloadTemplate
56
+ });
57
+ const registration = this.remoteConnectionService.register(remote);
58
+ const server = await this.serverProvider.getProxyServer(socket => {
59
+ remote.forwardOut(socket);
60
+ });
61
+ remote.onDidDisconnect(() => {
62
+ server.close();
63
+ registration.dispose();
64
+ });
65
+ const localPort = server.address().port;
66
+ remote.localPort = localPort;
67
+ return localPort.toString();
68
+ }
69
+ finally {
70
+ progress.cancel();
71
+ }
72
+ }
73
+ async establishSSHConnection(host, user) {
74
+ const deferred = new promise_util_1.Deferred();
75
+ const sshClient = new ssh2.Client();
76
+ const identityFiles = await this.identityFileCollector.gatherIdentityFiles();
77
+ const hostUrl = new URL(`ssh://${host}`);
78
+ const sshAuthHandler = this.getAuthHandler(user, hostUrl.hostname, identityFiles);
79
+ sshClient
80
+ .on('ready', async () => {
81
+ const connection = new RemoteSSHConnection({
82
+ client: sshClient,
83
+ id: (0, uuid_1.generateUuid)(),
84
+ name: hostUrl.hostname,
85
+ type: 'SSH'
86
+ });
87
+ try {
88
+ await this.testConnection(connection);
89
+ deferred.resolve(connection);
90
+ }
91
+ catch (err) {
92
+ deferred.reject(err);
93
+ }
94
+ }).on('end', () => {
95
+ console.log(`Ended remote connection to host '${user}@${hostUrl.hostname}'`);
96
+ }).on('error', err => {
97
+ deferred.reject(err);
98
+ }).connect({
99
+ host: hostUrl.hostname,
100
+ port: hostUrl.port ? parseInt(hostUrl.port, 10) : undefined,
101
+ username: user,
102
+ authHandler: (methodsLeft, successes, callback) => (sshAuthHandler(methodsLeft, successes, callback), undefined)
103
+ });
104
+ return deferred.promise;
105
+ }
106
+ /**
107
+ * Sometimes, ssh2.exec will not execute and retrieve any data right after the `ready` event fired.
108
+ * In this method, we just perform `echo hello` in a loop to ensure that the connection is really ready.
109
+ * See also https://github.com/mscdex/ssh2/issues/48
110
+ */
111
+ async testConnection(connection) {
112
+ for (let i = 0; i < 100; i++) {
113
+ const result = await connection.exec('echo hello');
114
+ if (result.stdout.includes('hello')) {
115
+ return;
116
+ }
117
+ await (0, promise_util_1.timeout)(50);
118
+ }
119
+ throw new Error('SSH connection failed testing. Could not execute "echo"');
120
+ }
121
+ getAuthHandler(user, host, identityKeys) {
122
+ let passwordRetryCount = this.passwordRetryCount;
123
+ let keyboardRetryCount = this.passphraseRetryCount;
124
+ // `false` is a valid return value, indicating that the authentication has failed
125
+ const END_AUTH = false;
126
+ // `null` indicates that we just want to continue with the next auth type
127
+ // eslint-disable-next-line no-null/no-null
128
+ const NEXT_AUTH = null;
129
+ return async (methodsLeft, _partialSuccess, callback) => {
130
+ if (!methodsLeft) {
131
+ return callback({
132
+ type: 'none',
133
+ username: user,
134
+ });
135
+ }
136
+ if (methodsLeft && methodsLeft.includes('publickey') && identityKeys.length) {
137
+ const identityKey = identityKeys.shift();
138
+ if (identityKey.isPrivate) {
139
+ return callback({
140
+ type: 'publickey',
141
+ username: user,
142
+ key: identityKey.parsedKey
143
+ });
144
+ }
145
+ if (!await fs.pathExists(identityKey.filename)) {
146
+ // Try next identity file
147
+ return callback(NEXT_AUTH);
148
+ }
149
+ const keyBuffer = await fs.promises.readFile(identityKey.filename);
150
+ let result = ssh2.utils.parseKey(keyBuffer); // First try without passphrase
151
+ if (result instanceof Error && result.message.match(/no passphrase given/)) {
152
+ let passphraseRetryCount = this.passphraseRetryCount;
153
+ while (result instanceof Error && passphraseRetryCount > 0) {
154
+ const passphrase = await this.quickInputService.input({
155
+ title: `Enter passphrase for ${identityKey.filename}`,
156
+ password: true
157
+ });
158
+ if (!passphrase) {
159
+ break;
160
+ }
161
+ result = ssh2.utils.parseKey(keyBuffer, passphrase);
162
+ passphraseRetryCount--;
163
+ }
164
+ }
165
+ if (!result || result instanceof Error) {
166
+ // Try next identity file
167
+ return callback(NEXT_AUTH);
168
+ }
169
+ const key = Array.isArray(result) ? result[0] : result;
170
+ return callback({
171
+ type: 'publickey',
172
+ username: user,
173
+ key
174
+ });
175
+ }
176
+ if (methodsLeft && methodsLeft.includes('password') && passwordRetryCount > 0) {
177
+ const password = await this.quickInputService.input({
178
+ title: `Enter password for ${user}@${host}`,
179
+ password: true
180
+ });
181
+ passwordRetryCount--;
182
+ return callback(password
183
+ ? {
184
+ type: 'password',
185
+ username: user,
186
+ password
187
+ }
188
+ : END_AUTH);
189
+ }
190
+ if (methodsLeft && methodsLeft.includes('keyboard-interactive') && keyboardRetryCount > 0) {
191
+ return callback({
192
+ type: 'keyboard-interactive',
193
+ username: user,
194
+ prompt: async (_name, _instructions, _instructionsLang, prompts, finish) => {
195
+ const responses = [];
196
+ for (const prompt of prompts) {
197
+ const response = await this.quickInputService.input({
198
+ title: `(${user}@${host}) ${prompt.prompt}`,
199
+ password: !prompt.echo
200
+ });
201
+ if (response === undefined) {
202
+ keyboardRetryCount = 0;
203
+ break;
204
+ }
205
+ responses.push(response);
206
+ }
207
+ keyboardRetryCount--;
208
+ finish(responses);
209
+ }
210
+ });
211
+ }
212
+ callback(END_AUTH);
213
+ };
214
+ }
215
+ };
216
+ __decorate([
217
+ (0, inversify_1.inject)(remote_connection_service_1.RemoteConnectionService),
218
+ __metadata("design:type", remote_connection_service_1.RemoteConnectionService)
219
+ ], RemoteSSHConnectionProviderImpl.prototype, "remoteConnectionService", void 0);
220
+ __decorate([
221
+ (0, inversify_1.inject)(remote_proxy_server_provider_1.RemoteProxyServerProvider),
222
+ __metadata("design:type", remote_proxy_server_provider_1.RemoteProxyServerProvider)
223
+ ], RemoteSSHConnectionProviderImpl.prototype, "serverProvider", void 0);
224
+ __decorate([
225
+ (0, inversify_1.inject)(ssh_identity_file_collector_1.SSHIdentityFileCollector),
226
+ __metadata("design:type", ssh_identity_file_collector_1.SSHIdentityFileCollector)
227
+ ], RemoteSSHConnectionProviderImpl.prototype, "identityFileCollector", void 0);
228
+ __decorate([
229
+ (0, inversify_1.inject)(remote_setup_service_1.RemoteSetupService),
230
+ __metadata("design:type", remote_setup_service_1.RemoteSetupService)
231
+ ], RemoteSSHConnectionProviderImpl.prototype, "remoteSetup", void 0);
232
+ __decorate([
233
+ (0, inversify_1.inject)(core_1.QuickInputService),
234
+ __metadata("design:type", Object)
235
+ ], RemoteSSHConnectionProviderImpl.prototype, "quickInputService", void 0);
236
+ __decorate([
237
+ (0, inversify_1.inject)(core_1.MessageService),
238
+ __metadata("design:type", core_1.MessageService)
239
+ ], RemoteSSHConnectionProviderImpl.prototype, "messageService", void 0);
240
+ RemoteSSHConnectionProviderImpl = __decorate([
241
+ (0, inversify_1.injectable)()
242
+ ], RemoteSSHConnectionProviderImpl);
243
+ exports.RemoteSSHConnectionProviderImpl = RemoteSSHConnectionProviderImpl;
244
+ class RemoteSSHConnection {
245
+ constructor(options) {
246
+ this.localPort = 0;
247
+ this.remotePort = 0;
248
+ this.onDidDisconnectEmitter = new core_1.Emitter();
249
+ this.id = options.id;
250
+ this.type = options.type;
251
+ this.name = options.name;
252
+ this.client = options.client;
253
+ this.onDidDisconnect(() => this.dispose());
254
+ this.client.on('end', () => {
255
+ this.onDidDisconnectEmitter.fire();
256
+ });
257
+ this.sftpClientPromise = this.setupSftpClient();
258
+ }
259
+ get onDidDisconnect() {
260
+ return this.onDidDisconnectEmitter.event;
261
+ }
262
+ async setupSftpClient() {
263
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
264
+ const sftpClient = new SftpClient();
265
+ // A hack to set the internal ssh2 client of the sftp client
266
+ // That way, we don't have to create a second connection
267
+ sftpClient.client = this.client;
268
+ // Calling this function establishes the sftp connection on the ssh client
269
+ await sftpClient.getSftpChannel();
270
+ return sftpClient;
271
+ }
272
+ forwardOut(socket) {
273
+ this.client.forwardOut(socket.localAddress, socket.localPort, '127.0.0.1', this.remotePort, (err, stream) => {
274
+ if (err) {
275
+ console.debug('Proxy message rejected', err);
276
+ }
277
+ else {
278
+ stream.pipe(socket).pipe(stream);
279
+ }
280
+ });
281
+ }
282
+ async copy(localPath, remotePath) {
283
+ const sftpClient = await this.sftpClientPromise;
284
+ await sftpClient.put(localPath, remotePath);
285
+ }
286
+ exec(cmd, args, options = {}) {
287
+ const deferred = new promise_util_1.Deferred();
288
+ cmd = this.buildCmd(cmd, args);
289
+ this.client.exec(cmd, options, (err, stream) => {
290
+ if (err) {
291
+ return deferred.reject(err);
292
+ }
293
+ let stdout = '';
294
+ let stderr = '';
295
+ stream.on('close', () => {
296
+ deferred.resolve({ stdout, stderr });
297
+ }).on('data', (data) => {
298
+ stdout += data.toString();
299
+ }).stderr.on('data', (data) => {
300
+ stderr += data.toString();
301
+ });
302
+ });
303
+ return deferred.promise;
304
+ }
305
+ execPartial(cmd, tester, args, options = {}) {
306
+ const deferred = new promise_util_1.Deferred();
307
+ cmd = this.buildCmd(cmd, args);
308
+ this.client.exec(cmd, {
309
+ ...options,
310
+ // Ensure that the process on the remote ends when the connection is closed
311
+ pty: true
312
+ }, (err, stream) => {
313
+ if (err) {
314
+ return deferred.reject(err);
315
+ }
316
+ // in pty mode we only have an stdout stream
317
+ // return stdout as stderr as well
318
+ let stdout = '';
319
+ stream.on('close', () => {
320
+ if (deferred.state === 'unresolved') {
321
+ deferred.resolve({ stdout, stderr: stdout });
322
+ }
323
+ }).on('data', (data) => {
324
+ if (deferred.state === 'unresolved') {
325
+ stdout += data.toString();
326
+ if (tester(stdout, stdout)) {
327
+ deferred.resolve({ stdout, stderr: stdout });
328
+ }
329
+ }
330
+ });
331
+ });
332
+ return deferred.promise;
333
+ }
334
+ buildCmd(cmd, args) {
335
+ const escapedArgs = (args === null || args === void 0 ? void 0 : args.map(arg => `"${arg.replace(/"/g, '\\"')}"`)) || [];
336
+ const fullCmd = cmd + (escapedArgs.length > 0 ? (' ' + escapedArgs.join(' ')) : '');
337
+ return fullCmd;
338
+ }
339
+ dispose() {
340
+ this.client.end();
341
+ this.client.destroy();
342
+ }
343
+ }
344
+ exports.RemoteSSHConnection = RemoteSSHConnection;
343
345
  //# sourceMappingURL=remote-ssh-connection-provider.js.map