Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project>
<!--
GOXF/DeskBox fork only (this file does not exist upstream, so upstream
version bumps never conflict with it).

Fork version = upstream base version (<Version> in src/DeskBox/DeskBox.csproj)
+ 4th segment ForkRevision, e.g. 1.5.9 + 1 => 1.5.9.1.

Directory.Build.targets is imported after the project body AND after the
SDK's DefaultAssemblyInfo targets have already derived AssemblyVersion /
FileVersion / InformationalVersion, so all four properties are set
explicitly here (a project-level <Version> alone would not propagate).

ForkRevision can be overridden with /p:ForkRevision=N or a ForkRevision
environment variable (the release-custom workflow does that). Bump the
default below when publishing a new fork build on the same upstream base;
reset it to 1 after upstream bumps <Version>.
-->
<PropertyGroup>
<ForkRevision Condition="'$(ForkRevision)' == ''">1</ForkRevision>
</PropertyGroup>

<PropertyGroup Condition="'$(MSBuildProjectName)' == 'DeskBox' and '$(ForkRevision)' != ''">
<ForkBaseVersion>$([System.Text.RegularExpressions.Regex]::Match('$(Version)', '^\d+\.\d+\.\d+').Value)</ForkBaseVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(MSBuildProjectName)' == 'DeskBox' and '$(ForkBaseVersion)' != ''">
<Version>$(ForkBaseVersion).$(ForkRevision)</Version>
<AssemblyVersion>$(ForkBaseVersion).$(ForkRevision)</AssemblyVersion>
<FileVersion>$(ForkBaseVersion).$(ForkRevision)</FileVersion>
<InformationalVersion>$(ForkBaseVersion).$(ForkRevision)</InformationalVersion>
</PropertyGroup>
</Project>
56 changes: 22 additions & 34 deletions src/DeskBox/Services/AppUpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ namespace DeskBox.Services;

