123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- using EVCB_OCPP.WSServer.Service;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Logging;
- using OCPPServer.Protocol;
- using SuperSocket.SocketBase;
- using SuperWebSocket;
- using SuperWebSocket.SubProtocol;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Security;
- using System.Security.Cryptography.X509Certificates;
- using System.Text;
- namespace EVCB_OCPP.WSServer.SuperSocket;
- 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;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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))
- {
-
- logger.LogWarning("===========================================");
- logger.LogWarning("session.Path EMPTY");
- logger.LogWarning("===========================================");
- }
- string[] words = session.Path.Split('/');
- session.ChargeBoxId = words.Last();
- foreach (var denyModel in GlobalConfig.DenyModelNames)
- {
- if (string.IsNullOrEmpty(denyModel)) break;
- if (session.ChargeBoxId.StartsWith(denyModel))
- {
- StringBuilder responseBuilder = new StringBuilder();
- responseBuilder.AppendFormatWithCrCf(@"HTTP/{0} {1} {2}", "1.1",
- (int)HttpStatusCode.Unauthorized, @"Unauthorized");
- responseBuilder.AppendWithCrCf();
- string sb = responseBuilder.ToString();
- byte[] data = Encoding.UTF8.GetBytes(sb);
- ((IWebSocketSession)session).SendRawData(data, 0, data.Length);
- logger.LogTrace(sb);
- return false;
- }
- }
- if (configuration["MaintainMode"] == "1")
- {
- session.ChargeBoxId = session.ChargeBoxId + "_2";
- }
- 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)
- {
- 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;
- }
-
-
- var configVaule = mainDbService.GetMachineSecurityProfile(session.ChargeBoxId).Result;
- int.TryParse(configVaule, out securityProfile);
- if (session.ISOCPP20)
- {
-
- securityProfile = 0;
- }
- if (securityProfile == 3 && session.UriScheme == "ws")
- {
- StringBuilder responseBuilder = new StringBuilder();
- responseBuilder.AppendFormatWithCrCf(@"HTTP/{0} {1} {2}", "1.1",
- (int)HttpStatusCode.Unauthorized, @"Unauthorized");
- 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")
- {
- authorizated = false;
- }
- if (session.Items.ContainsKey("Authorization") || session.Items.ContainsKey("authorization"))
- {
-
-
- 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 = Encoding.ASCII.GetString(data).Split(':');
- logger.LogInformation("***********Authorization " + Encoding.ASCII.GetString(data));
- if (base64Decoded.Count() == 2 && base64Decoded[0] == session.ChargeBoxId && base64Decoded[1] == authorizationKey)
- {
- authorizated = true;
- }
- }
- }
- else
- {
- authorizated = true;
- }
- if (!authorizated)
- {
- StringBuilder responseBuilder = new StringBuilder();
- responseBuilder.AppendFormatWithCrCf(@"HTTP/{0} {1} {2}", "1.1",
- (int)HttpStatusCode.Unauthorized, @"Unauthorized");
- responseBuilder.AppendWithCrCf();
- string sb = responseBuilder.ToString();
- byte[] data = Encoding.UTF8.GetBytes(sb);
- ((IWebSocketSession)session).SendRawData(data, 0, data.Length);
- logger.LogInformation(sb);
- return false;
- }
- }
- logger.LogInformation(string.Format("ValidateHandshake PASS: {0}", session.Path));
- return true;
- }
- }
|