跳到主要内容

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

浏览文件

first_logon.ps1

106 行 · 5.2 KB历史下载
1$logfile = "C:\Users\{{.User}}\lima-setup.log"
2$globalStart = Get-Date
3
4# Record logs
5Start-Transcript -Path $logfile -Append
6
7Write-Output "Lima Setup Started at $globalStart"
8
9$sectionStart = Get-Date
10# We need to change password because the current password is specified in autounattend.xml, so all users/processes can see it.
11# Generate a random 16 character password.
12# Avoid special characters to minimize potential keyboard layout issue.
13$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
14$newPassword = -join ((1..16) | ForEach-Object { $chars[(Get-Random -Maximum $chars.Length)] })
15
16# Store the password under the user directory so that user can know/change it.
17# Use ASCII encoding to avoid the UTF-8 BOM that Windows PowerShell's `Out-File -Encoding utf8`
18# prepends; the generated password only contains ASCII characters.
19$newPassword | Out-File -FilePath "C:\Users\{{.User}}\password.txt" -Encoding ascii -NoNewline
20
21# Change the password
22$username = $env:USERNAME
23$newSecurePassword = ConvertTo-SecureString $newPassword -AsPlainText -Force
24Set-LocalUser -Name $username -Password $newSecurePassword
25
26$elapsed = (Get-Date) - $sectionStart
27Write-Output "Password change completed in $($elapsed.TotalSeconds) seconds"
28
29
30$sectionStart = Get-Date
31# Install OpenSSH via MSI installer
32# Avoid using the OpenSSH.Server included in the OS because installing OpenSSH takes much longer than Win32-OpenSSH.
33$installer = "C:\Users\{{.User}}\openssh.msi"
34{{ $openSSHInstaller := "https://github.com/PowerShell/Win32-OpenSSH/releases/download/10.0.0.0p2-Preview/OpenSSH-Win64-v10.0.0.0.msi" -}}
35{{ $openSSHInstallerSHA256 := "ddec9c53864280759cf9f74791cefd387100e3946aa849a1c138a4ed1b96b7d9" -}}
36{{ if eq .Arch "aarch64" -}}
37{{ $openSSHInstaller = "https://github.com/PowerShell/Win32-OpenSSH/releases/download/10.0.0.0p2-Preview/OpenSSH-ARM64-v10.0.0.0.msi" -}}
38{{ $openSSHInstallerSHA256 = "7a17d0e22d004fb47ca4bfd8fef926fa305de4ebf70a6f3c7a29c39aabef0023" -}}
39{{ end -}}
40Invoke-WebRequest -Uri "{{ $openSSHInstaller }}" -OutFile $installer
41
42# Expected hash comes from https://github.com/powershell/win32-openssh/releases
43$expectedHash = "{{ $openSSHInstallerSHA256 }}".ToLower()
44$actualHash = (Get-FileHash -Path $installer -Algorithm SHA256).Hash.ToLower()
45
46# Verify the integrity of the downloaded installer before executing it.
47if ($actualHash -eq $expectedHash) {
48 Write-Output "OpenSSH SHA256 verification succeeded"
49}else{
50 Write-Output "OpenSSH SHA256 verification failed. Expected: ${expectedHash}, Actual: ${actualHash}"
51 Stop-Transcript
52 exit 1
53}
54
55msiexec /i $installer ADDLOCAL=Server
56[Environment]::SetEnvironmentVariable("Path", [Environment]::GetEnvironmentVariable("Path",[System.EnvironmentVariableTarget]::Machine) + ';' + ${Env:ProgramFiles} + '\OpenSSH', [System.EnvironmentVariableTarget]::Machine)
57Get-Service -Name ssh*
58
59$elapsed = (Get-Date) - $sectionStart
60Write-Output "OpenSSH server installation completed in $($elapsed.TotalSeconds) seconds"
61
62$sectionStart = Get-Date
63# Modify firewall rule
64# Note that Windows server may have a firewall rule for SSH by default, but it doesn't work on my env.
65# So I remove and recreate the rule.
66Remove-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -ErrorAction Ignore
67New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
68
69# Set a public key. Since a user `lima` is in Administrators group,
70# The public key should be located under C:\ProgramData\ssh instead of under C:\Users\lima\.ssh.
71$pubkey = Get-Content -Path F:\ssh_authorized_keys
72$pubkeyLocation = 'C:\ProgramData\ssh\administrators_authorized_keys'
73Add-Content -Force -Path $pubkeyLocation -Value $pubkey
74icacls $pubkeyLocation /inheritance:r
75icacls $pubkeyLocation /grant "SYSTEM:F"
76icacls $pubkeyLocation /grant "Administrators:F"
77
78$elapsed = (Get-Date) - $sectionStart
79Write-Output "SSH setting completed in $($elapsed.TotalSeconds) seconds"
80
81# Install provision runner and set task scheduler.
82$root = Split-Path -Path $PSScriptRoot -Qualifier
83powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "$root\install_provision.ps1" -Root "$root\"
84
85# Execute provision scripts here for the current boot.
86$runnerPath = "C:\ProgramData\Lima\provision\startup_runner.ps1"
87
88# Dependency scripts are only run here, not scheduled.
89powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "$runnerPath" -Mode "dependency"
90# Startup System scripts are run here, and run in startup of a next boot.
91powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "$runnerPath" -Mode "system"
92# Startup User scripts are run here, and run in startup of a next boot.
93powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "$runnerPath" -Mode "user"
94
95# Finally, it creates a marker file. Host agent will check the file.
96$bootDoneDir = 'C:\ProgramData\Lima'
97New-Item -ItemType Directory -Path $bootDoneDir -Force | Out-Null
98$bootDoneFile = Join-Path $bootDoneDir 'lima-boot-done.txt'
99'done' | Out-File -FilePath $bootDoneFile -Encoding ascii -Force -NoNewline
100
101
102$elapsed = (Get-Date) - $globalStart
103Write-Output "All works completed in $($elapsed.TotalSeconds) seconds"
104
105# Finish recording logs
106Stop-Transcript