-
Notifications
You must be signed in to change notification settings - Fork 5
/
WhatsappUpdater.cs
327 lines (285 loc) · 11.4 KB
/
WhatsappUpdater.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
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
using HtmlAgilityPack;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace MicrosoftStoreDownloader
{
public class MicrosoftStoreApp
{
private readonly string token = "9NKSQGP7F2NH";
private string response = null;
public List<AppxLocation> Locations { get; } = new List<AppxLocation>();
public static async Task Main(string[] args)
{
MicrosoftStoreApp app = new MicrosoftStoreApp();
await app.StartDownloadAsync();
}
public async Task StartDownloadAsync()
{
await InstallDependencyAsync();
await LoadAsync();
AppxLocation msixBundleLocation = FindFirstMsixBundleLocation();
if (msixBundleLocation != null)
{
string version = GetVersionFromFileName(msixBundleLocation.Name);
if (!IsNewVersion(version))
{
Console.WriteLine("The current version is equal or superior. No need to download.");
return;
}
DownloadMsixBundle(msixBundleLocation);
Console.WriteLine("Download complete.");
KillWhatsappProcess();
if (InstallAppx(msixBundleLocation.Name))
{
Console.WriteLine("Installation complete.");
DeleteMsixBundle(msixBundleLocation.Name);
RegisterVersion(version);
}
else
{
Console.WriteLine("Error during installation.");
}
}
else
{
Console.WriteLine("No .msixbundle files found for download.");
}
}
private void KillWhatsappProcess()
{
try
{
string processName = "Whatsapp"; // Name of the Whatsapp process
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(processName);
foreach (System.Diagnostics.Process process in processes)
{
process.Kill();
}
Console.WriteLine($"{processName} process stopped.");
}
catch (Exception ex)
{
Console.WriteLine($"Error stopping Whatsapp process: {ex.Message}");
}
}
private async Task LoadAsync()
{
try
{
string url = "https://store.rg-adguard.net/api/GetFiles";
string postData = $"type=url&url={Uri.EscapeUriString($"https://www.microsoft.com/store/apps/{token}")}";
using (HttpClient httpClient = new HttpClient())
{
var content = new StringContent(postData, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded");
var response = await httpClient.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
this.response = await response.Content.ReadAsStringAsync();
ParseLocations();
}
else
{
Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
private void ParseLocations()
{
Locations.Clear();
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(response);
List<HtmlNode> table = doc.DocumentNode.SelectSingleNode("//table[@class='tftable']")
.Descendants("tr")
.Where(tr => tr.Elements("td").Count() >= 1)
.ToList();
foreach (HtmlNode row in table)
{
List<HtmlNode> data = row.Elements("td").ToList();
string url = data[0].Descendants("a").FirstOrDefault()?.GetAttributeValue("href", null);
string name = data[0].InnerText;
string expire = data[1].InnerText;
string sha1 = data[2].InnerText;
string[] info = name.Split('_');
string[] allowedExtensions = { ".msixbundle" };
if (allowedExtensions.Contains(Path.GetExtension(name)))
{
Locations.Add(new AppxLocation()
{
URL = url,
Name = name,
ExpireTime = DateTime.Parse(expire),
SHA1 = sha1,
PackageName = info[0],
Version = Version.Parse(info[1]),
Architecture = info[2]
});
}
}
}
private AppxLocation FindFirstMsixBundleLocation()
{
return Locations.FirstOrDefault(location => location.Name.EndsWith(".msixbundle"));
}
private string GetVersionFromFileName(string fileName)
{
string pattern = @"_(\d+\.\d+\.\d+\.\d+)_";
Match match = Regex.Match(fileName, pattern);
return match.Success ? match.Groups[1].Value : null;
}
private bool IsNewVersion(string newVersion)
{
if (newVersion == null)
{
return false;
}
if (Registry.GetValue(@"HKEY_CURRENT_USER\Software\WhatsappUpdater", "LastVersion", null) is string existingVersion)
{
return string.Compare(newVersion, existingVersion) > 0;
}
return true;
}
private void RegisterVersion(string version)
{
Registry.SetValue(@"HKEY_CURRENT_USER\Software\WhatsappUpdater", "LastVersion", version);
}
private bool InstallAppx(string fileName)
{
try
{
string installCommand = $"Add-AppxPackage -Path .\\{fileName}";
Console.WriteLine($"Installing the application: {fileName}");
using (System.Diagnostics.Process process = new System.Diagnostics.Process())
{
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo
{
FileName = "powershell.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};
process.StartInfo = psi;
process.Start();
process.StandardInput.WriteLine(installCommand);
process.StandardInput.WriteLine("exit");
process.WaitForExit();
if (process.ExitCode == 0)
{
return true;
}
else
{
Console.WriteLine($"Error during installation. Exit code: {process.ExitCode}");
return false;
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error running PowerShell command: {ex.Message}");
return false;
}
}
private void DeleteMsixBundle(string fileName)
{
try
{
File.Delete(fileName);
}
catch (Exception ex)
{
Console.WriteLine($"Error deleting the file: {ex.Message}");
}
}
private void DownloadMsixBundle(AppxLocation location)
{
if (location != null)
{
try
{
string downloadUrl = location.URL;
string fileName = location.Name;
Console.WriteLine($"Downloading: {fileName}");
using (HttpClient httpClient = new HttpClient())
{
var content = httpClient.GetByteArrayAsync(downloadUrl).Result;
File.WriteAllBytes(fileName, content);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error downloading the file: {ex.Message}");
}
}
}
private async Task InstallDependencyAsync()
{
// Dependencia 1
string dependencyUrl1 = "https://github.com/microsoft/microsoft-ui-xaml/releases/download/v2.8.5/Microsoft.UI.Xaml.2.8.x64.appx";
string dependencyFileName1 = "Microsoft.UI.Xaml.2.8.x64.appx";
Console.WriteLine($"Downloading dependency 1: {dependencyFileName1}");
try
{
using (HttpClient httpClient = new HttpClient())
{
var content = await httpClient.GetByteArrayAsync(dependencyUrl1);
File.WriteAllBytes(dependencyFileName1, content);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error downloading dependency 1: {ex.Message}");
return;
}
Console.WriteLine($"Installing dependency 1: {dependencyFileName1}");
if (InstallAppx(dependencyFileName1))
{
Console.WriteLine($"Dependency 1 installed successfully: {dependencyFileName1}");
DeleteMsixBundle(dependencyFileName1);
}
else
{
Console.WriteLine($"Error installing dependency 1: {dependencyFileName1}");
}
// Dependencia 2
string dependencyUrl2 = "https://github.com/QuestYouCraft/Microsoft-Store-UnInstaller/raw/master/Packages/Microsoft.NET.Native.Framework.2.2_2.2.29512.0_x64__8wekyb3d8bbwe.Appx";
string dependencyFileName2 = "Microsoft.NET.Native.Framework.2.2_2.2.29512.0_x64__8wekyb3d8bbwe.Appx";
Console.WriteLine($"Downloading dependency 2: {dependencyFileName2}");
try
{
using (HttpClient httpClient = new HttpClient())
{
var content = await httpClient.GetByteArrayAsync(dependencyUrl2);
File.WriteAllBytes(dependencyFileName2, content);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error downloading dependency 2: {ex.Message}");
return;
}
Console.WriteLine($"Installing dependency 2: {dependencyFileName2}");
if (InstallAppx(dependencyFileName2))
{
Console.WriteLine($"Dependency 2 installed successfully: {dependencyFileName2}");
DeleteMsixBundle(dependencyFileName2);
}
else
{
Console.WriteLine($"Error installing dependency 2: {dependencyFileName2}");
}
}
}
}