import * as childProcess from "child_process" ; import inquirer = require ( "inquirer" ) ; import { OfficeAddinManifest } from "office-addin-manifest" ; import { URL } from "whatwg-url" ; import { ExpectedError } from "office-addin-usage-data" ; export const EdgeBrowserAppcontainerName = "Microsoft.MicrosoftEdge_8wekyb3d8bbwe" ; export const EdgeWebViewAppcontainerName = "Microsoft.win32webviewhost_cw5n1h2txyewy" ; export const EdgeBrowserName = "Microsoft Edge Web Browser" ; export const EdgeWebViewName = "Microsoft Edge WebView" ; param name throws export function addLoopbackExemptionForAppcontainer ( name ) { if ( ! isAppcontainerSupported ( ) ) { throw new ExpectedError ( `Platform not supported: ${ process . platform }.` ) ; } return new Promise ( ( resolve , reject ) => { const command = `CheckNetIsolation.exe LoopbackExempt -a -n=${ name }` ; childProcess . exec ( command , ( error , stdout ) => { if ( error ) { reject ( stdout ) ; } else { resolve ( ) ; } } ) ; } ) ; } returns export function isAppcontainerSupported ( ) { return process . platform === "win32" ; } param name throws export function isLoopbackExemptionForAppcontainer ( name ) { if ( ! isAppcontainerSupported ( ) ) { throw new ExpectedError ( `Platform not supported: ${ process . platform }.` ) ; } return new Promise ( ( resolve , reject ) => { const command = `CheckNetIsolation.exe LoopbackExempt -s` ; childProcess . exec ( command , ( error , stdout ) => { if ( error ) { reject ( stdout ) ; } else { const expr = new RegExp ( `Name: ${ name }` , "i" ) ; const found = expr . test ( stdout ) ; resolve ( found ) ; } } ) ; } ) ; } param sourceLocation param isFromStore export function getAppcontainerName ( sourceLocation , isFromStore = false ) { const url = new URL ( sourceLocation ) ; const origin = url . origin ; const addinType = isFromStore ? 0 : 1 ; const guid = "04ACA5EC-D79A-43EA-AB47-E50E47DD96FC" ; const name = `${ addinType }_${ origin }${ guid }` . replace ( /[://]/g , "_" ) ; return name ; } export async function getUserConfirmation ( name ) { const question = { message : `Allow localhost loopback for ${ name }?` , name : "didUserConfirm" , type : "confirm" , } ; const answers = await inquirer . prompt ( [ question ] ) ; return ( answers as any ) . didUserConfirm ; } export async function getAppcontainerNameFromManifestPath ( manifestPath ) { switch ( manifestPath . toLowerCase ( ) ) { case "edgewebview" : return EdgeWebViewAppcontainerName ; case "edgewebbrowser" : case "edge" : return EdgeBrowserAppcontainerName ; default : return await getAppcontainerNameFromManifest ( manifestPath ) ; } } export function getDisplayNameFromManifestPath ( manifestPath ) { switch ( manifestPath . toLowerCase ( ) ) { case "edgewebview" : return EdgeWebViewName ; case "edgewebbrowser" : case "edge" : return EdgeBrowserName ; default : return manifestPath ; } } export async function ensureLoopbackIsEnabled ( manifestPath , askForConfirmation = true ) { const name = await getAppcontainerNameFromManifestPath ( manifestPath ) ; let isEnabled = await isLoopbackExemptionForAppcontainer ( name ) ; if ( ! isEnabled ) { if ( ! askForConfirmation || ( await getUserConfirmation ( getDisplayNameFromManifestPath ( manifestPath ) ) ) ) { await addLoopbackExemptionForAppcontainer ( name ) ; isEnabled = true ; } } return isEnabled ; } param manifestPath export async function getAppcontainerNameFromManifest ( manifestPath ) { const manifest = await OfficeAddinManifest . readManifestFile ( manifestPath ) ; const sourceLocation = manifest . defaultSettings ? manifest . defaultSettings . sourceLocation : undefined ; if ( sourceLocation === undefined ) { throw new ExpectedError ( `The source location could not be retrieved from the manifest.` ) ; } return getAppcontainerName ( sourceLocation , false ) ; } param name throws export function removeLoopbackExemptionForAppcontainer ( name ) { if ( ! isAppcontainerSupported ( ) ) { throw new ExpectedError ( `Platform not supported: ${ process . platform }.` ) ; } return new Promise ( ( resolve , reject ) => { const command = `CheckNetIsolation.exe LoopbackExempt -d -n=${ name }` ; childProcess . exec ( command , ( error , stdout ) => { if ( error ) { reject ( stdout ) ; } else { resolve ( ) ; } } ) ; } ) ; }