Exemple #1
0
        public override bool Equals(object obj)
        {
            HostEntry entry = obj as HostEntry;

            if (entry == null)
            {
                return(false);
            }

            return(string.Equals(this.Name, entry.Name) && string.Equals(this.Address, entry.Address));
        }
        public static bool TryCreate(string name, string ipAddress, out HostEntry entry)
        {
            IPAddress testAddress;
            if (!IPAddress.TryParse(ipAddress, out testAddress) || string.IsNullOrEmpty(name))
            {
                entry = null;
                return false;
            }

            entry = new HostEntry(name, ipAddress);
            return true;
        }
Exemple #3
0
        public static bool TryCreate(string name, string ipAddress, out HostEntry entry)
        {
            IPAddress testAddress;

            if (!IPAddress.TryParse(ipAddress, out testAddress) || string.IsNullOrEmpty(name))
            {
                entry = null;
                return(false);
            }

            entry = new HostEntry(name, ipAddress);
            return(true);
        }
        /// <summary>
        /// Loads the contents of an existing host file. Will clear
        /// the current configuration of this instance.
        /// </summary>
        /// <param name="path"></param>
        public void ReloadTimerEvent(object param)
        {
            try
            {
                FileInfo fileInfo = new FileInfo(this.filepath);
                if (!fileInfo.Exists)
                {
                    LinuxCommunicator.Instance?.Tracer?.TraceInfo("[HostsFileManager] The hosts file doesn't exists: {0}", this.filepath);
                    return;
                }

                if (fileInfo.LastWriteTimeUtc <= this.lastModified)
                {
                    LinuxCommunicator.Instance?.Tracer?.TraceInfo("[HostsFileManager] The hosts file isn't changed since last load");
                    return;
                }

                bool manageByHPC = false;
                Dictionary <string, HostEntry> newEntries = new Dictionary <string, HostEntry>();
                foreach (var line in File.ReadAllLines(this.filepath))
                {
                    Match commentMatch = HostsFileManager.Comment.Match(line);
                    if (commentMatch.Success)
                    {
                        Match commentParameterMatch = HostsFileManager.CommentParameter.Match(line);
                        if (commentParameterMatch.Success && string.Equals(commentParameterMatch.Groups["parameter"].ToString(), ManageFileParameter, StringComparison.OrdinalIgnoreCase))
                        {
                            if (string.Equals(commentParameterMatch.Groups["value"].Value, "true", StringComparison.OrdinalIgnoreCase))
                            {
                                manageByHPC = true;
                            }
                        }

                        continue;
                    }

                    Match     ipEntryMatch = HostsFileManager.IpEntry.Match(line);
                    HostEntry entry;

                    if (ipEntryMatch.Success &&
                        manageByHPC &&
                        ipEntryMatch.Groups["comment"].Value.Equals(HostsFileManager.ManagedEntryKey, StringComparison.OrdinalIgnoreCase) &&
                        HostEntry.TryCreate(ipEntryMatch.Groups["dnsName"].Value, ipEntryMatch.Groups["ip"].Value, out entry))
                    {
                        newEntries[entry.Name] = entry;
                    }
                }

                if (manageByHPC)
                {
                    if (newEntries.Count != this.ManagedEntries.Count || !(new HashSet <HostEntry>(this.ManagedEntries)).SetEquals(new HashSet <HostEntry>(newEntries.Values)))
                    {
                        // Put the <NetworkType>.<NodeName> entries at the end of the list
                        var newHostList = newEntries.Values.Where(entry => !entry.Name.Contains('.')).ToList();
                        newHostList.AddRange(newEntries.Values.Where(entry => entry.Name.Contains('.')));
                        this.ManagedEntries = newHostList;
                        this.UpdateId       = Guid.NewGuid();
                        LinuxCommunicator.Instance?.Tracer?.TraceInfo("[HostsFileManager] The managed host entries updated, current update Id is {0}", this.UpdateId);
                    }
                    else
                    {
                        LinuxCommunicator.Instance?.Tracer?.TraceInfo("[HostsFileManager] No update to HPC managed host entries, current update Id is {0}", this.UpdateId);
                    }
                }
                else
                {
                    LinuxCommunicator.Instance?.Tracer?.TraceWarning("[HostsFileManager] Hosts file was not managed by HPC");
                    this.ManagedEntries.Clear();
                }

                this.ManagedByHPC = manageByHPC;
                this.lastModified = fileInfo.LastWriteTimeUtc;
            }
            catch (Exception e)
            {
                LinuxCommunicator.Instance?.Tracer?.TraceWarning("[HostsFileManager] Failed to reload host file: {0}", e);
            }
            finally
            {
                try
                {
                    this.reloadTimer.Change(ReloadInterval, Timeout.Infinite);
                }
                catch (Exception te)
                {
                    LinuxCommunicator.Instance?.Tracer?.TraceWarning("[HostsFileManager] Failed to restart reload timer: {0}", te);
                }
            }
        }