Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Implementation outline for dynamic destination ports with YARP #62

Merged
merged 1 commit into from
Feb 8, 2025
Merged
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
4 changes: 4 additions & 0 deletions FUNC/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public static async Task<NodeStatus> Get(string name)
var endpointAddressToken = config.GetValue("EndpointAddress");
string endpointAddress = endpointAddressToken?.Value<string>() ?? ":0";
port = int.Parse(endpointAddress[(endpointAddress.IndexOf(":") + 1)..]);
if (name == "algorand")
Shared.AlgoPort = port;
else if (name == "voi")
Shared.VoiPort = port;
}

string sc = string.Empty;
Expand Down
42 changes: 33 additions & 9 deletions FUNC/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FUNC;
using Microsoft.Net.Http.Headers;
//using Yarp.ReverseProxy.Transforms;
using Yarp.ReverseProxy.Transforms;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -18,14 +18,38 @@
});
});
builder.Services.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
//.AddTransforms(transformBuilderContext =>
//{
// transformBuilderContext.AddRequestTransform(transformContext =>
// {
// // TODO: change port on destination address based on node configuration value
// });
//});
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"))
.AddTransforms(transformBuilderContext =>
{
transformBuilderContext.AddRequestTransform(transformContext =>
{
// Get the original host
var ogPort = transformContext.HttpContext.Request.Host.Port;
var destPort = 8080;

// Check if it's a request to the legacy site
if (ogPort == 1234) {
destPort = Shared.VoiPort;

} else if (ogPort == 5678) {
// Algo Port
destPort = Shared.AlgoPort;
}

var destinationPrefix = transformContext.DestinationPrefix;
if (destinationPrefix != null)
{
var originalUri = new Uri(destinationPrefix);
var newUri = new UriBuilder(originalUri)
{
Port = destPort
}.Uri;
transformContext.DestinationPrefix = newUri.ToString();
}

return ValueTask.CompletedTask;
});
});

var app = builder.Build();

Expand Down
8 changes: 8 additions & 0 deletions FUNC/Shared.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace FUNC
{
public static class Shared
{
public static int AlgoPort { get; set; } = 8081;
public static int VoiPort { get; set; } = 8082;
}
}