Commit 3d746940 authored by Honfika's avatar Honfika

Allow to set upstream proxy in before request handler

parent b8cefa4c
......@@ -207,6 +207,11 @@ namespace Titanium.Web.Proxy.Examples.Basic
{
e.GetState().PipelineInfo.AppendLine(nameof(onRequest) + ":" + e.HttpClient.Request.RequestUri);
if (e.HttpClient.Request.Url.Contains("yahoo.com"))
{
e.CustomUpStreamProxy = new ExternalProxy("localhost", 8888);
}
await writeToConsole("Active Client Connections:" + ((ProxyServer)sender).ClientConnectionCount);
await writeToConsole(e.HttpClient.Request.Url);
......
......@@ -114,6 +114,14 @@ namespace Titanium.Web.Proxy.EventArguments
[Obsolete("Use HttpClient instead.")]
public HttpWebClient WebSession => HttpClient;
/// <summary>
/// Gets or sets the custom up stream proxy.
/// </summary>
/// <value>
/// The custom up stream proxy.
/// </value>
public IExternalProxy? CustomUpStreamProxy { get; set; }
/// <summary>
/// Are we using a custom upstream HTTP(S) proxy?
/// </summary>
......
......@@ -114,11 +114,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
}
// TODO: Apply authorization
var systemProxy = new ExternalProxy
{
HostName = proxyStr,
Port = port
};
var systemProxy = new ExternalProxy(proxyStr, port);
return systemProxy;
}
......@@ -134,12 +130,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
HttpSystemProxyValue? value = null;
if (ProxyInfo?.Proxies?.TryGetValue(protocolType.Value, out value) == true)
{
var systemProxy = new ExternalProxy
{
HostName = value!.HostName,
Port = value.Port
};
var systemProxy = new ExternalProxy(value!.HostName, value.Port);
return systemProxy;
}
}
......
......@@ -69,6 +69,39 @@ namespace Titanium.Web.Proxy.Models
/// </summary>
public int Port { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="ExternalProxy"/> class.
/// </summary>
public ExternalProxy()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ExternalProxy"/> class.
/// </summary>
/// <param name="hostName">Name of the host.</param>
/// <param name="port">The port.</param>
public ExternalProxy(string hostName, int port)
{
HostName = hostName;
Port = port;
}
/// <summary>
/// Initializes a new instance of the <see cref="ExternalProxy"/> class.
/// </summary>
/// <param name="hostName">Name of the host.</param>
/// <param name="port">The port.</param>
/// <param name="userName">Name of the user.</param>
/// <param name="password">The password.</param>
public ExternalProxy(string hostName, int port, string userName, string password)
{
HostName = hostName;
Port = port;
UserName = userName;
Password = password;
}
/// <summary>
/// returns data in Hostname:port format.
/// </summary>
......@@ -77,6 +110,5 @@ namespace Titanium.Web.Proxy.Models
{
return $"{HostName}:{Port}";
}
}
}
......@@ -114,10 +114,10 @@ namespace Titanium.Web.Proxy.Network.Tcp
applicationProtocols = new List<SslApplicationProtocol> { applicationProtocol };
}
IExternalProxy? customUpStreamProxy = null;
IExternalProxy? customUpStreamProxy = session.CustomUpStreamProxy;
bool isHttps = session.IsHttps;
if (server.GetCustomUpStreamProxyFunc != null)
if (customUpStreamProxy == null && server.GetCustomUpStreamProxyFunc != null)
{
customUpStreamProxy = await server.GetCustomUpStreamProxyFunc(session);
}
......@@ -169,10 +169,10 @@ namespace Titanium.Web.Proxy.Network.Tcp
internal async Task<TcpServerConnection> GetServerConnection(ProxyServer proxyServer, SessionEventArgsBase session, bool isConnect,
List<SslApplicationProtocol>? applicationProtocols, bool noCache, CancellationToken cancellationToken)
{
IExternalProxy? customUpStreamProxy = null;
IExternalProxy? customUpStreamProxy = session.CustomUpStreamProxy;
bool isHttps = session.IsHttps;
if (proxyServer.GetCustomUpStreamProxyFunc != null)
if (customUpStreamProxy == null && proxyServer.GetCustomUpStreamProxyFunc != null)
{
customUpStreamProxy = await proxyServer.GetCustomUpStreamProxyFunc(session);
}
......
......@@ -59,11 +59,7 @@ namespace Titanium.Web.Proxy.IntegrationTests
{
Assert.AreEqual("Test", session.UserData);
return await Task.FromResult(new Models.ExternalProxy
{
HostName = "localhost",
Port = proxy2.ProxyEndPoints[0].Port
});
return await Task.FromResult(new Models.ExternalProxy("localhost", proxy2.ProxyEndPoints[0].Port));
};
var client = testSuite.GetClient(proxy1, true);
......
......@@ -22,17 +22,8 @@ namespace Titanium.Web.Proxy.IntegrationTests.Setup
if (upStreamProxy != null)
{
ProxyServer.UpStreamHttpProxy = new ExternalProxy
{
HostName = "localhost",
Port = upStreamProxy.ProxyEndPoints[0].Port
};
ProxyServer.UpStreamHttpsProxy = new ExternalProxy
{
HostName = "localhost",
Port = upStreamProxy.ProxyEndPoints[0].Port
};
ProxyServer.UpStreamHttpProxy = new ExternalProxy("localhost", upStreamProxy.ProxyEndPoints[0].Port);
ProxyServer.UpStreamHttpsProxy = new ExternalProxy("localhost", upStreamProxy.ProxyEndPoints[0].Port);
}
ProxyServer.Start();
......
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