Skip to content

Commit

Permalink
Adding support for different URL to use when pulling beta version inf…
Browse files Browse the repository at this point in the history
…ormation
  • Loading branch information
bc3tech committed Jun 10, 2024
1 parent d5dec97 commit 4b04984
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
namespace LatestVersionFunction;

using System;
using System.Linq;
using System.Net.Http;
Expand All @@ -10,113 +12,124 @@
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;

namespace LatestVersionFunction
public static class GetLatestVersion
{
public static class GetLatestVersion
{
private static readonly HttpClient _client = new HttpClient();
private static readonly HttpClient _client = new HttpClient();

[FunctionName("GetLatestVersion")]
public static async System.Threading.Tasks.Task<IActionResult> RunAsync([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, ILogger log)
[FunctionName("GetLatestVersion")]
public static async System.Threading.Tasks.Task<IActionResult> RunAsync([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req, ILogger log)
{
//using (var changelogStream = await _client.GetStreamAsync(Environment.GetEnvironmentVariable(@"ChangeLogUrl")))
{
//using (var changelogStream = await _client.GetStreamAsync(Environment.GetEnvironmentVariable(@"ChangeLogUrl")))
{
// eg: https://www.phraseexpress.com/update13.php
var doc = XDocument.Load($@"{Environment.GetEnvironmentVariable(@"VersionCheckUrl")}?{Environment.GetEnvironmentVariable(@"VersionCheckQueryParams")}");
// eg: https://www.phraseexpress.com/update13.php
var doc = XDocument.Load($@"{Environment.GetEnvironmentVariable(@"VersionCheckUrl")}?{Environment.GetEnvironmentVariable(@"VersionCheckQueryParams")}");

/* sample doc
/* sample doc
<xml>
<debug></debug>
<product>PhraseExpress</product>
<interval>3</interval>
<webpage></webpage>
<info>1</info>
<client>
<versions>
<newversion>13.0.0</newversion>
<majorupdate>13.6.7</majorupdate>
<minorupdate>13.0.0</minorupdate>
<experimentalupdate>13.0.0</experimentalupdate>
<betaversion>13.0.0</betaversion>
</versions>
<installationtype>
* */
var clientElement = doc
.Element(@"xml")
.Element(@"client");
var versions = clientElement
.Element(@"versions")
.Elements();

var downloadBaseUrl = Environment.GetEnvironmentVariable(@"DownloadBaseUrl");

string zipHashString,
zipDownloadUrl = $@"{downloadBaseUrl}PhraseExpress_USB.zip",
msiHashString,
msiDownloadUrl = $@"{downloadBaseUrl}PhraseExpressSetup.msi";

string targetVersion;
if (req.GetQueryParameterDictionary().TryGetValue(@"beta", out var betaValue)
&& bool.TryParse(betaValue, out var isBeta)
&& isBeta)
<debug></debug>
<product>PhraseExpress</product>
<interval>3</interval>
<webpage></webpage>
<info>1</info>
<client>
<versions>
<newversion>13.0.0</newversion>
<majorupdate>13.6.7</majorupdate>
<minorupdate>13.0.0</minorupdate>
<experimentalupdate>13.0.0</experimentalupdate>
<betaversion>13.0.0</betaversion>
</versions>
<installationtype>
* */
var clientElement = doc
.Element(@"xml")
.Element(@"client");
var versions = clientElement
.Element(@"versions")
.Elements();

var downloadBaseUrl = Environment.GetEnvironmentVariable(@"DownloadBaseUrl");

string zipHashString,
zipDownloadUrl = $@"{downloadBaseUrl}PhraseExpress_USB.zip",
msiHashString,
msiDownloadUrl = $@"{downloadBaseUrl}PhraseExpressSetup.msi";

string targetVersion;
if (req.GetQueryParameterDictionary().TryGetValue(@"beta", out var betaValue)
&& bool.TryParse(betaValue, out var isBeta)
&& isBeta)
{
if (!bool.Parse(Environment.GetEnvironmentVariable(@"BetaIsCurrentlyOffered")))
{
if (!bool.Parse(Environment.GetEnvironmentVariable(@"BetaIsCurrentlyOffered")))
{
return new NoContentResult();
}
return new NoContentResult();
}

targetVersion = versions.Single(i => i.Name.LocalName.Equals(@"betaversion", StringComparison.OrdinalIgnoreCase)).Value;
var betaVersionSuffix = Environment.GetEnvironmentVariable(@"BetaVersionSuffix");
zipDownloadUrl = $@"{downloadBaseUrl}PhraseExpress{betaVersionSuffix}_USB.zip";
msiDownloadUrl = $@"{downloadBaseUrl}PhraseExpress{betaVersionSuffix}Setup.exe";
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("BetaVersionCheckUrl")))
{
doc = XDocument.Load($@"{Environment.GetEnvironmentVariable(@"BetaVersionCheckUrl")}?{Environment.GetEnvironmentVariable(@"VersionCheckQueryParams")}");
clientElement = doc
.Element(@"xml")
.Element(@"client");
versions = clientElement
.Element(@"versions")
.Elements();
}
else
{ // assuming the default download URL is always a minor version
// pick the larges of new, major, and minor updates shown in the response

Version.TryParse(versions.Single(i => i.Name.LocalName.Equals(@"minorupdate", StringComparison.OrdinalIgnoreCase)).Value, out var minorVersion);
Version.TryParse(versions.Single(i => i.Name.LocalName.Equals(@"majorupdate", StringComparison.OrdinalIgnoreCase)).Value, out var majorVersion);
Version.TryParse(versions.Single(i => i.Name.LocalName.Equals(@"newversion", StringComparison.OrdinalIgnoreCase)).Value, out var newVersion);
targetVersion = versions.Single(i => i.Name.LocalName.Equals(@"betaversion", StringComparison.OrdinalIgnoreCase)).Value;
var betaVersionSuffix = Environment.GetEnvironmentVariable(@"BetaVersionSuffix");
zipDownloadUrl = $@"{downloadBaseUrl}PhraseExpress{betaVersionSuffix}_USB.zip";
msiDownloadUrl = $@"{downloadBaseUrl}PhraseExpress{betaVersionSuffix}Setup.exe";
}
else
{ // assuming the default download URL is always a minor version
// pick the larges of new, major, and minor updates shown in the response

if (newVersion > majorVersion)
{
targetVersion = newVersion > minorVersion ? newVersion.ToString() : minorVersion.ToString();
}
else
{
targetVersion = majorVersion > minorVersion ? majorVersion.ToString() : minorVersion.ToString();
}
Version.TryParse(versions.Single(i => i.Name.LocalName.Equals(@"minorupdate", StringComparison.OrdinalIgnoreCase)).Value, out var minorVersion);
Version.TryParse(versions.Single(i => i.Name.LocalName.Equals(@"majorupdate", StringComparison.OrdinalIgnoreCase)).Value, out var majorVersion);
Version.TryParse(versions.Single(i => i.Name.LocalName.Equals(@"newversion", StringComparison.OrdinalIgnoreCase)).Value, out var newVersion);

if (newVersion > majorVersion)
{
targetVersion = newVersion > minorVersion ? newVersion.ToString() : minorVersion.ToString();
}
else
{
targetVersion = majorVersion > minorVersion ? majorVersion.ToString() : minorVersion.ToString();
}
}

using (var sha = SHA256.Create())
using (var sha = SHA256.Create())
{
try
{
try
log.LogInformation("Downloading ZIP file to compute hash...");
using (var versionDownload = await _client.GetStreamAsync(zipDownloadUrl))
{
using (var versionDownload = await _client.GetStreamAsync(zipDownloadUrl))
{
var hashBytes = sha.ComputeHash(versionDownload);
zipHashString = string.Join(string.Empty, hashBytes.Select(b => b.ToString("X2")));
log.LogInformation($@"ZIP computed hash: {zipHashString}");
}
GC.Collect();

using (var versionDownload = await _client.GetStreamAsync(msiDownloadUrl))
{
var hashBytes = sha.ComputeHash(versionDownload);
msiHashString = string.Join(string.Empty, hashBytes.Select(b => b.ToString("X2")));
log.LogInformation($@"MSI computed hash: {msiHashString}");
}
GC.Collect();
var hashBytes = sha.ComputeHash(versionDownload);
zipHashString = string.Join(string.Empty, hashBytes.Select(b => b.ToString("X2")));
log.LogInformation($@"ZIP computed hash: {zipHashString}");
}
catch (HttpRequestException ex)
GC.Collect();

log.LogInformation("Downloading MSI file to compute hash...");
using (var versionDownload = await _client.GetStreamAsync(msiDownloadUrl))
{
return new NotFoundObjectResult(new { zipDownloadUrl, msiDownloadUrl, exception = ex.ToString() });
var hashBytes = sha.ComputeHash(versionDownload);
msiHashString = string.Join(string.Empty, hashBytes.Select(b => b.ToString("X2")));
log.LogInformation($@"MSI computed hash: {msiHashString}");
}
GC.Collect();
}
catch (HttpRequestException ex)
{
return new NotFoundObjectResult(new { zipDownloadUrl, msiDownloadUrl, exception = ex.ToString() });
}
GC.Collect();

return new OkObjectResult(new { version = targetVersion, zip = new { download = zipDownloadUrl, hash = zipHashString }, msi = new { download = msiDownloadUrl, hash = msiHashString } });
}

GC.Collect();

return new OkObjectResult(new { version = targetVersion, zip = new { download = zipDownloadUrl, hash = zipHashString }, msi = new { download = msiDownloadUrl, hash = msiHashString } });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"VersionCheckUrl": "https://www.phraseexpress.com/update16.php",
"VersionCheckQueryParams": "license=DEMO&hs=43f755f83538369dd7668077bee7448b",
"DownloadBaseUrl": "https://www.phraseexpress.com/",
"BetaIsCurrentlyOffered": false
"BetaIsCurrentlyOffered": true,
"BetaVersionSuffix": "Beta",
"BetaVersionCheckUrl": "https://www.phraseexpress.com/update16.php"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
# From fiddler trace of app installation
GET https://www.phraseexpress.com/update16.php?license=DEMO&version=16.1.6&hwid=CE6BBECC&hs=43f755f83538369dd7668077bee7448b&langid=09&priority=1 HTTP/1.1
GET https://www.phraseexpress.com/update16.php?license=DEMO&hs=43f755f83538369dd7668077bee7448b HTTP/1.1

###

# Next major
GET https://www.phraseexpress.com/update17.php?license=DEMO&hs=43f755f83538369dd7668077bee7448b HTTP/1.1

###

# To hit local instance of the Function
GET http://localhost:7071/api/GetLatestVersion HTTP/1.1
Connection : keep-alive

###

# To hit local instance of the Function w/ Beta option
GET http://localhost:7071/api/GetLatestVersion?beta=true HTTP/1.1
Connection : keep-alive

0 comments on commit 4b04984

Please sign in to comment.