OCPPWSServer.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. 
  2. using EVCB_OCPP.Domain;
  3. using NLog;
  4. using OCPPPackage.Profiles;
  5. using SuperWebSocket;
  6. using SuperWebSocket.SubProtocol;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Configuration;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Net.Security;
  13. using System.Security.Cryptography.X509Certificates;
  14. using System.Text;
  15. namespace OCPPServer.Protocol
  16. {
  17. public class OCPPWSServer : WebSocketServer<ClientData>
  18. {
  19. static private ILogger logger = NLog.LogManager.GetCurrentClassLogger();
  20. /// <summary>
  21. /// 可允許連線Clinet數
  22. /// </summary>
  23. public int connectNum { get; set; }
  24. /// <summary>
  25. /// 是否限制連線Clinet數
  26. /// </summary>
  27. public bool beConnectLimit { get; set; }
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="WebSocketServer"/> class.
  30. /// </summary>
  31. /// <param name="subProtocols">The sub protocols.</param>
  32. public OCPPWSServer(IEnumerable<ISubProtocol<ClientData>> subProtocols)
  33. : base(subProtocols)
  34. {
  35. }
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="WebSocketServer"/> class.
  38. /// </summary>
  39. /// <param name="subProtocol">The sub protocol.</param>
  40. public OCPPWSServer(ISubProtocol<ClientData> subProtocol)
  41. : base(subProtocol)
  42. {
  43. }
  44. /// <summary>
  45. /// Initializes a new instance of the <see cref="WebSocketServer"/> class.
  46. /// </summary>
  47. public OCPPWSServer()
  48. : base(new List<ISubProtocol<ClientData>>())
  49. {
  50. }
  51. protected override bool ValidateClientCertificate(ClientData session, object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
  52. {
  53. // Console.WriteLine(string.Format("{0} :{1}", session.ChargeBoxId + " ValidateClientCertificate", sslPolicyErrors));
  54. return true;
  55. // return base.ValidateClientCertificate(session, sender, certificate, chain, sslPolicyErrors);
  56. }
  57. protected override bool ValidateHandshake(ClientData session, string origin)
  58. {
  59. session.ISOCPP20 = session.SecWebSocketProtocol.ToLower().Contains("ocpp2.0");
  60. int securityProfile = 0;
  61. string authorizationKey = string.Empty;
  62. if (string.IsNullOrEmpty(session.Path))
  63. {
  64. logger.Warn("===========================================");
  65. logger.Warn("session.Path EMPTY");
  66. logger.Warn("===========================================");
  67. }
  68. string[] words = session.Path.Split('/');
  69. session.ChargeBoxId = words.Last();
  70. if (ConfigurationManager.AppSettings["MaintainMode"] == "1")
  71. {
  72. session.ChargeBoxId = session.ChargeBoxId + "_2";
  73. }
  74. logger.Info(string.Format("ValidateHandshake: {0}", session.Path));
  75. bool isExistedSN = false;
  76. bool authorizated = false;
  77. // using (var db = new MainDBContext())
  78. {
  79. session.CustomerId = Guid.Empty;
  80. session.MachineId = String.Empty;
  81. isExistedSN =true;
  82. if (!isExistedSN)
  83. {
  84. StringBuilder responseBuilder = new StringBuilder();
  85. responseBuilder.AppendFormatWithCrCf(@"HTTP/{0} {1} {2}", "1.1",
  86. (int)HttpStatusCode.NotFound, @"Not Found");
  87. responseBuilder.AppendWithCrCf();
  88. string sb = responseBuilder.ToString();
  89. byte[] data = Encoding.UTF8.GetBytes(sb);
  90. ((IWebSocketSession)session).SendRawData(data, 0, data.Length);
  91. logger.Info(sb);
  92. return false;
  93. }
  94. if (session.ISOCPP20)
  95. {
  96. // 1.6 server only support change server function
  97. securityProfile = 0;
  98. }
  99. }
  100. if (securityProfile == 3 && session.UriScheme == "ws")
  101. {
  102. StringBuilder responseBuilder = new StringBuilder();
  103. responseBuilder.AppendFormatWithCrCf(@"HTTP/{0} {1} {2}", "1.1",
  104. (int)HttpStatusCode.Unauthorized, @"Unauthorized");
  105. responseBuilder.AppendWithCrCf();
  106. string sb = responseBuilder.ToString();
  107. byte[] data = Encoding.UTF8.GetBytes(sb);
  108. ((IWebSocketSession)session).SendRawData(data, 0, data.Length);
  109. logger.Info(sb);
  110. return false;
  111. }
  112. if ((securityProfile == 1 || securityProfile == 2))
  113. {
  114. if (securityProfile == 2 && session.UriScheme == "ws")
  115. {
  116. authorizated = false;
  117. }
  118. }
  119. return true;
  120. }
  121. }
  122. }