97 lines
2.8 KiB
C#
97 lines
2.8 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using AspNetCoreRateLimit;
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
|
using Models.Model.External;
|
|
using NetMQ;
|
|
using NetMQ.Sockets;
|
|
const string myAllowSpecificOrigins = "_myAllowSpecificOrigins";
|
|
|
|
WebApplicationBuilder builder = WebApplication.CreateSlimBuilder(args);
|
|
|
|
builder.Services.ConfigureHttpJsonOptions(options =>
|
|
{
|
|
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
|
|
});
|
|
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy(name: myAllowSpecificOrigins, x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
|
|
});
|
|
|
|
// Add memory cache and rate limiting services
|
|
builder.Services.AddMemoryCache();
|
|
builder.Services.Configure<IpRateLimitOptions>(options =>
|
|
{
|
|
options.GeneralRules =
|
|
[
|
|
new()
|
|
{
|
|
Endpoint = "*", // Apply to all endpoints
|
|
Period = "10s", // Rate limiting window of 10 second
|
|
Limit = 5 // Maximum 5 requests per 10 seconds
|
|
}
|
|
];
|
|
});
|
|
|
|
builder.Services.AddInMemoryRateLimiting();
|
|
builder.Services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
|
|
|
|
WebApplication app = builder.Build();
|
|
|
|
app.UseForwardedHeaders(new()
|
|
{
|
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
|
});
|
|
|
|
|
|
app.UseIpRateLimiting(); // Apply IP-based rate limiting middleware
|
|
|
|
app.UseCors(myAllowSpecificOrigins);
|
|
|
|
RouteGroupBuilder progressApi = app.MapGroup("/progress");
|
|
progressApi.MapGet("/", () =>
|
|
{
|
|
CommunicationObject communicationObject = new()
|
|
{
|
|
Command = CommunicationCommand.GetScanningProgress
|
|
};
|
|
|
|
byte[] bytes = JsonSerializer.SerializeToUtf8Bytes(communicationObject);
|
|
|
|
using RequestSocket client = new();
|
|
client.Connect("tcp://127.0.0.1:5556");
|
|
client.SendFrame(bytes);
|
|
byte[] msg = client.ReceiveFrameBytes();
|
|
client.Close();
|
|
|
|
return JsonSerializer.Deserialize<ScanningStatus>(msg);
|
|
});
|
|
|
|
/*
|
|
RouteGroupBuilder searchApi = app.MapGroup("/search");
|
|
searchApi.MapGet("/{term}", (string term) =>
|
|
{
|
|
CommunicationObject communicationObject = new();
|
|
communicationObject.Command = CommunicationCommand.GetSearches;
|
|
communicationObject.SearchTerm = term;
|
|
|
|
byte[] bytes = JsonSerializer.SerializeToUtf8Bytes(communicationObject);
|
|
|
|
using RequestSocket client = new();
|
|
client.Connect("tcp://127.0.0.1:5556");
|
|
client.SendFrame(bytes);
|
|
string msg = client.ReceiveFrameString();
|
|
client.Close();
|
|
|
|
return JsonSerializer.Deserialize<SearchResults?>(msg);
|
|
});
|
|
*/
|
|
app.Run();
|
|
|
|
[JsonSerializable(typeof(ScanningStatus))]
|
|
//[JsonSerializable(typeof(SearchResults))]
|
|
[JsonSerializable(typeof(CommunicationObject))]
|
|
internal partial class AppJsonSerializerContext : JsonSerializerContext
|
|
{
|
|
} |