Skip to content

Commit

Permalink
[OOBE] Get "What's New" behind authenticated proxy and strict firewal…
Browse files Browse the repository at this point in the history
…l. (#18695)

* Attempt to get username and password for proxy authentication. We should not use it.

* Using default credentials for system proxy authentication.
  • Loading branch information
lncubus committed Jun 14, 2022
1 parent b5531a1 commit e8bb2de
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 20 deletions.
29 changes: 19 additions & 10 deletions src/settings-ui/Settings.UI/OOBE/Views/OobeWhatsNew.xaml
Expand Up @@ -31,15 +31,7 @@
TextWrapping="Wrap" />
</HyperlinkButton>
</StackPanel>
<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto">
<Grid Margin="32,24,32,24">
<muxc:ProgressRing Grid.Row="1"
x:Name="LoadingProgressRing"
IsIndeterminate="True"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Visibility="Visible"/>
<muxc:InfoBar
<muxc:InfoBar
Severity="Error"
x:Name="ErrorInfoBar"
Grid.Row="1"
Expand All @@ -49,9 +41,26 @@
IsClosable="False"
IsOpen="True"
IsTabStop="True" />
<muxc:InfoBar
x:Name="ProxyWarningInfoBar"
Severity="Warning"
Grid.Row="1"
Visibility="Collapsed"
x:Uid="Oobe_WhatsNew_ProxyAuthenticationWarning"
VerticalAlignment="Top"
IsClosable="False"
IsOpen="True"
IsTabStop="True" />
<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto">
<Grid Margin="32,24,32,24">
<muxc:ProgressRing
x:Name="LoadingProgressRing"
IsIndeterminate="True"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Visibility="Visible"/>
<toolkitcontrols:MarkdownTextBlock x:Name="ReleaseNotesMarkdown"
Visibility="Collapsed"
Grid.Row="1"
Header1FontSize="20"
Header2FontSize="17"
Header2FontWeight="SemiBold"
Expand Down
49 changes: 39 additions & 10 deletions src/settings-ui/Settings.UI/OOBE/Views/OobeWhatsNew.xaml.cs
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
Expand All @@ -18,7 +19,6 @@
using Microsoft.PowerToys.Settings.UI.OOBE.ViewModel;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation;
using Windows.UI.Core;

namespace Microsoft.PowerToys.Settings.UI.OOBE.Views
{
Expand Down Expand Up @@ -55,14 +55,21 @@ public OobeWhatsNew()
private static async Task<string> GetReleaseNotesMarkdown()
{
string releaseNotesJSON = string.Empty;
using (HttpClient getReleaseInfoClient = new HttpClient())

// Let's use system proxy
using var proxyClientHandler = new HttpClientHandler
{
// GitHub APIs require sending an user agent
// https://docs.github.com/en/rest/overview/resources-in-the-rest-api#user-agent-required
getReleaseInfoClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "PowerToys");
releaseNotesJSON = await getReleaseInfoClient.GetStringAsync("https://api.github.com/repos/microsoft/PowerToys/releases");
}
DefaultProxyCredentials = CredentialCache.DefaultCredentials,
Proxy = WebRequest.GetSystemWebProxy(),
PreAuthenticate = true,
};

using var getReleaseInfoClient = new HttpClient(proxyClientHandler);

// GitHub APIs require sending an user agent
// https://docs.github.com/en/rest/overview/resources-in-the-rest-api#user-agent-required
getReleaseInfoClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "PowerToys");
releaseNotesJSON = await getReleaseInfoClient.GetStringAsync("https://api.github.com/repos/microsoft/PowerToys/releases");
IList<PowerToysReleaseInfo> releases = JsonSerializer.Deserialize<IList<PowerToysReleaseInfo>>(releaseNotesJSON);

// Get the latest releases
Expand All @@ -83,23 +90,45 @@ private static async Task<string> GetReleaseNotesMarkdown()
return releaseNotesHtmlBuilder.ToString();
}

private async void Page_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
private async Task Reload()
{
try
{
string releaseNotesMarkdown = await GetReleaseNotesMarkdown();

ProxyWarningInfoBar.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed;
ErrorInfoBar.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed;

ReleaseNotesMarkdown.Text = releaseNotesMarkdown;
ReleaseNotesMarkdown.Visibility = Microsoft.UI.Xaml.Visibility.Visible;
LoadingProgressRing.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed;
}
catch (HttpRequestException httpEx)
{
Logger.LogError("Exception when loading the release notes", httpEx);
if (httpEx.Message.Contains("407", StringComparison.CurrentCulture))
{
ProxyWarningInfoBar.Visibility = Microsoft.UI.Xaml.Visibility.Visible;
}
else
{
ErrorInfoBar.Visibility = Microsoft.UI.Xaml.Visibility.Visible;
}
}
catch (Exception ex)
{
Logger.LogError("Exception when loading the release notes", ex);

LoadingProgressRing.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed;
ErrorInfoBar.Visibility = Microsoft.UI.Xaml.Visibility.Visible;
}
finally
{
LoadingProgressRing.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed;
}
}

private async void Page_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
await Reload();
}

/// <inheritdoc/>
Expand Down
6 changes: 6 additions & 0 deletions src/settings-ui/Settings.UI/Strings/en-us/Resources.resw
Expand Up @@ -1488,6 +1488,12 @@ From there, simply click on one of the supported files in the File Explorer and
<data name="Oobe_WhatsNew_LoadingError.Message" xml:space="preserve">
<value>Please check your internet connection.</value>
</data>
<data name="Oobe_WhatsNew_ProxyAuthenticationWarning.Title" xml:space="preserve">
<value>Couldn't load the release notes.</value>
</data>
<data name="Oobe_WhatsNew_ProxyAuthenticationWarning.Message" xml:space="preserve">
<value>Your proxy server requires authentication.</value>
</data>
<data name="Oobe_WhatsNew_DetailedReleaseNotesLink.Text" xml:space="preserve">
<value>See more detailed release notes on GitHub</value>
<comment>Don't loc "GitHub", it's the name of a product</comment>
Expand Down

0 comments on commit e8bb2de

Please sign in to comment.