| | 1 | | using System.Net; |
| | 2 | | using System.Net.Sockets; |
| | 3 | | using Microsoft.Extensions.Logging; |
| | 4 | | using Microsoft.Extensions.Options; |
| | 5 | |
|
| | 6 | | namespace NLightning.Infrastructure.Node.Factories; |
| | 7 | |
|
| | 8 | | using Domain.Exceptions; |
| | 9 | | using Domain.Factories; |
| | 10 | | using Domain.Node.Options; |
| | 11 | | using Domain.Protocol.Factories; |
| | 12 | | using Domain.Protocol.Managers; |
| | 13 | | using Interfaces; |
| | 14 | | using Models; |
| | 15 | |
|
| | 16 | | public class PeerFactory : IPeerFactory |
| | 17 | | { |
| | 18 | | private readonly ILoggerFactory _loggerFactory; |
| | 19 | | private readonly IMessageFactory _messageFactory; |
| | 20 | | private readonly IMessageServiceFactory _messageServiceFactory; |
| | 21 | | private readonly IPingPongServiceFactory _pingPongServiceFactory; |
| | 22 | | private readonly ISecureKeyManager _secureKeyManager; |
| | 23 | | private readonly ITransportServiceFactory _transportServiceFactory; |
| | 24 | | private readonly NodeOptions _nodeOptions; |
| | 25 | |
|
| 0 | 26 | | public PeerFactory(ILoggerFactory loggerFactory, IMessageFactory messageFactory, |
| 0 | 27 | | IMessageServiceFactory messageServiceFactory, IPingPongServiceFactory pingPongServiceFactory, |
| 0 | 28 | | ISecureKeyManager secureKeyManager, ITransportServiceFactory transportServiceFactory, |
| 0 | 29 | | IOptions<NodeOptions> nodeOptions) |
| | 30 | | { |
| 0 | 31 | | _loggerFactory = loggerFactory; |
| 0 | 32 | | _messageFactory = messageFactory; |
| 0 | 33 | | _messageServiceFactory = messageServiceFactory; |
| 0 | 34 | | _pingPongServiceFactory = pingPongServiceFactory; |
| 0 | 35 | | _secureKeyManager = secureKeyManager; |
| 0 | 36 | | _transportServiceFactory = transportServiceFactory; |
| 0 | 37 | | _nodeOptions = nodeOptions.Value; |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Creates a peer that we're connecting to. |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="peerAddress">Peer address</param> |
| | 44 | | /// <param name="tcpClient">TCP client</param> |
| | 45 | | /// <returns>A task that represents the asynchronous operation. The task result contains the created peer.</returns> |
| | 46 | | /// <exception cref="ConnectionException">Thrown when the connection to the peer fails.</exception> |
| | 47 | | public async Task<Peer> CreateConnectedPeerAsync(Protocol.Models.PeerAddress peerAddress, TcpClient tcpClient) |
| | 48 | | { |
| | 49 | | // Create a specific logger for the Peer class |
| 0 | 50 | | var logger = _loggerFactory.CreateLogger<Peer>(); |
| | 51 | |
|
| | 52 | | // Create and Initialize the transport service |
| 0 | 53 | | var keyBytes = _secureKeyManager.GetNodeKey().ToBytes(); |
| 0 | 54 | | var transportService = _transportServiceFactory |
| 0 | 55 | | .CreateTransportService(true, keyBytes, peerAddress.PubKey.ToBytes(), tcpClient); |
| | 56 | | try |
| | 57 | | { |
| 0 | 58 | | await transportService.InitializeAsync(); |
| 0 | 59 | | } |
| 0 | 60 | | catch (Exception ex) |
| | 61 | | { |
| 0 | 62 | | throw new ConnectionException($"Error connecting to peer {peerAddress.Host}:{peerAddress.Port}", ex); |
| | 63 | | } |
| | 64 | |
|
| | 65 | | // Create the message service |
| 0 | 66 | | var messageService = _messageServiceFactory.CreateMessageService(transportService); |
| | 67 | |
|
| | 68 | | // Create the ping pong service |
| 0 | 69 | | var pingPongService = _pingPongServiceFactory.CreatePingPongService(); |
| | 70 | |
|
| 0 | 71 | | return new Peer(_nodeOptions.Features, logger, _messageFactory, messageService, _nodeOptions.NetworkTimeout, |
| 0 | 72 | | peerAddress, pingPongService); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public async Task<Peer> CreateConnectingPeerAsync(TcpClient tcpClient) |
| | 76 | | { |
| | 77 | | // Create a specific logger for the Peer class |
| 0 | 78 | | var logger = _loggerFactory.CreateLogger<Peer>(); |
| | 79 | |
|
| 0 | 80 | | var remoteEndPoint = (IPEndPoint)(tcpClient.Client.RemoteEndPoint |
| 0 | 81 | | ?? throw new Exception("Failed to get remote endpoint")); |
| 0 | 82 | | var ipAddress = remoteEndPoint.Address.ToString(); |
| 0 | 83 | | var port = remoteEndPoint.Port; |
| | 84 | |
|
| | 85 | | // Create and Initialize the transport service |
| 0 | 86 | | var key = _secureKeyManager.GetNodeKey(); |
| 0 | 87 | | var transportService = _transportServiceFactory |
| 0 | 88 | | .CreateTransportService(false, key.ToBytes(), key.PubKey.ToBytes(), tcpClient); |
| | 89 | | try |
| | 90 | | { |
| 0 | 91 | | await transportService.InitializeAsync(); |
| 0 | 92 | | } |
| 0 | 93 | | catch (Exception ex) |
| | 94 | | { |
| 0 | 95 | | throw new ConnectionException($"Error establishing connection to peer {ipAddress}:{port}", ex); |
| | 96 | | } |
| | 97 | |
|
| 0 | 98 | | if (transportService.RemoteStaticPublicKey is null) |
| | 99 | | { |
| 0 | 100 | | throw new ErrorException("Failed to get remote static public key"); |
| | 101 | | } |
| | 102 | |
|
| | 103 | | // Create the message service |
| 0 | 104 | | var messageService = _messageServiceFactory.CreateMessageService(transportService); |
| | 105 | |
|
| | 106 | | // Create the ping pong service |
| 0 | 107 | | var pingPongService = _pingPongServiceFactory.CreatePingPongService(); |
| | 108 | |
|
| 0 | 109 | | var peerAddress = new Protocol.Models.PeerAddress(transportService.RemoteStaticPublicKey, ipAddress, port); |
| | 110 | |
|
| 0 | 111 | | return new Peer(_nodeOptions.Features, logger, _messageFactory, messageService, _nodeOptions.NetworkTimeout, |
| 0 | 112 | | peerAddress, pingPongService); |
| 0 | 113 | | } |
| | 114 | | } |