| | 1 | | using System.Net; |
| | 2 | | using System.Text.RegularExpressions; |
| | 3 | |
|
| | 4 | | namespace NLightning.Infrastructure.Protocol.Models; |
| | 5 | |
|
| | 6 | | using NLightning.Domain.Crypto.ValueObjects; |
| | 7 | | using NLightning.Domain.Node.ValueObjects; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Represents a peer address. |
| | 11 | | /// </summary> |
| | 12 | | public sealed partial class PeerAddress |
| | 13 | | { |
| | 14 | | [GeneratedRegex(@"\d+")] |
| | 15 | | private static partial Regex OnlyDigitsRegex(); |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Gets the public key. |
| | 19 | | /// </summary> |
| 16 | 20 | | public CompactPubKey PubKey { get; } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Gets the host. |
| | 24 | | /// </summary> |
| 20 | 25 | | public IPAddress Host { get; } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Gets the port. |
| | 29 | | /// </summary> |
| 16 | 30 | | public int Port { get; } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Initializes a new instance of the <see cref="PeerAddress"/> class. |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="address">The address.</param> |
| | 36 | | /// <remarks> |
| | 37 | | /// The address is in the format of "pubkey@host:port". |
| | 38 | | /// </remarks> |
| 4 | 39 | | public PeerAddress(string address) |
| | 40 | | { |
| 4 | 41 | | var parts = address.Split('@'); |
| 4 | 42 | | if (parts.Length != 2) |
| 0 | 43 | | throw new FormatException("Invalid address format, should be pubkey@host:port"); |
| | 44 | |
|
| 4 | 45 | | PubKey = new CompactPubKey(Convert.FromHexString(parts[0])); |
| | 46 | |
|
| | 47 | | // Check if the address starts with http |
| 4 | 48 | | if (parts[1].StartsWith("http")) |
| | 49 | | { |
| | 50 | | // split on first // to get the address |
| 0 | 51 | | var hostPort = parts[1].Split("//")[1].Split(":"); |
| 0 | 52 | | Host = Dns.GetHostAddresses(hostPort[0])[0]; |
| | 53 | |
|
| | 54 | | // Port may have an extra / at the end. Use regex to keep only the number in the port |
| 0 | 55 | | Port = int.Parse(OnlyDigitsRegex().Match(hostPort[1]).Value); |
| | 56 | | } |
| | 57 | | else |
| | 58 | | { |
| 4 | 59 | | var hostPort = parts[1].Split(':'); |
| 4 | 60 | | Host = IPAddress.Parse(hostPort[0]); |
| 4 | 61 | | Port = int.Parse(hostPort[1]); |
| | 62 | | } |
| 4 | 63 | | } |
| | 64 | |
|
| 0 | 65 | | public PeerAddress(PeerAddressInfo peerAddressInfo) : this(peerAddressInfo.Address) |
| | 66 | | { |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// Initializes a new instance of the <see cref="PeerAddress"/> class. |
| | 71 | | /// </summary> |
| | 72 | | /// <param name="pubKey">The public key.</param> |
| | 73 | | /// <param name="address">The address.</param> |
| | 74 | | /// <remarks> |
| | 75 | | /// The address is in the format of "http://host:port" or "host:port". |
| | 76 | | /// </remarks> |
| 4 | 77 | | public PeerAddress(CompactPubKey pubKey, string address) |
| | 78 | | { |
| 4 | 79 | | PubKey = pubKey; |
| | 80 | |
|
| | 81 | | // Check if the address starts with http |
| 4 | 82 | | if (address.StartsWith("http")) |
| | 83 | | { |
| | 84 | | // split on first // to get the address |
| 4 | 85 | | var host = address.Split("//")[1]; |
| 4 | 86 | | Host = Dns.GetHostAddresses(host.Split(":")[0])[0]; |
| | 87 | |
|
| | 88 | | // Port may have an extra / at the end. Use regex to keep only the number in the port |
| 4 | 89 | | Port = int.Parse(OnlyDigitsRegex().Match(host.Split(":")[1]).Value); |
| | 90 | | } |
| | 91 | | else |
| | 92 | | { |
| 0 | 93 | | var hostPort = address.Split(':'); |
| 0 | 94 | | Host = IPAddress.Parse(hostPort[0]); |
| 0 | 95 | | Port = int.Parse(hostPort[1]); |
| | 96 | | } |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | /// <summary> |
| | 100 | | /// Initializes a new instance of the <see cref="PeerAddress"/> class. |
| | 101 | | /// </summary> |
| | 102 | | /// <param name="pubKey">The public key.</param> |
| | 103 | | /// <param name="host">The host.</param> |
| | 104 | | /// <param name="port">The port.</param> |
| 8 | 105 | | public PeerAddress(CompactPubKey pubKey, string host, int port) |
| | 106 | | { |
| 8 | 107 | | PubKey = pubKey; |
| 8 | 108 | | Host = IPAddress.Parse(host); |
| 8 | 109 | | Port = port; |
| 8 | 110 | | } |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Returns a string that represents the address. |
| | 114 | | /// </summary> |
| | 115 | | /// <returns>A string in the format of "pubkey@host:port".</returns> |
| | 116 | | public override string ToString() |
| | 117 | | { |
| 4 | 118 | | return $"{PubKey}@{Host}:{Port}"; |
| | 119 | | } |
| | 120 | | } |