Use event triggered update method instead of checking Proxy every time doing Http request

This commit is contained in:
张弘韬 2020-12-22 00:31:00 +08:00
parent 5ab8c4faa3
commit 88fa862277
2 changed files with 105 additions and 40 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,60 +33,54 @@ 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;
}
}
private static WebProxy _proxy = new WebProxy();
public static WebProxy WebProxy {
get
{
UpdateProxy();
return _proxy;
}
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 static async Task Download([NotNull] string url, [NotNull] string filePath)
{
UpdateProxy();
using var response = await client.GetAsync(url);
if (response.StatusCode == HttpStatusCode.OK)
{
@ -99,7 +95,6 @@ namespace Flow.Launcher.Infrastructure.Http
public static async Task<string> Get([NotNull] string url, string encoding = "UTF-8")
{
UpdateProxy();
Log.Debug($"|Http.Get|Url <{url}>");
var response = await client.GetAsync(url);
await using var stream = await response.Content.ReadAsStreamAsync();
@ -111,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);
}
}
}