🐛 Fix known proxies

This commit is contained in:
2025-09-06 22:15:27 +08:00
parent 68bfdebcbd
commit 02680d224a
6 changed files with 55 additions and 105 deletions

View File

@@ -1,6 +1,7 @@
using System.Net; using System.Net;
using DysonNetwork.Develop.Identity; using DysonNetwork.Develop.Identity;
using DysonNetwork.Shared.Auth; using DysonNetwork.Shared.Auth;
using DysonNetwork.Shared.Http;
using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.HttpOverrides;
using Prometheus; using Prometheus;
@@ -18,7 +19,7 @@ public static class ApplicationConfiguration
app.UseRequestLocalization(); app.UseRequestLocalization();
ConfigureForwardedHeaders(app, configuration); app.ConfigureForwardedHeaders(configuration);
app.UseAuthentication(); app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
@@ -30,26 +31,4 @@ public static class ApplicationConfiguration
return app; return app;
} }
private static void ConfigureForwardedHeaders(WebApplication app, IConfiguration configuration)
{
var knownProxiesSection = configuration.GetSection("KnownProxies");
var forwardedHeadersOptions = new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.All };
if (knownProxiesSection.Exists())
{
var proxyAddresses = knownProxiesSection.Get<string[]>();
if (proxyAddresses != null)
foreach (var proxy in proxyAddresses)
if (IPAddress.TryParse(proxy, out var ipAddress))
forwardedHeadersOptions.KnownProxies.Add(ipAddress);
}
else
{
forwardedHeadersOptions.KnownProxies.Add(IPAddress.Any);
forwardedHeadersOptions.KnownProxies.Add(IPAddress.IPv6Any);
}
app.UseForwardedHeaders(forwardedHeadersOptions);
}
} }

View File

@@ -1,4 +1,5 @@
using DysonNetwork.Gateway.Startup; using DysonNetwork.Gateway.Startup;
using DysonNetwork.Shared.Http;
using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.HttpOverrides;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@@ -17,10 +18,7 @@ builder.Services.AddControllers();
var app = builder.Build(); var app = builder.Build();
app.UseForwardedHeaders(new ForwardedHeadersOptions app.ConfigureForwardedHeaders(app.Configuration);
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseRequestTimeouts(); app.UseRequestTimeouts();
app.UseCors(opts => app.UseCors(opts =>

View File

@@ -5,6 +5,7 @@ using DysonNetwork.Pass.Credit;
using DysonNetwork.Pass.Leveling; using DysonNetwork.Pass.Leveling;
using DysonNetwork.Pass.Permission; using DysonNetwork.Pass.Permission;
using DysonNetwork.Pass.Wallet; using DysonNetwork.Pass.Wallet;
using DysonNetwork.Shared.Http;
using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
using Prometheus; using Prometheus;
@@ -23,7 +24,7 @@ public static class ApplicationConfiguration
app.UseRequestLocalization(); app.UseRequestLocalization();
ConfigureForwardedHeaders(app, configuration); app.ConfigureForwardedHeaders(configuration);
app.UseCors(opts => app.UseCors(opts =>
opts.SetIsOriginAllowed(_ => true) opts.SetIsOriginAllowed(_ => true)
@@ -51,28 +52,6 @@ public static class ApplicationConfiguration
return app; return app;
} }
private static void ConfigureForwardedHeaders(WebApplication app, IConfiguration configuration)
{
var knownProxiesSection = configuration.GetSection("KnownProxies");
var forwardedHeadersOptions = new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.All };
if (knownProxiesSection.Exists())
{
var proxyAddresses = knownProxiesSection.Get<string[]>();
if (proxyAddresses != null)
foreach (var proxy in proxyAddresses)
if (IPAddress.TryParse(proxy, out var ipAddress))
forwardedHeadersOptions.KnownProxies.Add(ipAddress);
}
else
{
forwardedHeadersOptions.KnownProxies.Add(IPAddress.Any);
forwardedHeadersOptions.KnownProxies.Add(IPAddress.IPv6Any);
}
app.UseForwardedHeaders(forwardedHeadersOptions);
}
public static WebApplication ConfigureGrpcServices(this WebApplication app) public static WebApplication ConfigureGrpcServices(this WebApplication app)
{ {
app.MapGrpcService<AccountServiceGrpc>(); app.MapGrpcService<AccountServiceGrpc>();

View File

@@ -1,5 +1,6 @@
using System.Net; using System.Net;
using DysonNetwork.Pusher.Services; using DysonNetwork.Pusher.Services;
using DysonNetwork.Shared.Http;
using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.HttpOverrides;
namespace DysonNetwork.Pusher.Startup; namespace DysonNetwork.Pusher.Startup;
@@ -15,7 +16,7 @@ public static class ApplicationConfiguration
app.UseRequestLocalization(); app.UseRequestLocalization();
ConfigureForwardedHeaders(app, configuration); app.ConfigureForwardedHeaders(configuration);
app.UseCors(opts => app.UseCors(opts =>
opts.SetIsOriginAllowed(_ => true) opts.SetIsOriginAllowed(_ => true)
@@ -35,33 +36,4 @@ public static class ApplicationConfiguration
return app; return app;
} }
private static void ConfigureForwardedHeaders(WebApplication app, IConfiguration configuration)
{
var knownProxiesSection = configuration.GetSection("KnownProxies");
var forwardedHeadersOptions = new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.All };
if (knownProxiesSection.Exists())
{
var proxyAddresses = knownProxiesSection.Get<string[]>();
if (proxyAddresses != null)
foreach (var proxy in proxyAddresses)
if (IPAddress.TryParse(proxy, out var ipAddress))
forwardedHeadersOptions.KnownProxies.Add(ipAddress);
}
else
{
forwardedHeadersOptions.KnownProxies.Add(IPAddress.Any);
forwardedHeadersOptions.KnownProxies.Add(IPAddress.IPv6Any);
}
app.UseForwardedHeaders(forwardedHeadersOptions);
}
public static WebApplication ConfigureGrpcServices(this WebApplication app)
{
app.MapGrpcService<PusherServiceGrpc>();
return app;
}
} }

View File

@@ -0,0 +1,45 @@
using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Configuration;
using IPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork;
namespace DysonNetwork.Shared.Http;
public static class KnownProxiesConfiguration
{
public static WebApplication ConfigureForwardedHeaders(this WebApplication app, IConfiguration configuration)
{
var knownProxiesSection = configuration.GetSection("KnownProxies");
var forwardedHeadersOptions = new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.All };
if (knownProxiesSection.Exists())
{
var proxyAddresses = knownProxiesSection.Get<string[]>();
if (proxyAddresses != null)
{
foreach (var proxy in proxyAddresses)
{
if (IPAddress.TryParse(proxy, out var ipAddress))
{
forwardedHeadersOptions.KnownProxies.Add(ipAddress);
}
else if (IPNetwork.TryParse(proxy, out var ipNetwork))
{
forwardedHeadersOptions.KnownNetworks.Add(ipNetwork);
}
}
}
}
if (forwardedHeadersOptions.KnownProxies.Count == 0 && forwardedHeadersOptions.KnownNetworks.Count == 0)
{
forwardedHeadersOptions.KnownProxies.Add(IPAddress.Any);
forwardedHeadersOptions.KnownProxies.Add(IPAddress.IPv6Any);
}
app.UseForwardedHeaders(forwardedHeadersOptions);
return app;
}
}

