-
Notifications
You must be signed in to change notification settings - Fork 40.1k
Expand file tree
/
Copy pathsshRemoteAgentHostServiceImpl.ts
More file actions
227 lines (191 loc) · 8.12 KB
/
sshRemoteAgentHostServiceImpl.ts
File metadata and controls
227 lines (191 loc) · 8.12 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Emitter, Event } from '../../../base/common/event.js';
import { Disposable, toDisposable } from '../../../base/common/lifecycle.js';
import { ILogService } from '../../log/common/log.js';
import { IConfigurationService } from '../../configuration/common/configuration.js';
import { ISharedProcessService } from '../../ipc/electron-browser/services.js';
import { ProxyChannel } from '../../../base/parts/ipc/common/ipc.js';
import { IRemoteAgentHostService, RemoteAgentHostEntryType } from '../common/remoteAgentHostService.js';
import { IInstantiationService } from '../../instantiation/common/instantiation.js';
import { SSHRelayTransport } from './sshRelayTransport.js';
import { RemoteAgentHostProtocolClient } from './remoteAgentHostProtocolClient.js';
import {
ISSHRemoteAgentHostService,
SSH_REMOTE_AGENT_HOST_CHANNEL,
type ISSHAgentHostConfig,
type ISSHAgentHostConnection,
type ISSHRemoteAgentHostMainService,
type ISSHResolvedConfig,
type ISSHConnectProgress,
} from '../common/sshRemoteAgentHost.js';
/**
* Renderer-side implementation of {@link ISSHRemoteAgentHostService} that
* delegates the actual SSH work to the main process via IPC, then registers
* the resulting connection with the renderer-local {@link IRemoteAgentHostService}.
*/
export class SSHRemoteAgentHostService extends Disposable implements ISSHRemoteAgentHostService {
declare readonly _serviceBrand: undefined;
private readonly _mainService: ISSHRemoteAgentHostMainService;
private readonly _onDidChangeConnections = this._register(new Emitter<void>());
readonly onDidChangeConnections: Event<void> = this._onDidChangeConnections.event;
readonly onDidReportConnectProgress: Event<ISSHConnectProgress>;
private readonly _connections = new Map<string, SSHAgentHostConnectionHandle>();
constructor(
@ISharedProcessService sharedProcessService: ISharedProcessService,
@IRemoteAgentHostService private readonly _remoteAgentHostService: IRemoteAgentHostService,
@ILogService private readonly _logService: ILogService,
@IInstantiationService private readonly _instantiationService: IInstantiationService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
) {
super();
this._mainService = ProxyChannel.toService<ISSHRemoteAgentHostMainService>(
sharedProcessService.getChannel(SSH_REMOTE_AGENT_HOST_CHANNEL),
);
this.onDidReportConnectProgress = this._mainService.onDidReportConnectProgress;
// When shared process fires onDidCloseConnection, clean up the renderer-side handle.
// Do NOT remove the configured entry — it stays in settings so startup reconnect
// can re-establish the SSH tunnel on next launch.
this._register(this._mainService.onDidCloseConnection(connectionId => {
const handle = this._connections.get(connectionId);
if (handle) {
this._connections.delete(connectionId);
handle.fireClose();
handle.dispose();
this._onDidChangeConnections.fire();
}
}));
}
get connections(): readonly ISSHAgentHostConnection[] {
return [...this._connections.values()];
}
async connect(config: ISSHAgentHostConfig): Promise<ISSHAgentHostConnection> {
this._logService.info('[SSHRemoteAgentHost] Connecting to ' + config.host);
const augmentedConfig = this._augmentConfig(config);
const result = await this._mainService.connect(augmentedConfig);
this._logService.trace('[SSHRemoteAgentHost] SSH tunnel established, connectionId=' + result.connectionId);
const existing = this._connections.get(result.connectionId);
if (existing) {
this._logService.trace('[SSHRemoteAgentHost] Returning existing connection handle');
return existing;
}
// Create relay transport + protocol client, then register with RemoteAgentHostService
try {
const protocolClient = this._createRelayClient(result);
await protocolClient.connect();
this._logService.trace('[SSHRemoteAgentHost] Protocol handshake completed');
await this._remoteAgentHostService.addSSHConnection({
name: result.name,
connectionToken: result.connectionToken,
connection: {
type: RemoteAgentHostEntryType.SSH,
address: result.address,
sshConfigHost: result.sshConfigHost,
hostName: result.config.host,
user: result.config.username || undefined,
port: result.config.port,
},
}, protocolClient);
} catch (err) {
this._logService.error('[SSHRemoteAgentHost] Connection setup failed', err);
this._mainService.disconnect(result.connectionId).catch(() => { /* best effort */ });
throw err;
}
const handle = new SSHAgentHostConnectionHandle(
result.config,
result.address,
result.name,
() => this._mainService.disconnect(result.connectionId),
);
this._connections.set(result.connectionId, handle);
this._onDidChangeConnections.fire();
return handle;
}
async disconnect(host: string): Promise<void> {
await this._mainService.disconnect(host);
}
async listSSHConfigHosts(): Promise<string[]> {
return this._mainService.listSSHConfigHosts();
}
async resolveSSHConfig(host: string): Promise<ISSHResolvedConfig> {
return this._mainService.resolveSSHConfig(host);
}
async reconnect(sshConfigHost: string, name: string): Promise<ISSHAgentHostConnection> {
const commandOverride = this._getRemoteAgentHostCommand();
const result = await this._mainService.reconnect(sshConfigHost, name, commandOverride);
const existing = this._connections.get(result.connectionId);
if (existing) {
return existing;
}
const protocolClient = this._createRelayClient(result);
await protocolClient.connect();
await this._remoteAgentHostService.addSSHConnection({
name: result.name,
connectionToken: result.connectionToken,
connection: {
type: RemoteAgentHostEntryType.SSH,
address: result.address,
sshConfigHost: result.sshConfigHost,
hostName: result.config.host,
user: result.config.username || undefined,
port: result.config.port,
},
}, protocolClient);
const handle = new SSHAgentHostConnectionHandle(
result.config,
result.address,
result.name,
() => this._mainService.disconnect(result.connectionId),
);
this._connections.set(result.connectionId, handle);
this._onDidChangeConnections.fire();
return handle;
}
private _createRelayClient(result: { connectionId: string; address: string }): RemoteAgentHostProtocolClient {
const transport = new SSHRelayTransport(result.connectionId, this._mainService);
return this._instantiationService.createInstance(
RemoteAgentHostProtocolClient, result.address, transport,
);
}
private _augmentConfig(config: ISSHAgentHostConfig): ISSHAgentHostConfig {
const commandOverride = this._getRemoteAgentHostCommand();
if (commandOverride) {
return { ...config, remoteAgentHostCommand: commandOverride };
}
return config;
}
private _getRemoteAgentHostCommand(): string | undefined {
return this._configurationService.getValue<string>('chat.sshRemoteAgentHostCommand') || undefined;
}
}
/**
* Lightweight renderer-side handle that represents a connection
* managed by the main process.
*/
class SSHAgentHostConnectionHandle extends Disposable implements ISSHAgentHostConnection {
private readonly _onDidClose = this._register(new Emitter<void>());
readonly onDidClose = this._onDidClose.event;
private _closedByMain = false;
constructor(
readonly config: ISSHAgentHostConnection['config'],
readonly localAddress: string,
readonly name: string,
disconnectFn: () => Promise<void>,
) {
super();
// When this handle is disposed, tear down the main-process tunnel
// (skip if already closed from the main process side)
this._register(toDisposable(() => {
if (!this._closedByMain) {
disconnectFn().catch(() => { /* best effort */ });
}
}));
}
/** Called by the service when the main process signals connection closure. */
fireClose(): void {
this._closedByMain = true;
this._onDidClose.fire();
}
}