Merge branch 'UpdateHttpMaster' of github.com:taooceros/Flow.Launcher into UpdateHttpMaster

This commit is contained in:
弘韬 张 2020-12-29 17:08:29 +08:00
commit 8ec781d305
3 changed files with 115 additions and 44 deletions

View file

@ -7,6 +7,7 @@ using JetBrains.Annotations;
using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Infrastructure.UserSettings;
using System; using System;
using System.ComponentModel;
namespace Flow.Launcher.Infrastructure.Http namespace Flow.Launcher.Infrastructure.Http
{ {
@ -15,6 +16,7 @@ namespace Flow.Launcher.Infrastructure.Http
private const string UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko"; private const string UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko";
private static HttpClient client; private static HttpClient client;
private static SocketsHttpHandler socketsHttpHandler = new SocketsHttpHandler() private static SocketsHttpHandler socketsHttpHandler = new SocketsHttpHandler()
{ {
UseProxy = true, UseProxy = true,
@ -31,56 +33,58 @@ namespace Flow.Launcher.Infrastructure.Http
client = new HttpClient(socketsHttpHandler, false); client = new HttpClient(socketsHttpHandler, false);
client.DefaultRequestHeaders.Add("User-Agent", UserAgent); client.DefaultRequestHeaders.Add("User-Agent", UserAgent);
} }
private static HttpProxy proxy; private static HttpProxy proxy;
public static HttpProxy Proxy public static HttpProxy Proxy
{ {
private get private get { return proxy; }
{
return proxy;
}
set set
{ {
proxy = value; proxy = value;
UpdateProxy(); proxy.PropertyChanged += UpdateProxy;
} }
} }
public static WebProxy WebProxy { get; private set; } = new WebProxy(); private static readonly WebProxy _proxy = new WebProxy();
public static WebProxy WebProxy
{
get { return _proxy; }
}
/// <summary> /// <summary>
/// Update the Address of the Proxy to modify the client Proxy /// Update the Address of the Proxy to modify the client Proxy
/// </summary> /// </summary>
public static void UpdateProxy() public static void UpdateProxy(ProxyProperty property)
// TODO: need test with a proxy
{ {
if (Proxy != null && Proxy.Enabled && !string.IsNullOrEmpty(Proxy.Server)) (_proxy.Address, _proxy.Credentials) = property switch
{ {
if (string.IsNullOrEmpty(Proxy.UserName) || string.IsNullOrEmpty(Proxy.Password)) ProxyProperty.Enabled => (Proxy.Enabled) switch
{ {
WebProxy.Address = new Uri($"http://{Proxy.Server}:{Proxy.Port}"); true => Proxy.UserName switch
WebProxy.Credentials = null; {
} var userName when !string.IsNullOrEmpty(userName) =>
else (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), null),
{ _ => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"),
WebProxy.Address = new Uri($"http://{Proxy.Server}:{Proxy.Port}"); new NetworkCredential(Proxy.UserName, Proxy.Password))
WebProxy.Credentials = new NetworkCredential(Proxy.UserName, Proxy.Password); },
} false => (null, null)
} },
else ProxyProperty.Server => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), _proxy.Credentials),
{ ProxyProperty.Port => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), _proxy.Credentials),
WebProxy.Address = new WebProxy().Address; ProxyProperty.UserName => (_proxy.Address, new NetworkCredential(Proxy.UserName, Proxy.Password)),
WebProxy.Credentials = null; ProxyProperty.Password => (_proxy.Address, new NetworkCredential(Proxy.UserName, Proxy.Password))
} };
} }
public async static Task Download([NotNull] string url, [NotNull] string filePath) public static async Task Download([NotNull] string url, [NotNull] string filePath)
{ {
using var response = await client.GetAsync(url); using var response = await client.GetAsync(url);
if (response.StatusCode == HttpStatusCode.OK) if (response.StatusCode == HttpStatusCode.OK)
{ {
using var fileStream = new FileStream(filePath, FileMode.CreateNew); await using var fileStream = new FileStream(filePath, FileMode.CreateNew);
await response.Content.CopyToAsync(fileStream); await response.Content.CopyToAsync(fileStream);
} }
else else
@ -93,7 +97,7 @@ namespace Flow.Launcher.Infrastructure.Http
{ {
Log.Debug($"|Http.Get|Url <{url}>"); Log.Debug($"|Http.Get|Url <{url}>");
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
using var stream = await response.Content.ReadAsStreamAsync(); await using var stream = await response.Content.ReadAsStreamAsync();
using var reader = new StreamReader(stream, Encoding.GetEncoding(encoding)); using var reader = new StreamReader(stream, Encoding.GetEncoding(encoding));
var content = await reader.ReadToEndAsync(); var content = await reader.ReadToEndAsync();
if (response.StatusCode == HttpStatusCode.OK) if (response.StatusCode == HttpStatusCode.OK)
@ -102,7 +106,8 @@ namespace Flow.Launcher.Infrastructure.Http
} }
else else
{ {
throw new HttpRequestException($"Error code <{response.StatusCode}> with content <{content}> returned from <{url}>"); throw new HttpRequestException(
$"Error code <{response.StatusCode}> with content <{content}> returned from <{url}>");
} }
} }
} }

