Esempio n. 1
0
        public HostsManager()
        {
            InitializeComponent();

            this.Load += OnLoad;

            btnNotepad.Click  += OpenNotepad;
            btnDelete.Click   += DeleteLines;
            btnToggle.Click   += ToggleLines;
            muiElevated.Click += ElevatedOnClick;

            lstHosts.KeyPress             += HostsOnKeyPress;
            lstHosts.SelectedIndexChanged += HostsSelectedIndexChanged;

            if (!this.DesignMode)
            {
                //Load the Hosts file.
                _hostsFile = Hosts.ListHostsFile().Result;
            }

            if (_hostsFile != null && _hostsFile.LineCount > 0)
            {
                lstHosts.BeginUpdate();
                lstHosts.Items.Clear();
                lstHosts.Items.AddRange(_hostsFile.Lines.ToArray());
                lstHosts.EndUpdate();

                lblCaption.Text = string.Format("Hosts file is located at: {0} ({1} lines, {2})", _hostsFile.Location,
                                                _hostsFile.LineCount, _hostsFile.FormattedAttributes);
            }

            HostsSelectedIndexChanged(lstHosts, new EventArgs());
        }
Esempio n. 2
0
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/>
        private HostsFileReader CreateHostsFileReader(string includesFile, string excludesFile
                                                      )
        {
            HostsFileReader hostsReader = new HostsFileReader(includesFile, (includesFile ==
                                                                             null || includesFile.IsEmpty()) ? null : this.rmContext.GetConfigurationProvider
                                                                  ().GetConfigurationInputStream(this.conf, includesFile), excludesFile, (excludesFile
                                                                                                                                          == null || excludesFile.IsEmpty()) ? null : this.rmContext.GetConfigurationProvider
                                                                  ().GetConfigurationInputStream(this.conf, excludesFile));

            return(hostsReader);
        }
Esempio n. 3
0
        /*
         * Original Visual Basic (6.0) Code Block
         *
         * Public Sub HostsDeleteLine(objList As ListBox)
         *  'delete ith line in hosts file (zero-based)
         *  Dim iAttr%, sDummy$, vContent As Variant, i&
         *  On Error Resume Next
         *  iAttr = GetAttr(sHostsFile)
         *  If (iAttr And 2048) Then iAttr = iAttr - 2048
         *  SetAttr sHostsFile, vbArchive
         *  If Err Then
         *      MsgBox "The hosts file is locked for reading and cannot be edited. " & vbCrLf & _
         *             "Make sure you have privileges to modify the hosts file and " & _
         *             "no program is protecting it against changes.", vbCritical
         *      Exit Sub
         *  End If
         *
         *  Open sHostsFile For Binary As #1
         *      sDummy = Input(FileLen(sHostsFile), #1)
         *  Close #1
         *  vContent = Split(sDummy, vbCrLf)
         *  If UBound(vContent) = 0 And InStr(vContent(0), Chr(10)) > 0 Then
         *      'unix style hosts file
         *      vContent = Split(sDummy, Chr(10))
         *  End If
         *
         *  Open sHostsFile For Output As #1
         *      With objList
         *          For i = 0 To UBound(vContent) - 1
         *              If Not .Selected(i) Then Print #1, vContent(i)
         *          Next i
         *      End With
         *  Close #1
         * End Sub
         */
        #endregion

        #region HostsToggleLine
        public static void HostsToggleLine(ref HostsFileReader hostsFile, int[] indices, string fileName = null)
        {
            if (indices.Count() <= 0)
            {
                return;
            }

            if (string.IsNullOrEmpty(fileName))
            {
                fileName = GetHostsFile;
            }

            var writeLines = new List <string>();

            var i = 0;

            foreach (var line in hostsFile.Lines)
            {
                if (indices.Contains(i))
                {
                    writeLines.Add(line.StartsWith("#") ? line.Substring(1) : line.Insert(0, "#"));
                }
                else
                {
                    writeLines.Add(line);
                }
                i++;
            }

            try
            {
                using (var fileStream = new FileStream(fileName, File.Exists(fileName) ? FileMode.Truncate : FileMode.CreateNew, FileAccess.ReadWrite))
                    using (var writer = new StreamWriter(fileStream))
                    {
                        writer.Write(string.Join(hostsFile.FileType == HostsFileType.Unix ? "\n" : "\r\n", writeLines.ToArray()));
                    }

                hostsFile.Lines = writeLines.ToArray();
            }
            catch (Exception)
            {
                //
                throw;
            }
        }
Esempio n. 4
0
 /// <exception cref="System.IO.IOException"/>
 private static HostFileManager.HostSet ReadFile(string type, string filename)
 {
     HostFileManager.HostSet res = new HostFileManager.HostSet();
     if (!filename.IsEmpty())
     {
         HashSet <string> entrySet = new HashSet <string>();
         HostsFileReader.ReadFileToSet(type, filename, entrySet);
         foreach (string str in entrySet)
         {
             IPEndPoint addr = ParseEntry(type, filename, str);
             if (addr != null)
             {
                 res.Add(addr);
             }
         }
     }
     return(res);
 }
