Fix WhatsApp Admin QR bind and show the scan prompt only after Bind.

Use a current WA protocol version, start the sidecar via local tsx with inherited proxy, and stop auto-opening the QR UI on unbound connecting.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
oliver 2026-08-17 23:54:48 +08:00
parent 5c6833612f
commit 9cf026f605
12 changed files with 593 additions and 101 deletions

View file

@ -0,0 +1,52 @@
# Resolve a proxy URL for the WhatsApp sidecar without hardcoding a port.
# Order: process env -> state\proxy.url -> Windows user Internet Settings.
function Get-OclawWhatsAppProxyUrl {
param(
[Parameter(Mandatory = $true)][string]$StateDir,
[switch]$Persist
)
$found = ""
foreach ($name in @("AIA_WHATSAPP_PROXY_URL", "HTTPS_PROXY", "HTTP_PROXY", "https_proxy", "http_proxy")) {
$val = [Environment]::GetEnvironmentVariable($name, "Process")
if ($val -and $val.Trim()) {
$found = $val.Trim()
break
}
}
if (-not $found) {
$proxyFile = Join-Path $StateDir "proxy.url"
if (Test-Path $proxyFile) {
$line = @(Get-Content -Path $proxyFile -ErrorAction SilentlyContinue) | Select-Object -First 1
if ($line -and $line.Trim()) { $found = $line.Trim() }
}
}
if (-not $found) {
try {
$reg = Get-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -ErrorAction Stop
$raw = [string]$reg.ProxyServer
# Clash TUN often sets ProxyEnable=0 but leaves the last mixed-port address.
if ($raw -and (($reg.ProxyEnable -eq 1) -or ($raw -match '(?i)127\.0\.0\.1|localhost|::1'))) {
$hostport = ""
if ($raw -match '(?i)https?=([^;]+)') {
$hostport = $Matches[1].Trim()
} elseif ($raw -match '^[^\s;]+$') {
$hostport = $raw.Trim()
}
if ($hostport) {
if ($hostport -notmatch '^[a-zA-Z][a-zA-Z0-9+.-]*://') {
$hostport = "http://$hostport"
}
$found = $hostport
}
}
} catch {}
}
if ($found -and $found.StartsWith('"') -and $found.EndsWith('"')) {
$found = $found.Substring(1, $found.Length - 2)
}
if ($Persist -and $found) {
New-Item -ItemType Directory -Force -Path $StateDir | Out-Null
Set-Content -Path (Join-Path $StateDir "proxy.url") -Value $found -Encoding ascii
}
return $found
}

View file

