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

Fix server connection count; fix UpstreamEndPoint

parent 464e7c95
...@@ -5,34 +5,6 @@ namespace Titanium.Web.Proxy.Extensions ...@@ -5,34 +5,6 @@ namespace Titanium.Web.Proxy.Extensions
{ {
internal static class TcpExtensions internal static class TcpExtensions
{ {
/// <summary>
/// verifies if the underlying socket is connected before using a TcpClient connection
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
internal static bool IsConnected(this Socket client)
{
// This is how you can determine whether a socket is still connected.
bool blockingState = client.Blocking;
try
{
var tmp = new byte[1];
client.Blocking = false;
client.Send(tmp, 0, 0);
return true;
}
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
return e.SocketErrorCode == SocketError.WouldBlock;
}
finally
{
client.Blocking = blockingState;
}
}
#if NET45 #if NET45
/// <summary> /// <summary>
......
...@@ -58,7 +58,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -58,7 +58,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
client = new TcpClient(upStreamEndPoint); client = new TcpClient(upStreamEndPoint);
#else #else
client = new TcpClient(); client = new TcpClient();
client.Client.Bind(server.UpStreamEndPoint); client.Client.Bind(upStreamEndPoint);
#endif #endif
//If this proxy uses another external proxy then create a tunnel request for HTTP/HTTPS connections //If this proxy uses another external proxy then create a tunnel request for HTTP/HTTPS connections
......
This diff is collapsed.
...@@ -164,24 +164,30 @@ namespace Titanium.Web.Proxy ...@@ -164,24 +164,30 @@ namespace Titanium.Web.Proxy
//create new connection //create new connection
using (var connection = await GetServerConnection(connectArgs, true)) using (var connection = await GetServerConnection(connectArgs, true))
{ {
if (isClientHello) try
{ {
if (clientStream.Available > 0) if (isClientHello)
{ {
//send the buffered data if (clientStream.Available > 0)
var data = new byte[clientStream.Available]; {
await clientStream.ReadAsync(data, 0, data.Length); //send the buffered data
await connection.Stream.WriteAsync(data, 0, data.Length); var data = new byte[clientStream.Available];
await connection.Stream.FlushAsync(); await clientStream.ReadAsync(data, 0, data.Length);
await connection.Stream.WriteAsync(data, 0, data.Length);
await connection.Stream.FlushAsync();
}
var serverHelloInfo = await SslTools.PeekServerHello(connection.Stream);
((ConnectResponse)connectArgs.WebSession.Response).ServerHelloInfo = serverHelloInfo;
} }
var serverHelloInfo = await SslTools.PeekServerHello(connection.Stream); await TcpHelper.SendRaw(clientStream, connection.Stream, BufferSize,
((ConnectResponse)connectArgs.WebSession.Response).ServerHelloInfo = serverHelloInfo; (buffer, offset, count) => { connectArgs.OnDataSent(buffer, offset, count); }, (buffer, offset, count) => { connectArgs.OnDataReceived(buffer, offset, count); });
}
finally
{
UpdateServerConnectionCount(false);
} }
await TcpHelper.SendRaw(clientStream, connection.Stream, BufferSize,
(buffer, offset, count) => { connectArgs.OnDataSent(buffer, offset, count); }, (buffer, offset, count) => { connectArgs.OnDataReceived(buffer, offset, count); });
UpdateServerConnectionCount(false);
} }
return; return;
...@@ -361,8 +367,8 @@ namespace Titanium.Web.Proxy ...@@ -361,8 +367,8 @@ namespace Titanium.Web.Proxy
&& !args.WebSession.UpStreamEndPoint.Equals(connection.UpStreamEndPoint)))) && !args.WebSession.UpStreamEndPoint.Equals(connection.UpStreamEndPoint))))
{ {
connection.Dispose(); connection.Dispose();
UpdateServerConnectionCount(false);
connection = null; connection = null;
UpdateServerConnectionCount(false);
} }
if (connection == null) if (connection == null)
......
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