Skip to content
Draft
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
10 changes: 5 additions & 5 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
<ItemGroup>
<PackageVersion Include="BouncyCastle.Cryptography" Version="2.6.2" />
<PackageVersion Include="BrotliSharpLib" Version="0.3.3" />
<PackageVersion Include="Google.Protobuf" Version="3.33.4" />
<PackageVersion Include="Google.Protobuf" Version="3.33.5" />
<PackageVersion Include="Grpc.AspNetCore" Version="2.76.0" />
<PackageVersion Include="Grpc.Tools" Version="2.76.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageVersion>
<PackageVersion Include="Microsoft.AspNetCore.Authentication.Certificate" Version="10.0.2" />
<PackageVersion Include="Microsoft.CodeCoverage" Version="17.13.5" />
<PackageVersion Include="Microsoft.CodeCoverage" Version="18.0.1" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="10.0.2" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.2" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="10.0.2" />
Expand All @@ -45,9 +45,9 @@
<PackageVersion Include="System.Memory" Version="4.6.3" />
<PackageVersion Include="System.Security.Principal.Windows" Version="5.0.0" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.6.3" />
<PackageVersion Include="TUnit" Version="1.12.0" />
<PackageVersion Include="TUnit.Assertions" Version="1.12.0" />
<PackageVersion Include="TUnit.Engine" Version="1.12.0" />
<PackageVersion Include="TUnit" Version="1.12.93" />
<PackageVersion Include="TUnit.Assertions" Version="1.12.93" />
<PackageVersion Include="TUnit.Engine" Version="1.12.93" />
<PackageVersion Include="xunit.v3" Version="3.2.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class HttpContinueClient

private static readonly Encoding _msgEncoding = HttpHelper.GetEncodingFromContentType(null);