View file

@ -1,11 +1,80 @@
namespace Flow.Launcher.Infrastructure.UserSettings using System.ComponentModel;
namespace Flow.Launcher.Infrastructure.UserSettings
{ {
public enum ProxyProperty
{
Enabled,
Server,
Port,
UserName,
Password
}
public class HttpProxy public class HttpProxy
{ {
public bool Enabled { get; set; } = false; private bool _enabled = false;
public string Server { get; set; } private string _server;
public int Port { get; set; } private int _port;
public string UserName { get; set; } private string _userName;
public string Password { get; set; } private string _password;
public bool Enabled
{
get => _enabled;
set
{
_enabled = value;
OnPropertyChanged(ProxyProperty.Enabled);
}
}
public string Server
{
get => _server;
set
{
_server = value;
OnPropertyChanged(ProxyProperty.Server);
}
}
public int Port
{
get => _port;
set
{
_port = value;
OnPropertyChanged(ProxyProperty.Port);
}
}
public string UserName
{
get => _userName;
set
{
_userName = value;
OnPropertyChanged(ProxyProperty.UserName);
}
}
public string Password
{
get => _password;
set
{
_password = value;
OnPropertyChanged(ProxyProperty.Password);
}
}
public delegate void ProxyPropertyChangedHandler(ProxyProperty property);
public event ProxyPropertyChangedHandler PropertyChanged;
private void OnPropertyChanged(ProxyProperty property)
{
PropertyChanged?.Invoke(property);
}
} }
} }

View file

@ -10,21 +10,19 @@ namespace Flow.Launcher.Plugin.PluginsManager.Models
internal class PluginsManifest internal class PluginsManifest
{ {
internal List<UserPlugin> UserPlugins { get; private set; } internal List<UserPlugin> UserPlugins { get; private set; }
internal PluginsManifest() internal PluginsManifest()
{ {
DownloadManifest(); DownloadManifest().Wait();
} }
private void DownloadManifest() private async Task DownloadManifest()
{ {
var json = string.Empty; var json = string.Empty;
try try
{ {
var t = Task.Run( json = await Http.Get(
async () => "https://raw.githubusercontent.com/Flow-Launcher/Flow.Launcher.PluginsManifest/main/plugins.json");
json = await Http.Get("https://raw.githubusercontent.com/Flow-Launcher/Flow.Launcher.PluginsManifest/main/plugins.json"));
t.Wait();
UserPlugins = JsonConvert.DeserializeObject<List<UserPlugin>>(json); UserPlugins = JsonConvert.DeserializeObject<List<UserPlugin>>(json);
} }
@ -34,7 +32,6 @@ namespace Flow.Launcher.Plugin.PluginsManager.Models
UserPlugins = new List<UserPlugin>(); UserPlugins = new List<UserPlugin>();
} }
} }
} }
} }