跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

qa.ps1

73 行 · 9.4 KB历史下载
1#requires -Version 5.1
2param([Parameter(Mandatory)][string]$ProfilePath,[Parameter(Mandatory)][string]$EvidenceDirectory)
3$ErrorActionPreference='Stop';$ProgressPreference='SilentlyContinue'
4$profile=Get-Content -LiteralPath $ProfilePath -Raw|ConvertFrom-Json
5$executable=$profile.relay_executable;$helper=Join-Path ([IO.Path]::GetDirectoryName($executable)) 'cage-guest-files.exe'
6$utf8=[Text.UTF8Encoding]::new($false)
7$owned=[Collections.Generic.List[Diagnostics.Process]]::new()
8$script:phase='starting'
9function Start-Bridge([string]$Operation,[bool]$Files=$false){
10 $s=[Diagnostics.ProcessStartInfo]::new();$s.FileName=if($Files){$helper}else{$executable}
11 $s.Arguments=if($Files){$Operation}else{'--relay-profile "'+$ProfilePath+'" '+$Operation}
12 if($Operation -eq 'export'){$s.Arguments+=' --paths-stdin'}
13 $s.UseShellExecute=$false;$s.CreateNoWindow=$true;$s.RedirectStandardInput=$true;$s.RedirectStandardOutput=$true;$s.RedirectStandardError=$true;$s.StandardOutputEncoding=$utf8
14 $p=[Diagnostics.Process]::Start($s);$owned.Add($p);$null=$p.StandardError.ReadToEndAsync();return $p
15}
16function Send-JSON($Process,$Object){$b=$utf8.GetBytes((ConvertTo-Json -InputObject $Object -Compress -Depth 8)+"`n");$Process.StandardInput.BaseStream.Write($b,0,$b.Length);$Process.StandardInput.BaseStream.Flush()}
17function Read-JSON($Process){$r=$Process.StandardOutput.ReadLineAsync();if(!$r.Wait(30000) -or !$r.Result){throw ('The GUI bridge response did not complete at '+$script:phase+'.')};return ($r.Result|ConvertFrom-Json)}
18function Check-Exit($Process){if(!$Process.WaitForExit(30000) -or $Process.ExitCode -ne 0){throw 'The GUI bridge child failed.'}}
19function SHA([byte[]]$Bytes){return [BitConverter]::ToString([Security.Cryptography.SHA256]::Create().ComputeHash($Bytes)).Replace('-','').ToLowerInvariant()}
20$echoes=0
21function Read-Ack([long]$Serial){
22 while($true){$value=Read-JSON $clipboard;if($value.error){throw 'Clipboard write failed.'};if($value.ack -eq $Serial){return};if($value.source -eq 'guest' -and $value.text -eq $seed){$script:echoes++}}
23}
24function Read-Guest([string]$Format){while($true){$v=Read-JSON $clipboard;if($v.error){throw 'Clipboard selection failed.'};if($v.source -eq 'guest' -and $null -ne $v.$Format){return $v}}}
25function Invoke-Native([string]$Body){
26 $marker='__CQ_'+[guid]::NewGuid().ToString('N').Substring(0,8)+'='
27 $script='Add-Type -AssemblyName System.Windows.Forms;'+$Body+';[Console]::WriteLine("'+$marker+'"+$result)'
28 $encoded=[Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($script))
29 Send-JSON $terminal @{action='input';data=[Convert]::ToBase64String($utf8.GetBytes('powershell.exe -Sta -NoProfile -EncodedCommand '+$encoded+"`r"))}
30 $output=''
31 while($true){$m=Read-JSON $terminal;if($m.error -or $null -ne $m.exit){throw 'The ordinary GUI ConPTY failed.'};if($m.data){$output+=$utf8.GetString([Convert]::FromBase64String($m.data));$match=[regex]::Match($output,[regex]::Escape($marker)+'([a-zA-Z0-9_-]+)[\r\n]');if($match.Success){return $match.Groups[1].Value}}}
32}
33try {
34 $script:phase='clipboard_public_hello';$clipboard=Start-Bridge 'stream';$hello=Read-JSON $clipboard
35 if($hello.hello.os -ne 'windows' -or !$hello.hello.available -or $hello.version){throw 'The public clipboard hello shape is invalid.'}
36 $script:phase='terminal_ready';$terminal=Start-Bridge 'terminal';Send-JSON $terminal @{action='open';cols=160;rows=35;workspace='home';shell='powershell'}
37 if(!(Read-JSON $terminal).ready){throw 'The ordinary GUI ConPTY did not become ready.'}
38 Send-JSON $terminal @{action='resize';cols=160;rows=40}
39 $script:phase='host_text_ack';$seed="Windows host 中文 😀`nsecond line";Send-JSON $clipboard @{serial=1;text=$seed};Read-Ack 1
40 $expected=SHA $utf8.GetBytes($seed)
41 $script:phase='host_text_native_read';$read=Invoke-Native '$b=[Text.Encoding]::UTF8.GetBytes([Windows.Forms.Clipboard]::GetText());$result=[BitConverter]::ToString([Security.Cryptography.SHA256]::Create().ComputeHash($b)).Replace("-","").ToLowerInvariant()'
42 if($read -ne $expected){throw 'Native GUI clipboard text does not match the host write.'}
43 $guest='Windows guest 中文 😀';$guest64=[Convert]::ToBase64String($utf8.GetBytes($guest))
44 $script:phase='guest_text_write';$null=Invoke-Native ('$text=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String("'+$guest64+'"));[Windows.Forms.Clipboard]::SetText($text);$result="written"')
45 if((Read-Guest 'text').text -ne $guest){throw 'Native guest text did not reach the host.'}
46 # A small valid PNG fixture has stable bytes, dimensions and transparency.
47 Add-Type -AssemblyName System.Drawing
48 $image=[Drawing.Bitmap]::new(13,7);$g=[Drawing.Graphics]::FromImage($image);$g.Clear([Drawing.Color]::FromArgb(177,21,80,140));$bytes=[IO.MemoryStream]::new();$image.Save($bytes,[Drawing.Imaging.ImageFormat]::Png);$g.Dispose();$image.Dispose();$png=$bytes.ToArray();$bytes.Dispose()
49 $script:phase='host_png_ack';Send-JSON $clipboard @{serial=2;png=[Convert]::ToBase64String($png)};Read-Ack 2
50 $script:phase='host_png_native_read';$read=Invoke-Native '$b=([IO.MemoryStream][Windows.Forms.Clipboard]::GetData("PNG")).ToArray();$result=[BitConverter]::ToString([Security.Cryptography.SHA256]::Create().ComputeHash($b)).Replace("-","").ToLowerInvariant()'
51 if($read -ne (SHA $png)){throw 'Native PNG bytes do not match the host write.'}
52 $script:phase='guest_png_write';$null=Invoke-Native 'Add-Type -AssemblyName System.Drawing;$im=[Drawing.Bitmap]::new(9,5);$g=[Drawing.Graphics]::FromImage($im);$g.Clear([Drawing.Color]::Coral);$s=[IO.MemoryStream]::new();$im.Save($s,[Drawing.Imaging.ImageFormat]::Png);$d=[Windows.Forms.DataObject]::new();$d.SetData("PNG",$false,$s);$d.SetImage($im);[Windows.Forms.Clipboard]::SetDataObject($d,$true);$g.Dispose();$im.Dispose();$result="written"'
53 $returned=[Convert]::FromBase64String((Read-Guest 'png').png);$pngImage=[Drawing.Image]::FromStream([IO.MemoryStream]::new($returned));if($pngImage.Width -ne 9 -or $pngImage.Height -ne 5){throw 'Native guest PNG did not reach the host.'};$pngImage.Dispose()
54 $fixture=Join-Path $EvidenceDirectory ([guid]::NewGuid().ToString('N'));$null=[IO.Directory]::CreateDirectory($fixture)
55 $file=Join-Path $fixture '中文😀.txt';$content=$utf8.GetBytes('Host to guest file 中文😀');[IO.File]::WriteAllBytes($file,$content)
56 $script:phase='host_file_import';$writer=Start-Bridge 'export' $true;Send-JSON $writer @($file);$writer.StandardInput.Close()
57 $importer=Start-Bridge 'import';$writer.StandardOutput.BaseStream.CopyTo($importer.StandardInput.BaseStream);$importer.StandardInput.Close();$imported=Read-JSON $importer;Check-Exit $writer;Check-Exit $importer
58 if(@($imported.paths).Count -ne 1){throw 'The imported file selection is invalid.'}
59 $script:phase='host_file_ack';Send-JSON $clipboard @{serial=3;files=@($imported.paths)};Read-Ack 3
60 $script:phase='host_file_native_read';$read=Invoke-Native '$files=[Windows.Forms.Clipboard]::GetFileDropList();if($files.Count -ne 1){throw "selection_invalid"};$b=[IO.File]::ReadAllBytes($files[0]);$result=[BitConverter]::ToString([Security.Cryptography.SHA256]::Create().ComputeHash($b)).Replace("-","").ToLowerInvariant()'
61 if($read -ne (SHA $content)){throw 'The actual native GUI file-drop bytes do not match.'}
62 $guestContent=$utf8.GetBytes('Guest to host file 中文😀');$guest64=[Convert]::ToBase64String($guestContent)
63 $script:phase='guest_file_native_write';$null=Invoke-Native ('$dir=Join-Path ([IO.Path]::GetTempPath()) ("cage-clipboard-qa-"+[guid]::NewGuid().ToString("N"));$null=[IO.Directory]::CreateDirectory($dir);$f=Join-Path $dir "guest.txt";[IO.File]::WriteAllBytes($f,[Convert]::FromBase64String("'+$guest64+'"));$list=[Collections.Specialized.StringCollection]::new();$null=$list.Add($f);[Windows.Forms.Clipboard]::SetFileDropList($list);$result="written"')
64 $selected=Read-Guest 'files';$exporter=Start-Bridge 'export';Send-JSON $exporter @($selected.files);$exporter.StandardInput.Close()
65 $reader=Start-Bridge 'import' $true;$exporter.StandardOutput.BaseStream.CopyTo($reader.StandardInput.BaseStream);$reader.StandardInput.Close();$received=Read-JSON $reader;Check-Exit $exporter;Check-Exit $reader
66 if(@($received.paths).Count -ne 1 -or (SHA ([IO.File]::ReadAllBytes($received.paths[0]))) -ne (SHA $guestContent)){throw 'The native guest selection did not export completely.'}
67 # Host paths cannot bypass the GUI agent's recorded selection grants.
68 $rejected=Start-Bridge 'export';Send-JSON $rejected @('C:\Windows\win.ini');$rejected.StandardInput.Close();$rejected.StandardOutput.BaseStream.CopyTo([IO.Stream]::Null);if(!$rejected.WaitForExit(30000) -or $rejected.ExitCode -eq 0){throw 'An unselected guest path was exported.'}
69 if($echoes -ne 0){throw 'A host clipboard write was echoed as a guest selection.'}
70 $result=[ordered]@{public_hello=$true;ordinary_gui_conpty=$true;terminal_resize=$true;text_host_to_guest=$true;text_guest_to_host=$true;png_host_to_guest_bytes=$true;png_guest_to_host_dimensions=$true;files_host_to_guest_bytes=$true;files_guest_to_host_bytes=$true;unselected_export_denied=$true;host_echoes=0}
71 $null=[IO.Directory]::CreateDirectory($EvidenceDirectory);[IO.File]::WriteAllText((Join-Path $EvidenceDirectory 'clipboard-result.json'),($result|ConvertTo-Json -Depth 3),$utf8)
72 $result|ConvertTo-Json -Compress
73} finally {foreach($p in $owned){try{$p.StandardInput.Close();if(!$p.WaitForExit(1000)){$p.Kill()};$p.Dispose()}catch{}}}