Skip to content

Commit

Permalink
feat: add ServiceDiscoveryIPv4 to EnvironmentInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
romanov.ah committed Jul 24, 2024
1 parent 39f7480 commit 9fe0c60
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public void FQDN_should_not_be_null_or_empty()
Console.Out.WriteLine(EnvironmentInfo.FQDN);
}

[Test]
public void IpAddress_should_not_be_null_or_empty()
{
EnvironmentInfo.ServiceDiscoveryIPv4.Should().NotBeNullOrEmpty();

Console.Out.WriteLine(EnvironmentInfo.ServiceDiscoveryIPv4);
}

[Test]
public void HomeDirectory_should_not_be_null_or_empty()
{
Expand Down
37 changes: 37 additions & 0 deletions Vostok.Commons.Environment/EnvironmentInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.InteropServices;
using JetBrains.Annotations;
Expand All @@ -18,10 +19,12 @@ internal static class EnvironmentInfo
{
public const string LocalHostnameVariable = "VOSTOK_LOCAL_HOSTNAME";
public const string LocalFQDNVariable = "VOSTOK_LOCAL_FQDN";
public const string LocalServiceDiscoveryIPv4 = "VOSTOK_LOCAL_SERVICE_DISCOVERY_IPV4";

private static Lazy<string> application = new Lazy<string>(ObtainApplicationName);
private static Lazy<string> host = new Lazy<string>(ObtainHostname);
private static Lazy<string> fqdn = new Lazy<string>(ObtainFQDN);
private static Lazy<string> serviceDiscoveryIPv4 = new Lazy<string>(ObtainServiceDiscoveryIPv4);
private static Lazy<string> processName = new Lazy<string>(GetProcessNameOrNull);
private static Lazy<string> homeDirectory = new Lazy<string>(ObtainHomeDirectory);
private static Lazy<int?> processId = new Lazy<int?>(GetProcessIdOrNull);
Expand All @@ -41,6 +44,11 @@ internal static class EnvironmentInfo
/// </summary>
public static string FQDN => fqdn.Value;

/// <summary>
/// Returns the IPv4 of the machine running the application.
/// </summary>
public static string ServiceDiscoveryIPv4 => serviceDiscoveryIPv4.Value;

/// <summary>
/// Returns the name of current process.
/// </summary>
Expand Down Expand Up @@ -193,6 +201,35 @@ private static string ObtainFQDN()
}
}

private static string ObtainServiceDiscoveryIPv4()
{
try
{
var dnsAddresses = Dns.GetHostAddresses(ObtainHostname());

var interfaceAddresses = NetworkInterface
.GetAllNetworkInterfaces()
.Where(iface => iface.OperationalStatus == OperationalStatus.Up)
.Where(iface => iface.GetIPProperties().GatewayAddresses.Any(info => !info.Address.Equals(IPAddress.Any)))
.SelectMany(iface => iface.GetIPProperties().UnicastAddresses)
.Select(info => info.Address)
.ToList();

var address = dnsAddresses
.Where(address => address.AddressFamily == AddressFamily.InterNetwork)
.Where(address => !IPAddress.IsLoopback(address))
.Where(address => interfaceAddresses.Contains(address))
.Select(address => address.ToString())
.First();

return address;
}
catch
{
return null;
}
}

private static string GetWindowsDomainName()
{
try
Expand Down

0 comments on commit 9fe0c60

Please sign in to comment.