Path: blob/main/src/vs/platform/agentHost/electron-browser/sshRelayTransport.ts
13394 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Emitter } from '../../../base/common/event.js';6import { Disposable } from '../../../base/common/lifecycle.js';7import type { AhpServerNotification, JsonRpcResponse, ProtocolMessage } from '../common/state/sessionProtocol.js';8import type { IProtocolTransport } from '../common/state/sessionTransport.js';9import type { ISSHRelayMessage, ISSHRemoteAgentHostMainService } from '../common/sshRemoteAgentHost.js';1011/**12* A protocol transport that relays messages through the shared process13* SSH tunnel via IPC, instead of using a direct WebSocket connection.14*15* The shared process manages the actual WebSocket-over-SSH connection16* and forwards messages bidirectionally through this IPC channel.17*/18export class SSHRelayTransport extends Disposable implements IProtocolTransport {1920private readonly _onMessage = this._register(new Emitter<ProtocolMessage>());21readonly onMessage = this._onMessage.event;2223private readonly _onClose = this._register(new Emitter<void>());24readonly onClose = this._onClose.event;2526constructor(27private readonly _connectionId: string,28private readonly _sshService: ISSHRemoteAgentHostMainService,29) {30super();3132// Listen for relay messages from the shared process33this._register(this._sshService.onDidRelayMessage((msg: ISSHRelayMessage) => {34if (msg.connectionId === this._connectionId) {35try {36const parsed = JSON.parse(msg.data) as ProtocolMessage;37this._onMessage.fire(parsed);38} catch {39// Malformed message — drop40}41}42}));4344// Listen for relay close45this._register(this._sshService.onDidRelayClose((closedId: string) => {46if (closedId === this._connectionId) {47this._onClose.fire();48}49}));50}5152send(message: ProtocolMessage | AhpServerNotification | JsonRpcResponse): void {53this._sshService.relaySend(this._connectionId, JSON.stringify(message)).catch(() => {54// Send failed — connection probably closed55});56}57}585960