Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/sessions/contrib/chat/browser/openInVSCodeUtils.ts
13401 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 { IRemoteAgentHostService, IRemoteAgentHostSSHConnection, RemoteAgentHostEntryType } from '../../../../platform/agentHost/common/remoteAgentHostService.js';
7
import { ISessionsProvidersService } from '../../../services/sessions/browser/sessionsProvidersService.js';
8
import { isAgentHostProvider } from '../../../common/agentHostSessionsProvider.js';
9
import { encodeHex, VSBuffer } from '../../../../base/common/buffer.js';
10
11
/**
12
* Resolves the VS Code remote authority for the given session provider,
13
* e.g. `ssh-remote+myhost` or `tunnel+myTunnel`.
14
*
15
* Returns `undefined` for local or WebSocket-only providers where no
16
* VS Code remote extension can handle the connection.
17
*/
18
export function resolveRemoteAuthority(
19
providerId: string,
20
sessionsProvidersService: ISessionsProvidersService,
21
remoteAgentHostService: IRemoteAgentHostService,
22
): string | undefined {
23
const provider = sessionsProvidersService.getProvider(providerId);
24
if (!provider || !isAgentHostProvider(provider) || !provider.remoteAddress) {
25
return undefined;
26
}
27
28
const entry = remoteAgentHostService.getEntryByAddress(provider.remoteAddress);
29
if (!entry) {
30
return undefined;
31
}
32
33
switch (entry.connection.type) {
34
case RemoteAgentHostEntryType.SSH:
35
if (entry.connection.sshConfigHost) {
36
return `ssh-remote+${entry.connection.sshConfigHost}`;
37
}
38
return `ssh-remote+${sshAuthorityString(entry.connection)}`;
39
case RemoteAgentHostEntryType.Tunnel:
40
return `tunnel+${entry.connection.label ?? `${entry.connection.tunnelId}.${entry.connection.clusterId}`}`;
41
default:
42
return undefined;
43
}
44
}
45
46
/**
47
* Encodes an SSH connection into the authority string format expected by
48
* the Remote SSH extension.
49
*/
50
export function sshAuthorityString(connection: IRemoteAgentHostSSHConnection): string {
51
const hostName = connection.hostName;
52
const needsEncoding = connection.user || connection.port
53
|| /[A-Z/\\+]/.test(hostName) || !/^[a-zA-Z0-9.:\-]+$/.test(hostName);
54
if (!needsEncoding) {
55
return hostName;
56
}
57
58
const obj: Record<string, string | number> = { hostName };
59
if (connection.user) {
60
obj.user = connection.user;
61
}
62
if (connection.port) {
63
obj.port = connection.port;
64
}
65
66
const json = JSON.stringify(obj);
67
return encodeHex(VSBuffer.fromString(json));
68
}
69
70