DevServerApp.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Net;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using PCC.App.Security;
  5. using PCC.App.Tpm;
  6. using PCC.Common.AssemblyInject.Interfaces;
  7. using PCC.Common.EventBus;
  8. using PCC.Common.Networking;
  9. namespace PCC.DevServer;
  10. internal class DevServerApp(
  11. DevServerPccConfigManager configManager,
  12. TrustedPeerManager tpm,
  13. IEventBus eventBus,
  14. ILogger<DevServerApp> logger,
  15. ILogger<KestrelTcpServer> ktsLogger) : IAssemblyInjectSyncInitStarStop
  16. {
  17. private KestrelTcpServer _tcpServer;
  18. public void Init()
  19. {
  20. logger.LogInformation("init");
  21. //eventBus.Subscript<TrustedPeerManager.TPM_EVT_PEER_IX>(OnIncome);
  22. //eventBus.Subscript<TrustedPeerManager.TPM_EVT_PEER_RX>(OnRx);
  23. //eventBus.Subscript<TrustedPeerManager.TPM_EVT_PEER_DX>(OnDx);
  24. //eventBus.Subscript<TrustedPeerManager.TPM_EVT_PEER_XX>(OnXx);
  25. }
  26. public void Start()
  27. {
  28. logger.LogInformation("starting...");
  29. byte[] myPri, myPub;
  30. if (configManager.Instance.MyKeyPrivate == null || configManager.Instance.MyKeyPublic == null)
  31. {
  32. logger.LogInformation("Generating 8192bit RSA key... be patient, may take up to 30 seconds...");
  33. (myPub, myPri) = RsaUtility.GeneratePKCS1(8192);
  34. configManager.Instance.MyKeyPrivate = Convert.ToBase64String(myPri, Base64FormattingOptions.None);
  35. configManager.Instance.MyKeyPublic = Convert.ToBase64String(myPub, Base64FormattingOptions.None);
  36. configManager.Save();
  37. logger.LogInformation("RSA key generated and saved.");
  38. }
  39. else
  40. {
  41. myPri = Convert.FromBase64String(configManager.Instance.MyKeyPrivate);
  42. myPub = Convert.FromBase64String(configManager.Instance.MyKeyPublic);
  43. }
  44. //eventBus.Publish(new TrustedPeerManager.TPM_EVT_CMD_INIT(myPub, myPri));
  45. //logger.LogInformation("Your public key for share to trusted peer: " + configManager.Instance.MyKeyPublic);
  46. //if (configManager.Instance.ListenPort == null || false == IPAddress.TryParse(configManager.Instance.ListenAddress, out _))
  47. //{
  48. // logger.LogError($"Missing config `{nameof(configManager.Instance.ListenPort)}' or `{nameof(configManager.Instance.ListenAddress)}', HALT");
  49. // return;
  50. //}
  51. //if (configManager.Instance.TrustPeerKeyPub == null)
  52. //{
  53. // logger.LogError($"Missing config `{nameof(configManager.Instance.TrustPeerKeyPub)}', please obtain from trusted peer, HALT");
  54. // return;
  55. //}
  56. ////warn just example for server
  57. //tpm.AddPeer(Convert.FromBase64String(configManager.Instance.TrustPeerKeyPub), "", 0);
  58. //_tcpServer = new KestrelTcpServer(configManager.Instance.ListenAddress, configManager.Instance.ListenPort.Value, tpm.HandleIncomingPeerAsync, ktsLogger);
  59. //Task.Run(async () =>
  60. //{
  61. // try
  62. // {
  63. // await _tcpServer.StartAsync();
  64. // }
  65. // catch (Exception e)
  66. // {
  67. // logger.LogError(e, "Start TCP server");
  68. // }
  69. //});
  70. }
  71. //private void OnIncome(TrustedPeerManager.TPM_EVT_PEER_IX obj)
  72. //{
  73. // logger.LogInformation("Income:" + obj.PeerId);
  74. //}
  75. //private void OnRx(TrustedPeerManager.TPM_EVT_PEER_RX obj)
  76. //{
  77. // logger.LogInformation($"Rx from <{obj.PeerId}>");
  78. // logger.LogInformation($"Rx content: {Encoding.UTF8.GetString(obj.payload.Span)}");
  79. // tpm.SendToPeer(obj.PeerId, SHA256.HashData(obj.payload.Span));
  80. //}
  81. //private void OnDx(TrustedPeerManager.TPM_EVT_PEER_DX obj)
  82. //{
  83. // logger.LogInformation("Disconnected:" + obj.PeerId);
  84. //}
  85. //private void OnXx(TrustedPeerManager.TPM_EVT_PEER_XX obj)
  86. //{
  87. // logger.LogWarning($"有内鬼,终止交易! {obj.Kind} {obj.PeerId}");
  88. //}
  89. public void Stop()
  90. {
  91. // logger.LogInformation("stop");
  92. // _ = _tcpServer.StopAsync(); //FAF
  93. // eventBus.Publish(new TrustedPeerManager.TPM_EVT_CMD_SHUTDOWN());
  94. }
  95. }