Unverified Commit 347c310c authored by honfika's avatar honfika Committed by GitHub

Merge pull request #662 from ByronAP/use-interfaces

ExternalProxy interface
parents 21f81907 97279508
...@@ -106,7 +106,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -106,7 +106,7 @@ namespace Titanium.Web.Proxy.EventArguments
/// <summary> /// <summary>
/// Are we using a custom upstream HTTP(S) proxy? /// Are we using a custom upstream HTTP(S) proxy?
/// </summary> /// </summary>
public ExternalProxy? CustomUpStreamProxyUsed { get; internal set; } public IExternalProxy? CustomUpStreamProxyUsed { get; internal set; }
/// <summary> /// <summary>
/// Local endpoint via which we make the request. /// Local endpoint via which we make the request.
......
...@@ -95,7 +95,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp ...@@ -95,7 +95,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
return true; return true;
} }
public ExternalProxy? GetProxy(Uri destination) public IExternalProxy? GetProxy(Uri destination)
{ {
if (GetAutoProxies(destination, out var proxies)) if (GetAutoProxies(destination, out var proxies))
{ {
......
...@@ -6,7 +6,7 @@ namespace Titanium.Web.Proxy.Models ...@@ -6,7 +6,7 @@ namespace Titanium.Web.Proxy.Models
/// <summary> /// <summary>
/// An upstream proxy this proxy uses if any. /// An upstream proxy this proxy uses if any.
/// </summary> /// </summary>
public class ExternalProxy public class ExternalProxy : IExternalProxy
{ {
private static readonly Lazy<NetworkCredential> defaultCredentials = private static readonly Lazy<NetworkCredential> defaultCredentials =
new Lazy<NetworkCredential>(() => CredentialCache.DefaultNetworkCredentials); new Lazy<NetworkCredential>(() => CredentialCache.DefaultNetworkCredentials);
......
namespace Titanium.Web.Proxy.Models
{
public interface IExternalProxy
{
/// <summary>
/// Use default windows credentials?
/// </summary>
bool UseDefaultCredentials { get; set; }
/// <summary>
/// Bypass this proxy for connections to localhost?
/// </summary>
bool BypassLocalhost { get; set; }
/// <summary>
/// Username.
/// </summary>
string? UserName { get; set; }
/// <summary>
/// Password.
/// </summary>
string? Password { get; set; }
/// <summary>
/// Host name.
/// </summary>
string HostName { get; set; }
/// <summary>
/// Port.
/// </summary>
int Port { get; set; }
string ToString();
}
}
...@@ -49,7 +49,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -49,7 +49,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
internal string GetConnectionCacheKey(string remoteHostName, int remotePort, internal string GetConnectionCacheKey(string remoteHostName, int remotePort,
bool isHttps, List<SslApplicationProtocol>? applicationProtocols, bool isHttps, List<SslApplicationProtocol>? applicationProtocols,
IPEndPoint? upStreamEndPoint, ExternalProxy? externalProxy) IPEndPoint? upStreamEndPoint, IExternalProxy? externalProxy)
{ {
// http version is ignored since its an application level decision b/w HTTP 1.0/1.1 // http version is ignored since its an application level decision b/w HTTP 1.0/1.1
// also when doing connect request MS Edge browser sends http 1.0 but uses 1.1 after server sends 1.1 its response. // also when doing connect request MS Edge browser sends http 1.0 but uses 1.1 after server sends 1.1 its response.
...@@ -115,7 +115,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -115,7 +115,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
applicationProtocols = new List<SslApplicationProtocol> { applicationProtocol }; applicationProtocols = new List<SslApplicationProtocol> { applicationProtocol };
} }
ExternalProxy? customUpStreamProxy = null; IExternalProxy? customUpStreamProxy = null;
bool isHttps = session.IsHttps; bool isHttps = session.IsHttps;
if (server.GetCustomUpStreamProxyFunc != null) if (server.GetCustomUpStreamProxyFunc != null)
...@@ -170,7 +170,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -170,7 +170,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
internal async Task<TcpServerConnection> GetServerConnection(ProxyServer server, SessionEventArgsBase session, bool isConnect, internal async Task<TcpServerConnection> GetServerConnection(ProxyServer server, SessionEventArgsBase session, bool isConnect,
List<SslApplicationProtocol>? applicationProtocols, bool noCache, CancellationToken cancellationToken) List<SslApplicationProtocol>? applicationProtocols, bool noCache, CancellationToken cancellationToken)
{ {
ExternalProxy? customUpStreamProxy = null; IExternalProxy? customUpStreamProxy = null;
bool isHttps = session.IsHttps; bool isHttps = session.IsHttps;
if (server.GetCustomUpStreamProxyFunc != null) if (server.GetCustomUpStreamProxyFunc != null)
...@@ -208,7 +208,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -208,7 +208,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
/// <returns></returns> /// <returns></returns>
internal async Task<TcpServerConnection> GetServerConnection(string remoteHostName, int remotePort, internal async Task<TcpServerConnection> GetServerConnection(string remoteHostName, int remotePort,
Version httpVersion, bool isHttps, List<SslApplicationProtocol>? applicationProtocols, bool isConnect, Version httpVersion, bool isHttps, List<SslApplicationProtocol>? applicationProtocols, bool isConnect,
ProxyServer proxyServer, SessionEventArgsBase? session, IPEndPoint? upStreamEndPoint, ExternalProxy? externalProxy, ProxyServer proxyServer, SessionEventArgsBase? session, IPEndPoint? upStreamEndPoint, IExternalProxy? externalProxy,
bool noCache, CancellationToken cancellationToken) bool noCache, CancellationToken cancellationToken)
{ {
var sslProtocol = session?.ProxyClient.Connection.SslProtocol ?? SslProtocols.None; var sslProtocol = session?.ProxyClient.Connection.SslProtocol ?? SslProtocols.None;
...@@ -262,7 +262,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -262,7 +262,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
/// <returns></returns> /// <returns></returns>
private async Task<TcpServerConnection> createServerConnection(string remoteHostName, int remotePort, private async Task<TcpServerConnection> createServerConnection(string remoteHostName, int remotePort,
Version httpVersion, bool isHttps, SslProtocols sslProtocol, List<SslApplicationProtocol>? applicationProtocols, bool isConnect, Version httpVersion, bool isHttps, SslProtocols sslProtocol, List<SslApplicationProtocol>? applicationProtocols, bool isConnect,
ProxyServer proxyServer, SessionEventArgsBase? session, IPEndPoint? upStreamEndPoint, ExternalProxy? externalProxy, string cacheKey, ProxyServer proxyServer, SessionEventArgsBase? session, IPEndPoint? upStreamEndPoint, IExternalProxy? externalProxy, string cacheKey,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
// deny connection to proxy end points to avoid infinite connection loop. // deny connection to proxy end points to avoid infinite connection loop.
...@@ -304,7 +304,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -304,7 +304,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
bool retry = true; bool retry = true;
var enabledSslProtocols = sslProtocol; var enabledSslProtocols = sslProtocol;
retry: retry:
try try
{ {
string hostname = useUpstreamProxy ? externalProxy!.HostName : remoteHostName; string hostname = useUpstreamProxy ? externalProxy!.HostName : remoteHostName;
......
...@@ -17,7 +17,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -17,7 +17,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
{ {
internal TcpServerConnection(ProxyServer proxyServer, TcpClient tcpClient, CustomBufferedStream stream, internal TcpServerConnection(ProxyServer proxyServer, TcpClient tcpClient, CustomBufferedStream stream,
string hostName, int port, bool isHttps, SslApplicationProtocol negotiatedApplicationProtocol, string hostName, int port, bool isHttps, SslApplicationProtocol negotiatedApplicationProtocol,
Version version, bool useUpstreamProxy, ExternalProxy? upStreamProxy, IPEndPoint? upStreamEndPoint, string cacheKey) Version version, bool useUpstreamProxy, IExternalProxy? upStreamProxy, IPEndPoint? upStreamEndPoint, string cacheKey)
{ {
this.tcpClient = tcpClient; this.tcpClient = tcpClient;
LastAccess = DateTime.Now; LastAccess = DateTime.Now;
...@@ -41,7 +41,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -41,7 +41,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
internal bool IsClosed => Stream.IsClosed; internal bool IsClosed => Stream.IsClosed;
internal ExternalProxy? UpStreamProxy { get; set; } internal IExternalProxy? UpStreamProxy { get; set; }
internal string HostName { get; set; } internal string HostName { get; set; }
......
...@@ -61,7 +61,7 @@ namespace Titanium.Web.Proxy ...@@ -61,7 +61,7 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
private WinHttpWebProxyFinder? systemProxyResolver; private WinHttpWebProxyFinder? systemProxyResolver;
/// <inheritdoc /> /// <inheritdoc />
/// <summary> /// <summary>
/// Initializes a new instance of ProxyServer class with provided parameters. /// Initializes a new instance of ProxyServer class with provided parameters.
...@@ -145,7 +145,7 @@ namespace Titanium.Web.Proxy ...@@ -145,7 +145,7 @@ namespace Titanium.Web.Proxy
/// Defaults to false. /// Defaults to false.
/// </summary> /// </summary>
public bool EnableWinAuth { get; set; } public bool EnableWinAuth { get; set; }
/// <summary> /// <summary>
/// Enable disable HTTP/2 support. /// Enable disable HTTP/2 support.
/// Warning: HTTP/2 support is very limited /// Warning: HTTP/2 support is very limited
...@@ -253,12 +253,12 @@ namespace Titanium.Web.Proxy ...@@ -253,12 +253,12 @@ namespace Titanium.Web.Proxy
/// <summary> /// <summary>
/// External proxy used for Http requests. /// External proxy used for Http requests.
/// </summary> /// </summary>
public ExternalProxy? UpStreamHttpProxy { get; set; } public IExternalProxy? UpStreamHttpProxy { get; set; }
/// <summary> /// <summary>
/// External proxy used for Https requests. /// External proxy used for Https requests.
/// </summary> /// </summary>
public ExternalProxy? UpStreamHttpsProxy { get; set; } public IExternalProxy? UpStreamHttpsProxy { get; set; }
/// <summary> /// <summary>
/// Local adapter/NIC endpoint where proxy makes request via. /// Local adapter/NIC endpoint where proxy makes request via.
...@@ -275,7 +275,7 @@ namespace Titanium.Web.Proxy ...@@ -275,7 +275,7 @@ namespace Titanium.Web.Proxy
/// A callback to provide authentication credentials for up stream proxy this proxy is using for HTTP(S) requests. /// A callback to provide authentication credentials for up stream proxy this proxy is using for HTTP(S) requests.
/// User should return the ExternalProxy object with valid credentials. /// User should return the ExternalProxy object with valid credentials.
/// </summary> /// </summary>
public Func<SessionEventArgsBase, Task<ExternalProxy?>>? GetCustomUpStreamProxyFunc { get; set; } public Func<SessionEventArgsBase, Task<IExternalProxy?>>? GetCustomUpStreamProxyFunc { get; set; }
/// <summary> /// <summary>
/// Callback for error events in this proxy instance. /// Callback for error events in this proxy instance.
...@@ -709,7 +709,7 @@ namespace Titanium.Web.Proxy ...@@ -709,7 +709,7 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
/// <param name="sessionEventArgs">The session.</param> /// <param name="sessionEventArgs">The session.</param>
/// <returns>The external proxy as task result.</returns> /// <returns>The external proxy as task result.</returns>
private Task<ExternalProxy?> getSystemUpStreamProxy(SessionEventArgsBase sessionEventArgs) private Task<IExternalProxy?> getSystemUpStreamProxy(SessionEventArgsBase sessionEventArgs)
{ {
var proxy = systemProxyResolver!.GetProxy(sessionEventArgs.HttpClient.Request.RequestUri); var proxy = systemProxyResolver!.GetProxy(sessionEventArgs.HttpClient.Request.RequestUri);
return Task.FromResult(proxy); return Task.FromResult(proxy);
......
...@@ -69,7 +69,7 @@ namespace Titanium.Web.Proxy.IntegrationTests ...@@ -69,7 +69,7 @@ namespace Titanium.Web.Proxy.IntegrationTests
}; };
var client = testSuite.GetClient(proxy1, true); var client = testSuite.GetClient(proxy1, true);
var response = await client.PostAsync(new Uri(server.ListeningHttpsUrl), var response = await client.PostAsync(new Uri(server.ListeningHttpsUrl),
new StringContent("hello server. I am a client.")); new StringContent("hello server. I am a client."));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment