using Microsoft.AspNetCore.Mvc.Filters; using ServiceShared; namespace ServiceOutside.Filter { public class ServiceInsideRequest : Attribute, IAsyncResultFilter, IAsyncAuthorizationFilter { /// /// IP of ServiceInside /// private static string ServiceInsideIP = ""; /// /// If log type is debug, request headers will be printed out in the console /// private static Log.Types _LogType = Log.Types.INFO; /// /// Sets the log type /// /// Log.Types public static void SetLog(Log.Types logType) { _LogType = logType; } /// /// Sets the IP of ServiceInside /// /// IP of ServiceInside 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; } } /// /// Logs request in debug mode /// /// /// /// 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(); } } }