81 lines
2.6 KiB
C#
81 lines
2.6 KiB
C#
using Microsoft.AspNetCore.Mvc.Filters;
|
|
using ServiceShared;
|
|
|
|
namespace ServiceOutside.Filter
|
|
{
|
|
public class ServiceInsideRequest : Attribute, IAsyncResultFilter, IAsyncAuthorizationFilter
|
|
{
|
|
/// <summary>
|
|
/// IP of ServiceInside
|
|
/// </summary>
|
|
private static string ServiceInsideIP = "";
|
|
|
|
/// <summary>
|
|
/// If log type is debug, request headers will be printed out in the console
|
|
/// </summary>
|
|
private static Log.Types _LogType = Log.Types.INFO;
|
|
|
|
/// <summary>
|
|
/// Sets the log type
|
|
/// </summary>
|
|
/// <param name="Log.Types">Log.Types</param>
|
|
public static void SetLog(Log.Types logType)
|
|
{
|
|
_LogType = logType;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the IP of ServiceInside
|
|
/// </summary>
|
|
/// <param name="serviceInsideIP">IP of ServiceInside</param>
|
|
public static void SetServiceInsideIP(string serviceInsideIP)
|
|
{
|
|
ServiceInsideIP = serviceInsideIP;
|
|
}
|
|
|
|
public virtual async Task OnAuthorizationAsync(AuthorizationFilterContext context)
|
|
{
|
|
if(context == null || context.HttpContext == null || context.HttpContext.Connection == null || context.HttpContext.Connection.RemoteIpAddress == null ||
|
|
context.HttpContext.Connection.RemoteIpAddress.ToString() != ServiceInsideIP)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logs request in debug mode
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
/// <param name="next"></param>
|
|
/// <returns></returns>
|
|
public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
|
|
{
|
|
if (_LogType == Log.Types.DEBUG)
|
|
{
|
|
string headers = "";
|
|
|
|
foreach (var header in context.HttpContext.Request.Headers)
|
|
{
|
|
headers += header.Key + "=" + header.Value + "\r\n";
|
|
}
|
|
|
|
string? message = "";
|
|
|
|
if (context != null &&
|
|
context.HttpContext != null &&
|
|
context.HttpContext.Connection != null &&
|
|
context.HttpContext.Connection.RemoteIpAddress != null)
|
|
{
|
|
message = context.HttpContext.Connection.RemoteIpAddress.ToString() + " " +
|
|
context.ActionDescriptor.DisplayName + "\r\nHeaders:" + headers;
|
|
|
|
}
|
|
|
|
Log.Debug(message);
|
|
}
|
|
|
|
await next();
|
|
}
|
|
}
|
|
}
|