Esempio n. 5
0
        public virtual void SetUp()
        {
            InlineDispatcher rmDispatcher = new InlineDispatcher();

            rmContext = new RMContextImpl(rmDispatcher, null, null, null, Org.Mockito.Mockito.Mock
                                          <DelegationTokenRenewer>(), null, null, null, null, null);
            NodesListManager nodesListManager = Org.Mockito.Mockito.Mock <NodesListManager>();
            HostsFileReader  reader           = Org.Mockito.Mockito.Mock <HostsFileReader>();

            Org.Mockito.Mockito.When(nodesListManager.GetHostsReader()).ThenReturn(reader);
            ((RMContextImpl)rmContext).SetNodesListManager(nodesListManager);
            scheduler = Org.Mockito.Mockito.Mock <YarnScheduler>();
            Org.Mockito.Mockito.DoAnswer(new _Answer_115(this)).When(scheduler).Handle(Matchers.Any
                                                                                       <SchedulerEvent>());
            rmDispatcher.Register(typeof(SchedulerEventType), new TestRMNodeTransitions.TestSchedulerEventDispatcher
                                      (this));
            rmDispatcher.Register(typeof(NodesListManagerEventType), new TestRMNodeTransitions.TestNodeListManagerEventDispatcher
                                      (this));
            NodeId nodeId = BuilderUtils.NewNodeId("localhost", 0);

            node = new RMNodeImpl(nodeId, rmContext, null, 0, 0, null, null, null);
            nodesListManagerEvent = null;
        }
Esempio n. 6
0
 private void DisableHostsFileReader(Exception ex)
 {
     Log.Warn("Failed to init hostsReader, disabling", ex);
     try
     {
         this.includesFile = conf.Get(YarnConfiguration.DefaultRmNodesIncludeFilePath);
         this.excludesFile = conf.Get(YarnConfiguration.DefaultRmNodesExcludeFilePath);
         this.hostsReader  = CreateHostsFileReader(this.includesFile, this.excludesFile);
         SetDecomissionedNMsMetrics();
     }
     catch (IOException ioe2)
     {
         // Should *never* happen
         this.hostsReader = null;
         throw new YarnRuntimeException(ioe2);
     }
     catch (YarnException e)
     {
         // Should *never* happen
         this.hostsReader = null;
         throw new YarnRuntimeException(e);
     }
 }
Esempio n. 7
0
 /// <exception cref="System.Exception"/>
 protected override void ServiceInit(Configuration conf)
 {
     this.conf = conf;
     // Read the hosts/exclude files to restrict access to the RM
     try
     {
         this.includesFile = conf.Get(YarnConfiguration.RmNodesIncludeFilePath, YarnConfiguration
                                      .DefaultRmNodesIncludeFilePath);
         this.excludesFile = conf.Get(YarnConfiguration.RmNodesExcludeFilePath, YarnConfiguration
                                      .DefaultRmNodesExcludeFilePath);
         this.hostsReader = CreateHostsFileReader(this.includesFile, this.excludesFile);
         SetDecomissionedNMsMetrics();
         PrintConfiguredHosts();
     }
     catch (YarnException ex)
     {
         DisableHostsFileReader(ex);
     }
     catch (IOException ioe)
     {
         DisableHostsFileReader(ioe);
     }
     base.ServiceInit(conf);
 }
Esempio n. 8
0
        public async Task <ICollection <IDetectionResultItem> > Scan()
        {
            var result = new Collection <IDetectionResultItem>();

            var sHostsFile = ReviveThis.Entities.Modules.Hosts.GetHostsFile;

            if (string.IsNullOrEmpty(sHostsFile) || !File.Exists(sHostsFile))
            {
                return(null);
            }

            HostsFileReader readResult = null;

            try
            {
                readResult = await ReviveThis.Entities.Modules.Hosts.ReadHostsFile(sHostsFile);
            }
            catch (FileNotFoundException)
            {
                result.Add(new HostsResult(HostsFileResultType.NotFound));
            }

            if (readResult != null)
            {
                if (
                    !readResult.Location.Equals(ReviveThis.Entities.Modules.Hosts.GetHostsFileDefault,
                                                StringComparison.InvariantCultureIgnoreCase))
                {
                    //TODO: Implement IgnoreList
                    result.Add(new HostsResult(HostsFileResultType.InvalidLocation, readResult.Location));

                    //sHit = "O1 - Hosts file is located at: " & sHostsFile
                    //If Not IsOnIgnoreList(sHit) Then frmMain.lstResults.AddItem sHit
                }

                //Legacy file format?
                if (readResult.FileType != HostsFileType.Default)
                {
                    result.Add(new HostsResult(HostsFileResultType.InvalidFormat));
                }

                //Remove all commented/empty lines (they aren't needed).
                var lines =
                    readResult.Lines.Where(
                        w =>
                        !string.IsNullOrEmpty(w.Trim()) && !w.Trim().StartsWith("#", StringComparison.InvariantCultureIgnoreCase))
                    .Select(s => s.Trim())
                    .ToList();
                if (lines.Any())
                {
                    //TODO: Future use, get all local IP addresses for white-listing
                    //foreach (var ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList.Where(ip => ip.AddressFamily == AddressFamily.InterNetwork || ip.AddressFamily == AddressFamily.InterNetworkV6))
                    //{
                    //}

                    //Remove anything for loopback addresses (IPv4 = "127.0.0.1" & IPv6 = "::1")
                    //TODO: Implement IgnoreList
                    var hiJackList =
                        lines.Where(
#if DEBUG
                            w => true
#else
                            w => !w.StartsWith(IPAddress.Loopback.ToString()) && !w.StartsWith(IPAddress.IPv6Loopback.ToString())
#endif
                            ).ToList();

                    foreach (var line in hiJackList)
                    {
                        result.Add(new HostsResult(HostsFileResultType.LineHiJack, line));
                    }
                }
            }

            return(result);
        }