|
@@ -1,4 +1,5 @@
|
|
|
-using Microsoft.AspNetCore.Builder;
|
|
|
+using EVCB_OCPP.WSServer.Helper;
|
|
|
+using Microsoft.AspNetCore.Builder;
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
@@ -46,6 +47,8 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
|
|
|
private readonly IMainDbService mainDbService;
|
|
|
private readonly ILogger<OcppWebsocketService> logger;
|
|
|
|
|
|
+ private readonly QueueSemaphore handshakeSemaphore;
|
|
|
+
|
|
|
public OcppWebsocketService(
|
|
|
IConfiguration configuration,
|
|
|
IServiceProvider serviceProvider,
|
|
@@ -56,6 +59,8 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
|
|
|
this.configuration = configuration;
|
|
|
this.mainDbService = mainDbService;
|
|
|
this.logger = logger;
|
|
|
+
|
|
|
+ handshakeSemaphore = new QueueSemaphore(5);
|
|
|
}
|
|
|
|
|
|
internal override async ValueTask<string> ValidateSupportedPortocol(HttpContext context)
|
|
@@ -76,6 +81,24 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
|
|
|
|
|
|
internal override async ValueTask<bool> ValidateHandshake(HttpContext context, WsClientData session)
|
|
|
{
|
|
|
+ try
|
|
|
+ {
|
|
|
+ using var canValidate = await handshakeSemaphore.GetToken();
|
|
|
+ var result = await ValidateHandshakeUnsafe(context, session);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ logger.LogError(ex.Message);
|
|
|
+ logger.LogError(ex.StackTrace);
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ internal async ValueTask<bool> ValidateHandshakeUnsafe(HttpContext context, WsClientData session)
|
|
|
+ {
|
|
|
+ if (context.RequestAborted.IsCancellationRequested) return false;
|
|
|
+
|
|
|
string authHeader = context?.Request?.Headers?.Authorization;
|
|
|
|
|
|
session.ISOCPP20 = context.WebSockets.WebSocketRequestedProtocols.Any(x => x.ToLower() == "ocpp2.0");
|
|
@@ -120,6 +143,7 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
|
|
|
bool authorizated = false;
|
|
|
|
|
|
var info = await mainDbService.GetMachineIdAndCustomerInfo(session.ChargeBoxId, context.RequestAborted);
|
|
|
+ if (context.RequestAborted.IsCancellationRequested) return false;
|
|
|
//var machine = db.Machine.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.IsDelete == false).Select(x => new { x.CustomerId, x.Id }).AsNoTracking().FirstOrDefault();
|
|
|
//session.CustomerName = machine == null ? "Unknown" : db.Customer.Where(x => x.Id == machine.CustomerId).Select(x => x.Name).FirstOrDefault();
|
|
|
//session.CustomerId = machine == null ? Guid.Empty : machine.CustomerId;
|
|
@@ -140,6 +164,7 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
|
|
|
//var configVaule = db.MachineConfigurations.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.ConfigureName == StandardConfiguration.SecurityProfile)
|
|
|
// .Select(x => x.ConfigureSetting).FirstOrDefault();
|
|
|
var configVaule = await mainDbService.GetMachineSecurityProfile(session.ChargeBoxId, context.RequestAborted);
|
|
|
+ if (context.RequestAborted.IsCancellationRequested) return false;
|
|
|
int.TryParse(configVaule, out securityProfile);
|
|
|
|
|
|
if (session.ISOCPP20)
|
|
@@ -168,6 +193,7 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
|
|
|
//authorizationKey = db.MachineConfigurations.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.ConfigureName == StandardConfiguration.AuthorizationKey)
|
|
|
// .Select(x => x.ConfigureSetting).FirstOrDefault();
|
|
|
authorizationKey = await mainDbService.GetMachineAuthorizationKey(session.ChargeBoxId, context.RequestAborted);
|
|
|
+ if (context.RequestAborted.IsCancellationRequested) return false;
|
|
|
|
|
|
if (session.ISOCPP20)
|
|
|
{
|