Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

ExternalProxy interface #662

Merged
merged 4 commits into from
Nov 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public bool EnableWinAuth
/// <summary>
/// Are we using a custom upstream HTTP(S) proxy?
/// </summary>
public ExternalProxy? CustomUpStreamProxyUsed { get; internal set; }
public IExternalProxy? CustomUpStreamProxyUsed { get; internal set; }

/// <summary>
/// Local endpoint via which we make the request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public bool GetAutoProxies(Uri destination, out IList<string>? proxyList)
return true;
}

public ExternalProxy? GetProxy(Uri destination)
public IExternalProxy? GetProxy(Uri destination)
{
if (GetAutoProxies(destination, out var proxies))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Titanium.Web.Proxy/Models/ExternalProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Titanium.Web.Proxy.Models
/// <summary>
/// An upstream proxy this proxy uses if any.
/// </summary>
public class ExternalProxy
public class ExternalProxy : IExternalProxy
{
private static readonly Lazy<NetworkCredential> defaultCredentials =
new Lazy<NetworkCredential>(() => CredentialCache.DefaultNetworkCredentials);
Expand Down
37 changes: 37 additions & 0 deletions src/Titanium.Web.Proxy/Models/IExternalProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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();
}
}
12 changes: 6 additions & 6 deletions src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal TcpConnectionFactory(ProxyServer server)

internal string GetConnectionCacheKey(string remoteHostName, int remotePort,
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
// also when doing connect request MS Edge browser sends http 1.0 but uses 1.1 after server sends 1.1 its response.
Expand Down Expand Up @@ -115,7 +115,7 @@ internal async Task<string> GetConnectionCacheKey(ProxyServer server, SessionEve
applicationProtocols = new List<SslApplicationProtocol> { applicationProtocol };
}

ExternalProxy? customUpStreamProxy = null;
IExternalProxy? customUpStreamProxy = null;

bool isHttps = session.IsHttps;
if (server.GetCustomUpStreamProxyFunc != null)
Expand Down Expand Up @@ -170,7 +170,7 @@ internal Task<TcpServerConnection> GetServerConnection(ProxyServer server, Sessi
internal async Task<TcpServerConnection> GetServerConnection(ProxyServer server, SessionEventArgsBase session, bool isConnect,
List<SslApplicationProtocol>? applicationProtocols, bool noCache, CancellationToken cancellationToken)
{
ExternalProxy? customUpStreamProxy = null;
IExternalProxy? customUpStreamProxy = null;

bool isHttps = session.IsHttps;
if (server.GetCustomUpStreamProxyFunc != null)
Expand Down Expand Up @@ -208,7 +208,7 @@ internal async Task<TcpServerConnection> GetServerConnection(ProxyServer server,
/// <returns></returns>
internal async Task<TcpServerConnection> GetServerConnection(string remoteHostName, int remotePort,
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)
{
var sslProtocol = session?.ProxyClient.Connection.SslProtocol ?? SslProtocols.None;
Expand Down Expand Up @@ -262,7 +262,7 @@ internal async Task<TcpServerConnection> GetServerConnection(string remoteHostNa
/// <returns></returns>
private async Task<TcpServerConnection> createServerConnection(string remoteHostName, int remotePort,
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)
{
// deny connection to proxy end points to avoid infinite connection loop.
Expand Down Expand Up @@ -304,7 +304,7 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
bool retry = true;
var enabledSslProtocols = sslProtocol;

retry:
retry:
try
{
string hostname = useUpstreamProxy ? externalProxy!.HostName : remoteHostName;
Expand Down
4 changes: 2 additions & 2 deletions src/Titanium.Web.Proxy/Network/Tcp/TcpServerConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class TcpServerConnection : IDisposable
{
internal TcpServerConnection(ProxyServer proxyServer, TcpClient tcpClient, CustomBufferedStream stream,
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;
LastAccess = DateTime.Now;
Expand All @@ -41,7 +41,7 @@ internal TcpServerConnection(ProxyServer proxyServer, TcpClient tcpClient, Custo

internal bool IsClosed => Stream.IsClosed;

internal ExternalProxy? UpStreamProxy { get; set; }
internal IExternalProxy? UpStreamProxy { get; set; }

internal string HostName { get; set; }

Expand Down
12 changes: 6 additions & 6 deletions src/Titanium.Web.Proxy/ProxyServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public partial class ProxyServer : IDisposable
/// </summary>
private WinHttpWebProxyFinder? systemProxyResolver;


/// <inheritdoc />
/// <summary>
/// Initializes a new instance of ProxyServer class with provided parameters.
Expand Down Expand Up @@ -145,7 +145,7 @@ public ProxyServer(string? rootCertificateName, string? rootCertificateIssuerNam
/// Defaults to false.
/// </summary>
public bool EnableWinAuth { get; set; }

/// <summary>
/// Enable disable HTTP/2 support.
/// Warning: HTTP/2 support is very limited
Expand Down Expand Up @@ -253,12 +253,12 @@ public ProxyServer(string? rootCertificateName, string? rootCertificateIssuerNam
/// <summary>
/// External proxy used for Http requests.
/// </summary>
public ExternalProxy? UpStreamHttpProxy { get; set; }
public IExternalProxy? UpStreamHttpProxy { get; set; }

/// <summary>
/// External proxy used for Https requests.
/// </summary>
public ExternalProxy? UpStreamHttpsProxy { get; set; }
public IExternalProxy? UpStreamHttpsProxy { get; set; }

/// <summary>
/// Local adapter/NIC endpoint where proxy makes request via.
Expand All @@ -275,7 +275,7 @@ public ProxyServer(string? rootCertificateName, string? rootCertificateIssuerNam
/// 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.
/// </summary>
public Func<SessionEventArgsBase, Task<ExternalProxy?>>? GetCustomUpStreamProxyFunc { get; set; }
public Func<SessionEventArgsBase, Task<IExternalProxy?>>? GetCustomUpStreamProxyFunc { get; set; }

/// <summary>
/// Callback for error events in this proxy instance.
Expand Down Expand Up @@ -709,7 +709,7 @@ private void validateEndPointAsSystemProxy(ExplicitProxyEndPoint endPoint)
/// </summary>
/// <param name="sessionEventArgs">The session.</param>
/// <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);
return Task.FromResult(proxy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task Smoke_Test_Nested_Proxy_UserData()
};

var client = testSuite.GetClient(proxy1, true);

var response = await client.PostAsync(new Uri(server.ListeningHttpsUrl),
new StringContent("hello server. I am a client."));

Expand Down