TrustedPeerManager.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections.Concurrent;
  2. using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
  3. using PCC.App.Security;
  4. using PCC.Common.AssemblyInject.Interfaces;
  5. using PCC.Common.EventBus;
  6. using PCC.Common.Networking;
  7. namespace PCC.App.Tpm;
  8. // Handshake 1 → Public Key SHA256
  9. // Handshake 2 ← ACK for Yes or No(close connection)
  10. // *Complete Handshake
  11. // SendText 1 → Payload
  12. // SendText 2 ← ACK
  13. // ACK: SHA256(incoming payload)
  14. public abstract class TrustedPeerManager(IEventBus eventBus, ILogger<TrustedPeerManager> logger, ILogger<KestrelTcpServer> ktsLogger) : IAssemblyInjectSyncInitStarStop<TrustedPeerManager>
  15. {
  16. private const int NONCE_LENGTH_BYTES = 16;
  17. private const int NONCE_EXPIRE_SECOND = 60;
  18. private const int NONCE_SKEW_SECOND = 30;
  19. private readonly SocketConnectionContextFactory _connectionContextFactory = new(new(), logger);
  20. private readonly ConcurrentDictionary<string, LocalPeerManager> _localPeers = new();
  21. private TimestampNonceManager? _nonceManager;
  22. public void Init()
  23. {
  24. _nonceManager = new(NONCE_LENGTH_BYTES - TimestampNonceManager.TimestampLength, TimeSpan.FromMicroseconds(NONCE_EXPIRE_SECOND), TimeSpan.FromSeconds(NONCE_SKEW_SECOND));
  25. }
  26. public void Start()
  27. {
  28. }
  29. public void Stop()
  30. {
  31. foreach (var item in _localPeers.Values) item.Stop();
  32. _localPeers.Clear();
  33. _nonceManager?.Dispose();
  34. }
  35. public void AddOrReplaceLocalPeer(ILocalPeerInfo localPeerInfo)
  36. {
  37. RemoveLocalPeer(localPeerInfo.PeerId);
  38. var newLocal = _localPeers[localPeerInfo.PeerId] = new LocalPeerManager(_connectionContextFactory, _nonceManager!, localPeerInfo, eventBus, logger, ktsLogger);
  39. Task.Run(newLocal.Start);
  40. }
  41. public bool AddOrReplaceRemotePeer(string localPeerId, IRemotePeerInfo remotePeerInfo)
  42. {
  43. if (!_localPeers.TryGetValue(localPeerId, out var localPeerManager)) return false;
  44. localPeerManager.AddOrReplaceRemotePeer(remotePeerInfo);
  45. return true;
  46. }
  47. public bool RemoveLocalPeer(string localPeerId)
  48. {
  49. if (_localPeers.Remove(localPeerId, out var localPeerManager) == false) return false;
  50. localPeerManager.Stop();
  51. return true;
  52. }
  53. public bool RemoveRemotePeer(string localPeerId, string remotePeerId)
  54. {
  55. return _localPeers.TryGetValue(localPeerId, out var localPeerManager) && localPeerManager.RemoveRemotePeer(remotePeerId);
  56. }
  57. public bool ConnectToPeerAsync(string localPeerId, string remotePeerId)
  58. {
  59. return _localPeers.TryGetValue(localPeerId, out var localPeerManager) && localPeerManager.ConnectToRemotePeer(remotePeerId);
  60. }
  61. public bool SendToPeer(string localPeerId, string remotePeerId, byte[] payload)
  62. {
  63. return _localPeers.TryGetValue(localPeerId, out var localPeerManager) && localPeerManager.SendToRemotePeer(remotePeerId, payload);
  64. }
  65. }