committed by
GitHub
29 changed files with 293 additions and 62 deletions
@ -0,0 +1,14 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<Import Project="..\..\..\common.props" /> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>net5.0</TargetFramework> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Volo.Abp.AspNetCore" Version="4.3.0" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,25 @@ |
|||||
|
using LINGYUN.Abp.AspNetCore.HttpOverrides.Forwarded; |
||||
|
using Microsoft.AspNetCore.Builder; |
||||
|
using Microsoft.Extensions.Configuration; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace LINGYUN.Abp.AspNetCore.HttpOverrides |
||||
|
{ |
||||
|
public class AbpAspNetCoreHttpOverridesModule : AbpModule |
||||
|
{ |
||||
|
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
var configuration = context.Services.GetConfiguration(); |
||||
|
|
||||
|
var forwardOptions = new AbpForwardedHeadersOptions(); |
||||
|
configuration.GetSection("Forwarded:Headers").Bind(forwardOptions); |
||||
|
context.Services.ExecutePreConfiguredActions(forwardOptions); |
||||
|
|
||||
|
Configure<ForwardedHeadersOptions>(options => |
||||
|
{ |
||||
|
options.Configure(forwardOptions); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
using Microsoft.AspNetCore.HttpOverrides; |
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace LINGYUN.Abp.AspNetCore.HttpOverrides.Forwarded |
||||
|
{ |
||||
|
public class AbpForwardedHeadersOptions |
||||
|
{ |
||||
|
public string ForwardedForHeaderName { get; set; } |
||||
|
public string ForwardedHostHeaderName { get; set; } |
||||
|
public string ForwardedProtoHeaderName { get; set; } |
||||
|
public string OriginalForHeaderName { get; set; } |
||||
|
public string OriginalHostHeaderName { get; set; } |
||||
|
public string OriginalProtoHeaderName { get; set; } |
||||
|
public ForwardedHeaders ForwardedHeaders { get; set; } |
||||
|
public int? ForwardLimit { get; set; } |
||||
|
public bool RequireHeaderSymmetry { get; set; } |
||||
|
public List<string> AllowedHosts { get; set; } |
||||
|
public List<string> KnownNetworks { get; set; } |
||||
|
public List<string> KnownProxies { get; set; } |
||||
|
public AbpForwardedHeadersOptions() |
||||
|
{ |
||||
|
ForwardedForHeaderName = ForwardedHeadersDefaults.XForwardedForHeaderName; |
||||
|
ForwardedHostHeaderName = ForwardedHeadersDefaults.XForwardedHostHeaderName; |
||||
|
ForwardedProtoHeaderName = ForwardedHeadersDefaults.XForwardedProtoHeaderName; |
||||
|
OriginalForHeaderName = ForwardedHeadersDefaults.XOriginalForHeaderName; |
||||
|
OriginalHostHeaderName = ForwardedHeadersDefaults.XOriginalHostHeaderName; |
||||
|
OriginalProtoHeaderName = ForwardedHeadersDefaults.XOriginalProtoHeaderName; |
||||
|
ForwardedHeaders = ForwardedHeaders.None; |
||||
|
ForwardLimit = 1; |
||||
|
RequireHeaderSymmetry = false; |
||||
|
AllowedHosts = new List<string>(); |
||||
|
KnownProxies = new List<string>(); |
||||
|
KnownNetworks = new List<string>(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,43 @@ |
|||||
|
using Microsoft.AspNetCore.Builder; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Microsoft.Extensions.Primitives; |
||||
|
using System.Linq; |
||||
|
using Volo.Abp.AspNetCore.WebClientInfo; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace LINGYUN.Abp.AspNetCore.WebClientInfo |
||||
|
{ |
||||
|
[Dependency(ServiceLifetime.Transient, ReplaceServices = true)] |
||||
|
[ExposeServices( |
||||
|
typeof(IWebClientInfoProvider), |
||||
|
typeof(HttpContextWebClientInfoProvider))] |
||||
|
public class RequestForwardedHeaderWebClientInfoProvider : HttpContextWebClientInfoProvider |
||||
|
{ |
||||
|
protected ForwardedHeadersOptions Options { get; } |
||||
|
public RequestForwardedHeaderWebClientInfoProvider( |
||||
|
ILogger<HttpContextWebClientInfoProvider> logger, |
||||
|
IOptions<ForwardedHeadersOptions> options, |
||||
|
IHttpContextAccessor httpContextAccessor) |
||||
|
: base(logger, httpContextAccessor) |
||||
|
{ |
||||
|
Options = options.Value; |
||||
|
} |
||||
|
|
||||
|
protected override string GetClientIpAddress() |
||||
|
{ |
||||
|
IHeaderDictionary requestHeaders = HttpContextAccessor.HttpContext?.Request?.Headers; |
||||
|
if (requestHeaders != null && |
||||
|
requestHeaders.TryGetValue(Options.ForwardedForHeaderName, out StringValues headers) == true) |
||||
|
{ |
||||
|
if (StringValues.IsNullOrEmpty(headers)) |
||||
|
{ |
||||
|
return headers.Last(); |
||||
|
} |
||||
|
} |
||||
|
return base.GetClientIpAddress(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,67 @@ |
|||||
|
using LINGYUN.Abp.AspNetCore.HttpOverrides.Forwarded; |
||||
|
using Microsoft.AspNetCore.HttpOverrides; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Net; |
||||
|
|
||||
|
namespace Microsoft.AspNetCore.Builder |
||||
|
{ |
||||
|
public static class ForwardedHeadersOptionsExtensions |
||||
|
{ |
||||
|
public static void Configure( |
||||
|
this ForwardedHeadersOptions options, |
||||
|
AbpForwardedHeadersOptions abpOptions) |
||||
|
{ |
||||
|
options.ForwardedForHeaderName = abpOptions.ForwardedForHeaderName; |
||||
|
options.ForwardedHeaders = abpOptions.ForwardedHeaders; |
||||
|
options.ForwardedHostHeaderName = abpOptions.ForwardedHostHeaderName; |
||||
|
options.ForwardedProtoHeaderName = abpOptions.ForwardedProtoHeaderName; |
||||
|
options.ForwardLimit = abpOptions.ForwardLimit; |
||||
|
options.OriginalForHeaderName = abpOptions.OriginalForHeaderName; |
||||
|
options.OriginalHostHeaderName = abpOptions.OriginalHostHeaderName; |
||||
|
options.OriginalProtoHeaderName = abpOptions.OriginalProtoHeaderName; |
||||
|
options.RequireHeaderSymmetry = abpOptions.RequireHeaderSymmetry; |
||||
|
|
||||
|
if (abpOptions.AllowedHosts.Any()) |
||||
|
{ |
||||
|
options.AllowedHosts = abpOptions.AllowedHosts; |
||||
|
} |
||||
|
|
||||
|
AddProxies(options.KnownProxies, abpOptions.KnownProxies); |
||||
|
|
||||
|
if (abpOptions.KnownNetworks.Any()) |
||||
|
{ |
||||
|
options.KnownNetworks.Clear(); |
||||
|
foreach (var proxy in abpOptions.KnownNetworks) |
||||
|
{ |
||||
|
// 192.168.1.0/24
|
||||
|
var spiltProxies = proxy.Split("/"); |
||||
|
if (spiltProxies.Length != 2) |
||||
|
{ |
||||
|
continue; |
||||
|
} |
||||
|
if (int.TryParse(spiltProxies[1], out int prefixLength) && |
||||
|
IPAddress.TryParse(spiltProxies[0], out IPAddress prefixIpAddress)) |
||||
|
{ |
||||
|
options.KnownNetworks.Add(new IPNetwork(prefixIpAddress, prefixLength)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static void AddProxies(IList<IPAddress> addresses, IList<string> proxiesAddress) |
||||
|
{ |
||||
|
if (proxiesAddress.Any()) |
||||
|
{ |
||||
|
addresses.Clear(); |
||||
|
foreach (var proxy in proxiesAddress) |
||||
|
{ |
||||
|
if (IPAddress.TryParse(proxy, out IPAddress iPAddress)) |
||||
|
{ |
||||
|
addresses.Add(iPAddress); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
# LINGYUN.Abp.AspNetCore.HttpOverrides |
||||
|
|
||||
|
## 模块说明 |
||||
|
|
||||
|
对于Http传输标头的一些重写类模块 |
||||
|
|
||||
|
目前实现通过解析 **X-Forwarded-For** 标头获取反向代理中的真实客户地址,通常获取标头中的最后一个设置值 |
||||
|
|
||||
|
### 基础模块 |
||||
|
|
||||
|
### 高阶模块 |
||||
|
|
||||
|
### 权限定义 |
||||
|
|
||||
|
### 功能定义 |
||||
|
|
||||
|
### 配置定义 |
||||
|
|
||||
|
### 如何使用 |
||||
|
|
||||
|
|
||||
|
```csharp |
||||
|
|
||||
|
[DependsOn( |
||||
|
typeof(AbpAspNetCoreHttpOverridesModule))] |
||||
|
public class YouProjectModule : AbpModule |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
``` |
||||
|
|
||||
|
### 更新日志 |
||||
Loading…
Reference in new issue