View File

@@ -1,8 +1,7 @@
using System.Net;
using DysonNetwork.Shared.Auth; using DysonNetwork.Shared.Auth;
using DysonNetwork.Shared.Http;
using DysonNetwork.Sphere.Connection; using DysonNetwork.Sphere.Connection;
using DysonNetwork.Sphere.Publisher; using DysonNetwork.Sphere.Publisher;
using Microsoft.AspNetCore.HttpOverrides;
using Prometheus; using Prometheus;
namespace DysonNetwork.Sphere.Startup; namespace DysonNetwork.Sphere.Startup;
@@ -19,7 +18,7 @@ public static class ApplicationConfiguration
app.UseRequestLocalization(); app.UseRequestLocalization();
ConfigureForwardedHeaders(app, configuration); app.ConfigureForwardedHeaders(configuration);
app.UseWebSockets(); app.UseWebSockets();
app.UseAuthentication(); app.UseAuthentication();
@@ -34,26 +33,4 @@ public static class ApplicationConfiguration
return app; return app;
} }
private static void ConfigureForwardedHeaders(WebApplication app, IConfiguration configuration)
{
var knownProxiesSection = configuration.GetSection("KnownProxies");
var forwardedHeadersOptions = new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.All };
if (knownProxiesSection.Exists())
{
var proxyAddresses = knownProxiesSection.Get<string[]>();
if (proxyAddresses != null)
foreach (var proxy in proxyAddresses)
if (IPAddress.TryParse(proxy, out var ipAddress))
forwardedHeadersOptions.KnownProxies.Add(ipAddress);
}
else
{
forwardedHeadersOptions.KnownProxies.Add(IPAddress.Any);
forwardedHeadersOptions.KnownProxies.Add(IPAddress.IPv6Any);
}
app.UseForwardedHeaders(forwardedHeadersOptions);
}
} }