Path: blob/main/src/vs/platform/agentHost/node/serverUrls.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 * as os from 'os';67export interface IResolvedServerUrls {8readonly local: readonly string[];9readonly network: readonly string[];10}1112const loopbackHosts = new Set(['localhost', '127.0.0.1', '::1', '0000:0000:0000:0000:0000:0000:0000:0001']);13const wildcardHosts = new Set(['0.0.0.0', '::', '0000:0000:0000:0000:0000:0000:0000:0000']);1415export function resolveServerUrls(host: string | undefined, port: number, networkInterfaces: ReturnType<typeof os.networkInterfaces> = os.networkInterfaces()): IResolvedServerUrls {16if (host === undefined) {17return { local: [formatWebSocketUrl('localhost', port)], network: [] };18}1920if (!wildcardHosts.has(host)) {21const url = formatWebSocketUrl(host, port);22return loopbackHosts.has(host)23? { local: [url], network: [] }24: { local: [], network: [url] };25}2627const network = new Set<string>();28for (const netInterface of Object.values(networkInterfaces)) {29for (const detail of netInterface ?? []) {30if (detail.family !== 'IPv4' || detail.internal) {31continue;32}3334network.add(formatWebSocketUrl(detail.address, port));35}36}3738return {39local: [formatWebSocketUrl('localhost', port)],40network: [...network],41};42}4344export function formatWebSocketUrl(host: string, port: number): string {45const normalizedHost = host.includes(':') ? `[${host}]` : host;46return `ws://${normalizedHost}:${port}`;47}484950