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

Commit 82ac963

Browse files
authored
Merge pull request #727 from justcoding121/beta
stable
2 parents 3a62c35 + fce77d7 commit 82ac963

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public ProxyTestController()
4242
await writeToConsole(exception.Message, ConsoleColor.Red);
4343
}
4444
};
45+
46+
proxyServer.TcpTimeWaitSeconds = 10;
47+
proxyServer.ConnectionTimeOutSeconds = 15;
48+
proxyServer.ReuseSocket = false;
49+
proxyServer.EnableConnectionPool = false;
4550
proxyServer.ForwardToUpstreamGateway = true;
4651
proxyServer.CertificateManager.SaveFakeCertificates = true;
4752
//proxyServer.ProxyBasicAuthenticateFunc = async (args, userName, password) =>

src/Titanium.Web.Proxy/Helpers/HttpServerStream.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ internal async ValueTask<ResponseStatusInfo> ReadResponseStatus(CancellationToke
3939
{
4040
// is this really possible?
4141
httpStatus = await ReadLineAsync(cancellationToken) ??
42-
throw new ServerConnectionException("Server connection was closed.");
42+
throw new ServerConnectionException("Server connection was closed. Response status is empty.");
4343
}
4444

4545
Response.ParseResponseLine(httpStatus, out var version, out int statusCode, out string description);
4646
return new ResponseStatusInfo { Version = version, StatusCode = statusCode, Description = description };
4747
}
4848
catch (Exception e) when (!(e is ServerConnectionException))
4949
{
50-
throw new ServerConnectionException("Server connection was closed.");
50+
throw new ServerConnectionException("Server connection was closed. Exception while reading the response status.", e);
5151
}
5252
}
5353
}

src/Titanium.Web.Proxy/Helpers/HttpStream.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,12 @@ private async Task copyBodyChunkedAsync(IHttpStreamWriter writer, Action<byte[],
10961096
{
10971097
while (true)
10981098
{
1099-
string chunkHead = (await ReadLineAsync(cancellationToken))!;
1099+
string? chunkHead = await ReadLineAsync(cancellationToken);
1100+
if (chunkHead == null)
1101+
{
1102+
return;
1103+
}
1104+
11001105
int idx = chunkHead.IndexOf(";");
11011106
if (idx >= 0)
11021107
{

src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
302302
}
303303
}
304304

305+
if (isHttps && sslProtocol == SslProtocols.None)
306+
{
307+
sslProtocol = proxyServer.SupportedSslProtocols;
308+
}
309+
305310
bool useUpstreamProxy1 = false;
306311

307312
// check if external proxy is set for HTTP/HTTPS

src/Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ private async Task handleHttpSessionRequest(ProxyEndPoint endPoint, HttpClientSt
6868
UserData = connectArgs?.UserData
6969
};
7070

71-
args.HttpClient.Request.IsHttps = isHttps;
71+
if (isHttps)
72+
{
73+
args.HttpClient.Request.IsHttps = true;
74+
}
7275

7376
try
7477
{
@@ -156,7 +159,19 @@ await HeaderParser.ReadHeaders(clientStream, args.HttpClient.Request.Headers,
156159
}
157160

158161
prefetchTask = null;
162+
}
159163

164+
if (connection != null)
165+
{
166+
var socket = connection.TcpSocket;
167+
bool part1 = socket.Poll(1000, SelectMode.SelectRead);
168+
bool part2 = socket.Available == 0;
169+
if (part1 & part2)
170+
{
171+
//connection is closed
172+
await tcpConnectionFactory.Release(connection, true);
173+
connection = null;
174+
}
160175
}
161176

162177
// create a new connection if cache key changes.
@@ -283,7 +298,11 @@ private async Task<RetryResult> handleHttpSessionRequest(SessionEventArgs args,
283298

284299
if (args.HttpClient.Request.UpgradeToWebSocket)
285300
{
286-
args.HttpClient.ConnectRequest!.TunnelType = TunnelType.Websocket;
301+
// connectRequest can be null for SOCKS connection
302+
if (args.HttpClient.ConnectRequest != null)
303+
{
304+
args.HttpClient.ConnectRequest!.TunnelType = TunnelType.Websocket;
305+
}
287306

288307
// if upgrading to websocket then relay the request without reading the contents
289308
await handleWebSocketUpgrade(args, args.ClientStream, connection, cancellationTokenSource, cancellationToken);

0 commit comments

Comments
 (0)