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

Commit 7f81e62

Browse files
committed
fix for #685
1 parent 4bdf8f3 commit 7f81e62

File tree

11 files changed

+76
-20
lines changed

11 files changed

+76
-20
lines changed

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
5858
return;
5959
}
6060

61-
var connectRequest = new ConnectRequest(requestLine.RequestUri.GetString())
61+
var connectRequest = new ConnectRequest(requestLine.RequestUri)
6262
{
6363
RequestUriString8 = requestLine.RequestUri,
6464
HttpVersion = requestLine.Version
@@ -269,7 +269,12 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
269269
try
270270
{
271271
// clientStream.Available should be at most BufferSize because it is using the same buffer size
272-
await clientStream.ReadAsync(data, 0, available, cancellationToken);
272+
int read = await clientStream.ReadAsync(data, 0, available, cancellationToken);
273+
if (read != available)
274+
{
275+
throw new Exception("Internal error.");
276+
}
277+
273278
await connection.Stream.WriteAsync(data, 0, available, true, cancellationToken);
274279
}
275280
finally
@@ -279,6 +284,11 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
279284
}
280285

281286
var serverHelloInfo = await SslTools.PeekServerHello(connection.Stream, BufferPool, cancellationToken);
287+
if (serverHelloInfo == null)
288+
{
289+
;
290+
}
291+
282292
((ConnectResponse)connectArgs.HttpClient.Response).ServerHelloInfo = serverHelloInfo;
283293
}
284294

src/Titanium.Web.Proxy/Http/ConnectRequest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using Titanium.Web.Proxy.Extensions;
3+
using Titanium.Web.Proxy.Models;
34
using Titanium.Web.Proxy.StreamExtended;
45

56
namespace Titanium.Web.Proxy.Http
@@ -9,7 +10,7 @@ namespace Titanium.Web.Proxy.Http
910
/// </summary>
1011
public class ConnectRequest : Request
1112
{
12-
public ConnectRequest(string authority)
13+
internal ConnectRequest(ByteString authority)
1314
{
1415
Method = "CONNECT";
1516
Authority = authority;

src/Titanium.Web.Proxy/Http/Request.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ internal ByteString RequestUriString8
4040
}
4141
}
4242

43-
internal string? Authority { get; set; }
43+
internal ByteString Authority { get; set; }
4444

