Commit f37e85f0 authored by justcoding121's avatar justcoding121 Committed by justcoding121

#309 upstream override

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