TrustedPeerManager.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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(IPeerInfoProvider peerInfoProvider, 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. }
  25. public void Start()
  26. {
  27. _nonceManager = new(NONCE_LENGTH_BYTES - TimestampNonceManager.TimestampLength, TimeSpan.FromMicroseconds(NONCE_EXPIRE_SECOND), TimeSpan.FromSeconds(NONCE_SKEW_SECOND));
  28. foreach (var item in peerInfoProvider.PeerInfo)
  29. {
  30. _localPeers[item.PeerId] = new LocalPeerManager(_connectionContextFactory, _nonceManager, item, eventBus, logger, ktsLogger);
  31. }
  32. foreach (var item in _localPeers.Values) item.Start();
  33. }
  34. public void Stop()
  35. {
  36. foreach (var item in _localPeers.Values) item.Stop();
  37. _localPeers.Clear();
  38. _nonceManager?.Dispose();
  39. }
  40. public void AddOrReplaceLocalPeer(ILocalPeerInfo localPeerInfo)
  41. {
  42. RemoveLocalPeer(localPeerInfo.PeerId);
  43. var newLocal = _localPeers[localPeerInfo.PeerId] = new LocalPeerManager(_connectionContextFactory, _nonceManager, localPeerInfo, eventBus, logger, ktsLogger);
  44. Task.Run(newLocal.Start);
  45. }
  46. public bool AddOrReplaceRemotePeer(string localPeerId, IRemotePeerInfo remotePeerInfo)
  47. {
  48. if (!_localPeers.TryGetValue(localPeerId, out var localPeerManager)) return false;
  49. localPeerManager.AddOrReplaceRemotePeer(remotePeerInfo);
  50. return true;
  51. }
  52. public bool RemoveLocalPeer(string localPeerId)
  53. {
  54. if (_localPeers.Remove(localPeerId, out var localPeerManager) == false) return false;
  55. localPeerManager.Stop();
  56. return true;
  57. }
  58. public bool RemoveRemotePeer(string localPeerId, string remotePeerId)
  59. {
  60. return _localPeers.TryGetValue(localPeerId, out var localPeerManager) && localPeerManager.RemoveRemotePeer(remotePeerId);
  61. }
  62. public bool ConnectToPeerAsync(string localPeerId, string remotePeerId)
  63. {
  64. return _localPeers.TryGetValue(localPeerId, out var localPeerManager) && localPeerManager.ConnectToRemotePeer(remotePeerId);
  65. }
  66. public bool SendToPeer(string localPeerId, string remotePeerId, byte[] payload)
  67. {
  68. return _localPeers.TryGetValue(localPeerId, out var localPeerManager) && localPeerManager.SendToRemotePeer(remotePeerId, payload);
  69. }
  70. }