Repository navigation
Expand file tree
/
Copy pathsetup.ps1
More file actions
547 lines (506 loc) · 30.9 KB
/
Copy pathsetup.ps1
File metadata and controls
547 lines (506 loc) · 30.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
<#
.SYNOPSIS
PhoneBox setup for Windows: installs the latest release, everything it needs, and the phone app.
.DESCRIPTION
Run it from a normal PowerShell window (not "Run as administrator"):
irm https://raw.githubusercontent.com/ersinkoc/phonebox/main/setup.ps1 | iex
With options:
& ([scriptblock]::Create((irm https://raw.githubusercontent.com/ersinkoc/phonebox/main/setup.ps1))) -NoPhone
What it does, asking first before anything outside the PhoneBox folders:
1. Checks this PC (Windows 10/11, x64) and finds the latest PhoneBox release.
2. Downloads the agent and checks its SHA-256 checksum (and its build provenance when the GitHub CLI is signed in).
3. Installs adb (Android platform tools) when it is missing: with winget, or from Google's download.
4. Installs the agent to %LOCALAPPDATA%\PhoneBox\app. Settings in %APPDATA%\PhoneBox are kept.
5. Adds a Start Menu shortcut, optionally starts PhoneBox with Windows, puts the app on a phone connected over USB, starts the agent and opens its settings.
Running it again updates PhoneBox or repairs the installation.
.PARAMETER Yes
Answer every question with its default: for unattended runs.
.PARAMETER NoPhone
Do not install the app on a phone over USB (for example when the phone will use Wi-Fi only).
.PARAMETER Serial
The phone to use when several are connected (see "adb devices").
.PARAMETER NoAutostart
Do not start PhoneBox with Windows.
.PARAMETER NoStart
Install only; do not start the agent.
.PARAMETER Version
Install this release (for example 0.9.8) instead of the latest.
.PARAMETER DryRun
Download and check everything, show what would happen, and change nothing.
.PARAMETER AllowAdministrator
Run even as administrator: only for PCs where every window runs elevated (UAC turned off) and for CI.
#>
param(
[switch]$Yes,
[switch]$NoPhone,
[string]$Serial,
[switch]$NoAutostart,
[switch]$NoStart,
[string]$Version,
[switch]$DryRun,
[switch]$AllowAdministrator
)
$Repo = 'ersinkoc/phonebox'
$Package = 'net.oxog.phonebox.deskpanel'
$SettingsUrl = 'http://127.0.0.1:8765'
function Write-Step([string]$Text) { Write-Host ''; Write-Host "==> $Text" -ForegroundColor Cyan }
function Write-Info([string]$Text) { Write-Host " $Text" }
function Write-Ok([string]$Text) { Write-Host " OK $Text" -ForegroundColor Green }
function Write-Caution([string]$Text) { Write-Host " ! $Text" -ForegroundColor Yellow }
function Test-Interactive {
if ($Yes) { return $false }
try { return [Environment]::UserInteractive -and -not [Console]::IsInputRedirected } catch { return $false }
}
# Asks a yes/no question; with -Yes or without a console, the default answer is taken.
function Read-YesNo([string]$Question, [bool]$Default = $true) {
$hint = if ($Default) { '[Y/n]' } else { '[y/N]' }
if (-not (Test-Interactive)) {
Write-Info "$Question $hint -> $(if ($Default) { 'yes' } else { 'no' }) (default)"
return $Default
}
while ($true) {
$answer = "$(Read-Host " $Question $hint")".Trim().ToLowerInvariant()
if ($answer -eq '') { return $Default }
if ($answer -eq 'y' -or $answer -eq 'yes') { return $true }
if ($answer -eq 'n' -or $answer -eq 'no') { return $false }
}
}
# Runs a native program without letting Windows PowerShell 5.1 turn its stderr lines into errors.
function Invoke-Native([string]$File, [string[]]$Arguments) {
$old = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
$output = & $File @Arguments 2>&1 | ForEach-Object { "$_" }
$code = $LASTEXITCODE
} finally { $ErrorActionPreference = $old }
[pscustomobject]@{ Code = $code; Output = @($output) }
}
function Get-Download([string]$Url, [string]$Path) {
Invoke-WebRequest -UseBasicParsing -Uri $Url -OutFile $Path -Headers @{ 'User-Agent' = 'phonebox-setup' }
}
function Get-LatestVersion {
try {
$release = Invoke-RestMethod -UseBasicParsing -Uri "https://api.github.com/repos/$Repo/releases/latest" `
-Headers @{ 'User-Agent' = 'phonebox-setup'; 'Accept' = 'application/vnd.github+json' } -TimeoutSec 30
if ($release.tag_name) { return [string]$release.tag_name }
} catch { }
# The API allows 60 requests an hour per address; the release page redirects to the newest tag as well.
$request = [System.Net.HttpWebRequest]::Create("https://github.com/$Repo/releases/latest")
$request.AllowAutoRedirect = $false
$request.Method = 'HEAD'
$request.UserAgent = 'phonebox-setup'
$response = $request.GetResponse()
try { $location = [string]$response.Headers['Location'] } finally { $response.Close() }
if ($location -match '/tag/([^/?#]+)$') { return $Matches[1] }
throw 'Could not find the latest release on GitHub. Check the internet connection, or pass -Version (for example -Version 0.9.8).'
}
function Assert-Checksum([string]$File, [string]$SumsFile) {
$name = Split-Path $File -Leaf
$pattern = '^[0-9a-fA-F]{64}\s+\*?' + [regex]::Escape($name) + '\s*$'
$line = Get-Content -LiteralPath $SumsFile | Where-Object { $_ -match $pattern } | Select-Object -First 1
if (-not $line) { throw "$name is not listed in the release checksums; the download was not used." }
$expected = [regex]::Match($line, '^[0-9a-fA-F]{64}').Value
$actual = (Get-FileHash -LiteralPath $File -Algorithm SHA256).Hash
if ($actual -ne $expected) {
throw "$name does not match the release checksum (expected $expected, got $actual). The download was damaged or changed on the way; nothing was installed. Run setup again."
}
Write-Ok "$name matches the release checksum"
}
function Test-Provenance([string]$File) {
$gh = Get-Command gh -ErrorAction SilentlyContinue | Select-Object -First 1
if (-not $gh -or (Invoke-Native $gh.Source @('auth', 'status')).Code -ne 0) {
Write-Info "Build provenance not checked (needs the GitHub CLI, signed in): gh attestation verify $(Split-Path $File -Leaf) --repo $Repo"
return
}
# gh 2.92.0 and earlier send the GitHub token to the TUF mirrors during verification (GHSA-8xvp-7hj6-mcj9)
$ghVersion = [regex]::Match("$((Invoke-Native $gh.Source @('--version')).Output)", 'gh version (\d+\.\d+\.\d+)').Groups[1].Value
if (-not $ghVersion -or [version]$ghVersion -lt [version]'2.93.0') {
Write-Caution "Build provenance not checked: GitHub CLI $(if ($ghVersion) { $ghVersion } else { '(unknown version)' }) has a security problem in"
Write-Caution '"gh attestation" (it sends your GitHub token to another server). Update it to 2.93.0 or later.'
return
}
$result = Invoke-Native $gh.Source @('attestation', 'verify', $File, '--repo', $Repo)
if ($result.Code -eq 0) { Write-Ok "$(Split-Path $File -Leaf) was built by this repository's release workflow (GitHub attestation)" }
elseif (($result.Output -join ' ') -match 'unknown command') { Write-Info 'Build provenance not checked: this GitHub CLI is too old for "gh attestation".' }
else { throw "The build provenance of $(Split-Path $File -Leaf) could not be verified; nothing was installed.`n$($result.Output -join "`n")" }
}
function Find-Adb {
$candidates = @()
$command = Get-Command adb.exe -ErrorAction SilentlyContinue | Select-Object -First 1
if ($command) { $candidates += $command.Source }
$candidates += Join-Path $env:LOCALAPPDATA 'Android\Sdk\platform-tools\adb.exe'
$winget = Join-Path $env:LOCALAPPDATA 'Microsoft\WinGet\Packages'
if (Test-Path $winget) {
$candidates += Get-ChildItem $winget -Directory -Filter 'Google.PlatformTools*' -ErrorAction SilentlyContinue |
ForEach-Object { Join-Path $_.FullName 'platform-tools\adb.exe' }
}
$candidates | Where-Object { $_ -and (Test-Path $_) } | Select-Object -First 1
}
function Install-Adb([string]$Temp) {
Write-Info 'adb (Android SDK Platform-Tools) is Google software under the Android SDK terms:'
Write-Info 'https://developer.android.com/studio/releases/platform-tools'
$winget = Get-Command winget -ErrorAction SilentlyContinue | Select-Object -First 1
if ($winget) {
Write-Info 'Installing it with winget (Google.PlatformTools) ...'
$result = Invoke-Native $winget.Source @('install', '--id', 'Google.PlatformTools', '--exact', '--source', 'winget',
'--accept-package-agreements', '--accept-source-agreements')
$adb = Find-Adb
if ($adb) { return $adb }
Write-Caution "winget could not install it (exit code $($result.Code)); using Google's download instead."
}
$zip = Join-Path $Temp 'platform-tools.zip'
Get-Download 'https://dl.google.com/android/repository/platform-tools-latest-windows.zip' $zip
$sdk = Join-Path $env:LOCALAPPDATA 'Android\Sdk'
New-Item -ItemType Directory -Force $sdk | Out-Null
Expand-Archive -LiteralPath $zip -DestinationPath $sdk -Force
Find-Adb
}
function Stop-PhoneBox {
$running = @(Get-Process PhoneBoxAgent, DeskAgent -ErrorAction SilentlyContinue)
foreach ($process in $running) {
try { $process.Kill(); [void]$process.WaitForExit(10000) } catch { }
}
if ($running.Count) { Write-Info "Stopped the running agent ($($running.Count) process(es))" }
}
# Copies the new release over the installed one; Windows can hold a just-stopped program's files for a moment.
function Copy-Release([string]$From, [string]$To) {
for ($attempt = 1; ; $attempt++) {
try { Copy-Item (Join-Path $From '*') $To -Recurse -Force -ErrorAction Stop; return }
catch {
if ($attempt -ge 20) { throw }
Start-Sleep -Milliseconds 500
}
}
}
function Get-PortOwner([int]$Port) {
try {
$listener = Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction Stop | Select-Object -First 1
if ($listener) { return (Get-Process -Id $listener.OwningProcess -ErrorAction SilentlyContinue).ProcessName }
} catch { }
$null
}
function Get-Devices([string]$Adb) {
foreach ($line in (Invoke-Native $Adb @('devices')).Output) {
# "<serial><TAB><state>"; the header and adb's "daemon started" lines have no tab
if ($line -match "^(\S+)`t(.+)$") { [pscustomobject]@{ Serial = $Matches[1]; State = $Matches[2].Trim() } }
}
}
function Show-PhoneHelp {
Write-Info 'On the phone (Android 5.0 or later):'
Write-Info ' 1. Settings > About phone: tap "Build number" seven times to turn on Developer options.'
Write-Info ' 2. Settings > System > Developer options: turn on "USB debugging".'
Write-Info ' Xiaomi / Redmi / POCO: also turn on "Install via USB" and "USB debugging (Security settings)".'
Write-Info ' 3. Connect it with a data cable (some cables only charge) and unlock the screen.'
Write-Info ' 4. Answer "Allow USB debugging?" with Allow; tick "Always allow from this computer".'
Write-Info 'If the phone never shows up, Windows may need its USB driver: Google phones use the Google USB Driver,'
Write-Info 'other brands have one on their support site (Device Manager shows the phone with a warning sign).'
}
# Waits for exactly one authorised phone; returns its serial, or $null to skip.
function Select-Phone([string]$Adb) {
[void](Invoke-Native $Adb @('start-server'))
$wanted = $Serial
while ($true) {
$devices = @(Get-Devices $Adb)
if ($wanted) { $devices = @($devices | Where-Object { $_.Serial -eq $wanted }) }
# only phones on a cable are picked automatically, not emulators or wireless-debugging connections
$ready = @($devices | Where-Object { $_.State -eq 'device' -and ($wanted -or ($_.Serial -notmatch '^emulator-|:|\._tcp')) })
if ($ready.Count -eq 1) { return $ready[0].Serial }
if ($ready.Count -gt 1) {
Write-Caution "Several phones are connected: $(($ready | ForEach-Object Serial) -join ', ')"
if (-not (Test-Interactive)) { Write-Caution 'Run setup again with -Serial <id> to pick one; skipping the phone app.'; return $null }
$choice = "$(Read-Host ' Type the serial of the phone to use (empty to skip)')".Trim()
if (-not $choice) { return $null }
$wanted = $choice
continue
}
foreach ($device in $devices) {
if ($device.State -eq 'unauthorized') { Write-Caution "$($device.Serial): waiting for you to allow USB debugging on the phone (unlock it and tap Allow)." }
elseif ($device.State -eq 'offline') { Write-Caution "$($device.Serial): offline. Unplug it and plug it in again." }
elseif ($device.State -eq 'device') { Write-Caution "$($device.Serial): an emulator or a wireless connection, not used unless you pass -Serial $($device.Serial)." }
else { Write-Caution "$($device.Serial): $($device.State)" }
}
if (-not $devices.Count) {
if ($wanted) { Write-Caution "No phone with the serial $wanted is connected." } else { Write-Caution 'No phone is connected over USB yet.' }
Show-PhoneHelp
}
if (-not (Test-Interactive)) { Write-Caution 'Skipping the phone app (no phone ready).'; return $null }
$answer = "$(Read-Host ' Press Enter to check again, or type S to skip the phone')".Trim().ToLowerInvariant()
if ($answer -eq 's' -or $answer -eq 'skip') { return $null }
[void](Invoke-Native $Adb @('reconnect', 'offline'))
}
}
function Install-PhoneApp([string]$Adb, [string]$Apk, [string]$Device) {
$sdkLevel = 0
[void][int]::TryParse(("$((Invoke-Native $Adb @('-s', $Device, 'shell', 'getprop', 'ro.build.version.sdk')).Output)").Trim(), [ref]$sdkLevel)
if ($sdkLevel -gt 0 -and $sdkLevel -lt 21) {
Write-Caution "This phone runs Android API $sdkLevel; PhoneBox needs Android 5.0 (API 21) or later. The app was not installed."
return $false
}
Write-Info "Installing $(Split-Path $Apk -Leaf) on $Device (the phone may ask you to confirm) ..."
$result = Invoke-Native $Adb @('-s', $Device, 'install', '-r', $Apk)
$text = $result.Output -join ' '
if ($text -match 'INSTALL_FAILED_UPDATE_INCOMPATIBLE') {
Write-Caution 'The phone has a PhoneBox app signed with a different key (for example one you built yourself).'
Write-Caution 'It was left as it is, with its data. To switch to the release app, uninstall PhoneBox on the phone'
Write-Caution '(this deletes its settings) and run setup again.'
return $false
}
if ($text -match 'INSTALL_FAILED_VERSION_DOWNGRADE') { Write-Ok 'The phone already has a newer PhoneBox app; it was kept.'; return $true }
if ($text -match 'INSTALL_FAILED_USER_RESTRICTED|INSTALL_FAILED_ABORTED') {
Write-Caution 'The phone refused the installation. Confirm it on the phone; Xiaomi / Redmi / POCO phones also need'
Write-Caution '"Install via USB" in Developer options. Then run setup again.'
return $false
}
if ($result.Code -ne 0 -or $text -notmatch 'Success') {
Write-Caution "The app could not be installed: $text"
return $false
}
Write-Ok 'PhoneBox app installed on the phone'
[void](Invoke-Native $Adb @('-s', $Device, 'shell', 'am', 'start', '-n', "$Package/.MainActivity"))
$true
}
function Install-PhoneBox {
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue' # the progress bar makes downloads in Windows PowerShell 5.1 many times slower
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
Write-Host ''
Write-Host 'PhoneBox setup' -ForegroundColor Cyan
if ($DryRun) { Write-Host 'Dry run: downloads and checks everything, changes nothing.' -ForegroundColor Yellow }
Write-Step 'Checking this PC'
$architecture = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE }
if ($architecture -eq 'ARM64') {
throw "There is no PhoneBox build for Windows on ARM yet: the x64 build would run, but could not update itself. Please ask for it in https://github.com/$Repo/issues."
}
if ($architecture -ne 'AMD64') { throw "PhoneBox needs 64-bit Windows (this PC reports $architecture)." }
$build = [Environment]::OSVersion.Version.Build
if ($build -lt 19041) { Write-Caution "PhoneBox is built for Windows 10 version 2004 (build 19041) or later; this PC has build $build, so some parts may not work." }
$identity = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
if ($identity.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) -and -not $AllowAdministrator) {
throw 'Run setup from a normal PowerShell window, not "Run as administrator". PhoneBox runs as you and needs no administrator rights; as administrator it would start with full rights, or install for the administrator account instead of yours.'
}
$scoop = if ($env:SCOOP) { $env:SCOOP } else { Join-Path $HOME 'scoop' }
if (Test-Path (Join-Path $scoop 'apps\phonebox')) {
throw 'PhoneBox is installed with Scoop. Update it with "scoop update phonebox". To switch to this setup instead, run "scoop uninstall phonebox" first (your settings stay) and then setup again; two copies would fight over the same port.'
}
$app = Join-Path $env:LOCALAPPDATA 'PhoneBox\app'
$exe = Join-Path $app 'PhoneBoxAgent.exe'
$installed = $null
if (Test-Path $exe) { $installed = ("$((Get-Item $exe).VersionInfo.ProductVersion)" -split '\+')[0] }
Write-Ok "Windows build $build, x64$(if ($installed) { ", PhoneBox $installed installed" })"
# Smart App Control (Windows 11) blocks programs that are neither signed nor known to Microsoft: 1 on, 2 evaluation
$smartAppControl = 0
try { $smartAppControl = [int](Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\CI\Policy' -ErrorAction Stop).VerifiedAndReputablePolicyState } catch { }
if ($smartAppControl -eq 1) {
Write-Caution 'Smart App Control is on. It blocks programs that are neither signed nor known to Microsoft, and'
Write-Caution 'PhoneBoxAgent.exe is not code-signed yet (signing through SignPath Foundation is pending:'
Write-Caution "https://github.com/$Repo/issues/56), so Windows will probably refuse to start it. Setup still installs it."
Write-Caution 'Please keep Smart App Control on: on older Windows builds it cannot be turned back on without resetting Windows.'
} elseif ($smartAppControl -eq 2) {
Write-Caution 'Smart App Control is in evaluation mode: it blocks nothing yet. If Windows turns it on later, it will block'
Write-Caution "PhoneBoxAgent.exe until the agent is code-signed (pending: https://github.com/$Repo/issues/56)."
}
Write-Step 'Finding the release'
$tag = if ($Version) { $Version } else { Get-LatestVersion }
$ver = $tag.Trim().TrimStart('v')
if ($ver -notmatch '^\d+\.\d+\.\d+$') { throw "Unexpected version '$tag'." }
$base = "https://github.com/$Repo/releases/download/v$ver"
if ($installed -eq $ver) { Write-Ok "PhoneBox $ver (already installed; it will be reinstalled, settings stay)" }
elseif ($installed) { Write-Ok "PhoneBox $ver (updating from $installed; settings stay)" }
else { Write-Ok "PhoneBox $ver" }
$temp = Join-Path ([IO.Path]::GetTempPath()) ('phonebox-setup-' + [guid]::NewGuid().ToString('N').Substring(0, 8))
New-Item -ItemType Directory -Force $temp | Out-Null
try {
Write-Step "Downloading PhoneBox $ver"
$zipName = "PhoneBoxAgent-$ver-win-x64.zip"
$sums = Join-Path $temp "SHA256SUMS-$ver.txt"
$zip = Join-Path $temp $zipName
try { Get-Download "$base/SHA256SUMS-$ver.txt" $sums } catch { throw "Release $ver was not found ($base). $($_.Exception.Message)" }
Write-Info "$zipName (about 65 MB) ..."
Get-Download "$base/$zipName" $zip
Assert-Checksum $zip $sums
Test-Provenance $zip
Write-Info 'The agent is not code-signed yet (the SignPath Foundation application is pending). Windows SmartScreen'
Write-Info 'or an antivirus can warn about new unsigned programs; the checksum above shows that this is the file'
Write-Info "GitHub built from the public source. Details: https://github.com/$Repo/issues/69"
$extract = Join-Path $temp 'agent'
Expand-Archive -LiteralPath $zip -DestinationPath $extract -Force
Get-ChildItem $extract -Recurse -File | Unblock-File
if (-not (Test-Path (Join-Path $extract 'PhoneBoxAgent.exe'))) {
throw "PhoneBoxAgent.exe is missing from the download: an antivirus probably removed it. See https://github.com/$Repo/issues/69 and restore it from quarantine, or add an exclusion, then run setup again."
}
Write-Step 'Checking adb (needed for the USB connection to the phone)'
$adb = Find-Adb
if ($adb) { Write-Ok $adb }
else {
Write-Caution 'adb is not installed. Without it PhoneBox still works over Wi-Fi, but cannot reach the phone over USB'
Write-Caution '(wake it on plug-in, install and update the phone app, fall back to USB when Wi-Fi drops).'
if ($DryRun) { Write-Info 'Would offer to install it.' }
elseif (Read-YesNo 'Install adb now?') {
$adb = Install-Adb $temp
if ($adb) { Write-Ok "adb installed: $adb" } else { Write-Caution 'adb could not be installed; PhoneBox will work over Wi-Fi only until it is.' }
}
}
$owner = Get-PortOwner 8765
if ($owner -and $owner -ne 'PhoneBoxAgent' -and $owner -ne 'DeskAgent') {
Write-Caution "Port 8765 is used by $owner. PhoneBox needs this port; close $owner or PhoneBox will not start."
}
if ($DryRun) {
Write-Step 'Dry run finished'
Write-Info "Would stop the running agent and install PhoneBox $ver to $app (settings in $env:APPDATA\PhoneBox stay)."
Write-Info 'Would add a PhoneBox shortcut to the Start Menu.'
if (-not $NoAutostart) { Write-Info 'Would ask to start PhoneBox with Windows.' }
if (-not $NoPhone) { Write-Info 'Would offer to install the app on a phone connected over USB.' }
Write-Info 'Nothing was changed.'
return
}
Write-Step "Installing to $app"
Stop-PhoneBox
# The files are replaced in place: the folder itself cannot be renamed while another program has it as its
# working directory, such as the adb server the agent started.
$old = "$app.old"
Remove-Item $old -Recurse -Force -ErrorAction SilentlyContinue
if (Test-Path $app) { Copy-Item $app $old -Recurse -Force } # to put back if the copy fails
try {
New-Item -ItemType Directory -Force $app | Out-Null
Copy-Release $extract $app
# files the new version no longer has, for example the DLLs of a framework-dependent (source) install
$keep = @{}
Get-ChildItem $extract -Recurse -File | ForEach-Object { $keep[$_.FullName.Substring($extract.Length)] = $true }
Get-ChildItem $app -Recurse -File | Where-Object { -not $keep[$_.FullName.Substring($app.Length)] } |
Remove-Item -Force -ErrorAction SilentlyContinue
} catch {
if (Test-Path $old) { try { Copy-Item (Join-Path $old '*') $app -Recurse -Force } catch { } }
throw "Installing failed and the previous version was put back: $($_.Exception.Message). Close PhoneBox (tray icon > Exit) and run setup again."
}
Remove-Item $old -Recurse -Force -ErrorAction SilentlyContinue
Write-Ok "PhoneBox $ver installed; settings are in $env:APPDATA\PhoneBox"
$programs = [Environment]::GetFolderPath('Programs')
if ($programs -and (Test-Path $programs)) {
try {
$shell = New-Object -ComObject WScript.Shell
$startLink = $shell.CreateShortcut((Join-Path $programs 'PhoneBox.lnk'))
$startLink.TargetPath = $exe
$startLink.WorkingDirectory = $app
$startLink.Description = 'PhoneBox agent'
$startLink.Save()
Write-Ok 'Added to the Start Menu (search "PhoneBox" to start it anytime)'
} catch {
Write-Caution "Could not create Start Menu shortcut: $($_.Exception.Message)"
}
}
$startup = [Environment]::GetFolderPath('Startup')
Remove-Item (Join-Path $startup 'DeskAgent.lnk') -ErrorAction SilentlyContinue # the pre-PhoneBox prototype
$shortcut = Join-Path $startup 'PhoneBox.lnk'
if ($NoAutostart) {
if (Test-Path $shortcut) {
Remove-Item $shortcut -Force
Write-Info 'Removed the Start-with-Windows shortcut (-NoAutostart).'
}
} else {
Write-Step 'Start with Windows'
if (Read-YesNo 'Start PhoneBox when you sign in to Windows?') {
$shell = New-Object -ComObject WScript.Shell
$link = $shell.CreateShortcut($shortcut)
$link.TargetPath = $exe
$link.WorkingDirectory = $app
$link.Description = 'PhoneBox agent'
$link.Save()
Write-Ok 'Added to the Startup folder (turn it off in Task Manager > Startup apps)'
} elseif (Test-Path $shortcut) {
Remove-Item $shortcut -Force
Write-Info 'Removed the existing Start-with-Windows shortcut.'
}
}
$phoneDone = $false
if (-not $NoPhone) {
Write-Step 'Phone app'
if (-not $adb) {
Write-Caution 'Skipped: adb is not installed.'
} elseif (Read-YesNo 'Install the PhoneBox app on a phone connected over USB now?') {
Write-Info 'USB debugging lets an allowed computer control the phone. Allow only your own computers; you can'
Write-Info 'revoke them under Developer options > "Revoke USB debugging authorisations".'
$device = Select-Phone $adb
if ($device) {
# The phone app is optional: a failed download or checksum skips it, and the agent (already
# installed and stopped for the update) still starts below.
try {
$apk = Join-Path $temp "PhoneBox-$ver.apk"
Get-Download "$base/PhoneBox-$ver.apk" $apk
Assert-Checksum $apk $sums
$phoneDone = Install-PhoneApp $adb $apk $device
} catch {
Write-Caution "Phone app skipped: $($_.Exception.Message)"
}
}
}
}
if ($NoStart) {
Write-Step 'Done (not started)'
Write-Info "Start it with $exe"
} else {
Write-Step 'Starting PhoneBox'
$blocked = $false
try {
Start-Process -FilePath $exe -WorkingDirectory $app -ErrorAction Stop
} catch {
# Smart App Control and other App Control / AppLocker policies refuse the file (Win32 errors 4551 and 1260).
# Start-Process keeps only the message, in the system language, so compare it with the system's own text.
$why = "$($_.Exception.Message)"
$says = { param($code) $text = ([ComponentModel.Win32Exception]::new($code).Message).Trim().TrimEnd('.'); $text -and $why.Contains($text) }
$appControl = & $says 4551 # App Control for Business, which Smart App Control uses
$groupPolicy = & $says 1260 # AppLocker or a software restriction policy
if (-not ($appControl -or $groupPolicy)) { throw }
$blocked = $true
Write-Caution "Windows did not let PhoneBoxAgent.exe start: $why"
if ($appControl -and $smartAppControl -eq 1) {
Write-Caution 'PhoneBox is installed, but Smart App Control will not run it until the agent is code-signed.'
Write-Caution "Signing is pending: https://github.com/$Repo/issues/56. Please keep Smart App Control on."
} else {
Write-Caution 'PhoneBox is installed, but a policy on this computer does not allow it to run. On a work or school'
Write-Caution 'computer, ask its administrator to allow PhoneBoxAgent.exe.'
}
}
$up = $false
for ($i = 0; $i -lt 30 -and -not $up -and -not $blocked; $i++) {
Start-Sleep -Milliseconds 700
try { Invoke-RestMethod -UseBasicParsing "$SettingsUrl/api/info" -TimeoutSec 2 | Out-Null; $up = $true } catch { }
}
if ($up) {
Write-Ok "PhoneBox is running: settings at $SettingsUrl (also from the tray icon)"
try { Start-Process $SettingsUrl } catch { }
} elseif ($blocked) {
# explained above
} elseif (-not (Test-Path $exe)) {
Write-Caution "PhoneBoxAgent.exe was removed, probably by an antivirus. See https://github.com/$Repo/issues/69."
} else {
Write-Caution "The agent did not answer. Its log: $env:LOCALAPPDATA\PhoneBox\agent.log"
if ($smartAppControl -eq 1) {
Write-Caution 'Smart App Control may have blocked it: see Windows Security > App & browser control, and the warning above.'
}
}
}
Write-Step 'Good to know'
if ($phoneDone) { Write-Info 'Keep the phone plugged in: PhoneBox wakes it and opens the panel.' }
elseif (-not $NoPhone) {
Write-Info "Phone app later: plug the phone in and use Settings > General > Install the phone app, or run setup again."
}
Write-Info 'Wi-Fi only: install the app from the release page on the phone, then on the computer open'
Write-Info 'Settings > General > Wi-Fi & Tailscale > Pair a phone over Wi-Fi and scan the QR code.'
Write-Info 'When you turn Wi-Fi on, Windows asks to let PhoneBox through the firewall: allow Private networks only.'
Write-Info "Until then PhoneBox only listens on this computer. Every connection it makes: https://github.com/$Repo/blob/main/PRIVACY.md"
Write-Info 'Updates: PhoneBox updates itself from Settings. Run setup again any time to repair it.'
Write-Info "Uninstall: & ([scriptblock]::Create((irm https://raw.githubusercontent.com/$Repo/main/scripts/uninstall.ps1))) [-Purge] [-Apk]"
} finally {
Remove-Item $temp -Recurse -Force -ErrorAction SilentlyContinue
}
}
# Everything runs from here, so a download cut off half way cannot run half a script.
try {
Install-PhoneBox
$global:LASTEXITCODE = 0 # optional native checks (gh, winget) may have left a non-zero code; setup succeeded
} catch {
Write-Host ''
Write-Host "Setup stopped: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "Help: https://github.com/$Repo#quick-start - issues: https://github.com/$Repo/issues" -ForegroundColor Red
if (-not (Test-Interactive)) { throw } # unattended runs must fail visibly
}