提交 d27ab54d 编写于 作者: M Matt Bierner

Extract port mapping helper function

上级 402281ba
......@@ -9,6 +9,20 @@ import * as modes from 'vs/editor/common/modes';
import { REMOTE_HOST_SCHEME } from 'vs/platform/remote/common/remoteHosts';
import { ITunnelService, RemoteTunnel } from 'vs/platform/remote/common/tunnel';
export function extractLocalHostUriMetaDataForPortMapping(uri: URI): { address: string, port: number } | undefined {
if (uri.scheme !== 'http' && uri.scheme !== 'https') {
return undefined;
}
const localhostMatch = /^(localhost):(\d+)$/.exec(uri.authority);
if (!localhostMatch) {
return undefined;
}
return {
address: localhostMatch[1],
port: +localhostMatch[2],
};
}
export class WebviewPortMappingManager extends Disposable {
private readonly _tunnels = new Map<number, Promise<RemoteTunnel>>();
......@@ -23,31 +37,25 @@ export class WebviewPortMappingManager extends Disposable {
public async getRedirect(url: string): Promise<string | undefined> {
const uri = URI.parse(url);
if (uri.scheme !== 'http' && uri.scheme !== 'https') {
return undefined;
}
const localhostMatch = /^localhost:(\d+)$/.exec(uri.authority);
if (!localhostMatch) {
return undefined;
const requestLocalHostInfo = extractLocalHostUriMetaDataForPortMapping(uri);
if (!requestLocalHostInfo) {
return requestLocalHostInfo;
}
const port = +localhostMatch[1];
for (const mapping of this.mappings()) {
if (mapping.webviewPort === port) {
if (mapping.webviewPort === requestLocalHostInfo.port) {
if (this.extensionLocation && this.extensionLocation.scheme === REMOTE_HOST_SCHEME) {
const tunnel = await this.getOrCreateTunnel(mapping.extensionHostPort);
if (tunnel) {
return url.replace(
new RegExp(`^${uri.scheme}://localhost:${mapping.webviewPort}(/|$)`),
`${uri.scheme}://localhost:${tunnel.tunnelLocalPort}$1`);
return uri.with({
authority: `127.0.0.1:${tunnel.tunnelLocalPort}`,
}).toString();
}
}
if (mapping.webviewPort !== mapping.extensionHostPort) {
return url.replace(
new RegExp(`^${uri.scheme}://localhost:${mapping.webviewPort}(/|$)`),
`${uri.scheme}://localhost:${mapping.extensionHostPort}$1`);
return uri.with({
authority: `${requestLocalHostInfo.address}:${mapping.extensionHostPort}`
}).toString();
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册