Files
caliverse_server/UGQApiServer/ApiFilter.cs
2025-05-01 07:20:41 +09:00

143 lines
3.6 KiB
C#

using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
namespace UGQApiServer;
public enum UgqApiType
{
None,
UgqAdmin,
UgqIngame,
UgqApi,
UgqAllInOne
}
public class ApiAllowSettings
{
public bool AllowInGameApi { get; init; } = false;
public bool AllowWebApi { get; init; } = false;
public bool AllowAdminApi { get; init; } = false;
public ServerType ServerType { get; init; } = ServerType.None;
public ApiAllowSettings(UgqApiType type)
{
switch (type)
{
case UgqApiType.UgqAdmin:
AllowAdminApi = true;
ServerType = ServerType.UgqAdmin;
break;
case UgqApiType.UgqApi:
AllowWebApi = true;
ServerType = ServerType.UgqApi;
break;
case UgqApiType.UgqIngame:
AllowInGameApi = true;
ServerType = ServerType.UgqIngame;
break;
case UgqApiType.UgqAllInOne:
AllowAdminApi = true;
AllowWebApi = true;
AllowInGameApi = true;
break;
}
}
}
public class UGQInGameApiAttribute : Attribute, IResourceFilter
{
public void OnResourceExecuted(ResourceExecutedContext context)
{
}
public void OnResourceExecuting(ResourceExecutingContext context)
{
var settings = context.HttpContext.RequestServices.GetService<ApiAllowSettings>();
if (settings == null)
{
context.Result = new NotFoundResult();
}
else
{
if(settings.AllowInGameApi == false)
context.Result = new NotFoundResult();
}
}
}
public class UGQWebApiAttribute : Attribute, IResourceFilter
{
public void OnResourceExecuted(ResourceExecutedContext context)
{
}
public void OnResourceExecuting(ResourceExecutingContext context)
{
var settings = context.HttpContext.RequestServices.GetService<ApiAllowSettings>();
if (settings == null)
{
context.Result = new NotFoundResult();
}
else
{
if (settings.AllowWebApi == false)
context.Result = new NotFoundResult();
}
}
}
public class UGQAdminApiAttribute : Attribute, IResourceFilter
{
public void OnResourceExecuted(ResourceExecutedContext context)
{
}
public void OnResourceExecuting(ResourceExecutingContext context)
{
var settings = context.HttpContext.RequestServices.GetService<ApiAllowSettings>();
if (settings == null)
{
context.Result = new NotFoundResult();
}
else
{
if (settings.AllowAdminApi == false)
context.Result = new NotFoundResult();
}
}
}
public class DevelopmentOnlyAttribute : Attribute, IResourceFilter
{
public void OnResourceExecuted(ResourceExecutedContext context)
{
}
public void OnResourceExecuting(ResourceExecutingContext context)
{
bool cmd_develop = false;
var cmdOptions = context.HttpContext.RequestServices.GetService<CommandLineOptions>();
if (cmdOptions != null)
{
cmd_develop = cmdOptions.m_develop;
}
bool isDevelopment = false;
var env = context.HttpContext.RequestServices.GetService<IWebHostEnvironment>();
if (env != null)
{
isDevelopment = env.IsDevelopment() || (env.EnvironmentName == "AWSDev") || cmd_develop;
}
if (isDevelopment == false)
context.Result = new NotFoundResult();
}
}