public static async Task<Response> Post(string server, int port, string content)
public static async Task<Response?> Post(string server, int port, string content)
{
var message = _msgEncoding.GetBytes(content);
var client = new TcpClient(server, port);
Expand All @@ -29,7 +29,7 @@ public static async Task<Response> Post(string server, int port, string content)

var buffer = new byte[1024];
var responseMsg = string.Empty;
Response response;
Response? response;

while ((response = HttpMessageParsing.ParseResponse(responseMsg)) == null)
{
Expand Down
19 changes: 10 additions & 9 deletions Keboo.Web.Proxy.IntegrationTests/Helpers/HttpContinueServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ namespace Keboo.Web.Proxy.IntegrationTests.Helpers;
internal class HttpContinueServer
{
private static readonly Encoding _msgEncoding = HttpHelper.GetEncodingFromContentType(null);
public HttpStatusCode ExpectationResponse;
public string ResponseBody;
public HttpStatusCode ExpectationResponse { get; set; }
public string? ResponseBody { get; set; }

public async Task HandleRequest(ConnectionContext context)
{
var request = await ReadHeaders(context.Transport.Input);
var request = await ReadHeaders(context.Transport.Input) ?? throw new Exception("Failed to read headers");

if (request.ExpectContinue)
{
Expand All @@ -37,7 +37,7 @@ public async Task HandleRequest(ConnectionContext context)

request = await ReadBody(request, context.Transport.Input);

var responseMsg = _msgEncoding.GetBytes(ResponseBody);
var responseMsg = _msgEncoding.GetBytes(ResponseBody ?? "");
var respondOk = new Response(responseMsg)
{
HttpVersion = new Version(1, 1),
Expand All @@ -49,9 +49,9 @@ public async Task HandleRequest(ConnectionContext context)
context.Transport.Output.Complete();
}

private static async Task<Request> ReadHeaders(PipeReader input)
private static async Task<Request?> ReadHeaders(PipeReader input)
{
Request request = null;
Request? request = null;
try
{
var requestMsg = string.Empty;
Expand All @@ -74,12 +74,13 @@ private static async Task<Request> ReadHeaders(PipeReader input)
return request;
}

private static async Task<Request> ReadBody(Request request, PipeReader input)
private static async Task<Request?> ReadBody(Request request, PipeReader input)
{
var msg = request.HeaderText;
Request? parsedRequest = request;
try
{
while ((request = HttpMessageParsing.ParseRequest(msg, true)) == null)
while ((parsedRequest = HttpMessageParsing.ParseRequest(msg, true)) is null)
{
var result = await input.ReadAsync();
foreach (var seg in result.Buffer)
Expand All @@ -95,6 +96,6 @@ private static async Task<Request> ReadBody(Request request, PipeReader input)
Console.WriteLine($"{ex.GetType()}: {ex.Message}");
}

return request;
return parsedRequest;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.IO;
using System.IO;
using System.Text;
using Keboo.Web.Proxy.Http;

Expand All @@ -15,7 +15,7 @@ internal static class HttpMessageParsing
/// <param name="messageText">The request message</param>
/// <param name="requireBody"></param>
/// <returns>Request object if message complete, null otherwise</returns>
internal static Request ParseRequest(string messageText, bool requireBody)
internal static Request? ParseRequest(string messageText, bool requireBody)
{
var reader = new StringReader(messageText);
var line = reader.ReadLine();
Expand Down Expand Up @@ -65,7 +65,7 @@ internal static Request ParseRequest(string messageText, bool requireBody)
/// </summary>
/// <param name="messageText">The response message</param>
/// <returns>Response object if message complete, null otherwise</returns>
internal static Response ParseResponse(string messageText)
internal static Response? ParseResponse(string messageText)
{
var reader = new StringReader(messageText);
var line = reader.ReadLine();
Expand Down
21 changes: 17 additions & 4 deletions Keboo.Web.Proxy.IntegrationTests/Helpers/TestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Net;
using System.Net.Http;

Expand All @@ -11,14 +11,27 @@ public static HttpClient GetHttpClient(int localProxyPort,
{
var proxy = new TestProxy($"http://localhost:{localProxyPort}", enableBasicProxyAuthorization);

var handler = new HttpClientHandler { Proxy = proxy, UseProxy = true };
var handler = new HttpClientHandler
{
Proxy = proxy,
UseProxy = true,
// Accept self-signed certificates generated for testing. This is safe in integration tests
// where we control both the client and server, but should not be used in production code.
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
};

return new HttpClient(handler);
}

public static HttpClient GetHttpClient()
{
return new HttpClient(new HttpClientHandler());
var handler = new HttpClientHandler
{
// Accept self-signed certificates generated for testing. This is safe in integration tests
// where we control both the client and server, but should not be used in production code.
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
};
return new HttpClient(handler);
}

public class TestProxy : IWebProxy
Expand All @@ -38,7 +51,7 @@ private TestProxy(Uri proxyUri)
}

public Uri ProxyUri { get; set; }
public ICredentials Credentials { get; set; }
public ICredentials? Credentials { get; set; }

public Uri GetProxy(Uri destination)
{
Expand Down
2 changes: 1 addition & 1 deletion Keboo.Web.Proxy.IntegrationTests/NestedProxyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task Smoke_Test_Nested_Proxy_UserData()
var proxy1 = TestSuite.GetProxy();
proxy1.ProxyBasicAuthenticateFunc = async (session, username, password) =>
{
session.UserData = "Test";
session!.UserData = "Test";
return await Task.FromResult(true);
};

Expand Down
2 changes: 1 addition & 1 deletion Keboo.Web.Proxy.IntegrationTests/ReverseProxyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public async Task Smoke_Test_Https_To_Https_Reverse_Proxy_Tunnel_Without_Decrypt

var proxy = TestSuite.GetReverseProxy();
var endpoint =
proxy.ProxyEndPoints.Where(x => x is TransparentProxyEndPoint).First() as TransparentProxyEndPoint;
proxy.ProxyEndPoints.OfType<TransparentProxyEndPoint>().First();

endpoint.BeforeSslAuthenticate += async (sender, e) =>
{
Expand Down
4 changes: 2 additions & 2 deletions Keboo.Web.Proxy.IntegrationTests/Setup/TestProxyServer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Net;
using Keboo.Web.Proxy.Models;
using Keboo.Web.Proxy.Network;
Expand All @@ -7,7 +7,7 @@ namespace Keboo.Web.Proxy.IntegrationTests.Setup;

public class TestProxyServer : IDisposable
{
public TestProxyServer(bool isReverseProxy, ProxyServer upStreamProxy = null)
public TestProxyServer(bool isReverseProxy, ProxyServer? upStreamProxy = null)
{
ProxyServer = new ProxyServer();

Expand Down
26 changes: 13 additions & 13 deletions Keboo.Web.Proxy.IntegrationTests/Setup/TestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class TestServer : IDisposable
{
private readonly IHost host;

private Func<HttpContext, Task> requestHandler;
private Func<ConnectionContext, Task> tcpRequestHandler;
private Func<HttpContext, Task>? _requestHandler;
private Func<ConnectionContext, Task>? _tcpRequestHandler;

public TestServer(X509Certificate2 serverCertificate, bool requireMutualTls)
{
Expand All @@ -35,7 +35,7 @@ public TestServer(X509Certificate2 serverCertificate, bool requireMutualTls)
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup(x => new Startup(() => requestHandler));
webBuilder.UseStartup(x => new Startup(() => _requestHandler));
webBuilder.ConfigureKestrel(options =>
{
options.Listen(IPAddress.Loopback, 0);
Expand All @@ -59,12 +59,12 @@ public TestServer(X509Certificate2 serverCertificate, bool requireMutualTls)
{
listenOptions.Run(context =>
{
if (tcpRequestHandler == null)
if (_tcpRequestHandler == null)
{
throw new Exception("Test server not configured to handle tcp request.");
}

return tcpRequestHandler(context);
return _tcpRequestHandler(context);
});
});
});
Expand All @@ -75,7 +75,7 @@ public TestServer(X509Certificate2 serverCertificate, bool requireMutualTls)

var addresses = host.Services.GetRequiredService<IServer>()
.Features.Get<IServerAddressesFeature>()
.Addresses.ToArray();
?.Addresses.ToArray() ?? [];

HttpListeningPort = new Uri(addresses[0]).Port;
HttpsListeningPort = new Uri(addresses[1]).Port;
Expand All @@ -98,33 +98,33 @@ public void Dispose()

public void HandleRequest(Func<HttpContext, Task> requestHandler)
{
this.requestHandler = requestHandler;
this._requestHandler = requestHandler;
}

public void HandleTcpRequest(Func<ConnectionContext, Task> tcpRequestHandler)
{
this.tcpRequestHandler = tcpRequestHandler;
this._tcpRequestHandler = tcpRequestHandler;
}

private class Startup
{
private readonly Func<Func<HttpContext, Task>> requestHandler;
private readonly Func<Func<HttpContext, Task>?>? _requestHandler;

public Startup(Func<Func<HttpContext, Task>> requestHandler)
public Startup(Func<Func<HttpContext, Task>?>? requestHandler)
{
this.requestHandler = requestHandler;
_requestHandler = requestHandler;
}

public void Configure(IApplicationBuilder app)
{
app.Run(context =>
{
if (requestHandler == null)
if (_requestHandler is null)
{
throw new Exception("Test server not configured to handle request.");
}

return requestHandler()(context);
return _requestHandler()?.Invoke(context) ?? Task.CompletedTask;
});
}

Expand Down
7 changes: 4 additions & 3 deletions Keboo.Web.Proxy.IntegrationTests/Setup/TestSuite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class TestSuite
public TestSuite(bool requireMutualTls = false)
{
var dummyProxy = new ProxyServer();
var serverCertificate = dummyProxy.CertificateManager.CreateServerCertificate("localhost").Result;
var serverCertificate = dummyProxy.CertificateManager.CreateServerCertificate("localhost").Result
?? throw new InvalidOperationException("Failed to create certificate");
server = new TestServer(serverCertificate, requireMutualTls);
}

Expand All @@ -20,7 +21,7 @@ public TestServer GetServer()
return server;
}

public static ProxyServer GetProxy(ProxyServer upStreamProxy = null)
public static ProxyServer GetProxy(ProxyServer? upStreamProxy = null)
{
if (upStreamProxy != null)
{
Expand All @@ -30,7 +31,7 @@ public static ProxyServer GetProxy(ProxyServer upStreamProxy = null)
return new TestProxyServer(false).ProxyServer;
}

public static ProxyServer GetReverseProxy(ProxyServer upStreamProxy = null)
public static ProxyServer GetReverseProxy(ProxyServer? upStreamProxy = null)
{
if (upStreamProxy != null)
{
Expand Down
Loading
Loading