Skip to content

Commit

Permalink
Show special message for GitHub 403 with option to jump to auth tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Feb 9, 2018
1 parent 9852b4c commit 990e923
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 3 deletions.
6 changes: 6 additions & 0 deletions Cmdline/Action/Install.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ public int RunCommand(CKAN.KSP ksp, object raw_options)
user.RaiseMessage(kraken.ToString());
return Exit.ERROR;
}
catch (DownloadThrottledKraken kraken)
{
user.RaiseMessage(kraken.ToString());
user.RaiseMessage($"Try the authtoken command. See {kraken.infoUrl} for details.");
return Exit.ERROR;
}
catch (DownloadErrorsKraken)
{
user.RaiseMessage("One or more files failed to download, stopped.");
Expand Down
7 changes: 7 additions & 0 deletions ConsoleUI/InstallScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ public override void Run(Action process = null)
RaiseError(ex.ToString());
} catch (ModuleDownloadErrorsKraken ex) {
RaiseError(ex.ToString());
} catch (DownloadThrottledKraken ex) {
if (RaiseYesNoDialog($"{ex.ToString()}\n\nEdit authentication tokens now?")) {
if (ex.infoUrl != null) {
ModInfoScreen.LaunchURL(ex.infoUrl);
}
LaunchSubScreen(new AuthTokenScreen());
}
} catch (MissingCertificateKraken ex) {
RaiseError(ex.ToString());
} catch (InconsistentKraken ex) {
Expand Down
8 changes: 8 additions & 0 deletions Core/Net/Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public class Net
private static readonly ILog Log = LogManager.GetLogger(typeof (Net));
private static readonly TxFileManager FileTransaction = new TxFileManager();

public static readonly Dictionary<string, Uri> ThrottledHosts = new Dictionary<string, Uri>()
{
{
"api.github.com",
new Uri("https://github.com/KSP-CKAN/CKAN/wiki/Adding-a-GitHub-API-authtoken")
}
};

/// <summary>
/// Downloads the specified url, and stores it in the filename given.
/// If no filename is supplied, a temporary file will be generated.
Expand Down
20 changes: 17 additions & 3 deletions Core/Net/NetAsyncDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,24 @@ public void DownloadAndWait(ICollection<Net.DownloadTarget> urls)
{
// Check if it's a certificate error. If so, report that instead,
// as this is common (and user-fixable) under Linux.
if (downloads[i].error is WebException
&& certificatePattern.IsMatch(downloads[i].error.Message))
if (downloads[i].error is WebException)
{
throw new MissingCertificateKraken();
WebException wex = downloads[i].error as WebException;
if (certificatePattern.IsMatch(wex.Message))
{
throw new MissingCertificateKraken();
}
else switch ((wex.Response as HttpWebResponse)?.StatusCode)
{
// Handle HTTP 403 used for throttling
case HttpStatusCode.Forbidden:
Uri infoUrl;
if (Net.ThrottledHosts.TryGetValue(downloads[i].url.Host, out infoUrl))
{
throw new DownloadThrottledKraken(downloads[i].url, infoUrl);
}
break;
}
}
// Otherwise just note the error and which download it came from,
// then throw them all at once later.
Expand Down
18 changes: 18 additions & 0 deletions Core/Types/Kraken.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net;
using System.Text;
using System.Collections.Generic;

Expand Down Expand Up @@ -380,6 +381,23 @@ public override string ToString()
}
}

public class DownloadThrottledKraken : Kraken
{
public readonly Uri throttledUrl;
public readonly Uri infoUrl;

public DownloadThrottledKraken(Uri url, Uri info) : base()
{
throttledUrl = url;
infoUrl = info;
}

public override string ToString()
{
return $"Download from {throttledUrl.Host} was throttled.\r\nConsider adding an authentication token to increase the throtting limit.";
}
}

public class RegistryInUseKraken : Kraken
{
public readonly string lockfilePath;
Expand Down
23 changes: 23 additions & 0 deletions GUI/MainInstall.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
Expand Down Expand Up @@ -194,6 +195,28 @@ private void InstallMods(object sender, DoWorkEventArgs e) // this probably need
GUI.user.RaiseMessage(kraken.ToString());
return;
}
catch (DownloadThrottledKraken kraken)
{
string msg = kraken.ToString();
GUI.user.RaiseMessage(msg);
if (YesNoDialog($"{msg}\r\n\r\nOpen settings now?"))
{
// Launch the URL describing this host's throttling practices, if any
if (kraken.infoUrl != null)
{
Process.Start(new ProcessStartInfo()
{
UseShellExecute = true,
FileName = kraken.infoUrl.ToString()
});
}
// Now pretend they clicked the menu option for the settings
Enabled = false;
settingsDialog.ShowDialog();
Enabled = true;
}
return;
}
catch (ModuleDownloadErrorsKraken kraken)
{
GUI.user.RaiseMessage(kraken.ToString());
Expand Down

0 comments on commit 990e923

Please sign in to comment.