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

beta #687

Merged
merged 2 commits into from
Nov 30, 2019
Merged

beta #687

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
9 changes: 7 additions & 2 deletions src/Titanium.Web.Proxy/ExplicitClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
return;
}

var connectRequest = new ConnectRequest(requestLine.RequestUri.GetString())
var connectRequest = new ConnectRequest(requestLine.RequestUri)
{
RequestUriString8 = requestLine.RequestUri,
HttpVersion = requestLine.Version
Expand Down Expand Up @@ -269,7 +269,12 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
try
{
// clientStream.Available should be at most BufferSize because it is using the same buffer size
await clientStream.ReadAsync(data, 0, available, cancellationToken);
int read = await clientStream.ReadAsync(data, 0, available, cancellationToken);
if (read != available)
{
throw new Exception("Internal error.");
}

await connection.Stream.WriteAsync(data, 0, available, true, cancellationToken);
}
finally
Expand Down
3 changes: 2 additions & 1 deletion src/Titanium.Web.Proxy/Http/ConnectRequest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.StreamExtended;

namespace Titanium.Web.Proxy.Http
Expand All @@ -9,7 +10,7 @@ namespace Titanium.Web.Proxy.Http
/// </summary>
public class ConnectRequest : Request
{
public ConnectRequest(string authority)
internal ConnectRequest(ByteString authority)
{
Method = "CONNECT";
Authority = authority;
Expand Down
4 changes: 2 additions & 2 deletions src/Titanium.Web.Proxy/Http/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal ByteString RequestUriString8
}
}

internal string? Authority { get; set; }
internal ByteString Authority { get; set; }

/// <summary>
/// Request HTTP Uri.
Expand Down Expand Up @@ -75,7 +75,7 @@ public string Url
string url = RequestUriString8.GetString();
if (getUriScheme(RequestUriString8).Length == 0)
{
string? hostAndPath = Host ?? Authority;
string? hostAndPath = Host ?? Authority.GetString();

if (url.StartsWith("/"))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Titanium.Web.Proxy/Http2/Http2Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output,
request.HttpVersion = HttpVersion.Version20;
request.Method = method.GetString();
request.IsHttps = headerListener.Scheme == ProxyServer.UriSchemeHttps;
request.Authority = headerListener.Authority.GetString();
request.Authority = headerListener.Authority;
request.RequestUriString8 = path;

//request.RequestUri = headerListener.GetUri();
Expand Down Expand Up @@ -475,7 +475,7 @@ private static async Task sendHeader(Http2Settings settings, Http2FrameHeader fr
encoder.EncodeHeader(writer, StaticTable.KnownHeaderMethod, request.Method.GetByteString());
encoder.EncodeHeader(writer, StaticTable.KnownHeaderAuhtority, uri.Authority.GetByteString());
encoder.EncodeHeader(writer, StaticTable.KnownHeaderScheme, uri.Scheme.GetByteString());
encoder.EncodeHeader(writer, StaticTable.KnownHeaderPath, request.Url.GetByteString(), false,
encoder.EncodeHeader(writer, StaticTable.KnownHeaderPath, request.RequestUriString8, false,
HpackUtil.IndexType.None, false);
}
else
Expand Down
21 changes: 21 additions & 0 deletions src/Titanium.Web.Proxy/Models/ByteString.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Text;
using Titanium.Web.Proxy.Extensions;

namespace Titanium.Web.Proxy.Models
{
Expand Down Expand Up @@ -28,11 +29,31 @@ public bool Equals(ByteString other)
return Data.Span.SequenceEqual(other.Data.Span);
}

public int IndexOf(byte value)
{
return Span.IndexOf(value);
}

public ByteString Slice(int start)
{
return Data.Slice(start);
}

public ByteString Slice(int start, int length)
{
return Data.Slice(start, length);
}

public override int GetHashCode()
{
return Data.GetHashCode();
}

public override string ToString()
{
return this.GetString();
}

public static explicit operator ByteString(string str) => new ByteString(Encoding.ASCII.GetBytes(str));

public static implicit operator ByteString(byte[] data) => new ByteString(data);
Expand Down
34 changes: 29 additions & 5 deletions src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,34 @@ internal async Task<TcpServerConnection> GetServerConnection(ProxyServer server,

session.CustomUpStreamProxyUsed = customUpStreamProxy;

var uri = session.HttpClient.Request.RequestUri;
var request = session.HttpClient.Request;
string host;
int port;
if (request.Authority.Length > 0)
{
var authority = request.Authority;
int idx = authority.IndexOf((byte)':');
if (idx == -1)
{
host = authority.GetString();
port = 80;
}
else
{
host = authority.Slice(0, idx).GetString();
port = int.Parse(authority.Slice(idx + 1).GetString());
}
}
else
{
var uri = request.RequestUri;
host = uri.Host;
port = uri.Port;
}

return await GetServerConnection(
uri.Host,
uri.Port,
host,
port,
session.HttpClient.Request.HttpVersion,
isHttps, applicationProtocols, isConnect,
server, session, session.HttpClient.UpStreamEndPoint ?? server.UpStreamEndPoint,
Expand Down Expand Up @@ -426,11 +450,11 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost

if (externalProxy != null && (isConnect || isHttps))
{
string authority = $"{remoteHostName}:{remotePort}";
var authority = $"{remoteHostName}:{remotePort}".GetByteString();
var connectRequest = new ConnectRequest(authority)
{
IsHttps = isHttps,
RequestUriString8 = HttpHeader.Encoding.GetBytes(authority),
RequestUriString8 = authority,
HttpVersion = httpVersion
};

Expand Down
2 changes: 1 addition & 1 deletion src/Titanium.Web.Proxy/StreamExtended/SslTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public static async Task<bool> IsServerHello(IPeekStream stream, IBufferPool buf
int recordLength = ((recordType & 0x7f) << 8) + peekStream.ReadByte();
if (recordLength < 38)
{
// Message body too short.
// Message body too short.
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Titanium.Web.Proxy/Titanium.Web.Proxy.Mono.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<ItemGroup>
<PackageReference Include="BrotliSharpLib" Version="0.3.3" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5.2" />
<PackageReference Include="System.Buffers" Version="4.5.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Titanium.Web.Proxy/Titanium.Web.Proxy.NetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<ItemGroup>
<PackageReference Include="BrotliSharpLib" Version="0.3.3" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5.2" />
<PackageReference Include="System.Buffers" Version="4.5.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Titanium.Web.Proxy/Titanium.Web.Proxy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<ItemGroup>
<PackageReference Include="BrotliSharpLib" Version="0.3.3" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5.2" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.1'">
Expand Down
8 changes: 4 additions & 4 deletions src/Titanium.Web.Proxy/Titanium.Web.Proxy.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
<dependencies>
<group targetFramework="net45">
<dependency id="BrotliSharpLib" version="0.3.3" />
<dependency id="Portable.BouncyCastle" version="1.8.5" />
<dependency id="Portable.BouncyCastle" version="1.8.5.2" />
<dependency id="System.Buffers" version="4.5.0" />
<dependency id="System.Memory" version="4.5.3" />
<dependency id="System.Threading.Tasks.Extensions" version="4.5.3" />
</group>
<group targetFramework="net461">
<dependency id="BrotliSharpLib" version="0.3.3" />
<dependency id="Portable.BouncyCastle" version="1.8.5" />
<dependency id="Portable.BouncyCastle" version="1.8.5.2" />
<dependency id="Microsoft.Win32.Registry" version="4.6.0" />
<dependency id="System.Buffers" version="4.5.0" />
<dependency id="System.Memory" version="4.5.3" />
Expand All @@ -32,7 +32,7 @@
</group>
<group targetFramework="netstandard2.0">
<dependency id="BrotliSharpLib" version="0.3.3" />
<dependency id="Portable.BouncyCastle" version="1.8.5" />
<dependency id="Portable.BouncyCastle" version="1.8.5.2" />
<dependency id="Microsoft.Win32.Registry" version="4.6.0" />
<dependency id="System.Buffers" version="4.5.0" />
<dependency id="System.Memory" version="4.5.3" />
Expand All @@ -41,7 +41,7 @@
</group>
<group targetFramework="netstandard2.1">
<dependency id="BrotliSharpLib" version="0.3.3" />
<dependency id="Portable.BouncyCastle" version="1.8.5" />
<dependency id="Portable.BouncyCastle" version="1.8.5.2" />
<dependency id="Microsoft.Win32.Registry" version="4.6.0" />
<dependency id="System.Security.Principal.Windows" version="4.6.0" />
</group>
Expand Down