跳到主要内容

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

浏览文件

windows-guest-media.ps1

156 行 · 8.0 KB历史下载
1#requires -Version 5.1
2# Pinned to the x64 standalone ZIP in Microsoft's PowerShell v7.6.9 release.
3$GuestPowerShellVersion = '7.6.9'
4$GuestPowerShellSHA256 = 'ce7484804c0f0ad4dda10c958e71acdaf7eebfb22505c1b480f89e0e9f1da94d'
5
6function Add-GuestPowerShellToAnswer([string]$Source, [xml]$Answer, [string]$PowerShellZip) {
7 $ErrorActionPreference = 'Stop'
8 $ns = [Xml.XmlNamespaceManager]::new($Answer.NameTable)
9 $unattend = 'urn:schemas-microsoft-com:unattend'
10 $ns.AddNamespace('u', $unattend)
11 $root = $Answer.SelectSingleNode('/u:unattend', $ns)
12 if (!$root) { throw 'Expected a Windows Setup answer document.' }
13 $archive = Join-Path $Source 'PowerShell.zip'
14 $script = Join-Path $Source 'install.cmd'
15 $xml = Join-Path $Source 'Autounattend.xml'
16 foreach ($path in @($archive, $script, $xml)) {
17 if (Test-Path -LiteralPath $path) { throw "Answer DVD staging file already exists: $path" }
18 }
19 if ($PowerShellZip) {
20 $local = Get-Item -LiteralPath $PowerShellZip
21 [IO.File]::Copy($local.FullName, $archive)
22 } else {
23 $ProgressPreference = 'SilentlyContinue'
24 $url = "https://github.com/PowerShell/PowerShell/releases/download/v$GuestPowerShellVersion/PowerShell-$GuestPowerShellVersion-win-x64.zip"
25 Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $archive
26 }
27 if ((Get-FileHash -LiteralPath $archive -Algorithm SHA256).Hash -ne $GuestPowerShellSHA256) {
28 throw 'The PowerShell archive does not match the pinned Microsoft release SHA-256.'
29 }
30 $template = [IO.File]::ReadAllText((Join-Path $PSScriptRoot 'install-guest-powershell.cmd'))
31 $installer = $template.Replace('__CAGE_PWSH_VERSION__', $GuestPowerShellVersion).Replace('__CAGE_PWSH_SHA256__', $GuestPowerShellSHA256)
32 [IO.File]::WriteAllText($script, ($installer -replace '\r?\n', "`r`n"), [Text.Encoding]::ASCII)
33 $scriptHash = (Get-FileHash -LiteralPath $script -Algorithm SHA256).Hash
34 # Windows Setup limits Path to 259 characters. Select the single labeled
35 # DVD even when the installation DVD takes a different drive letter.
36 $command = 'powershell -NoProfile -c "$d=@(Get-Volume -FileSystemLabel CAGE_ANSWER);if($d.Count -eq 1){$p=$d[0].DriveLetter+'':\install.cmd'';if((Get-FileHash $p).Hash -eq ''HASH''){&$p;exit $LASTEXITCODE}};exit 1"'.Replace('HASH', $scriptHash)
37 if ($command.Length -gt 259) { throw 'Windows Setup command exceeds its maximum length.' }
38
39 $settings = $root.SelectSingleNode('u:settings[@pass="specialize"]', $ns)
40 if (!$settings) {
41 $settings = $root.AppendChild($Answer.CreateElement('settings', $unattend))
42 $settings.SetAttribute('pass', 'specialize')
43 }
44 $deployment = $settings.SelectSingleNode('u:component[@name="Microsoft-Windows-Deployment" and @processorArchitecture="amd64"]', $ns)
45 if (!$deployment) {
46 $deployment = $settings.AppendChild($Answer.CreateElement('component', $unattend))
47 $deployment.SetAttribute('name', 'Microsoft-Windows-Deployment')
48 $deployment.SetAttribute('processorArchitecture', 'amd64')
49 $deployment.SetAttribute('publicKeyToken', '31bf3856ad364e35')
50 $deployment.SetAttribute('language', 'neutral')
51 $deployment.SetAttribute('versionScope', 'nonSxS')
52 }
53 $run = $deployment.SelectSingleNode('u:RunSynchronous', $ns)
54 if (!$run) { $run = $deployment.AppendChild($Answer.CreateElement('RunSynchronous', $unattend)) }
55 $order = 1
56 foreach ($existing in $run.SelectNodes('u:RunSynchronousCommand/u:Order', $ns)) {
57 $order = [Math]::Max($order, 1 + [int]$existing.InnerText)
58 }
59 $entry = $run.AppendChild($Answer.CreateElement('RunSynchronousCommand', $unattend))
60 $action = $Answer.CreateAttribute('wcm', 'action', 'http://schemas.microsoft.com/WMIConfig/2002/State')
61 $action.Value = 'add'
62 $null = $entry.Attributes.Append($action)
63 $entry.AppendChild($Answer.CreateElement('Order', $unattend)).InnerText = [string]$order
64 $entry.AppendChild($Answer.CreateElement('Path', $unattend)).InnerText = $command
65 $Answer.Save($xml)
66}
67
68function Write-AnswerISO([string]$Source, [string]$Output) {
69 if (!('CageEvaluationIsoStream' -as [type])) {
70 Add-Type -TypeDefinition @'
71using System;
72using System.IO;
73using System.Runtime.InteropServices;
74using System.Runtime.InteropServices.ComTypes;
75public static class CageEvaluationIsoStream {
76 public static void Save(object nativeStream, string destination) {
77 var source = (IStream)nativeStream;
78 var buffer = new byte[32768];
79 var count = Marshal.AllocHGlobal(sizeof(int));
80 try {
81 using (var file = new FileStream(destination, FileMode.CreateNew, FileAccess.Write, FileShare.None)) {
82 while (true) {
83 source.Read(buffer, buffer.Length, count);
84 int n = Marshal.ReadInt32(count);
85 if (n == 0) break;
86 file.Write(buffer, 0, n);
87 }
88 }
89 } finally { Marshal.FreeHGlobal(count); }
90 }
91}
92'@
93 }
94 $filesystem = New-Object -ComObject IMAPI2FS.MsftFileSystemImage
95 $root = $result = $stream = $null
96 try {
97 $filesystem.ChooseImageDefaultsForMediaType(6) # DVD+R
98 $filesystem.FileSystemsToCreate = 4 # UDF
99 $filesystem.VolumeName = 'CAGE_ANSWER'
100 $root = $filesystem.Root
101 $root.AddTree($Source, $false)
102 $result = $filesystem.CreateResultImage()
103 $stream = $result.ImageStream
104 [CageEvaluationIsoStream]::Save($stream, $Output)
105 } finally {
106 # IMAPI can retain handles to the source ZIP until its image is released.
107 foreach ($object in @($stream, $result, $root, $filesystem)) {
108 if ($null -ne $object) { $null = [Runtime.InteropServices.Marshal]::ReleaseComObject($object) }
109 }
110 }
111}
112
113function Grant-VMAnswerReadAccess([string]$ISO, [guid]$Id) {
114 $isoAcl = Get-Acl -LiteralPath $ISO
115 $worker = [Security.Principal.NTAccount]::new("NT VIRTUAL MACHINE\$Id")
116 $isoAcl.AddAccessRule([Security.AccessControl.FileSystemAccessRule]::new($worker, 'ReadAndExecute', 'Allow'))
117 Set-Acl -LiteralPath $ISO -AclObject $isoAcl
118}
119
120# Build input in a private, same-instance staging directory. Only the ISO is
121# published. Evaluation Setup can reuse the ZIP on the original generic DVD.
122function New-WindowsGuestAnswerISO([string]$Output, [xml]$Answer, [string]$PowerShellZip, [string]$GenericAnswer) {
123 $parent = [IO.Path]::GetDirectoryName($Output)
124 $stage = Join-Path $parent ([guid]::NewGuid().ToString('N'))
125 $source = Join-Path $stage 'source'
126 $candidate = Join-Path $stage 'answer.iso'
127 $null = [IO.Directory]::CreateDirectory($source)
128 try {
129 $mountOwned = $false
130 try {
131 if (!$PowerShellZip -and $GenericAnswer -and (Test-Path -LiteralPath $GenericAnswer -PathType Leaf)) {
132 if (!(Get-DiskImage -ImagePath $GenericAnswer).Attached) {
133 $null = Mount-DiskImage -ImagePath $GenericAnswer -Access ReadOnly -StorageType ISO
134 $mountOwned = $true
135 }
136 $timer = [Diagnostics.Stopwatch]::StartNew()
137 do {
138 $volumes = @(Get-DiskImage -ImagePath $GenericAnswer | Get-Volume -ErrorAction SilentlyContinue)
139 if ($volumes.Count -eq 1 -and $volumes[0].DriveLetter) { break }
140 Start-Sleep -Milliseconds 250
141 } while ($timer.Elapsed.TotalSeconds -lt 15)
142 if ($volumes.Count -ne 1 -or !$volumes[0].DriveLetter -or $volumes[0].FileSystemLabel -ne 'CAGE_ANSWER') {
143 throw 'Cannot read the original Cage guest tools DVD.'
144 }
145 $PowerShellZip = '{0}:\PowerShell.zip' -f $volumes[0].DriveLetter
146 }
147 Add-GuestPowerShellToAnswer $source $Answer $PowerShellZip
148 } finally {
149 if ($mountOwned) { $null = Dismount-DiskImage -ImagePath $GenericAnswer }
150 }
151 Write-AnswerISO $source $candidate
152 [IO.File]::Move($candidate, $Output)
153 } finally {
154 [IO.Directory]::Delete($stage, $true)
155 }
156}