|
@@ -12,135 +12,162 @@ using Azure.Core;
|
|
using TEAMModelOS.SDK.DI;
|
|
using TEAMModelOS.SDK.DI;
|
|
using Microsoft.Extensions.Primitives;
|
|
using Microsoft.Extensions.Primitives;
|
|
using HTEXLib.Helpers.ShapeHelpers;
|
|
using HTEXLib.Helpers.ShapeHelpers;
|
|
|
|
+using System.Net.Http;
|
|
|
|
+using System.ServiceModel.Channels;
|
|
|
|
+using System.Net;
|
|
|
|
+using System.Net.Http.Json;
|
|
|
|
|
|
namespace TEAMModelOS.Filter
|
|
namespace TEAMModelOS.Filter
|
|
{
|
|
{
|
|
public class RequestAuditFilter : IAsyncActionFilter
|
|
public class RequestAuditFilter : IAsyncActionFilter
|
|
{
|
|
{
|
|
//private readonly ILogger _logger;
|
|
//private readonly ILogger _logger;
|
|
- private readonly HttpTrigger _httpTrigger;
|
|
|
|
- public RequestAuditFilter(/*ILoggerFactory loggerFactory*/HttpTrigger httpTrigger)
|
|
|
|
|
|
+ // private readonly HttpTrigger _httpTrigger;
|
|
|
|
+ private readonly IHttpClientFactory _httpClient;
|
|
|
|
+ private readonly DingDing _dingding;
|
|
|
|
+ public RequestAuditFilter(/*ILoggerFactory loggerFactory*/IHttpClientFactory httpClient, DingDing dingding)
|
|
{
|
|
{
|
|
// _logger = loggerFactory.CreateLogger<RequestAuditFilter>();
|
|
// _logger = loggerFactory.CreateLogger<RequestAuditFilter>();
|
|
- _httpTrigger = httpTrigger;
|
|
|
|
|
|
+ // _httpTrigger = httpTrigger;
|
|
|
|
+ _httpClient = httpClient;
|
|
|
|
+ _dingding=dingding;
|
|
}
|
|
}
|
|
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
|
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
|
{
|
|
{
|
|
- string id = string.Empty, name = string.Empty, picture = string.Empty, school = string.Empty, scope = string.Empty, roles = string.Empty;
|
|
|
|
|
|
|
|
- //============== 这里是执行方法之前获取数据 ====================
|
|
|
|
|
|
+ try {
|
|
|
|
+ string id = string.Empty, name = string.Empty, picture = string.Empty, school = string.Empty, scope = string.Empty, roles = string.Empty;
|
|
|
|
|
|
- // 获取控制器、路由信息
|
|
|
|
- //var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
|
|
|
|
|
|
+ //============== 这里是执行方法之前获取数据 ====================
|
|
|
|
|
|
- // 获取请求的方法
|
|
|
|
- //var method = actionDescriptor.MethodInfo;
|
|
|
|
|
|
+ // 获取控制器、路由信息
|
|
|
|
+ //var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
|
|
|
|
|
|
- // 获取 HttpContext 和 HttpRequest 对象
|
|
|
|
- var httpContext = context.HttpContext;
|
|
|
|
- string ua = httpContext.GetUserAgent();
|
|
|
|
- var httpRequest = httpContext.Request;
|
|
|
|
|
|
+ // 获取请求的方法
|
|
|
|
+ //var method = actionDescriptor.MethodInfo;
|
|
|
|
|
|
- // 获取客户端 Ipv4 地址
|
|
|
|
- var remoteIPv4 = httpContext.GetRemoteIpAddressToIPv4();
|
|
|
|
|
|
+ // 获取 HttpContext 和 HttpRequest 对象
|
|
|
|
+ var httpContext = context.HttpContext;
|
|
|
|
+ string ua = httpContext.GetUserAgent();
|
|
|
|
+ var httpRequest = httpContext.Request;
|
|
|
|
|
|
- // 获取请求的 Url 地址
|
|
|
|
- // var requestUrl = httpRequest.GetRequestUrlAddress();
|
|
|
|
|
|
+ // 获取客户端 Ipv4 地址
|
|
|
|
+ var remoteIPv4 = httpContext.GetRemoteIpAddressToIPv4();
|
|
|
|
|
|
|
|
+ // 获取请求的 Url 地址
|
|
|
|
+ // var requestUrl = httpRequest.GetRequestUrlAddress();
|
|
|
|
|
|
- // 获取来源 Url 地址
|
|
|
|
- var refererUrl = httpRequest.GetRefererUrlAddress();
|
|
|
|
|
|
|
|
- // 获取请求参数(写入日志,需序列化成字符串后存储)
|
|
|
|
- var parameters = context.ActionArguments;
|
|
|
|
|
|
+ // 获取来源 Url 地址
|
|
|
|
+ var refererUrl = httpRequest.GetRefererUrlAddress();
|
|
|
|
|
|
- // 获取操作人(必须授权访问才有值)"userId" 为你存储的 claims type,jwt 授权对应的是 payload 中存储的键名
|
|
|
|
- //var userId = httpContext.User?.FindFirstValue("userId");
|
|
|
|
- var authtoken = context.HttpContext.GetXAuth("AuthToken");
|
|
|
|
- string tokenSha = string.Empty, client = string.Empty;
|
|
|
|
- if (context.HttpContext.Request.Headers.TryGetValue("Authorization", out StringValues Authorization))
|
|
|
|
- {
|
|
|
|
- var jwt = new JwtSecurityTokenHandler().ReadJwtToken(Authorization.ToString().Replace("Bearer ", ""));
|
|
|
|
- client= roles = jwt.Claims.FirstOrDefault(claim => claim.Type.Equals("roles"))?.Value;
|
|
|
|
- tokenSha= ShaHashHelper.GetSHA1(Authorization.ToString());
|
|
|
|
- }
|
|
|
|
- if (context.HttpContext.Request.Headers.TryGetValue("X-Auth-IdToken", out StringValues XAuthIdToken))
|
|
|
|
- {
|
|
|
|
- var jwt = new JwtSecurityTokenHandler().ReadJwtToken(XAuthIdToken);
|
|
|
|
- id = jwt.Payload.Sub;
|
|
|
|
- name = jwt.Claims.FirstOrDefault(claim => claim.Type.Equals("name"))?.Value;
|
|
|
|
- if (string.IsNullOrEmpty(tokenSha))
|
|
|
|
|
|
+ // 获取请求参数(写入日志,需序列化成字符串后存储)
|
|
|
|
+ var parameters = context.ActionArguments;
|
|
|
|
+
|
|
|
|
+ // 获取操作人(必须授权访问才有值)"userId" 为你存储的 claims type,jwt 授权对应的是 payload 中存储的键名
|
|
|
|
+ //var userId = httpContext.User?.FindFirstValue("userId");
|
|
|
|
+ var authtoken = context.HttpContext.GetXAuth("AuthToken");
|
|
|
|
+ string tokenSha = string.Empty, client = string.Empty;
|
|
|
|
+ if (context.HttpContext.Request.Headers.TryGetValue("Authorization", out StringValues Authorization))
|
|
{
|
|
{
|
|
- tokenSha= ShaHashHelper.GetSHA1(XAuthIdToken.ToString());
|
|
|
|
|
|
+ var jwt = new JwtSecurityTokenHandler().ReadJwtToken(Authorization.ToString().Replace("Bearer ", ""));
|
|
|
|
+ client= roles = jwt.Claims.FirstOrDefault(claim => claim.Type.Equals("roles"))?.Value;
|
|
|
|
+ tokenSha= ShaHashHelper.GetSHA1(Authorization.ToString());
|
|
}
|
|
}
|
|
- }
|
|
|
|
- if (context.HttpContext.Request.Headers.TryGetValue("X-Auth-School", out StringValues XAuthSchool))
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
|
|
+ if (context.HttpContext.Request.Headers.TryGetValue("X-Auth-IdToken", out StringValues XAuthIdToken))
|
|
{
|
|
{
|
|
- school = XAuthSchool.ToString();
|
|
|
|
|
|
+ var jwt = new JwtSecurityTokenHandler().ReadJwtToken(XAuthIdToken);
|
|
|
|
+ id = jwt.Payload.Sub;
|
|
|
|
+ name = jwt.Claims.FirstOrDefault(claim => claim.Type.Equals("name"))?.Value;
|
|
|
|
+ if (string.IsNullOrEmpty(tokenSha))
|
|
|
|
+ {
|
|
|
|
+ tokenSha= ShaHashHelper.GetSHA1(XAuthIdToken.ToString());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (context.HttpContext.Request.Headers.TryGetValue("X-Auth-School", out StringValues XAuthSchool))
|
|
|
|
+ {
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ school = XAuthSchool.ToString();
|
|
|
|
+ }
|
|
|
|
+ catch (Exception ex) { }
|
|
}
|
|
}
|
|
- catch (Exception ex) { }
|
|
|
|
- }
|
|
|
|
|
|
|
|
- if (!string.IsNullOrWhiteSpace(authtoken))
|
|
|
|
- {
|
|
|
|
- var jwt = new JwtSecurityTokenHandler().ReadJwtToken(authtoken);
|
|
|
|
- id = jwt.Payload.Sub;
|
|
|
|
- school = jwt.Payload.Azp;
|
|
|
|
- name = jwt.Claims.FirstOrDefault(claim => claim.Type.Equals("name"))?.Value;
|
|
|
|
- scope = jwt.Claims.FirstOrDefault(claim => claim.Type.Equals("scope"))?.Value;
|
|
|
|
|
|
+ if (!string.IsNullOrWhiteSpace(authtoken))
|
|
|
|
+ {
|
|
|
|
+ var jwt = new JwtSecurityTokenHandler().ReadJwtToken(authtoken);
|
|
|
|
+ id = jwt.Payload.Sub;
|
|
|
|
+ school = jwt.Payload.Azp;
|
|
|
|
+ name = jwt.Claims.FirstOrDefault(claim => claim.Type.Equals("name"))?.Value;
|
|
|
|
+ scope = jwt.Claims.FirstOrDefault(claim => claim.Type.Equals("scope"))?.Value;
|
|
|
|
+ if (string.IsNullOrEmpty(tokenSha))
|
|
|
|
+ {
|
|
|
|
+ tokenSha= ShaHashHelper.GetSHA1(authtoken);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ string secChUaPlatform = string.Empty;
|
|
|
|
+ if (httpContext.Request.Headers.TryGetValue("Sec-Ch-Ua-Platform", out var values))
|
|
|
|
+ {
|
|
|
|
+ secChUaPlatform = values.FirstOrDefault();
|
|
|
|
+ }
|
|
if (string.IsNullOrEmpty(tokenSha))
|
|
if (string.IsNullOrEmpty(tokenSha))
|
|
|
|
+
|
|
{
|
|
{
|
|
- tokenSha= ShaHashHelper.GetSHA1(authtoken);
|
|
|
|
|
|
+ tokenSha= ShaHashHelper.GetSHA1($"{ua}{remoteIPv4}{httpRequest.Host}{secChUaPlatform}");
|
|
}
|
|
}
|
|
- }
|
|
|
|
- string secChUaPlatform = string.Empty;
|
|
|
|
- if (httpContext.Request.Headers.TryGetValue("Sec-Ch-Ua-Platform", out var values))
|
|
|
|
- {
|
|
|
|
- secChUaPlatform = values.FirstOrDefault();
|
|
|
|
- }
|
|
|
|
- if (string.IsNullOrEmpty(tokenSha))
|
|
|
|
|
|
+ // 请求时间
|
|
|
|
+ var requestedTime = DateTimeOffset.Now.GetGMTTime(8).ToUnixTimeMilliseconds();
|
|
|
|
+ //============== 这里是执行方法之后获取数据 ====================
|
|
|
|
+ var actionContext = await next();
|
|
|
|
+ // 获取返回的结果
|
|
|
|
+ // var returnResult = actionContext.Result;
|
|
|
|
+
|
|
|
|
+ // 判断是否请求成功,没有异常就是请求成功
|
|
|
|
+ // var isRequestSucceed = actionContext.Exception == null;
|
|
|
|
+
|
|
|
|
+ // 获取调用堆栈信息,提供更加简单明了的调用和异常堆栈
|
|
|
|
+ // var stackTrace = EnhancedStackTrace.Current();
|
|
|
|
+ // string region = await _searcher.SearchIpAsync(remoteIPv4);
|
|
|
|
+ //同一个账号,同一IP,同一接口,UA标识(UA标识随意切换则表示可能会存在DDOS),时间段
|
|
|
|
+ //_logger.LogInformation(new{ ua=httpContext.GetUserAgent(), ip=remoteIPv4,time=requestedTime,path =$"{httpRequest.PathBase}{httpRequest.Path}",host= $"{httpRequest.Host}", param=parameters,id ,name ,school,succeed =isRequestSucceed }.ToJsonString());
|
|
|
|
+
|
|
|
|
+ var data = new
|
|
|
|
+ {
|
|
|
|
+ //ua =ua,
|
|
|
|
+ ip = remoteIPv4,
|
|
|
|
+ time = requestedTime,
|
|
|
|
+ path = $"{httpRequest.PathBase}{httpRequest.Path}",
|
|
|
|
+ host = $"{httpRequest.Host}",
|
|
|
|
+ param = parameters,
|
|
|
|
+ id = id,
|
|
|
|
+ name = name,
|
|
|
|
+ school = school,
|
|
|
|
+ client = client,
|
|
|
|
+ tid = tokenSha,
|
|
|
|
+ scope = scope,
|
|
|
|
+ // referer = refererUrl,
|
|
|
|
+ //platform = secChUaPlatform,
|
|
|
|
+ p = "bi",
|
|
|
|
+ //idToken=XAuthIdToken
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+#if DEBUG
|
|
|
|
+ var response = await _httpClient.CreateClient().PostAsJsonAsync("http://cdhabook.teammodel.cn:8805/api/http-log", data);
|
|
|
|
+ if (response.StatusCode==HttpStatusCode.OK)
|
|
|
|
+ {
|
|
|
|
+ string result = await response.Content.ReadAsStringAsync();
|
|
|
|
|
|
- {
|
|
|
|
- tokenSha= ShaHashHelper.GetSHA1($"{ua}{remoteIPv4}{httpRequest.Host}{secChUaPlatform}");
|
|
|
|
|
|
+ }
|
|
|
|
+#else
|
|
|
|
+ _= _httpClient.CreateClient().PostAsJsonAsync("http://cdhabook.teammodel.cn:8805/api/http-log",data);
|
|
|
|
+#endif
|
|
|
|
+ // _ = _httpTrigger.RequestHttpTrigger(data, "China", "http-log");
|
|
}
|
|
}
|
|
- // 请求时间
|
|
|
|
- var requestedTime = DateTimeOffset.Now.GetGMTTime(8).ToUnixTimeMilliseconds();
|
|
|
|
- //============== 这里是执行方法之后获取数据 ====================
|
|
|
|
- var actionContext = await next();
|
|
|
|
- // 获取返回的结果
|
|
|
|
- // var returnResult = actionContext.Result;
|
|
|
|
-
|
|
|
|
- // 判断是否请求成功,没有异常就是请求成功
|
|
|
|
- // var isRequestSucceed = actionContext.Exception == null;
|
|
|
|
-
|
|
|
|
- // 获取调用堆栈信息,提供更加简单明了的调用和异常堆栈
|
|
|
|
- // var stackTrace = EnhancedStackTrace.Current();
|
|
|
|
- // string region = await _searcher.SearchIpAsync(remoteIPv4);
|
|
|
|
- //同一个账号,同一IP,同一接口,UA标识(UA标识随意切换则表示可能会存在DDOS),时间段
|
|
|
|
- //_logger.LogInformation(new{ ua=httpContext.GetUserAgent(), ip=remoteIPv4,time=requestedTime,path =$"{httpRequest.PathBase}{httpRequest.Path}",host= $"{httpRequest.Host}", param=parameters,id ,name ,school,succeed =isRequestSucceed }.ToJsonString());
|
|
|
|
-
|
|
|
|
- var data = new
|
|
|
|
|
|
+ catch (Exception ex)
|
|
{
|
|
{
|
|
- //ua =ua,
|
|
|
|
- ip = remoteIPv4,
|
|
|
|
- time = requestedTime,
|
|
|
|
- path = $"{httpRequest.PathBase}{httpRequest.Path}",
|
|
|
|
- host = $"{httpRequest.Host}",
|
|
|
|
- param = parameters,
|
|
|
|
- id = id,
|
|
|
|
- name = name,
|
|
|
|
- school = school,
|
|
|
|
- client = client,
|
|
|
|
- tid = tokenSha,
|
|
|
|
- scope = scope,
|
|
|
|
- // referer = refererUrl,
|
|
|
|
- //platform = secChUaPlatform,
|
|
|
|
- p = "os",
|
|
|
|
- //idToken=XAuthIdToken
|
|
|
|
- };
|
|
|
|
- _= _httpTrigger.RequestHttpTrigger(data, "China", "http-log");
|
|
|
|
|
|
+ await _dingding.SendBotMsg($"HTTP日志访问错误:{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
|
|
|
|
+ var actionContext = await next();
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|