Reverse Engineering / Authorized Penetration Testing / Security Research Skill Router Pack AI-powered routing + On-demand toolchain bootstrapping + Self-evolving knowledge base Supports Claude Code, Kiro, Cursor, Cline, and other AI coding clients 逆向/渗透/安全技能路由包 - AI 自动路由 + 按需自举工具链 + 自动进化经验库 | 支持 Claude Code / Kiro / Cursor / Cline 等代码 AI 客户端
Shell
Merge pull request #122 from HarryPD168/fix/ida-mcp-http-deadlock
zhaoxuya520b160d1073236
diff --cc skills/ida-reverse/scripts/IdaOpenHelpers.ps1index 4ff2690,a6ffa51..b1b96cf--- a/skills/ida-reverse/scripts/IdaOpenHelpers.ps1+++ b/skills/ida-reverse/scripts/IdaOpenHelpers.ps1@@@ -26,3 -26,75 +26,90 @@@ function Get-IdaOpenLockPlanPreferTempCopy = $hasLocked}}++ function Get-IdaMcpKeepaliveDir {+ $dir = Join-Path $env:LOCALAPPDATA 'reverse-skill\ida-mcp'+ if (-not (Test-Path -LiteralPath $dir)) {+ New-Item -ItemType Directory -Path $dir -Force | Out-Null+ }+ return $dir+ }++ function Get-IdaMcpLastHealthyPath {- return Join-Path (Get-IdaMcpKeepaliveDir) 'last-healthy.txt'++ param([int]$Port = 13337)++ return Join-Path (Get-IdaMcpKeepaliveDir) ("last-healthy-{0}.txt" -f $Port)+ }++ function Get-IdaMcpOpeningLockPath {- return Join-Path (Get-IdaMcpKeepaliveDir) 'opening.lock'++ param([int]$Port = 13337)++ return Join-Path (Get-IdaMcpKeepaliveDir) ("opening-{0}.lock" -f $Port)+ }++ function Write-IdaMcpLastHealthy {- Set-Content -LiteralPath (Get-IdaMcpLastHealthyPath) -Value 'ok' -Encoding ASCII++ param([int]$Port = 13337)++ Set-Content -LiteralPath (Get-IdaMcpLastHealthyPath -Port $Port) -Value 'ok' -Encoding ASCII+ }++ function Read-IdaMcpLastHealthy {- $path = Get-IdaMcpLastHealthyPath++ param([int]$Port = 13337)++ $path = Get-IdaMcpLastHealthyPath -Port $Port+ if (-not (Test-Path -LiteralPath $path)) { return $null }+ return (Get-Item -LiteralPath $path).LastWriteTime+ }++ function Set-IdaMcpOpeningLock {- param([int]$TtlSeconds = 600)++ param(++ [int]$Port = 13337,++ [int]$TtlSeconds = 600++ )+ $ttl = [Math]::Max(1, $TtlSeconds)+ $until = (Get-Date).AddSeconds($ttl).ToString('o')- Set-Content -LiteralPath (Get-IdaMcpOpeningLockPath) -Value $until -Encoding ASCII++ Set-Content -LiteralPath (Get-IdaMcpOpeningLockPath -Port $Port) -Value $until -Encoding ASCII+ }++ function Clear-IdaMcpOpeningLock {- $path = Get-IdaMcpOpeningLockPath++ param([int]$Port = 13337)++ $path = Get-IdaMcpOpeningLockPath -Port $Port+ if (Test-Path -LiteralPath $path) {+ Remove-Item -LiteralPath $path -Force -ErrorAction SilentlyContinue+ }+ }++ function Test-IdaMcpOpeningInFlight {- param([datetime]$Now = (Get-Date))- $path = Get-IdaMcpOpeningLockPath++ param(++ [int]$Port = 13337,++ [datetime]$Now = (Get-Date)++ )++ $path = Get-IdaMcpOpeningLockPath -Port $Port+ if (-not (Test-Path -LiteralPath $path)) { return $false }+ $raw = (Get-Content -LiteralPath $path -Raw -ErrorAction SilentlyContinue)- if ([string]::IsNullOrWhiteSpace($raw)) { return $true }++ if ([string]::IsNullOrWhiteSpace($raw)) {++ Remove-Item -LiteralPath $path -Force -ErrorAction SilentlyContinue++ return $false++ }+ try {+ $until = [datetime]::Parse($raw.Trim(), $null, [System.Globalization.DateTimeStyles]::RoundtripKind)+ } catch {- return $true++ Remove-Item -LiteralPath $path -Force -ErrorAction SilentlyContinue++ return $false+ }+ if ($Now -gt $until) {+ Remove-Item -LiteralPath $path -Force -ErrorAction SilentlyContinue+ return $false+ }+ return $true+ }++ function Test-IdaMcpKeepaliveDeadlockFromFacts {+ param(+ [bool]$GuiOwnsPort = $false,+ [bool]$OpeningInFlight = $false,+ $LastHealthy = $null,+ [datetime]$Now = (Get-Date),+ [int]$MaxUnhealthySeconds = 180+ )+ if ($GuiOwnsPort) { return $false }+ if ($OpeningInFlight) { return $false }+ if ($null -eq $LastHealthy) { return $false }+ return ($Now - [datetime]$LastHealthy).TotalSeconds -ge $MaxUnhealthySeconds+ }diff --cc skills/ida-reverse/scripts/open.ps1index 1ea7b4f,ddb1c58..d6e9f44--- a/skills/ida-reverse/scripts/open.ps1+++ b/skills/ida-reverse/scripts/open.ps1@@@ -230,6 -230,8 +230,8 @@@ $bodyObj = @}$body = $bodyObj | ConvertTo-Json -Depth 12 -Compress-Set-IdaMcpOpeningLock -TtlSeconds ($TimeoutSeconds + 60)++Set-IdaMcpOpeningLock -Port $Port -TtlSeconds ($TimeoutSeconds + 60)+$openJob = Start-Job -ScriptBlock {param($RequestBody, $RequestPort)try {@@@ -368,6 -370,7 +370,7 @@@ tryWrite-Output "ERR:$err"exit 1} finally {- Clear-IdaMcpOpeningLock++ Clear-IdaMcpOpeningLock -Port $Portif ($openJob) {Stop-Job -Job $openJob -ErrorAction SilentlyContinueRemove-Job -Job $openJob -Force -ErrorAction SilentlyContinuediff --cc skills/ida-reverse/scripts/start.ps1index 4d0b6f8,3e8791b..5721123--- a/skills/ida-reverse/scripts/start.ps1+++ b/skills/ida-reverse/scripts/start.ps1@@@ -347,13 -349,22 +349,22 @@@ if ($guiOwners.Count -gt 0)if (-not $Force) {if ($probe.Status -eq 'healthy') {- Write-IdaMcpLastHealthy++ Write-IdaMcpLastHealthy -Port $PortWrite-Output "OK:$($probe.Count)`:reuse"exit 0}if ($probe.Status -eq 'busy') {- Write-Output ("WARN:busy:port={0} pid={1}" -f $Port, ($probe.Owners -join ','))- Write-Output 'HINT: 13337 is listening but tools/list timed out (likely idb_open). Not killing supervisor.'- exit 0+ $deadlock = Test-IdaMcpKeepaliveDeadlockFromFacts `+ -GuiOwnsPort $false `- -OpeningInFlight (Test-IdaMcpOpeningInFlight) `- -LastHealthy (Read-IdaMcpLastHealthy) `++ -OpeningInFlight (Test-IdaMcpOpeningInFlight -Port $Port) `++ -LastHealthy (Read-IdaMcpLastHealthy -Port $Port) `+ -Now (Get-Date)+ if (-not $deadlock) {+ Write-Output ("WARN:busy:port={0} pid={1}" -f $Port, ($probe.Owners -join ','))+ Write-Output 'HINT: 13337 is listening but tools/list timed out (likely idb_open). Not killing supervisor.'+ exit 0+ }+ Write-Output ("INFO:deadlock:port={0} pid={1} (tools/list unhealthy >180s, replacing)" -f $Port, ($probe.Owners -join ','))}}@@@ -494,6 -505,7 +505,7 @@@ for ($i = 0; $i -lt $WaitSeconds; $i++Start-Sleep -Seconds 1$toolCount = Test-IdaMcpHealth -Port $Portif ($toolCount -gt 0) {- Write-IdaMcpLastHealthy++ Write-IdaMcpLastHealthy -Port $PortWrite-Output "OK:$toolCount"$ready = $truebreakdiff --cc skills/ida-reverse/scripts/watchdog.ps1index 02f2933,12befa5..873ff53--- a/skills/ida-reverse/scripts/watchdog.ps1+++ b/skills/ida-reverse/scripts/watchdog.ps1@@@ -64,20 -68,58 +68,61 @@@ function Probe-IdaMcpreturn @{ Status = 'stale'; Count = $count }}} catch {}- # Listen + RPC timeout = busy (single-threaded supervisor during idb_open).- # Only "nothing listening" or a quick stale (no py_eval) list is down.+ # Listen + RPC timeout: GUI or in-flight idb_open = busy.+ # Deadlock is consecutive tools/list failures >180s (last-healthy), not process age.if ($owners.Count -gt 0) {- return @{ Status = 'busy'; Count = 0 }+ return @{ Status = 'busy'; Count = 0; Owners = $owners }}- return @{ Status = 'down'; Count = 0 }+ return @{ Status = 'down'; Count = 0; Owners = @() }+ }++ function Get-IdaMcpProcessInfo {+ param([int]$ProcessId)+ return Get-CimInstance Win32_Process -Filter "ProcessId=$ProcessId" -ErrorAction SilentlyContinue+ }++ function Test-IdaGuiProcess {+ param([int]$ProcessId)+ $proc = Get-IdaMcpProcessInfo -ProcessId $ProcessId+ if (-not $proc) { return $false }+ return [string]$proc.Name -match '(?i)^(ida|ida64|idaq|idaq64)\.exe$'+ }++ function Test-DeadlockedSupervisor {- param([int[]]$Owners)++ param(++ [int[]]$Owners,++ [int]$Port++ )+ $gui = @($Owners | Where-Object { Test-IdaGuiProcess -ProcessId $_ })+ if ($gui.Count -gt 0) { return $false }+ return Test-IdaMcpKeepaliveDeadlockFromFacts `+ -GuiOwnsPort $false `- -OpeningInFlight (Test-IdaMcpOpeningInFlight) `- -LastHealthy (Read-IdaMcpLastHealthy) `++ -OpeningInFlight (Test-IdaMcpOpeningInFlight -Port $Port) `++ -LastHealthy (Read-IdaMcpLastHealthy -Port $Port) `+ -Now (Get-Date)}$probe = Probe-IdaMcp -Port $Portif ($probe.Status -eq 'healthy') {- Write-IdaMcpLastHealthy++ Write-IdaMcpLastHealthy -Port $PortWrite-WatchLog "OK:$($probe.Count)`:reuse"exit 0}if ($probe.Status -eq 'busy') {+ $owners = @($probe.Owners)- if (Test-DeadlockedSupervisor -Owners $owners) {++ if (Test-DeadlockedSupervisor -Owners $owners -Port $Port) {+ Write-WatchLog ("INFO:deadlock pids={0}, calling start.ps1 -Force" -f ($owners -join ','))+ $startScript = Join-Path $PSScriptRoot 'start.ps1'+ $startOutput = & $startScript -Port $Port -Force+ foreach ($line in @($startOutput)) {+ Write-WatchLog "START:$line"+ }+ $last = (@($startOutput) | Where-Object { $_ -match '^(OK|ERR|WARN):' } | Select-Object -Last 1)+ if ($last -match '^(OK:|WARN:gui_busy|WARN:busy)') {+ exit 0+ }+ exit 1+ }Write-WatchLog 'OK:busy:reuse'exit 0}diff --cc skills/scripts/test-parse-contracts.ps1index bada38c,bc76afe..1676f90--- a/skills/scripts/test-parse-contracts.ps1+++ b/skills/scripts/test-parse-contracts.ps1@@@ -46,6 -46,99 +46,125 @@@ tryRemove-Item -Recurse -Force $tmp -ErrorAction SilentlyContinue}+ $idaDir = Join-Path $skillsRoot 'ida-reverse/scripts'+ $idaFiles = @{+ 'start.ps1' = (Join-Path $idaDir 'start.ps1')+ 'watchdog.ps1' = (Join-Path $idaDir 'watchdog.ps1')+ 'recover.ps1' = (Join-Path $idaDir 'recover.ps1')+ 'run-supervisor.py' = (Join-Path $idaDir 'run-supervisor.py')+ }+ foreach ($name in @($idaFiles.Keys)) {+ $p = $idaFiles[$name]+ if (-not (Test-Path -LiteralPath $p)) { Bad ("missing {0}" -f $name); continue }+ if ($name -eq 'run-supervisor.py') { continue }+ $errs = $null+ $tokens = $null+ $null = [System.Management.Automation.Language.Parser]::ParseFile($p, [ref]$tokens, [ref]$errs)+ if ($errs -and $errs.Count -gt 0) {+ Bad ("parse fail {0}: {1}" -f $name, $errs[0].Message)+ } else {+ Ok ("parse {0}" -f $name)+ }+ }++ $now = Get-Date+ if (Test-IdaMcpKeepaliveDeadlockFromFacts -GuiOwnsPort $true -OpeningInFlight $false -LastHealthy $now.AddMinutes(-10) -Now $now) {+ Bad 'GUI owner must not be deadlock'+ } else { Ok 'GUI owner is not deadlock' }+ if (Test-IdaMcpKeepaliveDeadlockFromFacts -GuiOwnsPort $false -OpeningInFlight $true -LastHealthy $now.AddMinutes(-10) -Now $now) {+ Bad 'in-flight open must not be deadlock'+ } else { Ok 'in-flight open is not deadlock' }+ if (Test-IdaMcpKeepaliveDeadlockFromFacts -GuiOwnsPort $false -OpeningInFlight $false -LastHealthy $null -Now $now) {+ Bad 'never-healthy must not Force'+ } else { Ok 'never-healthy is not deadlock' }+ if (Test-IdaMcpKeepaliveDeadlockFromFacts -GuiOwnsPort $false -OpeningInFlight $false -LastHealthy $now.AddSeconds(-10) -Now $now) {+ Bad 'recent last-healthy must not be deadlock'+ } else { Ok 'recent last-healthy is not deadlock' }+ if (Test-IdaMcpKeepaliveDeadlockFromFacts -GuiOwnsPort $false -OpeningInFlight $false -LastHealthy $now.AddSeconds(-181) -Now $now) {+ Ok 'stale last-healthy is deadlock'+ } else { Bad 'tools/list unhealthy >180s should be deadlock' }+++$oldLocalAppData = $env:LOCALAPPDATA++$keepaliveTmp = Join-Path $tmp 'keepalive-state'++$env:LOCALAPPDATA = $keepaliveTmp++try {++ Set-IdaMcpOpeningLock -Port 13337 -TtlSeconds 60++ if ((Test-IdaMcpOpeningInFlight -Port 13337) -and -not (Test-IdaMcpOpeningInFlight -Port 13338)) {++ Ok 'opening lock is isolated per port'++ } else {++ Bad 'opening lock leaked across ports'++ }++ Write-IdaMcpLastHealthy -Port 13337++ if ((Read-IdaMcpLastHealthy -Port 13337) -and -not (Read-IdaMcpLastHealthy -Port 13338)) {++ Ok 'last-healthy state is isolated per port'++ } else {++ Bad 'last-healthy state leaked across ports'++ }++ Set-Content -LiteralPath (Get-IdaMcpOpeningLockPath -Port 13339) -Value 'corrupt' -Encoding ASCII++ if (-not (Test-IdaMcpOpeningInFlight -Port 13339)) {++ Ok 'corrupt opening lock fails closed and is cleared'++ } else {++ Bad 'corrupt opening lock blocks recovery forever'++ }++} finally {++ $env:LOCALAPPDATA = $oldLocalAppData++}+++ $recover = Get-Content -LiteralPath $idaFiles['recover.ps1'] -Raw -ErrorAction SilentlyContinue+ if ($recover -match '(?i)start\.ps1' -and $recover -match '-Force') {+ Ok 'recover.ps1 calls start.ps1 -Force'+ } else {+ Bad 'recover.ps1 must invoke start.ps1 -Force'+ }+ if ($recover -match '(?im)^\s*(&\s*)?taskkill(\.exe)?\s+[^\n]*/T\b') {+ Bad 'recover.ps1 must not taskkill /T'+ } else {+ Ok 'recover.ps1 has no taskkill /T'+ }++ $start = Get-Content -LiteralPath $idaFiles['start.ps1'] -Raw+ if ($start -match 'Test-IdaMcpKeepaliveDeadlockFromFacts' -and $start -match 'Write-IdaMcpLastHealthy' -and $start -notmatch 'CreationDate') {+ Ok 'start.ps1 deadlock uses last-healthy, not CreationDate'+ } else {+ Bad 'start.ps1 must use last-healthy deadlock, not process CreationDate'+ }+ if ($start -match '(?im)^\s*(&\s*)?taskkill(\.exe)?\s+[^\n]*/T\b') {+ Bad 'start.ps1 must not taskkill /T'+ } else {+ Ok 'start.ps1 has no taskkill /T'+ }++ $watch = Get-Content -LiteralPath $idaFiles['watchdog.ps1'] -Raw+ if ($watch -match 'Test-IdaMcpKeepaliveDeadlockFromFacts' -and $watch -match 'Test-IdaMcpOpeningInFlight' -and $watch -match '-Force') {+ Ok 'watchdog.ps1 Force-replaces only last-healthy deadlock'+ } else {+ Bad 'watchdog.ps1 missing last-healthy deadlock -Force path'+ }+ if ($watch -match 'CreationDate') {+ Bad 'watchdog.ps1 must not use process CreationDate'+ } else {+ Ok 'watchdog.ps1 has no CreationDate deadlock'+ }++ $open = Get-Content -LiteralPath (Join-Path $idaDir 'open.ps1') -Raw+ if ($open -match 'Set-IdaMcpOpeningLock' -and $open -match 'Clear-IdaMcpOpeningLock') {+ Ok 'open.ps1 holds opening.lock during idb_open'+ } else {+ Bad 'open.ps1 must set and clear opening.lock'+ }++ $py = Get-Content -LiteralPath $idaFiles['run-supervisor.py'] -Raw+ if ($py -match 'ThreadingHTTPServer' -and $py -match '_patch_streamable_http') {+ Ok 'run-supervisor.py patches ThreadingHTTPServer'+ } else {+ Bad 'run-supervisor.py missing streamable-http patch'+ }+ if ($py -match 'streamable-http patch skipped') {+ Ok 'run-supervisor.py skips a failed HTTP patch'+ } else {+ Bad 'run-supervisor.py must fail-open when the HTTP patch errors'+ }+if ($fail.Count -gt 0) {Write-Host ("FAILED {0}" -f $fail.Count) -ForegroundColor Red$fail | ForEach-Object { Write-Host " - $_" }