Pcc2Listener.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Net;
  2. using System.Net.Security;
  3. using System.Net.WebSockets;
  4. using System.Security.Cryptography.X509Certificates;
  5. using Microsoft.AspNetCore.Server.Kestrel.Https;
  6. using PCC2.AssemblyInject.Interfaces;
  7. using PCC2.Backend.Events;
  8. using PCC2.EventBus;
  9. using PCC2.Networking.KestrelSsl;
  10. namespace PCC2.Backend;
  11. internal class Pcc2Listener(IAsyncEventBus eventBus, ILogger<Pcc2Listener> logger, ILogger<KestrelSslListener> kwsLogger) : IAssemblyInjectASyncInitStarStop
  12. {
  13. private X509Certificate2? ServerCerSelect(string? sni)
  14. {
  15. throw new NotImplementedException();
  16. }
  17. private bool ClientCerValidate(X509Certificate2 clientCert, X509Chain? arg2, SslPolicyErrors arg3)
  18. {
  19. throw new NotImplementedException();
  20. }
  21. private Task ConnectHandler(X509Certificate2? clientCert, WebSocket ws)
  22. {
  23. throw new NotImplementedException();
  24. }
  25. // ==============================================================
  26. private KestrelSslListener? _listener;
  27. private ListenerStatus _status;
  28. private async Task SetAndPublishListenStatus(ListenerStatus status)
  29. {
  30. _status = status;
  31. await eventBus.PublishAsync(new ListenerStatusTellEvent(status));
  32. }
  33. public Task InitAsync()
  34. {
  35. eventBus.Subscript<ListenSettingChangedEvent>(async delegate
  36. {
  37. await StopAsync();
  38. await StartAsync();
  39. });
  40. eventBus.Subscript<ListenerStatusAskEvent>(async delegate
  41. {
  42. await eventBus.PublishAsync(new ListenerStatusTellEvent(_status));
  43. });
  44. return Task.CompletedTask;
  45. }
  46. public async Task StartAsync()
  47. {
  48. if (_listener != null) return;
  49. var ep = LoadListenEndPointSetting();
  50. if (ep == null)
  51. {
  52. await SetAndPublishListenStatus(ListenerStatus.NotStart);
  53. return;
  54. }
  55. await SetAndPublishListenStatus(ListenerStatus.Starting);
  56. _listener = new KestrelSslListener(ep, ServerCerSelect, ClientCerValidate, ConnectHandler, ClientCertificateMode.RequireCertificate, kwsLogger);
  57. try
  58. {
  59. await _listener.StartAsync();
  60. await SetAndPublishListenStatus(ListenerStatus.Started);
  61. }
  62. catch (Exception ex)
  63. {
  64. logger.LogError(ex, nameof(StartAsync));
  65. await SetAndPublishListenStatus(ListenerStatus.StartFail);
  66. }
  67. }
  68. public async Task StopAsync()
  69. {
  70. if (_listener == null) return;
  71. await SetAndPublishListenStatus(ListenerStatus.Stopping);
  72. try
  73. {
  74. await _listener.StopAsync();
  75. await SetAndPublishListenStatus(ListenerStatus.Stopped);
  76. }
  77. catch (Exception ex)
  78. {
  79. logger.LogError(ex, nameof(StopAsync));
  80. await SetAndPublishListenStatus(ListenerStatus.StopFail);
  81. }
  82. _listener = null;
  83. }
  84. private IPEndPoint? LoadListenEndPointSetting()
  85. {
  86. var settings = GetSettings();
  87. var ip = settings.ListenAddress;
  88. var port = settings.ListenPort;
  89. if (ip == null || port == null || IPAddress.TryParse(ip, out var address) == false) return null;
  90. return new IPEndPoint(address, port.Value);
  91. }
  92. }