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.UserSettings;
using System;
using System.ComponentModel;
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 static HttpClient client;
private static SocketsHttpHandler socketsHttpHandler = new SocketsHttpHandler()
{
UseProxy = true,
@ -31,56 +33,58 @@ namespace Flow.Launcher.Infrastructure.Http
client = new HttpClient(socketsHttpHandler, false);
client.DefaultRequestHeaders.Add("User-Agent", UserAgent);
}
private static HttpProxy proxy;
public static HttpProxy Proxy
{
private get
{
return proxy;
}
private get { return proxy; }
set
{
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>
/// Update the Address of the Proxy to modify the client Proxy
/// </summary>
public static void UpdateProxy()
// TODO: need test with a proxy
public static void UpdateProxy(ProxyProperty property)
{
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}");
WebProxy.Credentials = null;
}
else
{
WebProxy.Address = new Uri($"http://{Proxy.Server}:{Proxy.Port}");
WebProxy.Credentials = new NetworkCredential(Proxy.UserName, Proxy.Password);
}
}
else
{
WebProxy.Address = new WebProxy().Address;
WebProxy.Credentials = null;
}
true => Proxy.UserName switch
{
var userName when !string.IsNullOrEmpty(userName) =>
(new Uri($"http://{Proxy.Server}:{Proxy.Port}"), null),
_ => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"),
new NetworkCredential(Proxy.UserName, Proxy.Password))
},
false => (null, null)
},
ProxyProperty.Server => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), _proxy.Credentials),
ProxyProperty.Port => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), _proxy.Credentials),
ProxyProperty.UserName => (_proxy.Address, new NetworkCredential(Proxy.UserName, Proxy.Password)),
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);
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);
}
else
@ -93,7 +97,7 @@ namespace Flow.Launcher.Infrastructure.Http
{
Log.Debug($"|Http.Get|Url <{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));
var content = await reader.ReadToEndAsync();
if (response.StatusCode == HttpStatusCode.OK)
@ -102,7 +106,8 @@ namespace Flow.Launcher.Infrastructure.Http
}
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 bool Enabled { get; set; } = false;
public string Server { get; set; }
public int Port { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
private bool _enabled = false;
private string _server;
private int _port;
private string _userName;
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 List<UserPlugin> UserPlugins { get; private set; }
internal PluginsManifest()
{
DownloadManifest();
DownloadManifest().Wait();
}
private void DownloadManifest()
private async Task DownloadManifest()
{
var json = string.Empty;
try
{
var t = Task.Run(
async () =>
json = await Http.Get("https://raw.githubusercontent.com/Flow-Launcher/Flow.Launcher.PluginsManifest/main/plugins.json"));
t.Wait();
json = await Http.Get(
"https://raw.githubusercontent.com/Flow-Launcher/Flow.Launcher.PluginsManifest/main/plugins.json");
UserPlugins = JsonConvert.DeserializeObject<List<UserPlugin>>(json);
}
@ -34,7 +32,6 @@ namespace Flow.Launcher.Plugin.PluginsManager.Models
UserPlugins = new List<UserPlugin>();
}
}
}
}
}