DevClientApp.cs 4.0 KB

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