-
Notifications
You must be signed in to change notification settings - Fork 3
/
Update.ps1
319 lines (232 loc) · 14.2 KB
/
Update.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
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'Stop'
[System.Uri]$URL1 = 'https://go.microsoft.com/fwlink/?linkid=2084706&Channel=Canary&language=en'
[System.Uri]$URL2 = 'https://c2rsetup.edog.officeapps.live.com/c2r/downloadEdge.aspx?platform=Default&source=EdgeInsiderPage&Channel=Canary&language=en'
[System.String]$EdgeCanaryInstallerPath = "$env:tmp\MicrosoftEdgeSetupCanary.exe"
[System.String]$AppPath = "C:\Users\$env:USERNAME\AppData\Local\Microsoft\Edge SxS\Application"
[System.String]$StringsExe = "$env:tmp\strings64.exe"
Write-Host -Object "Current username is: '$env:USERNAME'"
#Region Downloading-Stuff
Write-Host -Object 'Downloading Edge Canary'
try {
Write-Host -Object 'Trying the primary URL'
$null = Invoke-RestMethod -Uri $URL1 -OutFile $EdgeCanaryInstallerPath
}
catch {
try {
Write-Host -Object 'Downloading from the primary URL failed, trying the secondary URL'
$null = Invoke-RestMethod -Uri $URL2 -OutFile $EdgeCanaryInstallerPath
}
catch {
Write-Host -Object 'Failed to download Edge from both URLs. Exiting...'
exit 1
}
}
Write-Host -Object 'Downloading Strings64.exe'
try {
$null = Invoke-RestMethod -Uri 'https://live.sysinternals.com/strings64.exe' -OutFile $StringsExe
}
catch {
Write-Host -Object 'Failed to download Strings64. Exiting...'
exit 2
}
#EndRegion Downloading-Stuff
Write-Host -Object 'Installing Edge Canary'
Start-Process -FilePath $EdgeCanaryInstallerPath
Write-Host -Object 'Waiting for Edge Canary to be downloaded and installed' -ForegroundColor Green
[System.Boolean]$IsEdgeInstalled = $false
# Checking for completion of the Edge Canary online installer by actively checking for the presence of the dll file that we need, every 5 seconds
# Setting a timer for 60 minutes
[System.TimeSpan]$Timer = New-TimeSpan -Minutes 60
$StopWatch = [System.Diagnostics.Stopwatch]::StartNew()
do {
[System.IO.FileInfo]$File = Get-ChildItem -Path $AppPath -Filter 'msedge.dll' -Recurse -File -ErrorAction SilentlyContinue | Select-Object -First 1
if ($File) {
Write-Host -Object "File found: $($File.FullName)"
$IsEdgeInstalled = $true
break
}
else {
Write-Host -Object 'File not found. Waiting for 5 second...'
Start-Sleep -Seconds 5
}
}
# Breaking out of the loop if the timer expires
while ($StopWatch.Elapsed -lt $Timer)
$StopWatch.Stop()
if (!$IsEdgeInstalled) {
Write-Host -Object 'Edge Canary installation failed. Exiting...'
exit 3
}
Write-Host -Object "Accepting Strings64's EULA via Registry"
# Add the necessary registry key to accept the EULA of the Strings from SysInternals
[System.String]$RegistryPath = 'HKCU:\Software\Sysinternals\Strings'
[System.String]$Name = 'EulaAccepted'
if (Test-Path -Path $RegistryPath) {
Set-ItemProperty -Path $RegistryPath -Name $Name -Value '1'
}
else {
$null = New-Item -Path $RegistryPath -Force
$null = New-ItemProperty -Path $RegistryPath -Name $Name -Value '1' -PropertyType 'DWORD' -Force
}
# Finding the latest version of the Edge Canary from its installation directory's name
Write-Host -Object 'Searching for the Edge Canary version that was just downloaded' -ForegroundColor Yellow
[System.String]$FullVersion = (Get-ChildItem -Path $AppPath -Directory | Where-Object -FilterScript { $_.Name -like '1*' } | Select-Object -First 1).Name
[System.Int32]$MajorVersion = $FullVersion.Split('.')[0]
[System.String]$DllPath = "$AppPath\$FullVersion\msedge.dll"
Write-Host -Object "DLL PATH: $DllPath" -ForegroundColor Yellow
# Expanding the current directory structure that is in GitHub repository to include the new Edge Canary version
# Create the root directory for the Edge Canary if it doesn't exist
if (-NOT (Test-Path -Path '.\Edge Canary')) { $null = New-Item -Path '.\Edge Canary' -ItemType Directory -Force }
# Create the directory for the current Edge Canary's major version if it doesn't exist
if (-NOT (Test-Path -Path ".\Edge Canary\$MajorVersion")) { $null = New-Item -Path ".\Edge Canary\$MajorVersion" -ItemType Directory -Force }
# Create the directory for the current Edge Canary's full version if it doesn't exist or if it exists but is empty
if (-NOT (Test-Path -Path ".\Edge Canary\$MajorVersion\$FullVersion\*")) {
# Creating a new directory for the new available Edge Canary's full version
$null = New-Item -Path ".\Edge Canary\$MajorVersion\$FullVersion" -ItemType Directory -Force
# Check whether the current Edge Canary version is the first release in a new major version. If it is, then some actions will be triggered
if ((Get-ChildItem -Path ".\Edge Canary\$MajorVersion" -Directory).count -eq 1) {
# Loop through each directory of major Edge canary versions and get the directory that belongs to the last previous version
foreach ($CurrentPipelineVersion in ((Get-ChildItem -Path '.\Edge Canary\' -Directory | Where-Object -FilterScript { $_.Name -match '^\d\d\d$' } | Sort-Object -Property Name -Descending).Name | Select-Object -Skip 1)) {
# Make sure the directory is not empty (which is kinda impossible to be empty, but just in case)
if ((Get-ChildItem -Path ".\Edge Canary\$CurrentPipelineVersion" -Directory | Sort-Object -Property Name -Descending | Select-Object -First 1).count -ne 0) {
# Locating the last major version of Edge Canary that was processed prior to this current major version so we can access its directory on repository
[System.String]$PreviousFullVersion = (Get-ChildItem -Path ".\Edge Canary\$CurrentPipelineVersion" -Directory | Sort-Object -Property Name -Descending | Select-Object -First 1).Name
# Get the major version
[System.Int32]$PreviousMajorVersion = $PreviousFullVersion.Split('.')[0]
# if the directory is found, stop the loop
break
}
else {
# if the directory that belongs to the previous major Edge canary version is completely empty, then skip the currently processing major version entirely and look for the one before it
continue
}
}
}
# if the current Edge canary version is not the first release in a major version
else {
[System.String]$PreviousFullVersion = (Get-ChildItem -Path ".\Edge Canary\$MajorVersion" -Directory | Sort-Object -Descending | Select-Object -Skip 1 -First 1).Name
[System.Int64]$PreviousMajorVersion = $PreviousFullVersion.Split('.')[0]
}
Write-Host -Object "Comparing version: $FullVersion with version: $PreviousFullVersion" -ForegroundColor Cyan
Write-Host -Object 'Strings64 Running...'
# Storing the output of the Strings64 in an array of strings
$CurrentOriginalFeatures = [System.Collections.Generic.HashSet[System.String]]@(foreach ($Item in (& $StringsExe $DllPath | Select-String -Pattern '^(ms[a-zA-Z0-9]{4,})$')) {
$Item.Matches.Groups[0].Value
})
# Outputting the object containing the final original results to a file
$CurrentOriginalFeatures | Out-File -FilePath ".\Edge Canary\$MajorVersion\$FullVersion\original.txt" -Force
Write-Host -Object "Saved: .\Edge Canary\$MajorVersion\$FullVersion\original.txt ($($CurrentOriginalFeatures.count) entries)"
$PreviousOriginalFeatures = [System.Collections.Generic.HashSet[System.String]]@(Get-Content -Path ".\Edge Canary\$PreviousMajorVersion\$PreviousFullVersion\original.txt")
[System.String[]]$Added = $CurrentOriginalFeatures | Where-Object -FilterScript { !$PreviousOriginalFeatures.Contains($_) }
$Added | Out-File -FilePath ".\Edge Canary\$MajorVersion\$FullVersion\added.txt" -Force
[System.String[]]$Removed = $PreviousOriginalFeatures | Where-Object -FilterScript { !$CurrentOriginalFeatures.Contains($_) }
$Removed | Out-File -FilePath ".\Edge Canary\$MajorVersion\$FullVersion\removed.txt" -Force
Write-Host -Object "Added features .\Edge Canary\$MajorVersion\$FullVersion\added.txt ($($Added.count) entries)"
Write-Host -Object "Removed features .\Edge Canary\$MajorVersion\$FullVersion\removed.txt ($($Removed.count) entries)"
# Storing the latest version in a file
$FullVersion | Out-File -FilePath .\last.txt -Force
#region ReadMe-Updater
[System.String]$DetailsToReplace = @"
`n### <a href="https://github.com/SpyNetGirl/MSEdgeFeatures"><img width="35" src="https://github.com/HotCakeX/Harden-Windows-Security/raw/main/images/WebP/Edge%20Canary.webp"></a> Latest Edge Canary version: $FullVersion`n
### Last processed at: $(Get-Date -AsUTC) (UTC+00:00)`n
<details>
<summary>$($Added.count) new features were added in the latest Edge Canary update</summary>
<br>
$($Added | ForEach-Object -Process {"* $_`n"})
</details>`n
"@
# Showing extra details on the Readme page of the repository about the latest Edge Canary version
[System.String]$Readme = Get-Content -Raw -Path 'README.md' -Force
$Readme = $Readme -replace '(?s)(?<=<!-- Edge-Canary-Version:START -->).*(?=<!-- Edge-Canary-Version:END -->)', $DetailsToReplace
Set-Content -Path 'README.md' -Value $Readme.TrimEnd() -Force
#endregion ReadMe-Updater
#region GitHub-Committing
# Committing the changes back to the repository
git config --global user.email '[email protected]'
git config --global user.name 'HotCakeX'
git add --all
git commit -m 'Automated Update'
git push
#endregion GitHub-Committing
#region Edge-Canary-ShortCut-Maker-Code
# Putting the Edge canary shortcut maker's code in the EdgeCanaryShortcutMaker.ps1 file
$null = New-Item -Path '.\EdgeCanaryShortcutMaker.ps1' -Force
[System.String[]]$AddedArray = $Added -join ','
$PreArguments = "--enable-features=$AddedArray"
$PreArguments = $PreArguments.TrimEnd(',')
# Content to add the PowerShell script that creates the bat script that launches Edge with new features
[System.String]$ContentToAdd = @"
`$FullVersionToUse = `"$FullVersion`"
`$Arguments = `"$PreArguments`"
`$content = @`"
powershell.exe -WindowStyle hidden -Command "```$UserSID = [System.Security.Principal.WindowsIdentity]::GetCurrent().user.value;```$UserName = (Get-LocalUser | where-object -FilterScript {```$_.SID -eq ```$UserSID}).name;Get-Process | where-object -FilterScript {```$_.path -eq "\"C:\Users\```$UserName\AppData\Local\Microsoft\Edge SxS\Application\msedge.exe\""} | ForEach-Object -Process {Stop-Process -Id ```$_.id -Force -ErrorAction SilentlyContinue};& \"C:\Users\```$UserName\AppData\Local\Microsoft\Edge SxS\Application\msedge.exe\" `$Arguments"
`"@
`$content | Out-File -FilePath "C:\Users\`$env:USERNAME\Downloads\EDGECAN Launcher `$FullVersionToUse.bat"
"@
Set-Content -Value $ContentToAdd -Path '.\EdgeCanaryShortcutMaker.ps1' -Force
#endregion Edge-Canary-ShortCut-Maker-Code
#region GitHub-Release-Publishing
# Publishing the Release without Body of the Release (i.e. text)
# Doing this first so that people watching the repository will see this in the Emails that they get
# But won't see the PowerShell link because it will be added to the Release body using the Patch method, after this
# Without this, the email would have empty content for the body of the Release
[System.String]$GitHubReleaseBodyContent = @"
# <img width="35" src="https://github.com/HotCakeX/Harden-Windows-Security/raw/main/images/WebP/Edge%20Canary.webp"> Automated update
## Processed at: $(Get-Date -AsUTC) (UTC+00:00)`n
Visit the GitHub's release section for full details on how to use it:
https://github.com/SpyNetGirl/MSEdgeFeatures/releases/tag/$FullVersion
### $($Added.count) New features were added
$($Added | ForEach-Object -Process {"* $_`n"})
<br>
### $($Removed.count) Features were removed
$($Removed | ForEach-Object -Process {"* $_`n"})
<br>
"@
# Get the latest commit SHA
$LATEST_SHA = git rev-parse HEAD
# Create a release with the latest commit as tag and target
$RELEASE_RESPONSE = Invoke-RestMethod -Uri 'https://api.github.com/repos/SpyNetGirl/MSEdgeFeatures/releases' `
-Method POST `
-Headers @{Authorization = "token $env:GITHUB_TOKEN" } `
-Body (
@{
tag_name = "$FullVersion"
target_commitish = $LATEST_SHA
name = "Edge Canary version $FullVersion"
body = "$GitHubReleaseBodyContent"
draft = $false
prerelease = $false
} | ConvertTo-Json
)
# Use the gh CLI command to upload the EdgeCanaryShortcutMaker.ps1 file to the release as asset
gh release upload $FullVersion ./EdgeCanaryShortcutMaker.ps1 --clobber
[System.String]$ASSET_NAME = 'EdgeCanaryShortcutMaker.ps1'
# Making sure the download link is direct
[System.String]$ASSET_DOWNLOAD_URL = "https://github.com/SpyNetGirl/MSEdgeFeatures/releases/download/$FullVersion/$ASSET_NAME"
# Body of the Release that is going to be added via a patch method
[System.String]$GitHubReleaseBodyContent = @"
# <img width="35" src="https://github.com/HotCakeX/Harden-Windows-Security/raw/main/images/WebP/Edge%20Canary.webp"> Automated update
## Processed at: $(Get-Date -AsUTC) (UTC+00:00)`n
### $($Added.count) New features were added
$($Added | ForEach-Object -Process {"* $_`n"})
<br>
### $($Removed.count) Features were removed
$($Removed | ForEach-Object -Process {"* $_`n"})
<br>
### How to use the new features in this Edge canary update
1. First make sure your Edge canary is up to date
2. Copy and paste the code below in your PowerShell. NO admin privileges required. An Edge canary `.bat` file will be created in your Downloads folder. Double-click/tap on it to launch Edge canary with the features added in this update.
<br>
``````powershell
invoke-restMethod '$ASSET_DOWNLOAD_URL' | Invoke-Expression
``````
"@
# Add the body of the Release with the link to the EdgeCanaryShortcutMaker.ps1 asset file included
Invoke-RestMethod -Uri "https://api.github.com/repos/SpyNetGirl/MSEdgeFeatures/releases/$($RELEASE_RESPONSE.id)" -Method Patch -Headers @{Authorization = "token $env:GITHUB_TOKEN" } -Body (@{body = "$GitHubReleaseBodyContent" } | ConvertTo-Json) -ContentType 'application/json'
#endregion GitHub-Release-Publishing
}
else {
Write-Host -Object 'BUILD ALREADY EXISTS, EXITING.' -ForegroundColor Red
exit 0
}