Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/agentHost/electron-browser/sshRelayTransport.ts
13394 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Emitter } from '../../../base/common/event.js';
7
import { Disposable } from '../../../base/common/lifecycle.js';
8
import type { AhpServerNotification, JsonRpcResponse, ProtocolMessage } from '../common/state/sessionProtocol.js';
9
import type { IProtocolTransport } from '../common/state/sessionTransport.js';
10
import type { ISSHRelayMessage, ISSHRemoteAgentHostMainService } from '../common/sshRemoteAgentHost.js';
11
12
/**
13
* A protocol transport that relays messages through the shared process
14
* SSH tunnel via IPC, instead of using a direct WebSocket connection.
15
*
16
* The shared process manages the actual WebSocket-over-SSH connection
17
* and forwards messages bidirectionally through this IPC channel.
18
*/
19
export class SSHRelayTransport extends Disposable implements IProtocolTransport {
20
21
private readonly _onMessage = this._register(new Emitter<ProtocolMessage>());
22
readonly onMessage = this._onMessage.event;
23
24
private readonly _onClose = this._register(new Emitter<void>());
25
readonly onClose = this._onClose.event;
26
27
constructor(
28
private readonly _connectionId: string,
29
private readonly _sshService: ISSHRemoteAgentHostMainService,
30
) {
31
super();
32
33
// Listen for relay messages from the shared process
34
this._register(this._sshService.onDidRelayMessage((msg: ISSHRelayMessage) => {
35
if (msg.connectionId === this._connectionId) {
36
try {
37
const parsed = JSON.parse(msg.data) as ProtocolMessage;
38
this._onMessage.fire(parsed);
39
} catch {
40
// Malformed message — drop
41
}
42
}
43
}));
44
45
// Listen for relay close
46
this._register(this._sshService.onDidRelayClose((closedId: string) => {
47
if (closedId === this._connectionId) {
48
this._onClose.fire();
49
}
50
}));
51
}
52
53
send(message: ProtocolMessage | AhpServerNotification | JsonRpcResponse): void {
54
this._sshService.relaySend(this._connectionId, JSON.stringify(message)).catch(() => {
55
// Send failed — connection probably closed
56
});
57
}
58
}
59
60