优化部分方法、namespace名称
修改日志输出文件名 细化日志等级存储
This commit is contained in:
parent
510c70cb83
commit
8a38a79af0
8 changed files with 40 additions and 46 deletions
|
@ -6,14 +6,9 @@ using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
namespace iFileProxy.Attributes
|
namespace iFileProxy.Attributes
|
||||||
{
|
{
|
||||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
|
||||||
public class AuthorizeAttribute : Attribute, IAuthorizationFilter
|
public class AuthorizeAttribute(params UserMask[] allowedMasks) : Attribute, IAuthorizationFilter
|
||||||
{
|
{
|
||||||
private readonly UserMask[] _allowedMasks;
|
private readonly UserMask[] _allowedMasks = allowedMasks;
|
||||||
|
|
||||||
public AuthorizeAttribute(params UserMask[] allowedMasks)
|
|
||||||
{
|
|
||||||
_allowedMasks = allowedMasks;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnAuthorization(AuthorizationFilterContext context)
|
public void OnAuthorization(AuthorizationFilterContext context)
|
||||||
{
|
{
|
||||||
|
|
|
@ -40,9 +40,9 @@ namespace iFileProxy.Config
|
||||||
{
|
{
|
||||||
return JsonSerializer.Deserialize<AppConfig>(File.ReadAllText(configPath),options);
|
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;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ namespace iFileProxy.Controllers
|
||||||
[HttpPut("{*proxyUrl}")]
|
[HttpPut("{*proxyUrl}")]
|
||||||
[HttpDelete("{*proxyUrl}")]
|
[HttpDelete("{*proxyUrl}")]
|
||||||
[HttpPatch("{*proxyUrl}")]
|
[HttpPatch("{*proxyUrl}")]
|
||||||
public async Task<IActionResult> ProxyGitRequest(string proxyUrl)
|
public async Task<IActionResult> ProxyRequest(string proxyUrl)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,16 +1,11 @@
|
||||||
using Microsoft.AspNetCore.Components;
|
using iFileProxy.Helpers;
|
||||||
|
|
||||||
namespace iFileProxy.Middleware
|
|
||||||
{
|
|
||||||
using iFileProxy.Helpers;
|
|
||||||
using iFileProxy.Models;
|
using iFileProxy.Models;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using System;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
|
namespace iFileProxy.Middlewares
|
||||||
|
{
|
||||||
public class ErrorHandlerMiddleware(RequestDelegate next)
|
public class ErrorHandlerMiddleware(RequestDelegate next)
|
||||||
{
|
{
|
||||||
private readonly RequestDelegate _next = next;
|
private readonly RequestDelegate _next = next;
|
||||||
|
|
|
@ -1,14 +1,10 @@
|
||||||
namespace iFileProxy.Middleware
|
using iFileProxy.Config;
|
||||||
{
|
|
||||||
using iFileProxy.Config;
|
|
||||||
using iFileProxy.Helpers;
|
using iFileProxy.Helpers;
|
||||||
using iFileProxy.Models;
|
using iFileProxy.Models;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
|
namespace iFileProxy.Middlewares
|
||||||
|
{
|
||||||
public class IPAccessLimitMiddleware(RequestDelegate next, Dictionary<string, Dictionary<string, uint>> IPAccessCountDict, AppConfig appConfig)
|
public class IPAccessLimitMiddleware(RequestDelegate next, Dictionary<string, Dictionary<string, uint>> IPAccessCountDict, AppConfig appConfig)
|
||||||
{
|
{
|
||||||
private readonly RequestDelegate _next = next;
|
private readonly RequestDelegate _next = next;
|
||||||
|
|
|
@ -5,7 +5,7 @@ using System.IdentityModel.Tokens.Jwt;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace iFileProxy.Middleware
|
namespace iFileProxy.Middlewares
|
||||||
{
|
{
|
||||||
public class JwtMiddleware(RequestDelegate next, IConfiguration configuration)
|
public class JwtMiddleware(RequestDelegate next, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
|
@ -48,7 +48,7 @@ namespace iFileProxy.Middleware
|
||||||
// 如果客户端信息不匹配,则拒绝访问
|
// 如果客户端信息不匹配,则拒绝访问
|
||||||
if (fingerprint != tokenFingerprint ||
|
if (fingerprint != tokenFingerprint ||
|
||||||
userAgent != tokenUserAgent ||
|
userAgent != tokenUserAgent ||
|
||||||
(ip != tokenIp && !IsLocalNetwork(ip, tokenIp))) // 允许本地网络IP变化
|
ip != tokenIp && !IsLocalNetwork(ip, tokenIp)) // 允许本地网络IP变化
|
||||||
{
|
{
|
||||||
context.Response.StatusCode = 401;
|
context.Response.StatusCode = 401;
|
||||||
await context.Response.WriteAsJsonAsync(new CommonRsp { Message = "环境发生变化, 请重新验证", Retcode = 401 });
|
await context.Response.WriteAsJsonAsync(new CommonRsp { Message = "环境发生变化, 请重新验证", Retcode = 401 });
|
||||||
|
@ -72,9 +72,9 @@ namespace iFileProxy.Middleware
|
||||||
if (string.IsNullOrEmpty(ip1) || string.IsNullOrEmpty(ip2)) return false;
|
if (string.IsNullOrEmpty(ip1) || string.IsNullOrEmpty(ip2)) return false;
|
||||||
|
|
||||||
// 检查是否都是内网IP
|
// 检查是否都是内网IP
|
||||||
return (ip1.StartsWith("192.168.") && ip2.StartsWith("192.168.")) ||
|
return ip1.StartsWith("192.168.") && ip2.StartsWith("192.168.") ||
|
||||||
(ip1.StartsWith("10.") && ip2.StartsWith("10.")) ||
|
ip1.StartsWith("10.") && ip2.StartsWith("10.") ||
|
||||||
(ip1.StartsWith("172.") && ip2.StartsWith("172."));
|
ip1.StartsWith("172.") && ip2.StartsWith("172.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
using iFileProxy.Config;
|
using iFileProxy.Config;
|
||||||
using iFileProxy.Middleware;
|
|
||||||
using iFileProxy.Helpers;
|
using iFileProxy.Helpers;
|
||||||
using iFileProxy.Handlers;
|
using iFileProxy.Handlers;
|
||||||
using iFileProxy.Services;
|
using iFileProxy.Services;
|
||||||
|
@ -8,6 +7,7 @@ using Microsoft.IdentityModel.Tokens;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using MySql.Data.MySqlClient;
|
using MySql.Data.MySqlClient;
|
||||||
|
using iFileProxy.Middlewares;
|
||||||
|
|
||||||
namespace iFileProxy
|
namespace iFileProxy
|
||||||
{
|
{
|
||||||
|
|
|
@ -5,8 +5,8 @@
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using iFileProxy.Sinks;
|
using iFileProxy.Sinks;
|
||||||
using iFileProxy.Services;
|
using iFileProxy.Services;
|
||||||
using ZstdSharp.Unsafe;
|
|
||||||
using iFileProxy.Helpers;
|
using iFileProxy.Helpers;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
|
||||||
public static class SerilogConfig
|
public static class SerilogConfig
|
||||||
{
|
{
|
||||||
|
@ -26,7 +26,7 @@
|
||||||
var loggerConfiguration = new LoggerConfiguration();
|
var loggerConfiguration = new LoggerConfiguration();
|
||||||
|
|
||||||
|
|
||||||
if (new CommandLineArgsHelper(args).GetBooleanValue("dev-logger"))
|
if (args != null && new CommandLineArgsHelper(args).GetBooleanValue("dev-logger"))
|
||||||
{
|
{
|
||||||
loggerConfiguration.MinimumLevel.Debug();
|
loggerConfiguration.MinimumLevel.Debug();
|
||||||
}
|
}
|
||||||
|
@ -42,9 +42,17 @@
|
||||||
outputTemplate: "{Timestamp:HH:mm:ss.fff} [{Level:u3}] [{SourceContext}] {ClientIp} {Message:lj} {contentType}{NewLine} {Exception}")
|
outputTemplate: "{Timestamp:HH:mm:ss.fff} [{Level:u3}] [{SourceContext}] {ClientIp} {Message:lj} {contentType}{NewLine} {Exception}")
|
||||||
// 错误和致命错误写入单独的文件
|
// 错误和致命错误写入单独的文件
|
||||||
.WriteTo.Logger(lc => lc
|
.WriteTo.Logger(lc => lc
|
||||||
.Filter.ByIncludingOnly(evt => evt.Level >= LogEventLevel.Error)
|
.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Error)
|
||||||
.WriteTo.File(
|
.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}",
|
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {ClientIp} {Message:lj} {contentType} {queryString}{NewLine}{Exception}",
|
||||||
rollingInterval: RollingInterval.Day,
|
rollingInterval: RollingInterval.Day,
|
||||||
fileSizeLimitBytes: 1073741824)) // 1GB
|
fileSizeLimitBytes: 1073741824)) // 1GB
|
||||||
|
@ -52,7 +60,7 @@
|
||||||
.WriteTo.Logger(lc => lc
|
.WriteTo.Logger(lc => lc
|
||||||
.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Warning)
|
.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Warning)
|
||||||
.WriteTo.File(
|
.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}",
|
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {ClientIp} {Message:lj} {contentType} {queryString}{NewLine}{Exception}",
|
||||||
rollingInterval: RollingInterval.Day,
|
rollingInterval: RollingInterval.Day,
|
||||||
fileSizeLimitBytes: 1073741824))
|
fileSizeLimitBytes: 1073741824))
|
||||||
|
@ -60,7 +68,7 @@
|
||||||
.WriteTo.Logger(lc => lc
|
.WriteTo.Logger(lc => lc
|
||||||
.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Information)
|
.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Information)
|
||||||
.WriteTo.File(
|
.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}",
|
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {ClientIp} {Message:lj} {contentType} {queryString}{NewLine}{Exception}",
|
||||||
rollingInterval: RollingInterval.Day,
|
rollingInterval: RollingInterval.Day,
|
||||||
fileSizeLimitBytes: 1073741824))
|
fileSizeLimitBytes: 1073741824))
|
||||||
|
@ -68,7 +76,7 @@
|
||||||
.WriteTo.Logger(lc => lc
|
.WriteTo.Logger(lc => lc
|
||||||
.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Debug)
|
.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Debug)
|
||||||
.WriteTo.File(
|
.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}",
|
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {ClientIp} {Message:lj} {contentType} {queryString}{NewLine}{Exception}",
|
||||||
rollingInterval: RollingInterval.Day,
|
rollingInterval: RollingInterval.Day,
|
||||||
fileSizeLimitBytes: 1073741824))
|
fileSizeLimitBytes: 1073741824))
|
||||||
|
|
Loading…
Reference in a new issue