4545
/// <summary>
4646
/// Request HTTP Uri.
@@ -75,7 +75,7 @@ public string Url
7575
string url = RequestUriString8.GetString();
7676
if (getUriScheme(RequestUriString8).Length == 0)
7777
{
78-
string? hostAndPath = Host ?? Authority;
78+
string? hostAndPath = Host ?? Authority.GetString();
7979

8080
if (url.StartsWith("/"))
8181
{

src/Titanium.Web.Proxy/Http2/Http2Helper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output,
264264
request.HttpVersion = HttpVersion.Version20;
265265
request.Method = method.GetString();
266266
request.IsHttps = headerListener.Scheme == ProxyServer.UriSchemeHttps;
267-
request.Authority = headerListener.Authority.GetString();
267+
request.Authority = headerListener.Authority;
268268
request.RequestUriString8 = path;
269269

270270
//request.RequestUri = headerListener.GetUri();
@@ -475,7 +475,7 @@ private static async Task sendHeader(Http2Settings settings, Http2FrameHeader fr
475475
encoder.EncodeHeader(writer, StaticTable.KnownHeaderMethod, request.Method.GetByteString());
476476
encoder.EncodeHeader(writer, StaticTable.KnownHeaderAuhtority, uri.Authority.GetByteString());
477477
encoder.EncodeHeader(writer, StaticTable.KnownHeaderScheme, uri.Scheme.GetByteString());
478-
encoder.EncodeHeader(writer, StaticTable.KnownHeaderPath, request.Url.GetByteString(), false,
478+
encoder.EncodeHeader(writer, StaticTable.KnownHeaderPath, request.RequestUriString8, false,
479479
HpackUtil.IndexType.None, false);
480480
}
481481
else

src/Titanium.Web.Proxy/Models/ByteString.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Text;
3+
using Titanium.Web.Proxy.Extensions;
34

45
namespace Titanium.Web.Proxy.Models
56
{
@@ -28,11 +29,31 @@ public bool Equals(ByteString other)
2829
return Data.Span.SequenceEqual(other.Data.Span);
2930
}
3031

32+
public int IndexOf(byte value)
33+
{
34+
return Span.IndexOf(value);
35+
}
36+
37+
public ByteString Slice(int start)
38+
{
39+
return Data.Slice(start);
40+
}
41+
42+
public ByteString Slice(int start, int length)
43+
{
44+
return Data.Slice(start, length);
45+
}
46+
3147
public override int GetHashCode()
3248
{
3349
return Data.GetHashCode();
3450
}
3551

52+
public override string ToString()
53+
{
54+
return this.GetString();
55+
}
56+
3657
public static explicit operator ByteString(string str) => new ByteString(Encoding.ASCII.GetBytes(str));
3758

3859
public static implicit operator ByteString(byte[] data) => new ByteString(data);

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,34 @@ internal async Task<TcpServerConnection> GetServerConnection(ProxyServer server,
180180

181181
session.CustomUpStreamProxyUsed = customUpStreamProxy;
182182

183-
var uri = session.HttpClient.Request.RequestUri;
183+
var request = session.HttpClient.Request;
184+
string host;
185+
int port;
186+
if (request.Authority.Length > 0)
187+
{
188+
var authority = request.Authority;
189+
int idx = authority.IndexOf((byte)':');
190+
if (idx == -1)
191+
{
192+
host = authority.GetString();
193+
port = 80;
194+
}
195+
else
196+
{
197+
host = authority.Slice(0, idx).GetString();
198+
port = int.Parse(authority.Slice(idx + 1).GetString());
199+
}
200+
}
201+
else
202+
{
203+
var uri = request.RequestUri;
204+
host = uri.Host;
205+
port = uri.Port;
206+
}
207+
184208
return await GetServerConnection(
185-
uri.Host,
186-
uri.Port,
209+
host,
210+
port,
187211
session.HttpClient.Request.HttpVersion,
188212
isHttps, applicationProtocols, isConnect,
189213
server, session, session.HttpClient.UpStreamEndPoint ?? server.UpStreamEndPoint,
@@ -426,11 +450,11 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
426450

427451
if (externalProxy != null && (isConnect || isHttps))
428452
{
429-
string authority = $"{remoteHostName}:{remotePort}";
453+
var authority = $"{remoteHostName}:{remotePort}".GetByteString();
430454
var connectRequest = new ConnectRequest(authority)
431455
{
432456
IsHttps = isHttps,
433-
RequestUriString8 = HttpHeader.Encoding.GetBytes(authority),
457+
RequestUriString8 = authority,
434458
HttpVersion = httpVersion
435459
};
436460

src/Titanium.Web.Proxy/StreamExtended/SslTools.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public static async Task<bool> IsServerHello(IPeekStream stream, IBufferPool buf
216216
int recordLength = ((recordType & 0x7f) << 8) + peekStream.ReadByte();
217217
if (recordLength < 38)
218218
{
219-
// Message body too short.
219+
// Message body too short.
220220
return null;
221221
}
222222

src/Titanium.Web.Proxy/Titanium.Web.Proxy.Mono.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<ItemGroup>
1515
<PackageReference Include="BrotliSharpLib" Version="0.3.3" />
16-
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5" />
16+
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5.2" />
1717
<PackageReference Include="System.Buffers" Version="4.5.0" />
1818
</ItemGroup>
1919

src/Titanium.Web.Proxy/Titanium.Web.Proxy.NetCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<ItemGroup>
1515
<PackageReference Include="BrotliSharpLib" Version="0.3.3" />
16-
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5" />
16+
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5.2" />
1717
<PackageReference Include="System.Buffers" Version="4.5.0" />
1818
</ItemGroup>
1919

src/Titanium.Web.Proxy/Titanium.Web.Proxy.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<ItemGroup>
1616
<PackageReference Include="BrotliSharpLib" Version="0.3.3" />
17-
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5" />
17+
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5.2" />
1818
</ItemGroup>
1919

2020
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.1'">

src/Titanium.Web.Proxy/Titanium.Web.Proxy.nuspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
<dependencies>
1717
<group targetFramework="net45">
1818
<dependency id="BrotliSharpLib" version="0.3.3" />
19-
<dependency id="Portable.BouncyCastle" version="1.8.5" />
19+
<dependency id="Portable.BouncyCastle" version="1.8.5.2" />
2020
<dependency id="System.Buffers" version="4.5.0" />
2121
<dependency id="System.Memory" version="4.5.3" />
2222
<dependency id="System.Threading.Tasks.Extensions" version="4.5.3" />
2323
</group>
2424
<group targetFramework="net461">
2525
<dependency id="BrotliSharpLib" version="0.3.3" />
26-
<dependency id="Portable.BouncyCastle" version="1.8.5" />
26+
<dependency id="Portable.BouncyCastle" version="1.8.5.2" />
2727
<dependency id="Microsoft.Win32.Registry" version="4.6.0" />
2828
<dependency id="System.Buffers" version="4.5.0" />
2929
<dependency id="System.Memory" version="4.5.3" />
@@ -32,7 +32,7 @@
3232
</group>
3333
<group targetFramework="netstandard2.0">
3434
<dependency id="BrotliSharpLib" version="0.3.3" />
35-
<dependency id="Portable.BouncyCastle" version="1.8.5" />
35+
<dependency id="Portable.BouncyCastle" version="1.8.5.2" />
3636
<dependency id="Microsoft.Win32.Registry" version="4.6.0" />
3737
<dependency id="System.Buffers" version="4.5.0" />
3838
<dependency id="System.Memory" version="4.5.3" />
@@ -41,7 +41,7 @@
4141
</group>
4242
<group targetFramework="netstandard2.1">
4343
<dependency id="BrotliSharpLib" version="0.3.3" />
44-
<dependency id="Portable.BouncyCastle" version="1.8.5" />
44+
<dependency id="Portable.BouncyCastle" version="1.8.5.2" />
4545
<dependency id="Microsoft.Win32.Registry" version="4.6.0" />
4646
<dependency id="System.Security.Principal.Windows" version="4.6.0" />
4747
</group>

0 commit comments

Comments
 (0)