DevServerApp.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.Security.Cryptography;
  2. using System.Text;
  3. using PCC.App.Security;
  4. using PCC.App.Tpm;
  5. using PCC.Common.AssemblyInject.Interfaces;
  6. using PCC.Common.EventBus;
  7. using PCC.DevShared.Configuration;
  8. namespace PCC.DevServer;
  9. internal class DevServerApp(
  10. PccDevConfigManagerBase configManager,
  11. IPeerInfoProvider peerInfoProvider,
  12. TrustedPeerManager tpm,
  13. IEventBus eventBus,
  14. ILogger<DevServerApp> logger) : IAssemblyInjectSyncInitStarStop
  15. {
  16. private ILocalPeerInfo? _local;
  17. private IRemotePeerInfo? _remote;
  18. public void Init()
  19. {
  20. logger.LogInformation("init");
  21. eventBus.Subscript<TPM_EVT_LISTENER_STATUS_CHANGED>(OnListenerStatusChanged);
  22. eventBus.Subscript<TPM_EVT_INBOUND_CON_ACCEPTED>(OnInboundAccepted);
  23. eventBus.Subscript<TPM_EVT_INBOUND_CON_STATUS_CHANGED>(OnInboundStatusChanged);
  24. eventBus.Subscript<TPM_EVT_INBOUND_CON_ERROR>(OnInboundConnectionError);
  25. eventBus.Subscript<TPM_EVT_INBOUND_RX>(OnInboundRx);
  26. eventBus.Subscript<TPM_EVT_INBOUND_TX>(OnInboundTx);
  27. }
  28. public void Start()
  29. {
  30. logger.LogInformation("starting...");
  31. if (configManager.Instance.MyKeyPrivate == null || configManager.Instance.MyKeyPublic == null)
  32. {
  33. logger.LogInformation("Generating 8192bit RSA key... be patient, may take up to 30 seconds...");
  34. var (myPub, myPri) = RsaUtility.GeneratePKCS1(8192);
  35. configManager.Instance.MyKeyPrivate = Convert.ToBase64String(myPri, Base64FormattingOptions.None);
  36. configManager.Instance.MyKeyPublic = Convert.ToBase64String(myPub, Base64FormattingOptions.None);
  37. configManager.Save();
  38. logger.LogInformation("RSA key generated and saved. Please fill conf and RESTART.");
  39. }
  40. _local = peerInfoProvider.PeerInfo.FirstOrDefault();
  41. if (_local != null)
  42. {
  43. _remote = _local.TrustedRemotePeers.FirstOrDefault();
  44. }
  45. }
  46. private void OnInboundTx(TPM_EVT_INBOUND_TX obj)
  47. {
  48. logger.LogInformation($"IB Tx L{obj.LocalPeerId} R{obj.RemotePeerId} {Encoding.UTF8.GetString(obj.payload.Span)}");
  49. }
  50. private void OnInboundRx(TPM_EVT_INBOUND_RX obj)
  51. {
  52. logger.LogInformation($"IB Rx L{obj.LocalPeerId} R{obj.RemotePeerId} {Encoding.UTF8.GetString(obj.payload.Span)}");
  53. tpm.SendToPeer(_local.PeerId, obj.RemotePeerId, SHA256.HashData(obj.payload.Span));
  54. }
  55. private void OnInboundConnectionError(TPM_EVT_INBOUND_CON_ERROR obj)
  56. {
  57. logger.LogError(obj.Exception, $"IB ERR L{obj.LocalPeerId} R{obj.RemotePeerId} C{obj.ConnectionId}");
  58. }
  59. private void OnInboundStatusChanged(TPM_EVT_INBOUND_CON_STATUS_CHANGED obj)
  60. {
  61. switch (obj.Status)
  62. {
  63. case TPM_EVT_INBOUND_CON_STATUS.HANDSHAKE_FAIL_NOT_TRUSTED:
  64. case TPM_EVT_INBOUND_CON_STATUS.SECURE_ALERT_REPLAY_ATTACK_DETECT:
  65. logger.LogError($"IB ST {obj.Status} L{obj.LocalPeerId} R{obj.RemotePeerId} C{obj.ConnectionId}");
  66. break;
  67. default:
  68. case TPM_EVT_INBOUND_CON_STATUS.INVALID:
  69. case TPM_EVT_INBOUND_CON_STATUS.SECURE_WARN_TIMESTAMP_SKEW:
  70. logger.LogWarning($"IB ST {obj.Status} L{obj.LocalPeerId} R{obj.RemotePeerId} C{obj.ConnectionId}");
  71. break;
  72. case TPM_EVT_INBOUND_CON_STATUS.HANDSHAKE_OK:
  73. case TPM_EVT_INBOUND_CON_STATUS.DISCONNECTED:
  74. logger.LogInformation($"IB ST {obj.Status} L{obj.LocalPeerId} R{obj.RemotePeerId} C{obj.ConnectionId}");
  75. break;
  76. }
  77. }
  78. private void OnInboundAccepted(TPM_EVT_INBOUND_CON_ACCEPTED obj)
  79. {
  80. logger.LogInformation($"Inbound accepted {obj.RemoteEndpoint} {obj.ConnectionId}");
  81. }
  82. private void OnListenerStatusChanged(TPM_EVT_LISTENER_STATUS_CHANGED obj)
  83. {
  84. logger.LogInformation($"Listener {obj.Status} {obj.Exception?.Message}");
  85. }
  86. public void Stop()
  87. {
  88. }
  89. }