Skip to content

Commit 20d4258

Browse files
committed
init proxy
1 parent 2800b3a commit 20d4258

6 files changed

Lines changed: 93 additions & 0 deletions

File tree

StackExchange.Redis.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
</Folder>
2525
<Folder Name="/src/">
2626
<File Path="src/Directory.Build.props" />
27+
<Project Path="src/RESPite.Proxy/RESPite.Proxy.csproj" />
2728
<Project Path="src/RESPite/RESPite.csproj" />
2829
<Project Path="src/StackExchange.Redis/StackExchange.Redis.csproj" />
2930
</Folder>

src/RESPite.Proxy/Program.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Net;
2+
using Microsoft.AspNetCore.Connections;
3+
using RESPite.Proxy;
4+
5+
var server = new ProxyServer()
6+
{
7+
Password = "letmein",
8+
};
9+
10+
/*
11+
// demonstrate cluster spoofing
12+
server.ServerType = ServerType.Cluster;
13+
var ep = server.AddEmptyNode();
14+
server.Migrate("key", ep);
15+
*/
16+
17+
var builder = WebApplication.CreateBuilder(args);
18+
builder.Services.AddSingleton<ProxyServer>(server);
19+
builder.WebHost.ConfigureKestrel(options =>
20+
{
21+
// HTTP 5000 (test/debug API only)
22+
options.ListenLocalhost(5000);
23+
24+
// this is the core of using Kestrel to create a TCP server
25+
// TCP 6379
26+
Action<Microsoft.AspNetCore.Server.Kestrel.Core.ListenOptions> builder = builder => builder.UseConnectionHandler<ProxyHandler>();
27+
foreach (var ep in server.GetEndPoints())
28+
{
29+
if (ep is IPEndPoint ip && ip.Address.Equals(IPAddress.Loopback))
30+
{
31+
options.ListenLocalhost(ip.Port, builder);
32+
}
33+
else
34+
{
35+
options.Listen(ep, builder);
36+
}
37+
}
38+
});
39+
40+
var app = builder.Build();
41+
42+
// run the server
43+
await app.RunAsync();

src/RESPite.Proxy/ProxyClient.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace RESPite.Proxy;
2+
3+
public class ProxyClient
4+
{
5+
private int _db;
6+
}

src/RESPite.Proxy/ProxyHandler.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Microsoft.AspNetCore.Connections;
2+
3+
namespace RESPite.Proxy;
4+
5+
public sealed class ProxyHandler(ProxyServer server) : ConnectionHandler
6+
{
7+
public override Task OnConnectedAsync(ConnectionContext connection)
8+
=> server.RunClientAsync(connection.Transport);
9+
}

src/RESPite.Proxy/ProxyServer.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.IO.Pipelines;
2+
using System.Net;
3+
4+
namespace RESPite.Proxy;
5+
6+
public class ProxyServer
7+
{
8+
public string Password { get; set; } = "";
9+
10+
public IEnumerable<EndPoint> GetEndPoints()
11+
{
12+
yield return new IPEndPoint(IPAddress.Loopback, 6379);
13+
}
14+
15+
public Task RunClientAsync(IDuplexPipe connectionTransport)
16+
{
17+
throw new NotImplementedException();
18+
}
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Remove="Microsoft.CodeAnalysis.PublicApiAnalyzers" />
12+
<ProjectReference Include="../RESPite/RESPite.csproj" />
13+
</ItemGroup>
14+
15+
</Project>

0 commit comments

Comments
 (0)