12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using PCC.App.Security;
- using PCC.Common.AssemblyInject.Interfaces;
- using PCC.Common.EventBus;
- using PCC.App;
- namespace PCC.DevClient;
- internal class DevClientApp(DevClientPccConfigManager configManager, TrustedPeerManager tpm, IEventBus eventBus, ILogger<DevClientApp> logger) : IAssemblyInjectSyncInitStarStop
- {
- public void Init()
- {
- logger.LogInformation("init");
- eventBus.Subscript<TrustedPeerManager.TPM_EVT_PEER_CX>(OnConnected);
- eventBus.Subscript<TrustedPeerManager.TPM_EVT_PEER_RX>(OnRx);
- }
- 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.TrustPeerKeyPub == null || configManager.Instance.TrustPeerHost == null || configManager.Instance.TrustPeerPort == null)
- {
- logger.LogError($"Missing config one of `{nameof(configManager.Instance.TrustPeerKeyPub)}' or `{nameof(configManager.Instance.TrustPeerHost)}' or `{nameof(configManager.Instance.TrustPeerPort)}', please obtain from trusted peer, HALT");
- return;
- }
- var tpk = Convert.FromBase64String(configManager.Instance.TrustPeerKeyPub);
- var tph = configManager.Instance.TrustPeerHost!;
- var tpp = configManager.Instance.TrustPeerPort!.Value;
- var peerId = tpm.AddTrustPeer(tpk, tph, tpp);
- tpm.ConnectToPeerAsync(peerId);
- }
- private void OnConnected(TrustedPeerManager.TPM_EVT_PEER_CX obj)
- {
- logger.LogInformation("Connected");
- tpm.SendToPeer(obj.PeerId, "Brr连上了?"u8.ToArray());
- }
- private void OnRx(TrustedPeerManager.TPM_EVT_PEER_RX obj)
- {
- logger.LogInformation("Rx:" + Convert.ToHexString(obj.block));
- }
- public void Stop()
- {
- logger.LogInformation("stop");
- eventBus.Publish<TrustedPeerManager.TPM_EVT_CMD_SHUTDOWN>();
- }
- }
|