diff --git a/src/GZCTF/Models/Internal/Configs.cs b/src/GZCTF/Models/Internal/Configs.cs index b203c8850..7e4b970b4 100644 --- a/src/GZCTF/Models/Internal/Configs.cs +++ b/src/GZCTF/Models/Internal/Configs.cs @@ -157,10 +157,11 @@ public void ToForwardedHeadersOptions(ForwardedHeadersOptions options) { // split the network into address and prefix length var parts = network.Split('/'); - if (parts.Length == 2 && int.TryParse(parts[1], out var prefixLength)) + if (parts.Length == 2 && + IPAddress.TryParse(parts[0], out var prefix) && + int.TryParse(parts[1], out var prefixLength)) { - var address = IPAddress.Parse(parts[0]); - options.KnownNetworks.Add(new IPNetwork(address, prefixLength)); + options.KnownNetworks.Add(new IPNetwork(prefix, prefixLength)); } }); diff --git a/src/GZCTF/Utils/LogHelper.cs b/src/GZCTF/Utils/LogHelper.cs index f80599584..22102b06f 100644 --- a/src/GZCTF/Utils/LogHelper.cs +++ b/src/GZCTF/Utils/LogHelper.cs @@ -84,7 +84,7 @@ public static void UseRequestLogging(this WebApplication app) { app.UseSerilogRequestLogging(options => { - options.MessageTemplate = "[{StatusCode}] @{Elapsed,8:####0.00}ms HTTP {RequestMethod,-6} {RequestPath} From {RemoteIP}"; + options.MessageTemplate = "[{StatusCode}] {Elapsed,8:####0.00}ms HTTP {RequestMethod,-6} {RequestPath} @ {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; @@ -110,54 +110,53 @@ public static void UseRequestLogging(this WebApplication app) private const string LogTemplate = "[{@t:yy-MM-dd HH:mm:ss.fff} {@l:u3}] {Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)}: {@m} {#if Length(Status) > 0}#{Status} <{UserName}>{#if Length(IP) > 0}@{IP}{#end}{#end}\n{@x}"; private const string InitLogTemplate = "[{@t:yy-MM-dd HH:mm:ss.fff} {@l:u3}] {@m}\n{@x}"; - public static Logger GetInitLogger() + public static Serilog.ILogger GetInitLogger() => new LoggerConfiguration() .Enrich.FromLogContext() .MinimumLevel.Debug() .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("AspNetCoreRateLimit", LogEventLevel.Warning) - .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information) .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Warning) .WriteTo.Async(t => t.Console( formatter: new ExpressionTemplate(InitLogTemplate, theme: TemplateTheme.Literate), restrictedToMinimumLevel: LogEventLevel.Debug )) - .CreateLogger(); + .CreateBootstrapLogger(); - public static Logger GetLogger(IConfiguration configuration, IServiceProvider serviceProvider) + public static Serilog.ILogger GetLogger(IConfiguration configuration, IServiceProvider serviceProvider) => new LoggerConfiguration() - .Enrich.FromLogContext() - .MinimumLevel.Debug() - .Filter.ByExcluding(logEvent => - logEvent.Exception != null && - logEvent.Exception.GetType() == typeof(OperationCanceledException)) - .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) - .MinimumLevel.Override("AspNetCoreRateLimit", LogEventLevel.Warning) - .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Warning) - .WriteTo.Async(t => t.Console( - formatter: new ExpressionTemplate(LogTemplate, theme: TemplateTheme.Literate), - restrictedToMinimumLevel: LogEventLevel.Debug - )) - .WriteTo.Async(t => t.File( - path: "log/log_.log", - formatter: new ExpressionTemplate(LogTemplate), - rollingInterval: RollingInterval.Day, - fileSizeLimitBytes: 10 * 1024 * 1024, - restrictedToMinimumLevel: LogEventLevel.Debug, - rollOnFileSizeLimit: true, - retainedFileCountLimit: 5, - hooks: new ArchiveHooks(CompressionLevel.Optimal, "log/archive/{UtcDate:yyyy-MM}") - )) - .WriteTo.Async(t => t.PostgreSQL( - connectionString: configuration.GetConnectionString("Database"), - tableName: "Logs", - respectCase: true, - columnOptions: ColumnWriters, - restrictedToMinimumLevel: LogEventLevel.Information, - period: TimeSpan.FromSeconds(30) - )) - .WriteTo.SignalR(serviceProvider) - .CreateLogger(); + .Enrich.FromLogContext() + .MinimumLevel.Debug() + .Filter.ByExcluding(logEvent => + logEvent.Exception != null && + logEvent.Exception.GetType() == typeof(OperationCanceledException)) + .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) + .MinimumLevel.Override("AspNetCoreRateLimit", LogEventLevel.Warning) + .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Warning) + .WriteTo.Async(t => t.Console( + formatter: new ExpressionTemplate(LogTemplate, theme: TemplateTheme.Literate), + restrictedToMinimumLevel: LogEventLevel.Debug + )) + .WriteTo.Async(t => t.File( + path: "log/log_.log", + formatter: new ExpressionTemplate(LogTemplate), + rollingInterval: RollingInterval.Day, + fileSizeLimitBytes: 10 * 1024 * 1024, + restrictedToMinimumLevel: LogEventLevel.Debug, + rollOnFileSizeLimit: true, + retainedFileCountLimit: 5, + hooks: new ArchiveHooks(CompressionLevel.Optimal, "log/archive/{UtcDate:yyyy-MM}") + )) + .WriteTo.Async(t => t.PostgreSQL( + connectionString: configuration.GetConnectionString("Database"), + tableName: "Logs", + respectCase: true, + columnOptions: ColumnWriters, + restrictedToMinimumLevel: LogEventLevel.Information, + period: TimeSpan.FromSeconds(30) + )) + .WriteTo.SignalR(serviceProvider) + .CreateLogger(); } public class TimeColumnWriter : ColumnWriterBase