Skip to content

Commit

Permalink
feat(proxy): config for ForwardedHeadersOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Aug 7, 2023
1 parent f456012 commit 31413cf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
2 changes: 2 additions & 0 deletions docs/pages/config/appsettings.zh.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import { Callout } from "nextra-theme-docs";
"RecaptchaThreshold": "0.5"
},
"ForwardedHeadersOptions": {
"ForwardedHeaders": 5, // a flag enum, see following link
"ForwardLimit": 1,
"ForwardedForHeaderName": "X-Forwarded-For"
}
Expand Down Expand Up @@ -187,6 +188,7 @@ GZCTF 仅支持 PostgreSQL 作为数据库,不支持 MySQL 等其他数据库

此处配置反向代理的相关信息,用于获取真实 IP 地址,可选项。

- **ForwardedHeaders:** 反向代理转发的标头枚举,默认请使用 `5`,详情请见 [ForwardedHeaders 枚举](https://learn.microsoft.com/zh-cn/dotnet/api/microsoft.aspnetcore.httpoverrides.forwardedheaders?view=aspnetcore-7.0)
- **ForwardLimit:** 反向代理层数限制
- **ForwardedForHeaderName:** 反向代理 IP 地址头名称

Expand Down
24 changes: 12 additions & 12 deletions src/GZCTF/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,6 @@

Log.Logger = LogHelper.GetLogger(app.Configuration, app.Services);

if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseOpenApi(options => options.PostProcess += (document, _) => document.Servers.Clear());
app.UseSwaggerUi3();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}

await app.RunPrelaunchWork();

app.UseResponseCompression();
Expand All @@ -267,6 +255,18 @@

app.UseForwardedHeaders();

if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseOpenApi(options => options.PostProcess += (document, _) => document.Servers.Clear());
app.UseSwaggerUi3();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}

app.UseRouting();

app.UseAuthentication();
Expand Down
6 changes: 5 additions & 1 deletion src/GZCTF/Utils/LogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,14 @@ public static void UseRequestLogging(this WebApplication app)
{
app.UseSerilogRequestLogging(options =>
{
options.MessageTemplate = "[{StatusCode}] @{Elapsed,8:####0.00}ms HTTP {RequestMethod,-6} {RequestPath}";
options.MessageTemplate = "[{StatusCode}] @{Elapsed,8:####0.00}ms HTTP {RequestMethod,-6} {RequestPath} From {RemoteIP}";
options.GetLevel = (context, time, ex) =>
time > 10000 && context.Response.StatusCode != 101 ? LogEventLevel.Warning :
(context.Response.StatusCode > 499 || ex is not null) ? LogEventLevel.Error : LogEventLevel.Debug;
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
{
diagnosticContext.Set("RemoteIP", httpContext.Connection.RemoteIpAddress);
};
});
}

Expand Down

0 comments on commit 31413cf

Please sign in to comment.