|
@@ -19,221 +19,218 @@ using System.Net.Security;
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
using System.Text;
|
|
|
|
|
|
-namespace OCPPServer.Protocol
|
|
|
+namespace OCPPServer.Protocol;
|
|
|
+
|
|
|
+public class OCPPWSServer : WebSocketServer<ClientData>
|
|
|
{
|
|
|
- public class OCPPWSServer : WebSocketServer<ClientData>
|
|
|
+
|
|
|
+ private readonly ILogger logger;
|
|
|
+ private readonly IConfiguration configuration;
|
|
|
+ private readonly IMainDbService mainDbService;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public int connectNum { get; set; }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public bool beConnectLimit { get; set; }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public OCPPWSServer(
|
|
|
+ IConfiguration configuration,
|
|
|
+ IMainDbService mainDbService,
|
|
|
+ ILogger<OCPPWSServer> logger)
|
|
|
+ : base(new List<ISubProtocol<ClientData>>())
|
|
|
{
|
|
|
+ this.configuration = configuration;
|
|
|
+ this.mainDbService = mainDbService;
|
|
|
+ this.logger = logger;
|
|
|
+ }
|
|
|
|
|
|
- static private ILogger logger;
|
|
|
- private readonly IConfiguration configuration;
|
|
|
- private readonly IServiceProvider serviceProvider;
|
|
|
- private readonly MainDbService mainDbService;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public int connectNum { get; set; }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public bool beConnectLimit { get; set; }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public OCPPWSServer(IEnumerable<ISubProtocol<ClientData>> subProtocols, IServiceProvider serviceProvider
|
|
|
- ,MainDbService mainDbService)
|
|
|
- : base(subProtocols)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ protected override bool ValidateClientCertificate(ClientData session, object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
|
|
+ {
|
|
|
+
|
|
|
+ return true;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override bool ValidateHandshake(ClientData session, string origin)
|
|
|
+ {
|
|
|
+ session.ISOCPP20 = session.SecWebSocketProtocol.ToLower().Contains("ocpp2.0");
|
|
|
+
|
|
|
+ int securityProfile = 0;
|
|
|
+ string authorizationKey = string.Empty;
|
|
|
+ if (string.IsNullOrEmpty(session.Path))
|
|
|
{
|
|
|
- this.configuration = serviceProvider.GetService<IConfiguration>();
|
|
|
- this.serviceProvider = serviceProvider;
|
|
|
- this.mainDbService = mainDbService;
|
|
|
- logger = serviceProvider.GetService<ILogger<OCPPWSServer>>();
|
|
|
+
|
|
|
+ logger.LogWarning("===========================================");
|
|
|
+ logger.LogWarning("session.Path EMPTY");
|
|
|
+ logger.LogWarning("===========================================");
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public OCPPWSServer(ISubProtocol<ClientData> subProtocol, IServiceProvider serviceProvider)
|
|
|
- : base(subProtocol)
|
|
|
- {
|
|
|
- this.configuration = serviceProvider.GetService<IConfiguration>();
|
|
|
- this.serviceProvider = serviceProvider;
|
|
|
+ string[] words = session.Path.Split('/');
|
|
|
+ session.ChargeBoxId = words.Last();
|
|
|
|
|
|
- logger = serviceProvider.GetService<ILogger<OCPPWSServer>>();
|
|
|
+ if (configuration["MaintainMode"] == "1")
|
|
|
+ {
|
|
|
+ session.ChargeBoxId = session.ChargeBoxId + "_2";
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public OCPPWSServer(IServiceProvider serviceProvider)
|
|
|
- : base(new List<ISubProtocol<ClientData>>())
|
|
|
+ logger.LogInformation(string.Format("ValidateHandshake: {0}", session.Path));
|
|
|
+ bool isExistedSN = false;
|
|
|
+ bool authorizated = false;
|
|
|
+
|
|
|
+ var info = mainDbService.GetMachineIdAndCustomerInfo(session.ChargeBoxId).Result;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ session.CustomerName = info.CustomerName;
|
|
|
+ session.CustomerId = info.CustomerId;
|
|
|
+ session.MachineId = info.MachineId;
|
|
|
+ isExistedSN = !string.IsNullOrEmpty(info.MachineId);
|
|
|
+
|
|
|
+ if (!isExistedSN)
|
|
|
{
|
|
|
- this.configuration = serviceProvider.GetService<IConfiguration>();
|
|
|
- this.serviceProvider = serviceProvider;
|
|
|
-
|
|
|
- logger = serviceProvider.GetService<ILogger<OCPPWSServer>>();
|
|
|
+ StringBuilder responseBuilder = new StringBuilder();
|
|
|
+
|
|
|
+ responseBuilder.AppendFormatWithCrCf(@"HTTP/{0} {1} {2}", "1.1",
|
|
|
+ (int)HttpStatusCode.NotFound, @"Not Found");
|
|
|
+
|
|
|
+ responseBuilder.AppendWithCrCf();
|
|
|
+ string sb = responseBuilder.ToString();
|
|
|
+ byte[] data = Encoding.UTF8.GetBytes(sb);
|
|
|
+ ((IWebSocketSession)session).SendRawData(data, 0, data.Length);
|
|
|
+
|
|
|
+ logger.LogInformation(sb);
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
- protected override bool ValidateClientCertificate(ClientData session, object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
|
|
+
|
|
|
+
|
|
|
+ var configVaule = mainDbService.GetMachineSecurityProfile(session.ChargeBoxId).Result;
|
|
|
+ int.TryParse(configVaule, out securityProfile);
|
|
|
+
|
|
|
+ if (session.ISOCPP20)
|
|
|
{
|
|
|
-
|
|
|
- return true;
|
|
|
-
|
|
|
+
|
|
|
+ securityProfile = 0;
|
|
|
}
|
|
|
-
|
|
|
- protected override bool ValidateHandshake(ClientData session, string origin)
|
|
|
+
|
|
|
+ if (securityProfile == 3 && session.UriScheme == "ws")
|
|
|
{
|
|
|
- session.ISOCPP20 = session.SecWebSocketProtocol.ToLower().Contains("ocpp2.0");
|
|
|
+ StringBuilder responseBuilder = new StringBuilder();
|
|
|
|
|
|
- int securityProfile = 0;
|
|
|
- string authorizationKey = string.Empty;
|
|
|
- if (string.IsNullOrEmpty(session.Path))
|
|
|
- {
|
|
|
-
|
|
|
- logger.LogWarning("===========================================");
|
|
|
- logger.LogWarning("session.Path EMPTY");
|
|
|
- logger.LogWarning("===========================================");
|
|
|
- }
|
|
|
+ responseBuilder.AppendFormatWithCrCf(@"HTTP/{0} {1} {2}", "1.1",
|
|
|
+ (int)HttpStatusCode.Unauthorized, @"Unauthorized");
|
|
|
|
|
|
- string[] words = session.Path.Split('/');
|
|
|
- session.ChargeBoxId = words.Last();
|
|
|
+ responseBuilder.AppendWithCrCf();
|
|
|
+ string sb = responseBuilder.ToString();
|
|
|
+ byte[] data = Encoding.UTF8.GetBytes(sb);
|
|
|
|
|
|
- if (configuration["MaintainMode"] == "1")
|
|
|
- {
|
|
|
- session.ChargeBoxId = session.ChargeBoxId + "_2";
|
|
|
- }
|
|
|
+ ((IWebSocketSession)session).SendRawData(data, 0, data.Length);
|
|
|
+ logger.LogInformation(sb);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- logger.LogInformation(string.Format("ValidateHandshake: {0}", session.Path));
|
|
|
- bool isExistedSN = false;
|
|
|
- bool authorizated = false;
|
|
|
-
|
|
|
- var info = mainDbService.GetMachineIdAndCustomerInfo(session.ChargeBoxId).Result;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- session.CustomerName = info.CustomerName;
|
|
|
- session.CustomerId = info.CustomerId;
|
|
|
- session.MachineId = info.MachineId;
|
|
|
- isExistedSN = !string.IsNullOrEmpty(info.MachineId);
|
|
|
-
|
|
|
- if (!isExistedSN)
|
|
|
+ if ((securityProfile == 1 || securityProfile == 2))
|
|
|
+ {
|
|
|
+ if (securityProfile == 2 && session.UriScheme == "ws")
|
|
|
{
|
|
|
- StringBuilder responseBuilder = new StringBuilder();
|
|
|
-
|
|
|
- responseBuilder.AppendFormatWithCrCf(@"HTTP/{0} {1} {2}", "1.1",
|
|
|
- (int)HttpStatusCode.NotFound, @"Not Found");
|
|
|
-
|
|
|
- responseBuilder.AppendWithCrCf();
|
|
|
- string sb = responseBuilder.ToString();
|
|
|
- byte[] data = Encoding.UTF8.GetBytes(sb);
|
|
|
- ((IWebSocketSession)session).SendRawData(data, 0, data.Length);
|
|
|
-
|
|
|
- logger.LogInformation(sb);
|
|
|
- return false;
|
|
|
+ authorizated = false;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- var configVaule = mainDbService.GetMachineSecurityProfile(session.ChargeBoxId).Result;
|
|
|
- int.TryParse(configVaule, out securityProfile);
|
|
|
-
|
|
|
- if (session.ISOCPP20)
|
|
|
- {
|
|
|
-
|
|
|
- securityProfile = 0;
|
|
|
- }
|
|
|
-
|
|
|
- if (securityProfile == 3 && session.UriScheme == "ws")
|
|
|
+ if (session.Items.ContainsKey("Authorization") || session.Items.ContainsKey("authorization"))
|
|
|
{
|
|
|
- StringBuilder responseBuilder = new StringBuilder();
|
|
|
-
|
|
|
- responseBuilder.AppendFormatWithCrCf(@"HTTP/{0} {1} {2}", "1.1",
|
|
|
- (int)HttpStatusCode.Unauthorized, @"Unauthorized");
|
|
|
+
|
|
|
+
|
|
|
+ authorizationKey = mainDbService.GetMachineAuthorizationKey(session.ChargeBoxId).Result;
|
|
|
|
|
|
- responseBuilder.AppendWithCrCf();
|
|
|
- string sb = responseBuilder.ToString();
|
|
|
- byte[] data = Encoding.UTF8.GetBytes(sb);
|
|
|
-
|
|
|
- ((IWebSocketSession)session).SendRawData(data, 0, data.Length);
|
|
|
- logger.LogInformation(sb);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if ((securityProfile == 1 || securityProfile == 2))
|
|
|
- {
|
|
|
- if (securityProfile == 2 && session.UriScheme == "ws")
|
|
|
+ if (session.ISOCPP20)
|
|
|
{
|
|
|
- authorizated = false;
|
|
|
+
|
|
|
+ securityProfile = 0;
|
|
|
}
|
|
|
+
|
|
|
+ logger.LogInformation("***********Authorization ");
|
|
|
|
|
|
- if (session.Items.ContainsKey("Authorization") || session.Items.ContainsKey("authorization"))
|
|
|
+ if (!string.IsNullOrEmpty(authorizationKey))
|
|
|
{
|
|
|
-
|
|
|
-
|
|
|
- authorizationKey = mainDbService.GetMachineAuthorizationKey(session.ChargeBoxId).Result;
|
|
|
-
|
|
|
- if (session.ISOCPP20)
|
|
|
- {
|
|
|
-
|
|
|
- securityProfile = 0;
|
|
|
- }
|
|
|
-
|
|
|
- logger.LogInformation("***********Authorization ");
|
|
|
-
|
|
|
- if (!string.IsNullOrEmpty(authorizationKey))
|
|
|
+ string base64Encoded = session.Items.ContainsKey("Authorization") ? session.Items["Authorization"].ToString().Replace("Basic ", "") : session.Items["authorization"].ToString().Replace("Basic ", "");
|
|
|
+ byte[] data = Convert.FromBase64String(base64Encoded);
|
|
|
+ string[] base64Decoded = System.Text.ASCIIEncoding.ASCII.GetString(data).Split(':');
|
|
|
+ logger.LogInformation("***********Authorization " + System.Text.ASCIIEncoding.ASCII.GetString(data));
|
|
|
+ if (base64Decoded.Count() == 2 && base64Decoded[0] == session.ChargeBoxId && base64Decoded[1] == authorizationKey)
|
|
|
{
|
|
|
- string base64Encoded = session.Items.ContainsKey("Authorization") ? session.Items["Authorization"].ToString().Replace("Basic ", "") : session.Items["authorization"].ToString().Replace("Basic ", "");
|
|
|
- byte[] data = Convert.FromBase64String(base64Encoded);
|
|
|
- string[] base64Decoded = System.Text.ASCIIEncoding.ASCII.GetString(data).Split(':');
|
|
|
- logger.LogInformation("***********Authorization " + System.Text.ASCIIEncoding.ASCII.GetString(data));
|
|
|
- if (base64Decoded.Count() == 2 && base64Decoded[0] == session.ChargeBoxId && base64Decoded[1] == authorizationKey)
|
|
|
- {
|
|
|
- authorizated = true;
|
|
|
- }
|
|
|
+ authorizated = true;
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- authorizated = true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ authorizated = true;
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
|
|
- if (!authorizated)
|
|
|
- {
|
|
|
- StringBuilder responseBuilder = new StringBuilder();
|
|
|
+ if (!authorizated)
|
|
|
+ {
|
|
|
+ StringBuilder responseBuilder = new StringBuilder();
|
|
|
|
|
|
- responseBuilder.AppendFormatWithCrCf(@"HTTP/{0} {1} {2}", "1.1",
|
|
|
- (int)HttpStatusCode.Unauthorized, @"Unauthorized");
|
|
|
+ responseBuilder.AppendFormatWithCrCf(@"HTTP/{0} {1} {2}", "1.1",
|
|
|
+ (int)HttpStatusCode.Unauthorized, @"Unauthorized");
|
|
|
|
|
|
- responseBuilder.AppendWithCrCf();
|
|
|
- string sb = responseBuilder.ToString();
|
|
|
- byte[] data = Encoding.UTF8.GetBytes(sb);
|
|
|
+ responseBuilder.AppendWithCrCf();
|
|
|
+ string sb = responseBuilder.ToString();
|
|
|
+ byte[] data = Encoding.UTF8.GetBytes(sb);
|
|
|
|
|
|
- ((IWebSocketSession)session).SendRawData(data, 0, data.Length);
|
|
|
- logger.LogInformation(sb);
|
|
|
- return false;
|
|
|
- }
|
|
|
+ ((IWebSocketSession)session).SendRawData(data, 0, data.Length);
|
|
|
+ logger.LogInformation(sb);
|
|
|
+ return false;
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
- return true;
|
|
|
- }
|
|
|
+ return true;
|
|
|
}
|
|
|
}
|