@ -23,16 +23,53 @@ if (-not (Test-Path (Join-Path $sidecarRoot "baileys_runner.ts"))) {
throw "whatsapp sidecar not installed. Run whatsapp_install.ps1 first."
}
$bridgeSrc = Join-Path $oclawRoot "runtime\\operations\\whatsapp_bridge"
if (Test-Path $bridgeSrc) {
foreach ($name in @("baileys_runner.ts", "auth.ts", "qr.ts", "status.ts")) {
$srcPath = Join-Path $bridgeSrc $name
if (Test-Path $srcPath) {
Copy-Item -Path $srcPath -Destination (Join-Path $sidecarRoot $name) -Force
}
}
}
$stopScript = Join-Path $PSScriptRoot "whatsapp_stop.ps1"
if (Test-Path $stopScript) {
Write-Host "[info] stopping existing sidecar so this console can print QR."
& $stopScript -ChannelId $ChannelId -Force
}
. (Join-Path $PSScriptRoot "lib\ResolveWhatsAppProxy.ps1")
$proxyUrl = Get-OclawWhatsAppProxyUrl -StateDir $stateDir -Persist
if ($proxyUrl) {
$env:AIA_WHATSAPP_PROXY_URL = $proxyUrl
$env:HTTPS_PROXY = $proxyUrl
$env:HTTP_PROXY = $proxyUrl
Write-Host "[info] whatsapp login proxy=$proxyUrl"
}
Set-Content -Path (Join-Path $sidecarRoot "pid.txt") -Value $PID
$nodeExe = Join-Path $systemNodeDir "node.exe"
if (-not (Test-Path $nodeExe)) { $nodeExe = "node.exe" }
$tsxCli = Join-Path $sidecarRoot "node_modules\tsx\dist\cli.mjs"
Push-Location $sidecarRoot
try {
$env:OCLAW_STATE_DIR = $stateDir
Write-Host "[info] whatsapp login started (foreground)."
Write-Host "[info] QR prints here if WhatsApp issues one. Background sidecar logs: data\\logs\\whatsapp_sidecar.log"
Write-Host "[info] If QR appears, scan it in WhatsApp -> Linked devices."
$runnerArgs = @("-y", "tsx", "baileys_runner.ts", "--login")
if ($VerboseRunner) {
$runnerArgs += "--verbose"
$runnerArgs = @()
if (Test-Path $tsxCli) {
$runnerArgs = @($tsxCli, "baileys_runner.ts", "--login")
if ($VerboseRunner) { $runnerArgs += "--verbose" }
& $nodeExe @runnerArgs
} else {
$runnerArgs = @("-y", "tsx", "baileys_runner.ts", "--login")
if ($VerboseRunner) { $runnerArgs += "--verbose" }
& npx.cmd @runnerArgs
}
& npx.cmd @runnerArgs
if ($LASTEXITCODE -ne 0) {
throw "whatsapp login runner exited with code $LASTEXITCODE"
}

View file

@ -18,6 +18,7 @@ $sidecarPidFile = Join-Path $sidecarRoot "pid.txt"
$env:PYTHONPATH = $oclawRoot
. (Join-Path $PSScriptRoot "lib\ResolveRuntimeLogDir.ps1")
. (Join-Path $PSScriptRoot "lib\ResolveWhatsAppProxy.ps1")
$runtimeLogDir = Get-OclawRuntimeLogDir -RepoRoot $oclawRoot
New-Item -ItemType Directory -Force -Path $sidecarRoot | Out-Null
@ -61,10 +62,23 @@ if (-not $resp -or $resp.StatusCode -ne 200) {
throw "oclaw gateway is not reachable at $healthUrl. Start it first: powershell -ExecutionPolicy Bypass -File .\\scripts\\start_gateway.ps1 -SkipInstall -Background"
}
$args = @(
"/c",
"cd /d $sidecarRoot && set OCLAW_STATE_DIR=$stateDir&& set AIA_GATEWAY_BASE_URL=$GatewayBaseUrl&& npx.cmd -y tsx baileys_runner.ts"
)
$p = Start-Process -FilePath "cmd.exe" -ArgumentList $args -WorkingDirectory $sidecarRoot -PassThru -WindowStyle Hidden -RedirectStandardOutput $logPath -RedirectStandardError $errPath
$proxyUrl = Get-OclawWhatsAppProxyUrl -StateDir $stateDir -Persist
if ($proxyUrl) {
$env:AIA_WHATSAPP_PROXY_URL = $proxyUrl
$env:HTTPS_PROXY = $proxyUrl
$env:HTTP_PROXY = $proxyUrl
Write-Host "[info] whatsapp sidecar proxy=$proxyUrl"
}
$nodeExe = Join-Path $systemNodeDir "node.exe"
if (-not (Test-Path $nodeExe)) { $nodeExe = "node.exe" }
$tsxCli = Join-Path $sidecarRoot "node_modules\tsx\dist\cli.mjs"
if (-not (Test-Path $tsxCli)) {
throw "tsx not installed under $sidecarRoot. Run whatsapp_install.ps1 first."
}
$env:OCLAW_STATE_DIR = $stateDir
$env:AIA_GATEWAY_BASE_URL = $GatewayBaseUrl
$p = Start-Process -FilePath $nodeExe -ArgumentList @($tsxCli, "baileys_runner.ts") -WorkingDirectory $sidecarRoot -PassThru -WindowStyle Hidden -RedirectStandardOutput $logPath -RedirectStandardError $errPath
Set-Content -Path $sidecarPidFile -Value $p.Id
Write-Host "[ok] started whatsapp sidecar pid=$($p.Id) mode=baileys out=$logPath err=$errPath"

View file

@ -46,4 +46,14 @@ if (Test-Path $sidecarPidFile) {
Remove-Item -Force $sidecarPidFile -ErrorAction SilentlyContinue
}
# cmd.exe wrapper can leave node/tsx children; sweep leftover runners for this channel.
$needle = "channel_sidecar\$ChannelId"
Get-CimInstance Win32_Process -ErrorAction SilentlyContinue | Where-Object {
$_.CommandLine -and
$_.CommandLine -like "*$needle*" -and
$_.CommandLine -like "*baileys_runner*"
} | ForEach-Object {
[void](Invoke-TaskKillQuiet -ProcessId ([string]$_.ProcessId) -ForceKill)
}
Write-Host "[ok] stopped whatsapp channel=$ChannelId"