import { Disposable } from '../utils/dispose' ; import * as vscode from 'vscode' ; import { DEFAULT_HOST } from '../utils/constants' ; import { isIPv4 } from 'net' ; description export interface ConnectionInfo { httpURI : vscode . Uri ; wsURI : vscode . Uri ; } description export class ConnectionManager extends Disposable { public httpServerBase : string | undefined ; public wsServerBase : string | undefined ; private _wsPort : number ; private _wsPath : string ; private _httpPort : number ; private _initHttpPort ; private _initWSPort ; private _initHost : string ; public host : string ; public get wsPort ( ) { return this . _wsPort ; } public get httpPort ( ) { return this . _httpPort ; } constructor ( httpPort , wsPort , host ) { super ( ) ; this . _initHttpPort = httpPort ; this . _initWSPort = wsPort ; if ( ! this . _validHost ( host ) ) { vscode . window . showErrorMessage ( `The local IP address "${ host }" is not formatted correctly. Using default ${ DEFAULT_HOST }.` ) ; this . _initHost = DEFAULT_HOST ; } else if ( vscode . env . remoteName && vscode . env . remoteName != '' && host != DEFAULT_HOST ) { vscode . window . showErrorMessage ( `Cannot use the host "${ host }" when using a remote connection. Using default ${ DEFAULT_HOST }.` ) ; this . _initHost = DEFAULT_HOST ; } else { this . _initHost = host ; } this . _httpPort = this . _initHttpPort ; this . _wsPort = this . _initWSPort ; this . _wsPath = '' ; this . host = this . _initHost ; } param httpPort param wsPort param wsPath public connected ( httpPort , wsPort , wsPath ) { this . _httpPort = httpPort ; this . _wsPort = wsPort ; this . _wsPath = wsPath ; const httpPortUri = this . constructLocalUri ( this . _httpPort ) ; const wsPortUri = this . constructLocalUri ( this . _wsPort , this . _wsPath ) ; vscode . env . asExternalUri ( httpPortUri ) . then ( ( externalHTTPUri ) => { vscode . env . asExternalUri ( wsPortUri ) . then ( ( externalWSUri ) => { this . _onConnected . fire ( { httpURI : externalHTTPUri , wsURI : externalWSUri , } ) ; } ) ; } ) ; } description public disconnected ( ) { this . _httpPort = this . _initHttpPort ; this . _wsPort = this . _initWSPort ; this . _wsPath = '' ; this . host = this . _initHost ; } description public set pendingPort ( port ) { this . _initHttpPort = port ; this . _initWSPort = port + 1 ; } public set pendingHost ( host ) { if ( this . _validHost ( host ) ) { this . _initHost = host ; } else { vscode . window . showErrorMessage ( `The local IP address "${ host }" is not formatted correctly. Will use default host ${ DEFAULT_HOST }.` ) ; this . _initHost = DEFAULT_HOST ; } } private readonly _onConnected = this . _register ( new vscode . EventEmitter < ConnectionInfo > ( ) ) ; public readonly onConnected = this . _onConnected . event ; returns { Promise < vscode . Uri > } public async resolveExternalHTTPUri ( ) { const httpPortUri = this . constructLocalUri ( this . _httpPort ) ; return vscode . env . asExternalUri ( httpPortUri ) ; } returns { Promise < vscode . Uri > } public async resolveExternalWSUri ( ) { const wsPortUri = this . constructLocalUri ( this . _wsPort , this . _wsPath ) ; return vscode . env . asExternalUri ( wsPortUri ) ; } public resetHostToDefault ( ) { if ( this . host != DEFAULT_HOST ) { vscode . window . showErrorMessage ( `The IP address "${ this . host }" cannot be used to host the server. Using default IP ${ DEFAULT_HOST }.` ) ; this . _initHost = DEFAULT_HOST ; this . host = this . _initHost ; } } private _validHost ( host ) { return isIPv4 ( host ) ; } param { number } port returns private constructLocalUri ( port , path ? ) { return vscode . Uri . parse ( `http://${ this . host }:${ port }${ path ?? '' }` ) ; } }