-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WindowsHostingBundleInstaller.cs
280 lines (240 loc) · 10.5 KB
/
WindowsHostingBundleInstaller.cs
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
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using Microsoft.Win32;
using Westwind.Utilities;
namespace DotnetRuntimeInstaller
{
/// <summary>
/// Configure the runtime version and download URL here
/// for the version you want to check for and install.
///
/// Note these two don't have to be the same. You can check for
/// a lower version but always install the latest patch version.
/// </summary>
internal class WindowsHostingBundleConfiguration
{
/// <summary>
/// Name of the Application used in prompts
/// </summary>
internal static string ApplicationName = "West Wind Web Connection";
/// <summary>
/// Minimum version of the Windows Hosting Bundle that should be present.
///
/// Launcher checks for installed version and if lower prompts
/// to install it.
/// </summary>
internal static string MinDotnetRuntimeVersion { get; } = "8.0.0";
/// <summary>
/// Direct download URL for the .NET Hosting Bundle Runtime Installer.
///
/// Recommend you update this link to the latest available patch version so if you need to install
/// you are installing the latest, not an older version.
/// MM allows patch roll forward meaning later patches work but you don't install if a
/// a compatible version is already installed.
///
/// Get this URL from the Microsoft .NET download site:
/// https://dotnet.microsoft.com/en-us/download/dotnet/8.0 (Download x64 Desktop Runtime)
/// </summary>
internal static string RuntimeDownloadUrl { get; } =
"https://download.visualstudio.microsoft.com/download/pr/00397fee-1bd9-44ef-899b-4504b26e6e96/ab9c73409659f3238d33faee304a8b7c/dotnet-hosting-8.0.4-win.exe";
/// <summary>
/// Optional SHA512 hash of the downloaded file to verify the file integrity.
///
/// Checked only if the value is set.
///
/// This value is also displayed on the Microsoft download page along with the download link
/// </summary>
internal static string DownloadExeSha512 { get; } = "2ae357f0d8e43c316874455ca56adee4d88081bf828721038527760d860beb3b510eca748aa18ebfc9509cd289b51e84156da388853d644cff308b539b04355c";
/// <summary>
/// Url to the latest ASP.NET Hosting Bundle Download Page.
/// </summary>
internal static string ManualDownloadPage { get; } = "https://dotnet.microsoft.com/en-us/download/dotnet/thank-you/runtime-aspnetcore-8.0.4-windows-hosting-bundle-installer";
}
}
/// <summary>
/// Console based downloader that downloads and installs the ASP.NET Windows Hosting Bundle Runtime
/// if it is not already installed.
///
/// * Checks if the Minimum Runtime version is installed by checking RT install folder
/// * If not installed prompts to download and install
/// * Downloads the installer to the Downloads folder
/// * Prompts to install the runtime
/// * Runs the runtime installer as admin
/// * Optionally can run silently (-silent) w/o prompting
///
/// </summary>
internal class WindowsHostingBundleInstaller
{
/// <summary>
/// Checks runtime version, downloads and installs runtime if not installed.
/// </summary>
/// <param name="isSilent">
/// if true doesn't prompt for confirmation of download and install
/// Use -silent when calling from an installer or other automated process
/// </param>
/// <returns></returns>
public static bool CheckAndInstallRuntime(bool isSilent = false)
{
if (RuntimeVersionCheck())
{
ConsoleWrite(
$".NET Windows Hosting Bundle v{WindowsHostingBundleConfiguration.MinDotnetRuntimeVersion} or later is already installed.",
ConsoleColor.Green);
return true;
}
bool result = false;
var text =
$"{WindowsHostingBundleConfiguration.ApplicationName} requires the .NET Windows Hosting Bundle v{WindowsHostingBundleConfiguration.MinDotnetRuntimeVersion} or later.";
var line = new string('-', text.Length);
ConsoleWrite($"{line}\n{text}\n{line}\n", ConsoleColor.Red);
if (isSilent)
{
result = DownloadAndInstall(true);
}
else
{
ConsoleWrite("Would you like to open to download and install the runtime now? [Y]/[n] ([m]anually download and install)",
ConsoleColor.Yellow);
var key = Console.ReadKey();
Console.WriteLine();
if (key.Key == ConsoleKey.M)
{
ShellUtils.GoUrl(WindowsHostingBundleConfiguration.ManualDownloadPage);
return true;
}
if (key.Key == ConsoleKey.Y || key.Key == ConsoleKey.Enter)
{
result = DownloadAndInstall();
}
}
return result;
}
private static bool DownloadAndInstall(bool isSilent = false)
{
var url = WindowsHostingBundleConfiguration.RuntimeDownloadUrl;
var filename = Path.GetFileName(url);
string pattern = @"(\d+\.\d+\.\d+)";
var dlFolder =
Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders",
"{374DE290-123F-4565-9164-39C4925E467B}", String.Empty).ToString();
var dlPath = Path.Combine(dlFolder, filename);
ConsoleWrite("==> Downloading.NET Desktop Runtime Installer", ConsoleColor.DarkCyan);
ConsoleWrite($@"
from:
{url}
to:
{dlPath}
", ConsoleColor.DarkGray);
Console.WriteLine("Please wait while we download...");
try
{
var client = new WebClient();
client.DownloadFile(url, dlPath);
}
catch (Exception ex)
{
ConsoleWrite("Download failed: " + ex.GetBaseException().Message, ConsoleColor.Red);
return false;
}
ConsoleWrite("Download complete.", ConsoleColor.DarkYellow);
if (!string.IsNullOrEmpty(WindowsHostingBundleConfiguration.DownloadExeSha512) &&
!CheckFileSha512(dlPath, WindowsHostingBundleConfiguration.DownloadExeSha512))
{
ConsoleWrite($"File Integrity check based on SHA512 Hash failed.\nexpected: {WindowsHostingBundleConfiguration.DownloadExeSha512}\n actual: {ComputedFileHash}", ConsoleColor.Red);
return false;
}
ConsoleWrite("File integrity SHA512 hash matches.", ConsoleColor.DarkYellow);
if (!isSilent)
{
ConsoleWrite(
"\nWould you like to install the runtime now? [Y]/[n] (Yes installs, no opens in Explorer)",
ConsoleColor.Yellow);
var key = Console.ReadKey();
if (key.Key != ConsoleKey.Y && key.Key != ConsoleKey.Enter)
{
ShellUtils.OpenFileInExplorer(dlPath);
return true;
}
}
string dlVersion = WindowsHostingBundleConfiguration.MinDotnetRuntimeVersion;
Match match = Regex.Match(filename, pattern);
if (match.Success)
{
dlVersion = match.Groups[1].Value;
}
ConsoleWrite($"\n==> Installing Runtime .NET Desktop Runtime v{dlVersion}...\n",
ConsoleColor.DarkCyan);
try
{
ShellUtils.ExecuteCommandLine(dlPath + (isSilent ? " /quiet" : null), null, verb: "RunAs", waitForExitMs: 180000);
}
catch (Exception ex)
{
ConsoleWrite("Runtime Installation failed: " + ex.GetBaseException().Message, ConsoleColor.Red);
return false;
}
ConsoleWrite("\nInstallation complete.", ConsoleColor.Green);
return true;
}
private static void ConsoleWrite(string text, ConsoleColor color = ConsoleColor.Yellow)
{
var oldColor = Console.ForegroundColor;
if (color == oldColor)
{
Console.WriteLine(text);
}
else
{
Console.ForegroundColor = color;
Console.WriteLine(text);
Console.ForegroundColor = oldColor;
}
}
private static bool RuntimeVersionCheck()
{
var desktopRuntimePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
@"dotnet\shared\Microsoft.AspNetCore.App"
);
if (!Directory.Exists(desktopRuntimePath))
{
return false;
}
bool found = Directory.GetDirectories(desktopRuntimePath).OrderByDescending(d => d)
.Any(d =>
{
try
{
var dirName = Path.GetFileName(d);
var res = dirName.StartsWith(WindowsHostingBundleConfiguration.MinDotnetRuntimeVersion.Substring(0, 2)) &&
new Version(dirName) >= new Version(WindowsHostingBundleConfiguration.MinDotnetRuntimeVersion);
return res;
}
catch
{
return false;
}
});
return found;
}
private static string ComputedFileHash = string.Empty;
private static bool CheckFileSha512(string filePath, string providedHash)
{
if (string.IsNullOrEmpty(providedHash))
return true;
using (var sha512 = SHA512.Create())
{
using (var fileStream = File.OpenRead(filePath))
{
byte[] fileHash = sha512.ComputeHash(fileStream);
ComputedFileHash = BitConverter.ToString(fileHash).Replace("-", "").ToLower();
return string.Equals(ComputedFileHash, providedHash, StringComparison.OrdinalIgnoreCase);
}
}
}
}
}