优化部分方法、namespace名称

修改日志输出文件名 细化日志等级存储
This commit is contained in:
root 2024-12-26 15:28:55 +08:00
parent 510c70cb83
commit 8a38a79af0
8 changed files with 40 additions and 46 deletions

View file

@ -6,14 +6,9 @@ using Microsoft.AspNetCore.Mvc.Filters;
namespace iFileProxy.Attributes
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class AuthorizeAttribute : Attribute, IAuthorizationFilter
public class AuthorizeAttribute(params UserMask[] allowedMasks) : Attribute, IAuthorizationFilter
{
private readonly UserMask[] _allowedMasks;
public AuthorizeAttribute(params UserMask[] allowedMasks)
{
_allowedMasks = allowedMasks;
}
private readonly UserMask[] _allowedMasks = allowedMasks;
public void OnAuthorization(AuthorizationFilterContext context)
{

View file

@ -40,9 +40,9 @@ namespace iFileProxy.Config
{
return JsonSerializer.Deserialize<AppConfig>(File.ReadAllText(configPath),options);
}
catch (Exception)
catch (Exception e)
{
_logger.Error("Config Parse Error!");
_logger.Fatal(e,"Config Parse Error!");
throw;
}
}

View file

@ -22,7 +22,7 @@ namespace iFileProxy.Controllers
[HttpPut("{*proxyUrl}")]
[HttpDelete("{*proxyUrl}")]
[HttpPatch("{*proxyUrl}")]
public async Task<IActionResult> ProxyGitRequest(string proxyUrl)
public async Task<IActionResult> ProxyRequest(string proxyUrl)
{
try
{

View file

@ -1,16 +1,11 @@
using Microsoft.AspNetCore.Components;
using iFileProxy.Helpers;
using iFileProxy.Models;
using Serilog;
using System.Net;
using System.Text.Json;
namespace iFileProxy.Middleware
namespace iFileProxy.Middlewares
{
using iFileProxy.Helpers;
using iFileProxy.Models;
using Microsoft.AspNetCore.Http;
using Serilog;
using System;
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;
public class ErrorHandlerMiddleware(RequestDelegate next)
{
private readonly RequestDelegate _next = next;

View file

@ -1,14 +1,10 @@
namespace iFileProxy.Middleware
{
using iFileProxy.Config;
using iFileProxy.Helpers;
using iFileProxy.Models;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading.Tasks;
using iFileProxy.Config;
using iFileProxy.Helpers;
using iFileProxy.Models;
using System.Text.Json;
namespace iFileProxy.Middlewares
{
public class IPAccessLimitMiddleware(RequestDelegate next, Dictionary<string, Dictionary<string, uint>> IPAccessCountDict, AppConfig appConfig)
{
private readonly RequestDelegate _next = next;
@ -22,7 +18,7 @@
if (appConfig.SecurityOptions.BlockedClientIP.IndexOf(ipStr) != -1)
{
context.Response.StatusCode = 403;
await context.Response.WriteAsJsonAsync(new CommonRsp { Retcode = 403, Message = "你的IP地址已经被管理员拉入黑名单!"});
await context.Response.WriteAsJsonAsync(new CommonRsp { Retcode = 403, Message = "你的IP地址已经被管理员拉入黑名单!" });
return;
}

View file

@ -5,7 +5,7 @@ using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
namespace iFileProxy.Middleware
namespace iFileProxy.Middlewares
{
public class JwtMiddleware(RequestDelegate next, IConfiguration configuration)
{
@ -48,10 +48,10 @@ namespace iFileProxy.Middleware
// 如果客户端信息不匹配,则拒绝访问
if (fingerprint != tokenFingerprint ||
userAgent != tokenUserAgent ||
(ip != tokenIp && !IsLocalNetwork(ip, tokenIp))) // 允许本地网络IP变化
ip != tokenIp && !IsLocalNetwork(ip, tokenIp)) // 允许本地网络IP变化
{
context.Response.StatusCode = 401;
await context.Response.WriteAsJsonAsync(new CommonRsp { Message = "环境发生变化, 请重新验证", Retcode = 401});
await context.Response.WriteAsJsonAsync(new CommonRsp { Message = "环境发生变化, 请重新验证", Retcode = 401 });
return;
}
@ -72,9 +72,9 @@ namespace iFileProxy.Middleware
if (string.IsNullOrEmpty(ip1) || string.IsNullOrEmpty(ip2)) return false;
// 检查是否都是内网IP
return (ip1.StartsWith("192.168.") && ip2.StartsWith("192.168.")) ||
(ip1.StartsWith("10.") && ip2.StartsWith("10.")) ||
(ip1.StartsWith("172.") && ip2.StartsWith("172."));
return ip1.StartsWith("192.168.") && ip2.StartsWith("192.168.") ||
ip1.StartsWith("10.") && ip2.StartsWith("10.") ||
ip1.StartsWith("172.") && ip2.StartsWith("172.");
}
}
}

View file

@ -1,5 +1,4 @@
using iFileProxy.Config;
using iFileProxy.Middleware;
using iFileProxy.Helpers;
using iFileProxy.Handlers;
using iFileProxy.Services;
@ -8,6 +7,7 @@ using Microsoft.IdentityModel.Tokens;
using Serilog;
using System.Text;
using MySql.Data.MySqlClient;
using iFileProxy.Middlewares;
namespace iFileProxy
{

View file

@ -5,8 +5,8 @@
using System.Net;
using iFileProxy.Sinks;
using iFileProxy.Services;
using ZstdSharp.Unsafe;
using iFileProxy.Helpers;
using Microsoft.IdentityModel.Tokens;
public static class SerilogConfig
{
@ -26,7 +26,7 @@
var loggerConfiguration = new LoggerConfiguration();
if (new CommandLineArgsHelper(args).GetBooleanValue("dev-logger"))
if (args != null && new CommandLineArgsHelper(args).GetBooleanValue("dev-logger"))
{
loggerConfiguration.MinimumLevel.Debug();
}
@ -42,9 +42,17 @@
outputTemplate: "{Timestamp:HH:mm:ss.fff} [{Level:u3}] [{SourceContext}] {ClientIp} {Message:lj} {contentType}{NewLine} {Exception}")
// 错误和致命错误写入单独的文件
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(evt => evt.Level >= LogEventLevel.Error)
.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Error)
.WriteTo.File(
Path.Combine(baseLogPath, $"{appName}.error.log"),
Path.Combine(baseLogPath, $"{appName}.error.date.log"),
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {ClientIp} {Message:lj} {contentType} {queryString}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: 1073741824)) // 1GB
// 错误和致命错误写入单独的文件
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Fatal)
.WriteTo.File(
Path.Combine(baseLogPath, $"{appName}.fatal.date.log"),
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {ClientIp} {Message:lj} {contentType} {queryString}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: 1073741824)) // 1GB
@ -52,7 +60,7 @@
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Warning)
.WriteTo.File(
Path.Combine(baseLogPath, $"{appName}.warning.log"),
Path.Combine(baseLogPath, $"{appName}.warning.date.log"),
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {ClientIp} {Message:lj} {contentType} {queryString}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: 1073741824))
@ -60,7 +68,7 @@
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Information)
.WriteTo.File(
Path.Combine(baseLogPath, $"{appName}.info.log"),
Path.Combine(baseLogPath, $"{appName}.info.date.log"),
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {ClientIp} {Message:lj} {contentType} {queryString}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: 1073741824))
@ -68,7 +76,7 @@
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Debug)
.WriteTo.File(
Path.Combine(baseLogPath, $"{appName}.debug.log"),
Path.Combine(baseLogPath, $"{appName}.debug.date.log"),
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {ClientIp} {Message:lj} {contentType} {queryString}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: 1073741824))