This commit is contained in:
TinyHerm committed 2026-11-24 23:40:08 +03:00
commit 1c7a8cf25f
579 files changed
+83196

No files matched your search

+66
View File
@@ -0,0 +1,66 @@
using System;
using System.Threading.Tasks;
namespace VibranceHud.Updates;
public static class ForcedUpdate
{
internal static async Task<UpdateService.HandOff> TryBeforeTheDeadEndAsync(AppSettings settings, Action save, TimeSpan ceiling, Func<Task<ReleaseInfo?>>? fetch = null, Func<ReleaseInfo, IProgress<int>?, Task<string?>>? download = null, IProgress<int>? progress = null, Action<string>? status = null)
{
try
{
string stagedVersion = settings.PendingUpdateVersion;
UpdateService.HandOff handOff = await UpdateService.HandOffToInstallerAsync(settings, ceiling, askTheManifest: false, () =>
{
status?.Invoke("Installing PlexusX " + stagedVersion + "…");
});
Persist(save);
if (handOff != UpdateService.HandOff.NothingToInstall)
{
return handOff;
}
if (!RemoteSwitches.IsOn("updates"))
{
return UpdateService.HandOff.NothingToInstall;
}
ReleaseInfo release = await (fetch ?? new Func<Task<ReleaseInfo>>(UpdateService.TryGetUpdateAsync))();
release = RefusedUpdate.Unless(settings, release);
if (release == null)
{
return UpdateService.HandOff.NothingToInstall;
}
status?.Invoke($"Downloading update {release.Version}…");
string text = await (download ?? new Func<ReleaseInfo, IProgress<int>, Task<string>>(UpdateService.DownloadAndStageAsync))(release, progress);
if (string.IsNullOrWhiteSpace(text))
{
return UpdateService.HandOff.NothingToInstall;
}
settings.PendingUpdateInstaller = text;
settings.PendingUpdateVersion = release.Version.ToString();
settings.PendingUpdateSha256 = UpdateService.FileSha256(text);
settings.PendingUpdateSignedManifest = release.SignedManifest ?? "";
Persist(save);
UpdateService.HandOff result = await UpdateService.HandOffToInstallerAsync(settings, ceiling, askTheManifest: false, () =>
{
status?.Invoke($"Installing PlexusX {release.Version}…");
});
Persist(save);
return result;
}
catch
{
return UpdateService.HandOff.NothingToInstall;
}
}
private static void Persist(Action save)
{
try
{
save();
}
catch
{
}
}
}
+47
View File
@@ -0,0 +1,47 @@
using System.Runtime.InteropServices;
namespace VibranceHud.Updates;
public static class SafeToRestart
{
private const int QUNS_NOT_PRESENT = 1;
private const int QUNS_BUSY = 2;
private const int QUNS_RUNNING_D3D_FULL_SCREEN = 3;
private const int QUNS_PRESENTATION_MODE = 4;
private const int QUNS_ACCEPTS_NOTIFICATIONS = 5;
private const int QUNS_QUIET_TIME = 6;
private const int QUNS_APP = 7;
[DllImport("shell32.dll")]
private static extern int SHQueryUserNotificationState(out int state);
internal static bool ForShellState(int state)
{
return state switch
{
1 => true,
5 => true,
6 => true,
_ => false,
};
}
public static bool IsNow()
{
try
{
int state;
return SHQueryUserNotificationState(out state) == 0 && ForShellState(state);
}
catch
{
return false;
}
}
}
@@ -0,0 +1,15 @@
using System.Text.Json.Serialization;
namespace VibranceHud.Updates;
public sealed class SignedManifestEnvelope
{
[JsonPropertyName("manifest")]
public string Manifest { get; set; } = "";
[JsonPropertyName("sig")]
public string Sig { get; set; } = "";
[JsonPropertyName("sig2")]
public string? Sig2 { get; set; }
}
+18
View File
@@ -0,0 +1,18 @@
namespace VibranceHud.Updates;
public static class UpdateKeys
{
public static readonly byte[] Verification = new byte[91]
{
48, 89, 48, 19, 6, 7, 42, 134, 72, 206,
61, 2, 1, 6, 8, 42, 134, 72, 206, 61,
3, 1, 7, 3, 66, 0, 4, 30, 27, 243,
148, 61, 246, 109, 250, 153, 192, 34, 252, 76,
14, 17, 166, 55, 140, 209, 238, 140, 37, 197,
218, 218, 113, 169, 109, 71, 153, 65, 178, 18,
186, 65, 134, 150, 23, 108, 234, 211, 210, 254,
180, 139, 28, 79, 244, 90, 93, 18, 21, 174,
224, 170, 21, 12, 67, 201, 9, 227, 107, 60,
122
};
}
+55
View File
@@ -0,0 +1,55 @@
using System;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace VibranceHud.Updates;
public sealed record UpdateManifest([property: JsonPropertyName("version")] string Version, [property: JsonPropertyName("installerUrl")] string InstallerUrl, [property: JsonPropertyName("sha256")] string Sha256, [property: JsonPropertyName("notes")] string? Notes = null, [property: JsonPropertyName("assemblySha256")] string? AssemblySha256 = null)
{
public const string Domain = "plexusx-update-v1|";
public const string DomainV2 = "plexusx-update-v2|";
public byte[] CanonicalBytes()
{
return Encoding.UTF8.GetBytes($"{"plexusx-update-v1|"}{Version}\n{InstallerUrl}\n{Sha256.ToLowerInvariant()}\n{Notes ?? ""}");
}
public byte[] CanonicalBytesV2()
{
return Encoding.UTF8.GetBytes($"{"plexusx-update-v2|"}{Version}\n{InstallerUrl}\n{Sha256.ToLowerInvariant()}\n{Notes ?? ""}\n{(AssemblySha256 ?? "").ToLowerInvariant()}");
}
public static bool InstallerUrlIsAllowed(string? url)
{
if (string.IsNullOrWhiteSpace(url))
{
return false;
}
if (url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
}
public static bool TryParse(string? json, out UpdateManifest? manifest)
{
manifest = null;
if (string.IsNullOrWhiteSpace(json))
{
return false;
}
try
{
manifest = JsonSerializer.Deserialize<UpdateManifest>(json);
return manifest != null && !string.IsNullOrWhiteSpace(manifest.Version) && !string.IsNullOrWhiteSpace(manifest.InstallerUrl) && !string.IsNullOrWhiteSpace(manifest.Sha256);
}
catch
{
manifest = null;
return false;
}
}
}
@@ -0,0 +1,125 @@
using System;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
namespace VibranceHud.Updates;
public static class UpdateManifestVerifier
{
public static bool TryVerify(string? envelopeJson, byte[] publicKey, out UpdateManifest? manifest)
{
manifest = null;
if (string.IsNullOrWhiteSpace(envelopeJson))
{
return false;
}
if (publicKey == null || publicKey.Length == 0)
{
return false;
}
try
{
SignedManifestEnvelope signedManifestEnvelope = JsonSerializer.Deserialize<SignedManifestEnvelope>(envelopeJson);
if (signedManifestEnvelope == null)
{
return false;
}
if (string.IsNullOrEmpty(signedManifestEnvelope.Manifest) || string.IsNullOrEmpty(signedManifestEnvelope.Sig))
{
return false;
}
byte[] bytes = Convert.FromBase64String(signedManifestEnvelope.Manifest);
byte[] signature = Convert.FromBase64String(signedManifestEnvelope.Sig);
if (!UpdateManifest.TryParse(Encoding.UTF8.GetString(bytes), out UpdateManifest manifest2) || manifest2 == null)
{
return false;
}
using ECDsa eCDsa = ECDsa.Create();
eCDsa.ImportSubjectPublicKeyInfo(publicKey, out var _);
if (!eCDsa.VerifyData(manifest2.CanonicalBytes(), signature, HashAlgorithmName.SHA256))
{
return false;
}
if (!UpdateManifest.InstallerUrlIsAllowed(manifest2.InstallerUrl))
{
return false;
}
manifest = manifest2;
return true;
}
catch
{
manifest = null;
return false;
}
}
public static bool TryReadPublishedAssemblyHash(string? envelopeJson, byte[] publicKey, out string? version, out string? assemblySha256)
{
version = null;
assemblySha256 = null;
if (string.IsNullOrWhiteSpace(envelopeJson))
{
return false;
}
if (publicKey == null || publicKey.Length == 0)
{
return false;
}
try
{
SignedManifestEnvelope signedManifestEnvelope = JsonSerializer.Deserialize<SignedManifestEnvelope>(envelopeJson);
if (signedManifestEnvelope == null)
{
return false;
}
if (string.IsNullOrEmpty(signedManifestEnvelope.Manifest) || string.IsNullOrEmpty(signedManifestEnvelope.Sig2))
{
return false;
}
byte[] bytes = Convert.FromBase64String(signedManifestEnvelope.Manifest);
byte[] signature = Convert.FromBase64String(signedManifestEnvelope.Sig2);
if (!UpdateManifest.TryParse(Encoding.UTF8.GetString(bytes), out UpdateManifest manifest) || manifest == null)
{
return false;
}
string assemblySha257 = manifest.AssemblySha256;
if (!LooksLikeASha256(assemblySha257))
{
return false;
}
using ECDsa eCDsa = ECDsa.Create();
eCDsa.ImportSubjectPublicKeyInfo(publicKey, out var _);
if (!eCDsa.VerifyData(manifest.CanonicalBytesV2(), signature, HashAlgorithmName.SHA256))
{
return false;
}
version = manifest.Version;
assemblySha256 = assemblySha257.ToLowerInvariant();
return true;
}
catch
{
version = null;
assemblySha256 = null;
return false;
}
}
private static bool LooksLikeASha256(string? value)
{
if (value == null || value.Length != 64)
{
return false;
}
foreach (char c in value)
{
if ((c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F'))
{
return false;
}
}
return true;
}
}
+52
View File
@@ -0,0 +1,52 @@
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using VibranceHud.Licensing;
namespace VibranceHud.Updates;
public static class UpdateVoteClient
{
private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(6.0);
public const int MaxNote = 500;
private static string Url => ActivationEndpoint.Origin + "/api/app/update-vote";
public static string BuildBody(string version, bool liked, string? note)
{
string text = (note ?? string.Empty).Trim();
if (text.Length > 500)
{
text = text.Substring(0, 500);
}
return JsonSerializer.Serialize(new
{
version = (version ?? string.Empty),
vote = (liked ? "up" : "down"),
note = ((text.Length == 0) ? null : text)
});
}
public static async Task SendAsync(string version, bool liked, string? note, CancellationToken cancel = default(CancellationToken))
{
try
{
using HttpClient client = new HttpClient
{
Timeout = Timeout
};
client.DefaultRequestHeaders.UserAgent.ParseAdd("PlexusX");
using StringContent body = new StringContent(BuildBody(version, liked, note), Encoding.UTF8, "application/json");
using (await client.PostAsync(Url, body, cancel).ConfigureAwait(continueOnCapturedContext: false))
{
}
}
catch
{
}
}
}