Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/agentHost/node/serverUrls.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 * as os from 'os';
7
8
export interface IResolvedServerUrls {
9
readonly local: readonly string[];
10
readonly network: readonly string[];
11
}
12
13
const loopbackHosts = new Set(['localhost', '127.0.0.1', '::1', '0000:0000:0000:0000:0000:0000:0000:0001']);
14
const wildcardHosts = new Set(['0.0.0.0', '::', '0000:0000:0000:0000:0000:0000:0000:0000']);
15
16
export function resolveServerUrls(host: string | undefined, port: number, networkInterfaces: ReturnType<typeof os.networkInterfaces> = os.networkInterfaces()): IResolvedServerUrls {
17
if (host === undefined) {
18
return { local: [formatWebSocketUrl('localhost', port)], network: [] };
19
}
20
21
if (!wildcardHosts.has(host)) {
22
const url = formatWebSocketUrl(host, port);
23
return loopbackHosts.has(host)
24
? { local: [url], network: [] }
25
: { local: [], network: [url] };
26
}
27
28
const network = new Set<string>();
29
for (const netInterface of Object.values(networkInterfaces)) {
30
for (const detail of netInterface ?? []) {
31
if (detail.family !== 'IPv4' || detail.internal) {
32
continue;
33
}
34
35
network.add(formatWebSocketUrl(detail.address, port));
36
}
37
}
38
39
return {
40
local: [formatWebSocketUrl('localhost', port)],
41
network: [...network],
42
};
43
}
44
45
export function formatWebSocketUrl(host: string, port: number): string {
46
const normalizedHost = host.includes(':') ? `[${host}]` : host;
47
return `ws://${normalizedHost}:${port}`;
48
}
49
50