123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using System.Text;
- using PCC.App.Security;
- using PCC.App.Tpm;
- using PCC.Common.AssemblyInject.Interfaces;
- using PCC.Common.EventBus;
- using PCC.Configuration;
- namespace PCC;
- internal class DevClientApp(
- PccDevConfigManagerBase configManager,
- DevPeerInfoProviderBase peerInfoProvider,
- TrustedPeerManager tpm,
- IEventBus eventBus,
- ILogger<DevClientApp> logger) : IAssemblyInjectSyncInitStarStop
- {
- private ILocalPeerInfo? _local;
- private IRemotePeerInfo? _remote;
- public void Init()
- {
- logger.LogInformation("init");
- eventBus.Subscript<TPM_EVT_LISTENER_STATUS_CHANGED>(OnListenerStatusChanged);
- eventBus.Subscript<TPM_EVT_OUTBOUND_CON_STATUS_CHANGED>(OnOutboundStatusChanged);
- eventBus.Subscript<TPM_EVT_OUTBOUND_CON_ERROR>(OnOutboundConnectionError);
- eventBus.Subscript<TPM_EVT_OUTBOUND_RX>(OnOutboundRx);
- eventBus.Subscript<TPM_EVT_OUTBOUND_TX>(OnOutboundTx);
- }
- private void OnListenerStatusChanged(TPM_EVT_LISTENER_STATUS_CHANGED obj)
- {
- logger.LogInformation($"Listener {obj.Status} {obj.Exception?.Message}");
- if (obj.Status == TPM_EVT_LISTEN_STATUS.STARTED)
- {
- if (_remote != null)
- {
- tpm.ConnectToPeerAsync(_local.PeerId, _remote.PeerId);
- }
- }
- }
- private void OnOutboundStatusChanged(TPM_EVT_OUTBOUND_CON_STATUS_CHANGED obj)
- {
- switch (obj.Status)
- {
- case TPM_EVT_OUTBOUND_CON_STATUS.HANDSHAKE_OK:
- logger.LogInformation($"OB ST {obj.Status} L{obj.LocalPeerId} R{obj.RemotePeerId}");
- tpm.SendToPeer(_local.PeerId, _remote.PeerId, "Brrr汉字SendDddd"u8.ToArray());
- break;
- case TPM_EVT_OUTBOUND_CON_STATUS.CONNECTION_ATTEMPT:
- case TPM_EVT_OUTBOUND_CON_STATUS.HANDSHAKE_ATTEMPT:
- case TPM_EVT_OUTBOUND_CON_STATUS.DISCONNECTED:
- logger.LogInformation($"OB ST {obj.Status} L{obj.LocalPeerId} R{obj.RemotePeerId}");
- break;
- default:
- case TPM_EVT_OUTBOUND_CON_STATUS.INVALID:
- case TPM_EVT_OUTBOUND_CON_STATUS.SECURE_WARN_TIMESTAMP_SKEW:
- logger.LogWarning($"OB ST {obj.Status} L{obj.LocalPeerId} R{obj.RemotePeerId}");
- break;
- case TPM_EVT_OUTBOUND_CON_STATUS.HANDSHAKE_FAIL_ACK_NOT_MATCHED:
- case TPM_EVT_OUTBOUND_CON_STATUS.SECURE_ALERT_REPLAY_ATTACK_DETECT:
- logger.LogError($"OB ST {obj.Status} L{obj.LocalPeerId} R{obj.RemotePeerId}");
- break;
- }
- }
- private void OnOutboundTx(TPM_EVT_OUTBOUND_TX obj)
- {
- logger.LogInformation($"OB Tx L{obj.LocalPeerId} R{obj.RemotePeerId} {Encoding.UTF8.GetString(obj.payload.Span)}");
- }
- private void OnOutboundRx(TPM_EVT_OUTBOUND_RX obj)
- {
- logger.LogInformation($"OB Rx L{obj.LocalPeerId} R{obj.RemotePeerId} {Encoding.UTF8.GetString(obj.payload.Span)}");
- }
- private void OnOutboundConnectionError(TPM_EVT_OUTBOUND_CON_ERROR obj)
- {
- logger.LogError(obj.Exception, $"OB ERR L{obj.LocalPeerId} R{obj.RemotePeerId}");
- }
- public void Start()
- {
- logger.LogInformation("starting...");
- if (configManager.Instance.MyKeyPrivate == null || configManager.Instance.MyKeyPublic == null)
- {
- logger.LogInformation("Generating 8192bit RSA key... be patient, may take up to 30 seconds...");
- var (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. Please fill conf and RESTART.");
- }
- _local = peerInfoProvider.PeerInfo.FirstOrDefault();
- if (_local != null)
- {
- _remote = _local.TrustedRemotePeers.FirstOrDefault();
- tpm.AddOrReplaceLocalPeer(_local);
- }
- }
- public void Stop()
- {
- }
- }
|