ServiceSpy by jjxtra

<PackageReference Include="ServiceSpy" Version="1.0.1" />

 ServiceMetadata

public sealed class ServiceMetadata
Represents metadata for a service
using System; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Reflection; using System.Runtime.CompilerServices; using System.Text; namespace ServiceSpy.Registry { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public sealed class ServiceMetadata { internal static readonly string defaultVersion = Assembly.GetEntryAssembly().GetName().Version?.ToString(3) ?? "1.0.0"; internal static readonly byte[] serviceSpyServiceMetadataGuid = Guid.Parse("62573135-7B6A-4FAC-B765-9BE43E83E444").ToByteArray(); public Guid Id { get; set; } public string Name { get; set; } = string.Empty; public string Version { get; set; } = defaultVersion; public string Group { get; set; } = string.Empty; public IPAddress IPAddress { get; set; } = IPAddress.Any; public string IPAddressString { get { return IPAddress.ToString(); } set { IPAddress = ((string.IsNullOrWhiteSpace(value) || value == "*") ? GetLocalIPAddress() : IPAddress.Parse(value)); } } public int Port { get; set; } = 443; public string Host { get; set; } = string.Empty; public string Path { get; set; } = string.Empty; public string HealthCheckPath { get; set; } = string.Empty; [System.Runtime.CompilerServices.NullableContext(2)] public bool EqualsExactly(ServiceMetadata m) { if ((object)m != null && Id == m.Id && Name == m.Name && Version == m.Version && Group == m.Group && IPAddress.Equals(m.IPAddress) && Port == m.Port && Host == m.Host && Path == m.Path) return HealthCheckPath == m.HealthCheckPath; return false; } public override string ToString() { DefaultInterpolatedStringHandler val = default(DefaultInterpolatedStringHandler); val..ctor(16, 9); val.AppendFormatted<Guid>(Id); val.AppendLiteral(": "); val.AppendFormatted(Name); val.AppendLiteral(", "); val.AppendFormatted(Version); val.AppendLiteral(", "); val.AppendFormatted(Group); val.AppendLiteral(", "); val.AppendFormatted<IPAddress>(IPAddress); val.AppendLiteral(", "); val.AppendFormatted<int>(Port); val.AppendLiteral(", "); val.AppendFormatted(Host); val.AppendLiteral(", "); val.AppendFormatted(Path); val.AppendLiteral(", "); val.AppendFormatted(HealthCheckPath); return val.ToStringAndClear(); } [System.Runtime.CompilerServices.NullableContext(2)] public override bool Equals([NotNullWhen(true)] object obj) { ServiceMetadata serviceMetadata = obj as ServiceMetadata; if ((object)serviceMetadata == null) return false; if (Id == serviceMetadata.Id && IPAddress.Equals(serviceMetadata.IPAddress)) return Port == serviceMetadata.Port; return false; } public static bool operator ==(ServiceMetadata lhs, ServiceMetadata rhs) { if ((object)lhs == null) { if ((object)rhs == null) return true; return false; } return lhs.Equals(rhs); } public static bool operator !=(ServiceMetadata lhs, ServiceMetadata rhs) { return !(lhs == rhs); } public override int GetHashCode() { return Id.GetHashCode() ^ IPAddress.GetHashCode() ^ Port; } [System.Runtime.CompilerServices.NullableContext(2)] public static ServiceMetadata FromBinary([System.Runtime.CompilerServices.Nullable(1)] Stream s, out bool deletion, out string healthCheck) { BinaryReader binaryReader = new BinaryReader(s, Encoding.UTF8); int num = binaryReader.Read7BitEncodedInt(); deletion = false; healthCheck = null; if (num == 16) { byte[] first = binaryReader.ReadBytes(num); if (first.SequenceEqual(serviceSpyServiceMetadataGuid)) { int num2 = binaryReader.Read7BitEncodedInt(); if (num2 == 1) { int num3 = binaryReader.Read7BitEncodedInt(); if (num3 == 16) { byte[] b = binaryReader.ReadBytes(num3); Guid id = new Guid(b); string name = binaryReader.ReadString(); string version = binaryReader.ReadString(); deletion = binaryReader.ReadBoolean(); healthCheck = binaryReader.ReadString(); healthCheck = ((healthCheck == "!") ? null : healthCheck); string group = binaryReader.ReadString(); int num4 = binaryReader.Read7BitEncodedInt(); if (num4 == 4 || num4 == 16) { byte[] address = binaryReader.ReadBytes(num4); IPAddress iPAddress = new IPAddress(address); int num5 = binaryReader.ReadInt32(); if (num5 > 0 && num5 <= 65535) { string text = binaryReader.ReadString(); if (text.Length < 128) { string text2 = binaryReader.ReadString(); if (text2.Length < 128) { string text3 = binaryReader.ReadString(); if (text3.Length < 128) return new ServiceMetadata { Id = id, Name = name, Version = version, Group = group, IPAddress = iPAddress, Port = num5, Host = text, Path = text2, HealthCheckPath = text3 }; } } } } } } } } return null; } public void ToBinary(Stream s, bool deletion = false, [System.Runtime.CompilerServices.Nullable(2)] string healthCheck = null) { BinaryWriter binaryWriter = new BinaryWriter(s, Encoding.UTF8); binaryWriter.Write7BitEncodedInt(serviceSpyServiceMetadataGuid.Length); binaryWriter.Write(serviceSpyServiceMetadataGuid); binaryWriter.Write7BitEncodedInt(1); byte[] array = Id.ToByteArray(); binaryWriter.Write7BitEncodedInt(array.Length); binaryWriter.Write(array); binaryWriter.Write(Name); binaryWriter.Write(Version); binaryWriter.Write(deletion); binaryWriter.Write((healthCheck == null) ? "!" : healthCheck); binaryWriter.Write(Group); byte[] addressBytes = IPAddress.GetAddressBytes(); binaryWriter.Write7BitEncodedInt(addressBytes.Length); binaryWriter.Write(addressBytes); binaryWriter.Write(Port); binaryWriter.Write(Host); binaryWriter.Write(Path); binaryWriter.Write(HealthCheckPath); } public static IPAddress GetLocalIPAddress() { using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.IP)) { socket.Connect("8.8.8.8", 65530); IPEndPoint iPEndPoint = socket.LocalEndPoint as IPEndPoint; if (iPEndPoint != null) return iPEndPoint.Address; } using (Socket socket2 = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.IP)) { socket2.Connect("2001:4860:4860::8888", 65530); IPEndPoint iPEndPoint2 = socket2.LocalEndPoint as IPEndPoint; if (iPEndPoint2 != null) return iPEndPoint2.Address; } throw new ApplicationException("No network adapters with an IPv4 or IPv6 address on the system"); } } }