public sealed partial class AppUpdateService : IAppUpdateService
{
public const string DefaultManifestUrl = "https://deskbox.fun/update/stable.json";
public const string DefaultGitHubLatestReleaseApiUrl = "https://api.github.com/repos/Tianyu199509/DeskBox/releases/latest";
public const string DefaultManualDownloadUrl = "https://deskbox.fun/download";
// GOXF fork: update sources live in ForkUpdateConfig (fork releases only).
public const string DefaultManifestUrl = ForkUpdateConfig.ManifestUrl;
public const string DefaultGitHubLatestReleaseApiUrl = ForkUpdateConfig.ForkLatestReleaseApiUrl;
public const string DefaultManualDownloadUrl = ForkUpdateConfig.ForkReleasesPageUrl;

private readonly HttpClient _httpClient;
private readonly string _manifestUrl;
Expand Down Expand Up @@ -191,10 +192,16 @@ public async Task<AppUpdateCheckResult> CheckForUpdatesAsync(CancellationToken c

public async Task<AppUpdateCheckResult> CheckForUpdatesAsync(string currentVersion, CancellationToken cancellationToken = default)
{
var manifestResult = await CheckManifestForUpdatesAsync(currentVersion, cancellationToken);
if (manifestResult.Status is AppUpdateCheckStatus.UpdateAvailable or AppUpdateCheckStatus.UpToDate)
// GOXF fork: no manifest by default (ForkUpdateConfig.ManifestUrl is
// empty), so the fork's GitHub latest release is the only source.
AppUpdateCheckResult? manifestResult = null;
if (!string.IsNullOrWhiteSpace(_manifestUrl))
{
return SetLastCheckResult(manifestResult);
manifestResult = await CheckManifestForUpdatesAsync(currentVersion, cancellationToken);
if (manifestResult.Status is AppUpdateCheckStatus.UpdateAvailable or AppUpdateCheckStatus.UpToDate)
{
return SetLastCheckResult(manifestResult);
}
}

var githubResult = await CheckGitHubLatestReleaseForUpdatesAsync(currentVersion, cancellationToken);
Expand All @@ -203,7 +210,7 @@ public async Task<AppUpdateCheckResult> CheckForUpdatesAsync(string currentVersi
return SetLastCheckResult(githubResult);
}

return SetLastCheckResult(manifestResult);
return SetLastCheckResult(manifestResult ?? githubResult);
}

private async Task<AppUpdateCheckResult> CheckManifestForUpdatesAsync(string currentVersion, CancellationToken cancellationToken)
Expand Down Expand Up @@ -707,6 +714,11 @@ public static bool IsManifestUsable(AppUpdateManifest? manifest)
public static async Task<AppUpdateManifest?> FetchContentManifestAsync(
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(DefaultManifestUrl))
{
return null;
}

try
{
return await FetchContentManifestAsync(DefaultManifestUrl, s_contentHttpClient, cancellationToken);
Expand Down Expand Up @@ -756,7 +768,7 @@ public static AppUpdateManifest CreateReleaseNotesOnlyManifest(string version)
{
Channel = "direct",
Version = version.Trim(),
ReleaseNotesUrl = $"https://github.com/Tianyu199509/DeskBox/releases/tag/v{version.Trim()}",
ReleaseNotesUrl = ForkUpdateConfig.GetReleaseTagUrl(version),
};
}

Expand All @@ -768,32 +780,8 @@ public static AppUpdateManifest CreateReleaseNotesOnlyManifest(string version)
/// </summary>
private static bool IsTrustedInstallerDownloadUri(Uri uri)
{
if (!string.Equals(uri.Scheme, "https", StringComparison.OrdinalIgnoreCase))
{
return false;
}

if (IsHostOrSubdomain(uri, "deskbox.fun"))
{
return true;
}

// GitHub hosts third-party repositories, so trusting the whole domain
// would let a tampered manifest point at any attacker repository with
// a matching hash. Only this project's own release downloads count;
// the actual asset bytes arrive via redirect from
// objects.githubusercontent.com, which the HTTP client follows and
// which therefore must never be trusted as a manifest origin itself.
return string.Equals(uri.Host, "github.com", StringComparison.OrdinalIgnoreCase) &&
uri.AbsolutePath.StartsWith(
"/Tianyu199509/DeskBox/releases/download/",
StringComparison.OrdinalIgnoreCase);
}

private static bool IsHostOrSubdomain(Uri uri, string domain)
{
return uri.Host.Equals(domain, StringComparison.OrdinalIgnoreCase) ||
uri.Host.EndsWith("." + domain, StringComparison.OrdinalIgnoreCase);
// GOXF fork: only GOXF/DeskBox release downloads (see ForkUpdateConfig).
return ForkUpdateConfig.IsTrustedInstallerDownloadUri(uri);
}

internal static bool TrySelectInstallerForArchitecture(
Expand Down
102 changes: 102 additions & 0 deletions src/DeskBox/Services/ForkUpdateConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
namespace DeskBox.Services;

/// <summary>
/// GOXF/DeskBox fork: where the installed app looks for its own updates and
/// for upstream (Tianyu199509/DeskBox) releases. Kept in a fork-only file so
/// upstream changes to <see cref="AppUpdateService"/> merge with only a few
/// one-line touch points.
///
/// Fork versions are the upstream base version plus a 4th "fork revision"
/// segment (1.5.9 + 1 => 1.5.9.1, see Directory.Build.targets). Fork releases
/// are tagged v1.5.9.1 on GOXF/DeskBox with the same asset names upstream
/// uses (DeskBox_Setup_1.5.9.1_x64.exe + .sha256).
/// </summary>
public static class ForkUpdateConfig
{
public const string ForkOwner = "GOXF";
public const string UpstreamOwner = "Tianyu199509";
public const string RepositoryName = "DeskBox";

/// <summary>The fork's own releases: the only source of installers.</summary>
public const string ForkLatestReleaseApiUrl = "https://api.github.com/repos/GOXF/DeskBox/releases/latest";

/// <summary>Upstream releases: informational only, never downloaded.</summary>
public const string UpstreamLatestReleaseApiUrl = "https://api.github.com/repos/Tianyu199509/DeskBox/releases/latest";

public const string ForkReleasesPageUrl = "https://github.com/GOXF/DeskBox/releases/latest";
public const string UpstreamReleasesPageUrl = "https://github.com/Tianyu199509/DeskBox/releases/latest";

/// <summary>
/// The fork publishes no JSON manifest. The upstream one at
/// deskbox.fun/update/stable.json describes upstream installers and must
/// never drive this build's updater, so the default is empty (= skip).
/// </summary>
public const string ManifestUrl = "";

internal const string ForkReleaseDownloadPathPrefix = "/GOXF/DeskBox/releases/download/";

/// <summary>
/// Only this fork's GitHub release downloads may be installed. The asset
/// bytes arrive by redirect from objects.githubusercontent.com, which the
/// HTTP client follows and which therefore is never trusted as an origin.
/// Upstream hosts (deskbox.fun, github.com/Tianyu199509) are deliberately
/// NOT trusted: an upstream installer would overwrite the fork build.
/// </summary>
public static bool IsTrustedInstallerDownloadUri(Uri uri)
{
return uri.IsAbsoluteUri &&
string.Equals(uri.Scheme, "https", StringComparison.OrdinalIgnoreCase) &&
string.Equals(uri.Host, "github.com", StringComparison.OrdinalIgnoreCase) &&
uri.AbsolutePath.StartsWith(ForkReleaseDownloadPathPrefix, StringComparison.OrdinalIgnoreCase);
}

/// <summary>
/// Release-tag page for one version: 4-segment versions are fork builds
/// (GOXF/DeskBox), 3-segment versions are upstream releases whose notes
/// live on Tianyu199509/DeskBox.
/// </summary>
public static string GetReleaseTagUrl(string version)
{
string trimmed = version.Trim();
if (trimmed.StartsWith('v') || trimmed.StartsWith('V'))
{
trimmed = trimmed[1..];
}

string owner = IsForkVersion(trimmed) ? ForkOwner : UpstreamOwner;
return $"https://github.com/{owner}/{RepositoryName}/releases/tag/v{trimmed}";
}

public static bool IsForkVersion(string? version)
{
return AppUpdateService.TryParseVersion(version, out Version parsed) && parsed.Revision >= 0;
}

/// <summary>
/// The upstream base (first three segments) of a fork or upstream
/// version: "1.5.9.1" => "1.5.9", "v1.6.0" => "1.6.0". Empty when the
/// text is not a version.
/// </summary>
public static string GetBaseVersion(string? version)
{
if (!AppUpdateService.TryParseVersion(version, out Version parsed))
{
return string.Empty;
}

return new Version(parsed.Major, parsed.Minor, Math.Max(0, parsed.Build)).ToString(3);
}

/// <summary>
/// True when upstream released a version newer than the upstream base
/// this build was made from. Only the first three segments are compared.
/// </summary>
public static bool IsUpstreamNewer(string? currentVersion, string? upstreamVersion)
{
string currentBase = GetBaseVersion(currentVersion);
string upstreamBase = GetBaseVersion(upstreamVersion);
return currentBase.Length > 0 &&
upstreamBase.Length > 0 &&
Version.Parse(upstreamBase) > Version.Parse(currentBase);
}
}
106 changes: 106 additions & 0 deletions src/DeskBox/Services/UpstreamReleaseCheckService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System.Text.Json;

namespace DeskBox.Services;

/// <summary>
/// Result of the informational upstream check: upstream's latest release
/// tag compared against the upstream base of the running fork build.
/// </summary>
public sealed record UpstreamReleaseInfo(
string UpstreamVersion,
string CurrentBaseVersion,
string ReleaseUrl,
bool IsNewer);

/// <summary>
/// GOXF/DeskBox fork: asks Tianyu199509/DeskBox's GitHub "latest release"
/// whether upstream has moved past the base this build was made from. Purely
/// informational: nothing is downloaded from upstream, and any failure
/// (offline, rate limit, malformed JSON) yields null so it can never block or
/// alter the normal update flow.
/// </summary>
public static class UpstreamReleaseCheckService
{
private static readonly HttpClient s_httpClient = CreateHttpClient();

private static HttpClient CreateHttpClient()
{
var client = new HttpClient
{
Timeout = TimeSpan.FromSeconds(10)
};
client.DefaultRequestHeaders.UserAgent.ParseAdd($"DeskBox/{AppUpdateService.GetCurrentAppVersion()}");
client.DefaultRequestHeaders.Accept.ParseAdd("application/vnd.github+json");
return client;
}

public static Task<UpstreamReleaseInfo?> CheckAsync(
string currentVersion,
CancellationToken cancellationToken = default)
{
return CheckAsync(currentVersion, s_httpClient, ForkUpdateConfig.UpstreamLatestReleaseApiUrl, cancellationToken);
}

internal static async Task<UpstreamReleaseInfo?> CheckAsync(
string currentVersion,
HttpClient httpClient,
string latestReleaseApiUrl,
CancellationToken cancellationToken)
{
try
{
using var response = await httpClient.GetAsync(latestReleaseApiUrl, cancellationToken).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
return null;
}

string json = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
return ParseLatestRelease(currentVersion, json);
}
catch (Exception ex)
{
App.Log($"[Update] Upstream release check skipped: {ex.GetType().Name}");
return null;
}
}

internal static UpstreamReleaseInfo? ParseLatestRelease(string currentVersion, string json)
{
try
{
using var document = JsonDocument.Parse(json);
JsonElement root = document.RootElement;
if (root.ValueKind != JsonValueKind.Object ||
!root.TryGetProperty("tag_name", out JsonElement tagElement) ||
tagElement.ValueKind != JsonValueKind.String)
{
return null;
}

string upstreamVersion = ForkUpdateConfig.GetBaseVersion(tagElement.GetString());
string currentBase = ForkUpdateConfig.GetBaseVersion(currentVersion);
if (upstreamVersion.Length == 0 || currentBase.Length == 0)
{
return null;
}

string releaseUrl = root.TryGetProperty("html_url", out JsonElement urlElement) &&
urlElement.ValueKind == JsonValueKind.String &&
Uri.TryCreate(urlElement.GetString(), UriKind.Absolute, out Uri? uri) &&
uri.Scheme == Uri.UriSchemeHttps
? uri.ToString()
: ForkUpdateConfig.UpstreamReleasesPageUrl;

return new UpstreamReleaseInfo(
upstreamVersion,
currentBase,
releaseUrl,
ForkUpdateConfig.IsUpstreamNewer(currentBase, upstreamVersion));
}
catch (JsonException)
{
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ public async Task CheckForUpdatesAsync()

_updateOperationCts?.Cancel();
_updateOperationCts = new CancellationTokenSource();
StartUpstreamReleaseCheck(); // GOXF fork: informational, never awaited
IsCheckingForUpdates = true;
UpdateStatusText = _localizationService.T("Settings.Update.Status.Checking");
UpdateDetailText = _localizationService.T("Settings.Update.Detail.Checking");
Expand Down
Loading
Loading