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

beta #710

Merged
merged 3 commits into from
Dec 23, 2019
Merged

beta #710

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
11 changes: 11 additions & 0 deletions examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ private async Task onRequest(object sender, SessionEventArgs e)
{
e.GetState().PipelineInfo.AppendLine(nameof(onRequest) + ":" + e.HttpClient.Request.RequestUri);

var clientLocalIp = e.ClientLocalEndPoint.Address;
if (!clientLocalIp.Equals(IPAddress.Loopback) && !clientLocalIp.Equals(IPAddress.IPv6Loopback))
{
e.HttpClient.UpStreamEndPoint = new IPEndPoint(clientLocalIp, 0);
}

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);

Expand Down
20 changes: 17 additions & 3 deletions src/Titanium.Web.Proxy/EventArguments/SessionEventArgsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private protected SessionEventArgsBase(ProxyServer server, ProxyEndPoint endPoin

ClientStream = clientStream;
HttpClient = new HttpWebClient(connectRequest, request, new Lazy<int>(() => clientStream.Connection.GetProcessId(endPoint)));
LocalEndPoint = endPoint;
ProxyEndPoint = endPoint;
EnableWinAuth = server.EnableWinAuth && isWindowsAuthenticationSupported;
}

Expand Down Expand Up @@ -103,6 +103,9 @@ public bool EnableWinAuth
/// </summary>
public IPEndPoint ClientRemoteEndPoint => (IPEndPoint)ClientConnection.RemoteEndPoint;

[Obsolete("Use ClientRemoteEndPoint instead.")]
public IPEndPoint ClientEndPoint => ClientRemoteEndPoint;

/// <summary>
/// The web client used to communicate with server for this session.
/// </summary>
Expand All @@ -111,6 +114,14 @@ public bool EnableWinAuth
[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>
Expand All @@ -119,12 +130,15 @@ public bool EnableWinAuth
/// <summary>
/// Local endpoint via which we make the request.
/// </summary>
public ProxyEndPoint LocalEndPoint { get; }
public ProxyEndPoint ProxyEndPoint { get; }

[Obsolete("Use ProxyEndPoint instead.")]
public ProxyEndPoint LocalEndPoint => ProxyEndPoint;

/// <summary>
/// Is this a transparent endpoint?
/// </summary>
public bool IsTransparent => LocalEndPoint is TransparentProxyEndPoint;
public bool IsTransparent => ProxyEndPoint is TransparentProxyEndPoint;

/// <summary>
/// The last exception that happened.
Expand Down
13 changes: 2 additions & 11 deletions src/Titanium.Web.Proxy/Helpers/WinHttp/WinHttpWebProxyFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,7 @@ public bool GetAutoProxies(Uri destination, out IList<string>? proxyList)
}

// TODO: Apply authorization
var systemProxy = new ExternalProxy
{
HostName = proxyStr,
Port = port
};
var systemProxy = new ExternalProxy(proxyStr, port);

return systemProxy;
}
Expand All @@ -134,12 +130,7 @@ public bool GetAutoProxies(Uri destination, out IList<string>? proxyList)
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;
}
}
Expand Down
34 changes: 33 additions & 1 deletion src/Titanium.Web.Proxy/Models/ExternalProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,39 @@ public string? Password
/// </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>
Expand All @@ -77,6 +110,5 @@ public override string ToString()
{
return $"{HostName}:{Port}";
}

}
}
8 changes: 4 additions & 4 deletions src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ internal async Task<string> GetConnectionCacheKey(ProxyServer server, SessionEve
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);
}
Expand Down Expand Up @@ -169,10 +169,10 @@ internal Task<TcpServerConnection> GetServerConnection(ProxyServer proxyServer,
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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Titanium.Web.Proxy/WinAuthHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private async Task rewriteUnauthorizedResponse(SessionEventArgs args)
// Add custom div to body to clarify that the proxy (not the client browser) failed authentication
string authErrorMessage =
"<div class=\"inserted-by-proxy\"><h2>NTLM authentication through Titanium.Web.Proxy (" +
args.ClientConnection.LocalEndPoint +
args.ClientLocalEndPoint +
") failed. Please check credentials.</h2></div>";
string originalErrorMessage =
"<div class=\"inserted-by-proxy\"><h3>Response from remote web server below.</h3></div><br/>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ public async Task Smoke_Test_Nested_Proxy_UserData()
{
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);
Expand Down
13 changes: 2 additions & 11 deletions tests/Titanium.Web.Proxy.IntegrationTests/Setup/TestProxyServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,8 @@ public TestProxyServer(bool isReverseProxy, ProxyServer upStreamProxy = null)

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();
Expand Down