|
| 1 | +param( |
| 2 | + [int]$ChromePort = 9222, |
| 3 | + [int]$ProxyPort = 9223, |
| 4 | + [string]$ProfileDir = "$env:TEMP\chrome-cdp-hermes", |
| 5 | + [string]$ChromePath, |
| 6 | + [switch]$Cleanup, |
| 7 | + [switch]$RemoveProfile |
| 8 | +) |
| 9 | + |
| 10 | +$ErrorActionPreference = "Stop" |
| 11 | + |
| 12 | +function Test-IsAdministrator { |
| 13 | + $principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) |
| 14 | + return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) |
| 15 | +} |
| 16 | + |
| 17 | +function Assert-Administrator { |
| 18 | + if (-not (Test-IsAdministrator)) { |
| 19 | + Write-Error "Run PowerShell as Administrator, then rerun this script." |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function Resolve-ChromePath { |
| 24 | + param([string]$OverridePath) |
| 25 | + |
| 26 | + if ($OverridePath) { |
| 27 | + if (Test-Path -LiteralPath $OverridePath) { |
| 28 | + return (Resolve-Path -LiteralPath $OverridePath).Path |
| 29 | + } |
| 30 | + |
| 31 | + Write-Error "ChromePath does not exist: $OverridePath" |
| 32 | + } |
| 33 | + |
| 34 | + $candidates = @( |
| 35 | + "C:\Program Files\Google\Chrome\Application\chrome.exe", |
| 36 | + "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" |
| 37 | + ) |
| 38 | + |
| 39 | + foreach ($candidate in $candidates) { |
| 40 | + if (Test-Path -LiteralPath $candidate) { |
| 41 | + return $candidate |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + Write-Error "Chrome was not found. Pass -ChromePath with the full path to chrome.exe." |
| 46 | +} |
| 47 | + |
| 48 | +function Test-CdpEndpoint { |
| 49 | + param([string]$Url) |
| 50 | + |
| 51 | + try { |
| 52 | + $response = Invoke-WebRequest -Uri $Url -UseBasicParsing -TimeoutSec 2 |
| 53 | + $json = $response.Content | ConvertFrom-Json |
| 54 | + return [bool]($json.Browser -and $json.webSocketDebuggerUrl) |
| 55 | + } |
| 56 | + catch { |
| 57 | + return $false |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function Wait-CdpEndpoint { |
| 62 | + param( |
| 63 | + [string]$Url, |
| 64 | + [int]$Attempts = 20, |
| 65 | + [int]$DelayMilliseconds = 500 |
| 66 | + ) |
| 67 | + |
| 68 | + for ($i = 1; $i -le $Attempts; $i++) { |
| 69 | + if (Test-CdpEndpoint -Url $Url) { |
| 70 | + return $true |
| 71 | + } |
| 72 | + |
| 73 | + Start-Sleep -Milliseconds $DelayMilliseconds |
| 74 | + } |
| 75 | + |
| 76 | + return $false |
| 77 | +} |
| 78 | + |
| 79 | +function Remove-PortProxy { |
| 80 | + param([int]$ListenPort) |
| 81 | + |
| 82 | + & netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=$ListenPort | Out-Null |
| 83 | +} |
| 84 | + |
| 85 | +function Add-PortProxy { |
| 86 | + param( |
| 87 | + [int]$ListenPort, |
| 88 | + [int]$ConnectPort |
| 89 | + ) |
| 90 | + |
| 91 | + Remove-PortProxy -ListenPort $ListenPort |
| 92 | + & netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=$ListenPort connectaddress=127.0.0.1 connectport=$ConnectPort | Out-Null |
| 93 | +} |
| 94 | + |
| 95 | +function Ensure-FirewallRule { |
| 96 | + param( |
| 97 | + [string]$DisplayName, |
| 98 | + [int]$LocalPort |
| 99 | + ) |
| 100 | + |
| 101 | + $existing = Get-NetFirewallRule -DisplayName $DisplayName -ErrorAction SilentlyContinue |
| 102 | + if ($existing) { |
| 103 | + $existing | Set-NetFirewallRule -Enabled True -Direction Inbound -Action Allow |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + New-NetFirewallRule ` |
| 108 | + -DisplayName $DisplayName ` |
| 109 | + -Direction Inbound ` |
| 110 | + -Action Allow ` |
| 111 | + -Protocol TCP ` |
| 112 | + -LocalPort $LocalPort | Out-Null |
| 113 | +} |
| 114 | + |
| 115 | +function Remove-FirewallRule { |
| 116 | + param([string]$DisplayName) |
| 117 | + |
| 118 | + Get-NetFirewallRule -DisplayName $DisplayName -ErrorAction SilentlyContinue | Remove-NetFirewallRule |
| 119 | +} |
| 120 | + |
| 121 | +if ($ChromePort -eq $ProxyPort) { |
| 122 | + Write-Error "ChromePort and ProxyPort must be different. Chrome uses $ChromePort locally; the portproxy should expose a separate port such as 9223." |
| 123 | +} |
| 124 | + |
| 125 | +$firewallRuleName = "Hermes Chrome CDP $ProxyPort" |
| 126 | +$chromeUrl = "http://127.0.0.1:$ChromePort/json/version" |
| 127 | +$proxyUrl = "http://127.0.0.1:$ProxyPort/json/version" |
| 128 | +$containerHostName = "host.docker.internal" |
| 129 | +$containerUrl = "http://host.docker.internal:$ProxyPort" |
| 130 | + |
| 131 | +Assert-Administrator |
| 132 | + |
| 133 | +if ($Cleanup) { |
| 134 | + Write-Host "Removing portproxy for 0.0.0.0:$ProxyPort..." |
| 135 | + Remove-PortProxy -ListenPort $ProxyPort |
| 136 | + |
| 137 | + Write-Host "Removing firewall rule '$firewallRuleName'..." |
| 138 | + Remove-FirewallRule -DisplayName $firewallRuleName |
| 139 | + |
| 140 | + if ($RemoveProfile) { |
| 141 | + if (Test-Path -LiteralPath $ProfileDir) { |
| 142 | + Write-Host "Removing Chrome profile directory: $ProfileDir" |
| 143 | + Remove-Item -LiteralPath $ProfileDir -Recurse -Force |
| 144 | + } |
| 145 | + } |
| 146 | + else { |
| 147 | + Write-Host "Leaving Chrome profile directory in place: $ProfileDir" |
| 148 | + Write-Host "Pass -RemoveProfile with -Cleanup to delete it." |
| 149 | + } |
| 150 | + |
| 151 | + Write-Host "Cleanup complete." |
| 152 | + exit 0 |
| 153 | +} |
| 154 | + |
| 155 | +$resolvedChromePath = Resolve-ChromePath -OverridePath $ChromePath |
| 156 | +New-Item -ItemType Directory -Path $ProfileDir -Force | Out-Null |
| 157 | + |
| 158 | +Write-Host "Launching Chrome CDP on 127.0.0.1:$ChromePort..." |
| 159 | +$chromeArgs = @( |
| 160 | + "--remote-debugging-port=$ChromePort", |
| 161 | + "--remote-debugging-address=127.0.0.1", |
| 162 | + "--user-data-dir=""$ProfileDir""", |
| 163 | + "--no-first-run", |
| 164 | + "--no-default-browser-check" |
| 165 | +) |
| 166 | + |
| 167 | +Start-Process -FilePath $resolvedChromePath -ArgumentList $chromeArgs | Out-Null |
| 168 | + |
| 169 | +Write-Host "Waiting for Chrome CDP at $chromeUrl..." |
| 170 | +if (-not (Wait-CdpEndpoint -Url $chromeUrl)) { |
| 171 | + Write-Error "Chrome CDP did not become available at $chromeUrl. Close existing Chrome instances using port $ChromePort, then rerun this script." |
| 172 | +} |
| 173 | + |
| 174 | +Write-Host "Creating portproxy 0.0.0.0:$ProxyPort -> 127.0.0.1:$ChromePort..." |
| 175 | +Add-PortProxy -ListenPort $ProxyPort -ConnectPort $ChromePort |
| 176 | + |
| 177 | +Write-Host "Ensuring firewall rule '$firewallRuleName' allows inbound TCP $ProxyPort..." |
| 178 | +Ensure-FirewallRule -DisplayName $firewallRuleName -LocalPort $ProxyPort |
| 179 | + |
| 180 | +Write-Host "Verifying local proxy at $proxyUrl..." |
| 181 | +if (-not (Wait-CdpEndpoint -Url $proxyUrl -Attempts 10)) { |
| 182 | + Write-Error "The portproxy was created, but $proxyUrl did not return Chrome CDP JSON. Check 'netsh interface portproxy show v4tov4' and Windows Firewall." |
| 183 | +} |
| 184 | + |
| 185 | +Write-Host "" |
| 186 | +Write-Host "Chrome CDP is available to containers at:" |
| 187 | +Write-Host $containerUrl |
| 188 | +Write-Host "" |
| 189 | +Write-Host "Test from inside the devcontainer:" |
| 190 | +Write-Host "CDP_HOST=`$(getent ahostsv4 $containerHostName | awk 'NR==1 {print `$1}')" |
| 191 | +Write-Host "curl http://`$CDP_HOST:$ProxyPort/json/version" |
| 192 | +Write-Host "" |
| 193 | +Write-Host "Hermes/CodeForge endpoint:" |
| 194 | +Write-Host "http://`$CDP_HOST:$ProxyPort" |
| 195 | +Write-Host "" |
| 196 | +Write-Host "Why not use host.docker.internal directly?" |
| 197 | +Write-Host "Chrome rejects non-IP Host headers for CDP. Use the resolved IPv4 address from inside the devcontainer." |
0 commit comments