using System.Net; using System.Security.Cryptography; using System.Text; using PCC.App; using PCC.App.Security; using PCC.Common.AssemblyInject.Interfaces; using PCC.Common.EventBus; using PCC.Common.Networking; namespace PCC.DevServer; internal class DevServerApp( DevServerPccConfigManager configManager, TrustedPeerManager tpm, IEventBus eventBus, ILogger logger, ILogger ktsLogger) : IAssemblyInjectSyncInitStarStop { private KestrelTcpServer _tcpServer; public void Init() { logger.LogInformation("init"); eventBus.Subscript(OnIncome); eventBus.Subscript(OnRx); eventBus.Subscript(OnDx); eventBus.Subscript(OnXx); } public void Start() { logger.LogInformation("starting..."); byte[] myPri, myPub; if (configManager.Instance.MyKeyPrivate == null || configManager.Instance.MyKeyPublic == null) { logger.LogInformation("Generating 8192bit RSA key... be patient, may take up to 30 seconds..."); (myPub, myPri) = RsaUtility.GeneratePKCS1(8192); configManager.Instance.MyKeyPrivate = Convert.ToBase64String(myPri, Base64FormattingOptions.None); configManager.Instance.MyKeyPublic = Convert.ToBase64String(myPub, Base64FormattingOptions.None); configManager.Save(); logger.LogInformation("RSA key generated and saved."); } else { myPri = Convert.FromBase64String(configManager.Instance.MyKeyPrivate); myPub = Convert.FromBase64String(configManager.Instance.MyKeyPublic); } eventBus.Publish(new TrustedPeerManager.TPM_EVT_CMD_INIT(myPub, myPri)); logger.LogInformation("Your public key for share to trusted peer: " + configManager.Instance.MyKeyPublic); if (configManager.Instance.ListenPort == null || false == IPAddress.TryParse(configManager.Instance.ListenAddress, out _)) { logger.LogError($"Missing config `{nameof(configManager.Instance.ListenPort)}' or `{nameof(configManager.Instance.ListenAddress)}', HALT"); return; } if (configManager.Instance.TrustPeerKeyPub == null) { logger.LogError($"Missing config `{nameof(configManager.Instance.TrustPeerKeyPub)}', please obtain from trusted peer, HALT"); return; } //warn just example for server tpm.AddPeer(Convert.FromBase64String(configManager.Instance.TrustPeerKeyPub), "", 0); _tcpServer = new KestrelTcpServer(configManager.Instance.ListenAddress, configManager.Instance.ListenPort.Value, tpm.HandleIncomingPeerAsync, ktsLogger); Task.Run(async () => { try { await _tcpServer.StartAsync(); } catch (Exception e) { logger.LogError(e, "Start TCP server"); } }); } private void OnIncome(TrustedPeerManager.TPM_EVT_PEER_IX obj) { logger.LogInformation("Income:" + obj.PeerId); } private void OnRx(TrustedPeerManager.TPM_EVT_PEER_RX obj) { logger.LogInformation($"Rx from <{obj.PeerId}>"); logger.LogInformation($"Rx content: {Encoding.UTF8.GetString(obj.payload.Span)}"); tpm.SendToPeer(obj.PeerId, SHA256.HashData(obj.payload.Span)); } private void OnDx(TrustedPeerManager.TPM_EVT_PEER_DX obj) { logger.LogInformation("Disconnected:" + obj.PeerId); } private void OnXx(TrustedPeerManager.TPM_EVT_PEER_XX obj) { logger.LogWarning($"有内鬼,终止交易! {obj.Kind} {obj.PeerId}"); } public void Stop() { logger.LogInformation("stop"); _ = _tcpServer.StopAsync(); //FAF eventBus.Publish(new TrustedPeerManager.TPM_EVT_CMD_SHUTDOWN()); } }