#define TRACE using System; using System.Diagnostics; using System.Globalization; using System.Linq; using System.Net; namespace ArchestrAServices.Common; public class HostNameValidator { public static bool IsValidHostAddress(string hostAddress) { bool result = false; try { UriHostNameType uriHostNameType = Uri.CheckHostName(hostAddress); if (uriHostNameType != UriHostNameType.Unknown) { result = true; SvcTrace.DiagDiagnostics.TraceEvent(TraceEventType.Information, 0, $"The Host Address {hostAddress} is a valid {uriHostNameType}."); } else { SvcTrace.DiagControl.TraceEvent(TraceEventType.Warning, 0, $"The given host Address {hostAddress} is invalid"); } } catch (Exception ex) { SvcTrace.DiagException.TraceEvent(TraceEventType.Error, 0, ex.Message); } return result; } public static bool IsValidIPAddress(string hostAddress) { bool result = false; try { UriHostNameType uriHostNameType = Uri.CheckHostName(hostAddress); if (uriHostNameType != UriHostNameType.Unknown) { result = true; SvcTrace.DiagDiagnostics.TraceEvent(TraceEventType.Information, 0, $"The Host Address {hostAddress} is a valid {uriHostNameType}."); } else { SvcTrace.DiagControl.TraceEvent(TraceEventType.Warning, 0, $"The given host Address {hostAddress} is invalid"); } } catch (Exception ex) { SvcTrace.DiagException.TraceEvent(TraceEventType.Error, 0, ex.Message); } return result; } public static bool IsValidFQDNAddress(string hostAddress) { bool result = false; try { UriHostNameType uriHostNameType = Uri.CheckHostName(hostAddress); if (uriHostNameType != UriHostNameType.Unknown) { result = true; SvcTrace.DiagDiagnostics.TraceEvent(TraceEventType.Information, 0, $"The Host Address {hostAddress} is a valid {uriHostNameType}."); } else { SvcTrace.DiagControl.TraceEvent(TraceEventType.Warning, 0, $"The given host Address {hostAddress} is invalid"); } } catch (Exception ex) { SvcTrace.DiagException.TraceEvent(TraceEventType.Error, 0, ex.Message); } return result; } public static bool IsValidNodeName(string nodeName) { bool result = false; try { UriHostNameType uriHostNameType = Uri.CheckHostName(nodeName); if (uriHostNameType != UriHostNameType.Unknown) { result = true; SvcTrace.DiagDiagnostics.TraceEvent(TraceEventType.Information, 0, $"The node name {nodeName} is a valid {uriHostNameType}."); } else { SvcTrace.DiagControl.TraceEvent(TraceEventType.Warning, 0, $"The given node name Address {nodeName} is invalid"); } } catch (Exception ex) { SvcTrace.DiagException.TraceEvent(TraceEventType.Error, 0, ex.Message); } return result; } public static bool IsEndpointEquals(string oldEndpoint, string newEndPoint) { bool flag = false; try { if (string.IsNullOrEmpty(oldEndpoint) && string.IsNullOrEmpty(newEndPoint)) { return true; } if (string.IsNullOrEmpty(oldEndpoint) ^ string.IsNullOrEmpty(newEndPoint)) { return false; } string text = string.Empty; string text2 = string.Empty; int num = 0; int num2 = 0; Uri uri = new Uri(oldEndpoint); if (null != uri) { text = GetHostName(uri.Host, returnSameHNIfNotAvailable: true); num = uri.Port; SvcTrace.DiagDiagnostics.TraceEvent(TraceEventType.Information, 0, $"Old endpoint host name {text}, and Port number: {num}"); } uri = new Uri(newEndPoint); if (null != uri) { text2 = GetHostName(uri.Host, returnSameHNIfNotAvailable: true); num2 = uri.Port; SvcTrace.DiagDiagnostics.TraceEvent(TraceEventType.Information, 0, $"New endpoint host {text2}, and Port number: {num2}"); } if (string.Equals(text, text2, StringComparison.CurrentCultureIgnoreCase) && num == num2) { SvcTrace.DiagControl.TraceEvent(TraceEventType.Information, 0, $"{oldEndpoint} and {newEndPoint} are equal"); return true; } SvcTrace.DiagControl.TraceEvent(TraceEventType.Information, 0, $"{oldEndpoint} and {newEndPoint} are Not equal"); return false; } catch (Exception ex) { SvcTrace.DiagException.TraceEvent(TraceEventType.Information, 0, ex.Message); return false; } } public static bool IsHostAddressEquals(string sourceHostAddr, string targetHostAddr) { bool flag = false; try { if (string.IsNullOrEmpty(sourceHostAddr) && string.IsNullOrEmpty(targetHostAddr)) { return true; } if (string.IsNullOrEmpty(sourceHostAddr) ^ string.IsNullOrEmpty(targetHostAddr)) { return false; } string hostName = GetHostName(sourceHostAddr, returnSameHNIfNotAvailable: true); string hostName2 = GetHostName(targetHostAddr, returnSameHNIfNotAvailable: true); return string.Equals(hostName, hostName2, StringComparison.CurrentCultureIgnoreCase); } catch (Exception ex) { SvcTrace.DiagException.TraceEvent(TraceEventType.Information, 0, ex.Message); return false; } } public static string GetHostName(string hostAddress, bool returnSameHNIfNotAvailable) { string text = string.Empty; bool flag = false; try { if (!string.IsNullOrEmpty(hostAddress) && IsValidHostAddress(hostAddress)) { if (IsLocalIPAddress(hostAddress)) { flag = true; text = Environment.MachineName; } else { IPHostEntry hostEntry = Dns.GetHostEntry(hostAddress); if (hostEntry != null) { flag = true; string hostName = hostEntry.HostName; if (!string.IsNullOrEmpty(hostName) && hostName.Contains('.')) { if (hostName.EndsWith(".")) { hostName = Dns.GetHostEntry(hostEntry.AddressList[0]).HostName; } text = hostName.Substring(0, hostName.IndexOf(".")); SvcTrace.DiagDiagnostics.TraceEvent(TraceEventType.Information, 0, $"The HostName for {hostAddress} is : {text}"); } else { text = hostEntry.HostName; } } } } else { flag = true; SvcTrace.DiagControl.TraceEvent(TraceEventType.Warning, 0, string.Format("The HostAddress is emtpy or invalid", hostAddress)); } } catch (Exception ex) { text = string.Empty; SvcTrace.DiagException.TraceEvent(TraceEventType.Information, 0, ex.Message); } finally { if (!flag) { if (returnSameHNIfNotAvailable) { text = hostAddress; } SvcTrace.DiagControl.TraceEvent(TraceEventType.Information, 0, $"Couldn't find the DNS server for givem hostAddress {hostAddress} "); } } return text; } public static string GetHostName(string endPointAddress, bool returnSameHNIfNotAvailable, out int portNumber) { string result = string.Empty; portNumber = 0; try { if (!string.IsNullOrEmpty(endPointAddress)) { Uri uri = new Uri(endPointAddress); if (null != uri) { result = GetHostName(uri.Host, returnSameHNIfNotAvailable); portNumber = uri.Port; } } } catch (Exception ex) { result = string.Empty; SvcTrace.DiagControl.TraceEvent(TraceEventType.Warning, 0, $"Invalid endpoint address {endPointAddress}."); SvcTrace.DiagException.TraceEvent(TraceEventType.Information, 0, ex.Message); } return result; } public static bool IsLocalMachine(string baseAddress) { bool flag = false; SvcTrace.DiagCommand.TraceEvent(TraceEventType.Information, 3, string.Format(CultureInfo.CurrentCulture, "IsLocalMachine called for URI {0} ", new object[1] { baseAddress })); try { IPAddress[] hostAddresses = GetHostAddresses(new Uri(baseAddress)); IPAddress[] hostAddresses2 = Dns.GetHostAddresses(Dns.GetHostName()); IPAddress[] array = hostAddresses; foreach (IPAddress iPAddress in array) { if (IPAddress.IsLoopback(iPAddress)) { SvcTrace.DiagDiagnostics.TraceEvent(TraceEventType.Information, 0, string.Format(CultureInfo.CurrentCulture, "IsLocalMachine: URI designates loopback, returning true")); flag = true; break; } IPAddress[] array2 = hostAddresses2; foreach (IPAddress obj in array2) { if (iPAddress.Equals(obj)) { SvcTrace.DiagDiagnostics.TraceEvent(TraceEventType.Information, 0, string.Format(CultureInfo.CurrentCulture, "IsLocalMachine: URI designates localhost, returning true")); flag = true; break; } } if (flag) { break; } } } catch (Exception ex) { SvcTrace.DiagException.TraceEvent(TraceEventType.Critical, 0, string.Format(CultureInfo.CurrentCulture, "IsLocalMachine method has an exception {0}.", new object[1] { ex.Message })); flag = false; } if (!flag) { SvcTrace.DiagDiagnostics.TraceEvent(TraceEventType.Information, 0, string.Format(CultureInfo.CurrentCulture, "IsLocalMachine: URI designates foreign node, returning false")); } return flag; } public static bool IsLocalIPAddress(string ipAddress) { bool result = false; if (IsValidIPAddress(ipAddress)) { IPAddress[] addressList = Dns.GetHostEntry(string.Empty).AddressList; for (int i = 0; i < addressList.Length; i++) { string b = addressList[i].ToString(); if (string.Equals(ipAddress, b, StringComparison.CurrentCultureIgnoreCase)) { result = true; break; } } } return result; } public static IPAddress[] GetHostAddresses(Uri uri) { IPAddress[] result = new IPAddress[0]; try { SvcTrace.DiagCommand.TraceEvent(TraceEventType.Information, 3, string.Format(CultureInfo.CurrentCulture, "GetHostAddresses called for URI {0} ", new object[1] { uri })); UriBuilder uriBuilder = new UriBuilder(uri); if (uriBuilder != null) { result = GetHostAddresses(uriBuilder.Host); } } catch (Exception ex) { SvcTrace.DiagException.TraceEvent(TraceEventType.Critical, 0, string.Format(CultureInfo.CurrentCulture, "GetHostAddresses method has an exception {0}.", new object[1] { ex.Message })); } return result; } public static IPAddress[] GetHostAddresses(string hostAddress) { IPAddress[] result = new IPAddress[0]; try { if (hostAddress != null) { SvcTrace.DiagCommand.TraceEvent(TraceEventType.Information, 3, string.Format(CultureInfo.CurrentCulture, "GetHostAddresses called for hostname {0} ", new object[1] { hostAddress })); if (hostAddress.StartsWith("localhost", ignoreCase: true, CultureInfo.CurrentCulture)) { hostAddress = hostAddress.Replace("localhost", Environment.MachineName); } result = Dns.GetHostAddresses(hostAddress.ToString()); } else { SvcTrace.DiagCommand.TraceEvent(TraceEventType.Information, 3, string.Format(CultureInfo.CurrentCulture, "GetHostAddresses called for 'null' hostname ")); } } catch (Exception ex) { SvcTrace.DiagException.TraceEvent(TraceEventType.Critical, 0, string.Format(CultureInfo.CurrentCulture, "GetHostAddresses method has an exception {0}.", new object[1] { ex.Message })); } return result; } public static bool IsRemoteNodeSameasSRNode(string remoteRepositoryNode, string defaultSRNodeName) { bool result = false; try { string value = string.Empty; if (string.IsNullOrEmpty(defaultSRNodeName)) { value = RegistryHandler.GetSrNode(out defaultSRNodeName); } if (string.IsNullOrEmpty(value) && IsValidNodeName(remoteRepositoryNode) && string.Equals(GetHostName(remoteRepositoryNode, returnSameHNIfNotAvailable: true), defaultSRNodeName, StringComparison.CurrentCultureIgnoreCase)) { result = true; } } catch (Exception ex) { SvcTrace.DiagException.TraceEvent(TraceEventType.Information, 0, $"IsRemoteNodeSameasSRNode failed: {ex.Message}"); } return result; } public static bool IsValidateEndpointUri(string endpointAddr) { bool result = false; try { if (string.IsNullOrEmpty(endpointAddr)) { SvcTrace.DiagDiagnostics.TraceEvent(TraceEventType.Information, 0, "Endpoint address is empty or null"); } else if (Uri.IsWellFormedUriString(endpointAddr, UriKind.RelativeOrAbsolute)) { SvcTrace.DiagDiagnostics.TraceEvent(TraceEventType.Information, 0, $"The endpoint address {endpointAddr} of the discovery service is valid URI address."); result = true; } else { SvcTrace.DiagDiagnostics.TraceEvent(TraceEventType.Information, 0, $"Invalid endpoint address '{endpointAddr}'."); } } catch (Exception ex) { SvcTrace.DiagDiagnostics.TraceEvent(TraceEventType.Information, 0, ex.Message); } return result; } }