From 045c9620d215a188b5aeb5fb131f7c838de9479e Mon Sep 17 00:00:00 2001 From: "romanov.ah" Date: Wed, 21 Aug 2024 17:22:52 +0500 Subject: [PATCH 1/7] feat: add ServiceDiscoveryIPv4 to EnvironmentInfo --- .../EnvironmentInfo_Tests.cs | 10 +++- Vostok.Commons.Environment/EnvironmentInfo.cs | 55 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs b/Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs index 88c2c5e..c0fe94d 100644 --- a/Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs +++ b/Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs @@ -15,7 +15,7 @@ public static void Application_name_should_be_not_null_or_empty() [Test] public static void Host_name_should_be_not_null_or_empty() - => string.IsNullOrEmpty(EnvironmentInfo.Application).Should().BeFalse(); + => string.IsNullOrEmpty(EnvironmentInfo.Host).Should().BeFalse(); [Test] public void FQDN_should_not_be_null_or_empty() @@ -25,6 +25,14 @@ public void FQDN_should_not_be_null_or_empty() Console.Out.WriteLine(EnvironmentInfo.FQDN); } + [Test] + public void ServiceDiscoveryIPv4_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() { diff --git a/Vostok.Commons.Environment/EnvironmentInfo.cs b/Vostok.Commons.Environment/EnvironmentInfo.cs index 159f50b..70ce3de 100644 --- a/Vostok.Commons.Environment/EnvironmentInfo.cs +++ b/Vostok.Commons.Environment/EnvironmentInfo.cs @@ -4,8 +4,10 @@ using System.Linq; using System.Net; using System.Net.NetworkInformation; +using System.Net.Sockets; using System.Reflection; using System.Runtime.InteropServices; +using System.Threading.Tasks; using JetBrains.Annotations; namespace Vostok.Commons.Environment @@ -18,6 +20,7 @@ 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 application = new Lazy(ObtainApplicationName); private static Lazy host = new Lazy(ObtainHostname); @@ -26,6 +29,8 @@ internal static class EnvironmentInfo private static Lazy homeDirectory = new Lazy(ObtainHomeDirectory); private static Lazy processId = new Lazy(GetProcessIdOrNull); + private static readonly TimeSpan CacheTtl = TimeSpan.FromSeconds(3); + /// /// Returns the name of the application. /// @@ -41,6 +46,11 @@ internal static class EnvironmentInfo /// public static string FQDN => fqdn.Value; + /// + /// Returns the IPv4 of the machine running the application. + /// + public static volatile string ServiceDiscoveryIPv4; + /// /// Returns the name of current process. /// @@ -61,6 +71,18 @@ internal static class EnvironmentInfo /// public static string HomeDirectory => homeDirectory.Value; + static EnvironmentInfo() + { + UpdateAndSchedule(); + } + + private static void UpdateAndSchedule() + { + ServiceDiscoveryIPv4 = ObtainServiceDiscoveryIPv4(); + + Task.Delay(CacheTtl).ContinueWith(_ => UpdateAndSchedule()); + } + private static string ObtainApplicationName() { try @@ -193,6 +215,39 @@ private static string ObtainFQDN() } } + private static string ObtainServiceDiscoveryIPv4() + { + try + { + var localIpV4 = System.Environment.GetEnvironmentVariable(LocalServiceDiscoveryIPv4); + if (localIpV4 != null) + return localIpV4; + + 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()) + .FirstOrDefault(); + + return address; + } + catch + { + return null; + } + } + private static string GetWindowsDomainName() { try From e10e9260a2ac1b606385cce90f7cf0506b0e10b9 Mon Sep 17 00:00:00 2001 From: "romanov.ah" Date: Thu, 22 Aug 2024 12:03:56 +0500 Subject: [PATCH 2/7] fix: rename LocalServiceDiscoveryIPv4 to LocalServiceDiscoveryIPv4Variable --- Vostok.Commons.Environment/EnvironmentInfo.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Vostok.Commons.Environment/EnvironmentInfo.cs b/Vostok.Commons.Environment/EnvironmentInfo.cs index 70ce3de..d842b8e 100644 --- a/Vostok.Commons.Environment/EnvironmentInfo.cs +++ b/Vostok.Commons.Environment/EnvironmentInfo.cs @@ -20,7 +20,7 @@ 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"; + public const string LocalServiceDiscoveryIPv4Variable = "VOSTOK_LOCAL_SERVICE_DISCOVERY_IPV4"; private static Lazy application = new Lazy(ObtainApplicationName); private static Lazy host = new Lazy(ObtainHostname); @@ -219,7 +219,7 @@ private static string ObtainServiceDiscoveryIPv4() { try { - var localIpV4 = System.Environment.GetEnvironmentVariable(LocalServiceDiscoveryIPv4); + var localIpV4 = System.Environment.GetEnvironmentVariable(LocalServiceDiscoveryIPv4Variable); if (localIpV4 != null) return localIpV4; From 2a4f324f8a1716f7ad0f08a89d82eb244b7f532d Mon Sep 17 00:00:00 2001 From: "romanov.ah" Date: Thu, 22 Aug 2024 15:19:49 +0500 Subject: [PATCH 3/7] fix: remove ServiceDiscoveryIPv4 caching --- Vostok.Commons.Environment/EnvironmentInfo.cs | 20 +++---------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/Vostok.Commons.Environment/EnvironmentInfo.cs b/Vostok.Commons.Environment/EnvironmentInfo.cs index d842b8e..7f9e97d 100644 --- a/Vostok.Commons.Environment/EnvironmentInfo.cs +++ b/Vostok.Commons.Environment/EnvironmentInfo.cs @@ -7,7 +7,6 @@ using System.Net.Sockets; using System.Reflection; using System.Runtime.InteropServices; -using System.Threading.Tasks; using JetBrains.Annotations; namespace Vostok.Commons.Environment @@ -25,12 +24,11 @@ internal static class EnvironmentInfo private static Lazy application = new Lazy(ObtainApplicationName); private static Lazy host = new Lazy(ObtainHostname); private static Lazy fqdn = new Lazy(ObtainFQDN); + private static Lazy serviceDiscoveryIPv4 = new Lazy(ObtainServiceDiscoveryIPv4); private static Lazy processName = new Lazy(GetProcessNameOrNull); private static Lazy homeDirectory = new Lazy(ObtainHomeDirectory); private static Lazy processId = new Lazy(GetProcessIdOrNull); - private static readonly TimeSpan CacheTtl = TimeSpan.FromSeconds(3); - /// /// Returns the name of the application. /// @@ -47,9 +45,9 @@ internal static class EnvironmentInfo public static string FQDN => fqdn.Value; /// - /// Returns the IPv4 of the machine running the application. + /// Returns the IPv4 through which the application is accessible on the hosting network. /// - public static volatile string ServiceDiscoveryIPv4; + public static string ServiceDiscoveryIPv4 => serviceDiscoveryIPv4.Value; /// /// Returns the name of current process. @@ -71,18 +69,6 @@ internal static class EnvironmentInfo /// public static string HomeDirectory => homeDirectory.Value; - static EnvironmentInfo() - { - UpdateAndSchedule(); - } - - private static void UpdateAndSchedule() - { - ServiceDiscoveryIPv4 = ObtainServiceDiscoveryIPv4(); - - Task.Delay(CacheTtl).ContinueWith(_ => UpdateAndSchedule()); - } - private static string ObtainApplicationName() { try From c4171a5f7e37a7aa0a3539b420006947d7d096a0 Mon Sep 17 00:00:00 2001 From: "romanov.ah" Date: Mon, 26 Aug 2024 17:53:02 +0500 Subject: [PATCH 4/7] fix: remove obtaining IPv4 --- Vostok.Commons.Environment/EnvironmentInfo.cs | 24 ++----------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/Vostok.Commons.Environment/EnvironmentInfo.cs b/Vostok.Commons.Environment/EnvironmentInfo.cs index 7f9e97d..928696a 100644 --- a/Vostok.Commons.Environment/EnvironmentInfo.cs +++ b/Vostok.Commons.Environment/EnvironmentInfo.cs @@ -46,6 +46,7 @@ internal static class EnvironmentInfo /// /// Returns the IPv4 through which the application is accessible on the hosting network. + /// This value is obtained once when the application starts and is cached for subsequent calls. /// public static string ServiceDiscoveryIPv4 => serviceDiscoveryIPv4.Value; @@ -205,28 +206,7 @@ private static string ObtainServiceDiscoveryIPv4() { try { - var localIpV4 = System.Environment.GetEnvironmentVariable(LocalServiceDiscoveryIPv4Variable); - if (localIpV4 != null) - return localIpV4; - - 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()) - .FirstOrDefault(); - - return address; + return System.Environment.GetEnvironmentVariable(LocalServiceDiscoveryIPv4Variable); } catch { From 9226e7f2f60beaea0523380a73b9102601d312dd Mon Sep 17 00:00:00 2001 From: "romanov.ah" Date: Thu, 29 Aug 2024 16:23:54 +0500 Subject: [PATCH 5/7] test: fix test --- Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs b/Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs index c0fe94d..2963264 100644 --- a/Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs +++ b/Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs @@ -26,9 +26,9 @@ public void FQDN_should_not_be_null_or_empty() } [Test] - public void ServiceDiscoveryIPv4_should_not_be_null_or_empty() + public void ServiceDiscoveryIPv4_should_be_null_or_empty_if_environment_variable_is_not_set() { - EnvironmentInfo.ServiceDiscoveryIPv4.Should().NotBeNullOrEmpty(); + EnvironmentInfo.ServiceDiscoveryIPv4.Should().BeNullOrEmpty(); Console.Out.WriteLine(EnvironmentInfo.ServiceDiscoveryIPv4); } From 1a7f393b0d78f430da5ff2e154cb5c1161b97db4 Mon Sep 17 00:00:00 2001 From: "romanov.ah" Date: Fri, 30 Aug 2024 10:29:58 +0500 Subject: [PATCH 6/7] fix: ServiceDiscoveryIPv4 should return null if string is empty --- Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs | 4 ++-- Vostok.Commons.Environment/EnvironmentInfo.cs | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs b/Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs index 2963264..4375e2e 100644 --- a/Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs +++ b/Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs @@ -26,9 +26,9 @@ public void FQDN_should_not_be_null_or_empty() } [Test] - public void ServiceDiscoveryIPv4_should_be_null_or_empty_if_environment_variable_is_not_set() + public void ServiceDiscoveryIPv4_should_be_null_if_environment_variable_is_not_set() { - EnvironmentInfo.ServiceDiscoveryIPv4.Should().BeNullOrEmpty(); + EnvironmentInfo.ServiceDiscoveryIPv4.Should().BeNull(); Console.Out.WriteLine(EnvironmentInfo.ServiceDiscoveryIPv4); } diff --git a/Vostok.Commons.Environment/EnvironmentInfo.cs b/Vostok.Commons.Environment/EnvironmentInfo.cs index 928696a..4a53b4a 100644 --- a/Vostok.Commons.Environment/EnvironmentInfo.cs +++ b/Vostok.Commons.Environment/EnvironmentInfo.cs @@ -206,7 +206,11 @@ private static string ObtainServiceDiscoveryIPv4() { try { - return System.Environment.GetEnvironmentVariable(LocalServiceDiscoveryIPv4Variable); + var localIPv4 = System.Environment.GetEnvironmentVariable(LocalServiceDiscoveryIPv4Variable); + if (!string.IsNullOrEmpty(localIPv4)) + return localIPv4; + + return null; } catch { From e7a058ed4d81af41717c908695e38e921101ae89 Mon Sep 17 00:00:00 2001 From: "romanov.ah" Date: Tue, 3 Sep 2024 13:36:04 +0500 Subject: [PATCH 7/7] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67f5640..e7913f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.1.5 (03-09-2024): + +Added `ServiceDiscoveryIPv4` to `EnvironmentInfo` + ## 0.1.4 (22-08-2022): Read Home Directory from `System.Environment.SpecialFolder.UserProfile`.