Commit 15f2969b authored by justcoding121's avatar justcoding121

#309 upstream override

parent 5494a981
......@@ -98,13 +98,15 @@ namespace Titanium.Web.Proxy.EventArguments
/// <summary>
/// Constructor to initialize the proxy
/// </summary>
internal SessionEventArgs(int bufferSize, ProxyEndPoint endPoint, Func<SessionEventArgs, Task> httpResponseHandler)
internal SessionEventArgs(int bufferSize,
ProxyEndPoint endPoint, IPEndPoint upStreamEndPoint,
Func<SessionEventArgs, Task> httpResponseHandler)
{
this.bufferSize = bufferSize;
this.httpResponseHandler = httpResponseHandler;
ProxyClient = new ProxyClient();
WebSession = new HttpWebClient(bufferSize);
WebSession = new HttpWebClient(bufferSize, upStreamEndPoint);
WebSession.ProcessId = new Lazy<int>(() =>
{
......
using Titanium.Web.Proxy.Models;
using System.Net;
using Titanium.Web.Proxy.Models;
namespace Titanium.Web.Proxy.EventArguments
{
......@@ -6,7 +7,8 @@ namespace Titanium.Web.Proxy.EventArguments
{
public bool IsHttpsConnect { get; set; }
public TunnelConnectSessionEventArgs(ProxyEndPoint endPoint) : base(0, endPoint, null)
public TunnelConnectSessionEventArgs(int bufferSize, ProxyEndPoint endPoint, IPEndPoint upStreamEndPoint)
: base(bufferSize, endPoint, upStreamEndPoint, null)
{
}
......
......@@ -25,6 +25,11 @@ namespace Titanium.Web.Proxy.Http
/// </summary>
public Guid RequestId { get; }
/// <summary>
/// Override UpStreamEndPoint for this request; Local NIC via request is made
/// </summary>
public IPEndPoint UpStreamEndPoint { get; set; }
/// <summary>
/// Headers passed with Connect.
/// </summary>
......@@ -51,9 +56,10 @@ namespace Titanium.Web.Proxy.Http
/// </summary>
public bool IsHttps => Request.IsHttps;
internal HttpWebClient(int bufferSize)
internal HttpWebClient(int bufferSize, IPEndPoint upStreamEndPoint)
{
this.bufferSize = bufferSize;
UpStreamEndPoint = upStreamEndPoint;
RequestId = Guid.NewGuid();
Request = new Request();
......
using StreamExtended.Network;
using System;
using System.Net;
using System.Net.Sockets;
using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Models;
......@@ -25,6 +26,11 @@ namespace Titanium.Web.Proxy.Network.Tcp
internal bool UseUpstreamProxy { get; set; }
/// <summary>
/// Local NIC via connection is made
/// </summary>
internal IPEndPoint UpStreamEndPoint { get; set; }
/// <summary>
/// Http version
/// </summary>
......
using StreamExtended.Network;
using System;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Text;
......@@ -17,19 +18,21 @@ namespace Titanium.Web.Proxy.Network.Tcp
internal class TcpConnectionFactory
{
/// <summary>
/// Creates a TCP connection to server
/// Creates a TCP connection to server
/// </summary>
/// <param name="server"></param>
/// <param name="remoteHostName"></param>
/// <param name="remotePort"></param>
/// <param name="httpVersion"></param>
/// <param name="isConnect"></param>
/// <param name="isHttps"></param>
/// <param name="isConnect"></param>
/// <param name="upStreamEndPoint"></param>
/// <param name="externalHttpProxy"></param>
/// <param name="externalHttpsProxy"></param>
/// <returns></returns>
internal async Task<TcpConnection> CreateClient(ProxyServer server, string remoteHostName, int remotePort, Version httpVersion, bool isHttps,
bool isConnect, ExternalProxy externalHttpProxy, ExternalProxy externalHttpsProxy)
internal async Task<TcpConnection> CreateClient(ProxyServer server,
string remoteHostName, int remotePort, Version httpVersion, bool isHttps,
bool isConnect, IPEndPoint upStreamEndPoint, ExternalProxy externalHttpProxy, ExternalProxy externalHttpsProxy)
{
bool useUpstreamProxy = false;
var externalProxy = isHttps ? externalHttpsProxy : externalHttpProxy;
......@@ -52,7 +55,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
try
{
#if NET45
client = new TcpClient(server.UpStreamEndPoint);
client = new TcpClient(upStreamEndPoint??server.UpStreamEndPoint);
#else
client = new TcpClient();
client.Client.Bind(server.UpStreamEndPoint);
......
......@@ -86,7 +86,7 @@ namespace Titanium.Web.Proxy
await HeaderParser.ReadHeaders(clientStreamReader, connectRequest.RequestHeaders);
var connectArgs = new TunnelConnectSessionEventArgs(endPoint);
var connectArgs = new TunnelConnectSessionEventArgs(BufferSize, endPoint, UpStreamEndPoint);
connectArgs.WebSession.Request = connectRequest;
connectArgs.ProxyClient.TcpClient = tcpClient;
connectArgs.ProxyClient.ClientStream = clientStream;
......@@ -293,7 +293,7 @@ namespace Titanium.Web.Proxy
break;
}
var args = new SessionEventArgs(BufferSize, endPoint, HandleHttpSessionResponse)
var args = new SessionEventArgs(BufferSize, endPoint, UpStreamEndPoint, HandleHttpSessionResponse)
{
ProxyClient = { TcpClient = client },
WebSession = { ConnectRequest = connectRequest }
......@@ -354,8 +354,10 @@ namespace Titanium.Web.Proxy
break;
}
//create a new connection if hostname changes
if (connection != null && !connection.HostName.Equals(args.WebSession.Request.RequestUri.Host, StringComparison.OrdinalIgnoreCase))
//create a new connection if hostname/upstream end point changes
if (connection != null
&& (!connection.HostName.Equals(args.WebSession.Request.RequestUri.Host, StringComparison.OrdinalIgnoreCase)
|| args.WebSession.UpStreamEndPoint != connection.UpStreamEndPoint))
{
connection.Dispose();
UpdateServerConnectionCount(false);
......@@ -571,7 +573,7 @@ namespace Titanium.Web.Proxy
/// <param name="args"></param>
/// <param name="isConnect"></param>
/// <returns></returns>
private async Task<TcpConnection> GetServerConnection(SessionEventArgs args, bool isConnect)
private async Task<TcpConnection> GetServerConnection(SessionEventArgs args, bool isConnect)
{
ExternalProxy customUpStreamHttpProxy = null;
ExternalProxy customUpStreamHttpsProxy = null;
......@@ -599,6 +601,7 @@ namespace Titanium.Web.Proxy
args.WebSession.Request.RequestUri.Port,
args.WebSession.Request.HttpVersion,
args.IsHttps, isConnect,
args.WebSession.UpStreamEndPoint,
customUpStreamHttpProxy ?? UpStreamHttpProxy,
customUpStreamHttpsProxy ?? UpStreamHttpsProxy);
}
......
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