-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScoopUpdateWrapper.ps1
434 lines (373 loc) · 15.8 KB
/
ScoopUpdateWrapper.ps1
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
# ScoopUpdateWrapper.ps1
<#
.SYNOPSIS
Scoop Update Wrapper
.DESCRIPTION
This PowerShell script updates scoop apps while updating the firewall rules correspondingly for you.
.LINK
https://github.com/osmiumsilver/ScoopUpdateWrapper
.NOTES
Author: osmiumsilver | License: GNU GPLv3
Version: Beta 0.2.0
#>
#Requires -Version 5.1
using namespace System.Security.Principal
param(
[Parameter()]
[Alias("M")]
[switch]$ManualMode,
[Parameter()]
[Alias("S")]
[switch]$SkipScoopUpdate,
[Parameter()]
[Alias("V")]
[switch]$VerboseMode
)
if ($VerboseMode) {
$DebugPreference = 'Continue'
}
# Scoop App Class
class ScoopApp {
[string]$Name
[string[]]$UserVersions
[string[]]$GlobalVersions
[string]$CurrentUserVersion
[string]$CurrentGlobalVersion
[bool]$IsUserInstalled
[bool]$IsGlobalInstalled
[System.Collections.Generic.List[FirewallRule]]$FirewallRules
ScoopApp([string]$name) {
$this.Name = $name
Write-Debug "Creating new ScoopApp instance for: $($this.Name)"
$this.FirewallRules = [System.Collections.Generic.List[FirewallRule]]::new()
}
[string[]] GetAllVersions() {
Write-Debug "Getting all versions for $($this.Name)"
$allVersions = @()
if ($this.IsUserInstalled) {
Write-Debug "User versions: $($this.UserVersions -join ', ')"
$allVersions += $this.UserVersions }
if ($this.IsGlobalInstalled) {
Write-Debug "Global versions: $($this.GlobalVersions -join ', ')"
$allVersions += $this.GlobalVersions }
$result = $allVersions | Select-Object -Unique | Sort-Object
Write-Debug "Final versions list: $($result -join ', ')"
return $result
}
}
# Firewall Rule Class
class FirewallRule {
[string]$InstanceID
[string]$Program
[string]$Action
[string]$Direction
[string]$RemoteAddress
[string]$Version # Versions
[string]$DisplayName
FirewallRule([string]$instanceID, [string]$program) {
$this.InstanceID = $instanceID
$this.Program = $program
}
[FirewallRule] Clone() {
$newRule = [FirewallRule]::new($this.InstanceID, $this.Program)
Write-Debug "Creating new firewall instance for: $($this.InstanceID)"
$newRule.Action = $this.Action
$newRule.Direction = $this.Direction
$newRule.RemoteAddress = $this.RemoteAddress
$newRule.Version = $this.Version
$newRule.DisplayName = $this.DisplayName
Write-Debug $newRule
return $newRule
}
}
class FirewallManager {
static [void] LoadFirewallRules([ScoopApp]$app) {
Write-Debug "Loading existing firewall rules for app: $($app.Name)"
$app.FirewallRules.Clear()
$rules = gsudo {
$appName = $args[0]
Get-NetFirewallApplicationFilter | Where-Object {
$_.Program -like "*$appName*" } | ForEach-Object {
$rule = Get-NetFirewallRule -AssociatedNetFirewallApplicationFilter $_
$addressFilter = ($rule | Get-NetFirewallAddressFilter)
@{
InstanceID = $rule.Name
DisplayName = $rule.DisplayName
Program = $_.Program
Action = $rule.Action
Direction = $rule.Direction
RemoteAddress = $addressFilter.RemoteAddress
}
}
} -args $app.Name
Write-Debug "Found $(($rules | Measure-Object).Count) firewall rules"
foreach ($rule in $rules) {
Write-Debug "Processing rule: $($rule.InstanceID)"
$fwRule = [FirewallRule]::new($rule.InstanceID, $rule.Program)
$fwRule.Action = $rule.Action
$fwRule.Direction = $rule.Direction
$fwRule.RemoteAddress = $rule.RemoteAddress
$fwRule.DisplayName = $rule.DisplayName
if ($rule.Program -match "\\$($app.Name)\\([\d.]+)\\") {
$fwRule.Version = $matches[1]
Write-Debug " Extracted version from path: $($fwRule.Version)"
}
$app.FirewallRules.Add($fwRule)
}
}
static [void] UpdateFirewallRules([ScoopApp]$app, [string]$newVersion) {
Write-Debug "Starting the process of updating firewall rules for $($app.Name) to version $newVersion"
[FirewallManager]::LoadFirewallRules($app)
$templateRules = [FirewallManager]::GetTemplateRules($app)
Write-Debug "Found $(($templateRules | Measure-Object).Count) template rules"
if ($templateRules.Count -gt 0) {
Write-Host "Creating new firewall rule for version $newVersion ..." -ForegroundColor Green
foreach ($template in $templateRules) {
[FirewallManager]::CreateRuleFromTemplate($app, $template, $newVersion)
}
}
else {
Write-Host "$($app.Name) does not have a pre-existing firewall rule, so there is nothing to update."
}
}
static [FirewallRule[]] GetTemplateRules([ScoopApp]$app) {
Write-Debug "Getting template rules for $($app.Name)"
$templates = @()
$versions = $app.GetAllVersions()
foreach ($version in $versions) {
$versionRules = $app.FirewallRules | Where-Object { $_.Version -eq $version }
if ($versionRules) {
Write-Debug "Using version $version as template"
$templates += $versionRules
break
}
}
return $templates
}
static [void] CreateRuleFromTemplate([ScoopApp]$app, [FirewallRule]$template, [string]$newVersion) {
Write-Debug "Creating new rule from template: $($template.InstanceID)"
$newRule = $template.Clone()
Write-Debug "newRule: $($newRule)"
# Update Rule name and version
$newRule.InstanceID = $template.InstanceID -replace $template.Version, $newVersion
$newRule.Program = $template.Program -replace $template.Version, $newVersion
$newRule.Version = $newVersion
Write-Debug "New rule id: $($newRule.InstanceID)"
Write-Debug "New rule program path: $($newRule.Program)"
Write-Debug "New rule program version: $($newRule.Version)"
# Check if the path is existed or not
if (Test-Path $newRule.Program) {
Write-Host "Program path exists, creating firewall rule" -ForegroundColor Green
try {
gsudo {
New-NetFirewallRule `
-DisplayName $args[0] `
-Direction $args[1] `
-Action $args[2] `
-Program $args[3] `
-RemoteAddress $args[4]
} -args @(
$newRule.DisplayName,
$newRule.Direction,
$newRule.Action,
$newRule.Program,
$newRule.RemoteAddress
)
Write-Host "Successfully created rule: $($newRule.DisplayName)" -ForegroundColor Green
}
catch {
Write-Error "Rule creation failed: $_"
}
}
else {
Write-Host "Path doesn't exists, Skipping for $($newRule.Program)"
}
}
static [void] CleanupFirewallRules([ScoopApp]$app) {
[FirewallManager]::LoadFirewallRules($app)
$validVersions = $app.GetAllVersions()
foreach ($rule in $app.FirewallRules) {
if ($rule.Version -and -not ($validVersions -contains $rule.Version)) {
Write-Host "删除无效版本 $($rule.Version) 的防火墙规则: $($rule.InstanceID)" -ForegroundColor Yellow
try {
gsudo { Remove-NetFirewallRule -Name $args[0] } -args $rule.InstanceID
}
catch {
Write-Error "删除防火墙规则失败: $_"
}
}
}
}
}
class PathManager {
static [string]$UserScoopPath
static [string]$GlobalScoopPath
static PathManager() {
[PathManager]::UserScoopPath = Join-Path $env:USERPROFILE "scoop\apps"
[PathManager]::GlobalScoopPath = Join-Path $env:ProgramData "scoop\apps"
}
static [string] GetUserAppPath([string]$appName) {
$path = Join-Path -Path ([PathManager]::UserScoopPath) -ChildPath $appName
Write-Debug "User app path for $($appName): $path"
return $path
}
static [string] GetGlobalAppPath([string]$appName) {
$path = Join-Path -Path ([PathManager]::GlobalScoopPath) -ChildPath $appName
Write-Debug "Global app path for $($appName): $path"
return $path
}
}
class ScoopManager {
static [System.Collections.Generic.List[ScoopApp]] GetAllInstalledApps() {
Write-Debug "Getting all installed apps"
$apps = [System.Collections.Generic.List[ScoopApp]]::new()
$userApps = @()
$globalApps = @()
$userPath = [PathManager]::UserScoopPath
$globalPath = [PathManager]::GlobalScoopPath
Write-Debug "Checking user path: $userPath"
Write-Debug "Checking global path: $globalPath"
if (Test-Path ([PathManager]::UserScoopPath)) {
$userApps = Get-ChildItem -Path ([PathManager]::UserScoopPath) -Directory -Exclude "scoop" | Select-Object -ExpandProperty Name
Write-Debug "Found user apps: $($userApps -join ', ')"
}
if (Test-Path ([PathManager]::GlobalScoopPath)) {
$globalApps = Get-ChildItem -Path ([PathManager]::GlobalScoopPath) -Directory -Exclude "scoop" | Select-Object -ExpandProperty Name
Write-Debug "Found global apps: $($globalApps -join ', ')"
}
$allApps = ($userApps + $globalApps) | Select-Object -Unique
Write-Debug "Total unique apps found: $($allApps -join ', ')"
foreach ($appName in $allApps) {
$apps.Add([ScoopManager]::GetAppInfo($appName))
}
return $apps
}
static [ScoopApp] GetAppInfo([string]$appName) {
Write-Debug "Getting app info for: $appName"
$app = [ScoopApp]::new($appName)
function Check-Installation {
param (
[string]$path,
[string]$scope
)
if (Test-Path $path) {
$app."Is${scope}Installed" = $true
$app."${scope}Versions" = (Get-ChildItem -Path $path -Directory | Where-Object { $_.Name -ne "current" }).Name
Write-Debug "${scope} versions: $($app."${scope}Versions" -join ', ')"
$currentLink = Join-Path $path "current"
if (Test-Path $currentLink) {
$target = (Get-Item $currentLink).Target
if (Test-Path $target) {
$app."Current${scope}Version" = Split-Path $target -Leaf
Write-Debug "Current ${scope} version: $($app."Current${scope}Version")"
} else {
throw "It seems like the current shortcut folder for $appName is broken, skipping this one..."
}
}
}
}
# Check both user and global installations
$userPath = [PathManager]::GetUserAppPath($appName)
Check-Installation -path $userPath -scope "User"
$globalPath = [PathManager]::GetGlobalAppPath($appName)
Check-Installation -path $globalPath -scope "Global"
return $app
}
static [void] UpdateApp([ScoopApp]$app) {
Write-Debug "Starting update for app: $($app.Name)"
if ($app.IsGlobalInstalled) {
Write-Host "Updating globally installed $($app.Name)..." -ForegroundColor Cyan
$GlobalInstallStatus = ""
(gsudo { scoop update -g $args[0] } -args $app.Name) *>&1 | Tee-Object -Variable GlobalInstallStatus
if ($GlobalInstallStatus -match "error|fail") {
throw "Update failure: $GlobalInstallStatus"
}
}
if ($app.IsUserInstalled) {
Write-Host "Updating locally installed $($app.Name)..." -ForegroundColor Cyan
$LocalInstallStatus = ""
(scoop update $app.Name) *>&1 | Tee-Object -Variable LocalInstallStatus
if ($LocalInstallStatus -match "error|fail") {
throw "Update failure: $LocalInstallStatus"
}
}
}
}
class PrivilegeManager {
static [bool] IsAdministrator() {
$identity = [WindowsIdentity]::GetCurrent()
$principal = [WindowsPrincipal]::new($identity)
return $principal.IsInRole([WindowsBuiltInRole]::Administrator)
}
static [void] EnsureNotAdmin() {
if ([PrivilegeManager]::IsAdministrator()) {
Write-Warning "This script should not be run with administrator privileges"
Write-Warning "Please run this script with normal user privileges, and it will automatically request a privilege elevation if needed."
$continue = Read-Host "Continue? (y/N)"
if ($continue -ne "y") {
exit
}
}
}
}
Write-Debug "Script started with parameters: ManualMode=$ManualMode, SkipScoopUpdate=$SkipScoopUpdate, Verbose=$VerboseMode"
if ($ManualMode) {
$appName = "syncthing"
$app = [ScoopManager]::GetAppInfo($appName)
Write-Host "Manual mode engaged: Stored info for $appName"
exit 0
}
[PrivilegeManager]::EnsureNotAdmin()
if (!$SkipScoopUpdate) {
Write-Host "Updating Scoop..." -ForegroundColor Cyan
$result = scoop update *>&1
if ($result -match "error|fail") {
Write-Error "Scoop update failed: $result"
exit 1
}
}
else{
Write-Warning "You seem to be using the -S parameter to skip the scoop manifest update, which may break the script if you haven't used "“scoop update"” to update the app manifest recently, since scoop takes it upon itself to try to automatically update the manifest before updating the app."
}
$status = scoop status -l
Write-Host $status
if (!$status) {
Write-Host "No updates required." -ForegroundColor Green
$choice = Read-Host "Would you like to cleanup old firewall rules? (Y/N)"
if ($choice -eq 'Y') {
# $apps = [ScoopManager]::GetAllInstalledApps()
# foreach ($app in $apps) {
# Write-Host "`nChecking firewall rule for : $($app.Name) ..." -ForegroundColor Cyan
# [FirewallManager]::CleanupFirewallRules($app)
# }
# Write-Host "`nSuccessfully cleaned firewall rules!" -ForegroundColor Green
Write-Host "The feature is still being tested!"
}
return
}
$updatedApps = @()
$failedApps = @()
foreach ($line in $status) {
$appName = $line."Name"
# $oldVersion = $line."Install Version"
$newVersion = $line."Latest Version"
try {
$app = [ScoopManager]::GetAppInfo($appName)
[ScoopManager]::UpdateApp($app)
[FirewallManager]::UpdateFirewallRules($app, [string]$newVersion)
$updatedApps += $appName
}
catch {
Write-Error "Update $appName failed: $_"
$failedApps += $appName
continue
}
}
if ($updatedApps.Count -gt 0) {
Write-Host "`nSuccessfully updated apps:" -ForegroundColor Green
$updatedApps | ForEach-Object { Write-Host "- $_" -ForegroundColor Green }
}
if ($failedApps.Count -gt 0) {
Write-Host "`nFailed:" -ForegroundColor Red
$failedApps | ForEach-Object { Write-Host "- $_" -ForegroundColor Red }
}