Ejemplo n.º 1
0
Archivo: Form1.cs Proyecto: berak/cs
        private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {
            scanIps = (ScanIpData)propertyGrid2.SelectedObject;
            string start = scanIps.Start;
            string stop = scanIps.Stop;
            int port = scanIps.Port;
            prefs = (PrefsData)propertyGrid3.SelectedObject;
            int timeout = prefs.Timeout;
            if (prefs.DoLog)
            {
                try
                {
                    logFile = new System.IO.StreamWriter("pscan_"+start+"_"+stop+".html");
                }
                catch (Exception) { }
            }

            PortScanner[] scanner = new PortScanner[16];
            ManualResetEvent[] reset = new ManualResetEvent[16];
            for (int i = 0; i < 16; i++)
            {
                scanner[i] = new PortScanner("", 0);
                scanner[i].timeout = timeout;
            }

            MethodInvoker ScanStart = delegate
            {
                scannedHosts.Items.Clear();
                progressBar2.Maximum = toInt(stop) - toInt(start);
                progressBar2.Value = 0;
            };
            Invoke(ScanStart);

            while (startIpScan.Text == "Stop")
            {
                for (int i = 0; i < 16; i++)
                {
                    start = incIp(start);
                    scanner[i].SERVER = start;
                    scanner[i].PORT = port;
                    scanner[i].ping1 = ping1;
                    reset[i] = new ManualResetEvent(false);
                    ThreadPool.QueueUserWorkItem(scanner[i].Run, reset[i]);
                }
                WaitHandle.WaitAll(reset, 5000);
                MethodInvoker ScanUpdate = delegate
                {
                    for (int i = 0; i < 16; i++)
                    {
                        if (scanner[i].result > 0)
                        {
                            scannedHosts.Items.Add(scanner[i].SERVER);
                            if (prefs.autoBrowse)
                            {
                                browse(scanner[i].SERVER, port);
                            }
                            if (logFile != null)
                            {
                                logFile.WriteLine("<a href=\"http://"+scanner[i].SERVER+"\">"+scanner[i].SERVER+"</a><br>\n");
                                logFile.Flush();
                            }
                        }
                        progressBar2.Increment(1);
                    };
                    scanIps.Start = start;
                    propertyGrid2.SelectedObject = scanIps;
                };
                Invoke(ScanUpdate);
                if (toInt(start) >= toInt(stop))
                    break;
            }
            if (logFile != null)
            {
                logFile.Close();
            }
            startIpScan.Text = "Start";
        }
Ejemplo n.º 2
0
Archivo: Form1.cs Proyecto: berak/cs
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            scanPorts = (ScanPortData)propertyGrid1.SelectedObject;
            prefs  = (PrefsData)propertyGrid3.SelectedObject;
            int timeout = prefs.Timeout;

            MethodInvoker ScanStart = delegate
            {
                scannedPorts.Items.Clear();
                progressBar1.Maximum = scanPorts.To - scanPorts.From;
                progressBar1.Value = 0;
            };
            Invoke(ScanStart);

            PortScanner[] scanner = new PortScanner[16];
            ManualResetEvent[] reset = new ManualResetEvent[16];
            for (int i = 0; i < 16; i++)
            {
                scanner[i] = new PortScanner(scanPorts.Ip, 0);
                scanner[i].timeout = prefs.timeout;
            }

            while (scanPorts.From < scanPorts.To && ((buttonStart.Text == "Stop")))
            {
                for (int i = 0; i < 16; i++)
                {
                    scanner[i].PORT = scanPorts.From++;
                    reset[i] = new ManualResetEvent(false);
                    ThreadPool.QueueUserWorkItem(scanner[i].Run, reset[i]);
                }
                WaitHandle.WaitAll(reset, timeout);
                for (int i = 0; i < 16; i++)
                {
                    bool hit = (scanner[i].result > 0);
                    MethodInvoker ScanUpdate = delegate
                    {
                        int p = scanner[i].PORT;
                        progressBar1.Increment(1);
                        status.Text = p + "    " + ports[p];
                        scanPorts.From = p;
                        propertyGrid1.SelectedObject = scanPorts;
                        string name = p + "\t" + ports[p];
                        if ((hit) && (!scannedPorts.Items.Contains(name)))
                            scannedPorts.Items.Add(name);
                    };
                    Invoke(ScanUpdate);
                }
            }
            buttonStart.Text = "Start";
            //int ntw = 0;
            //int ncp = 0;
            //ThreadPool.GetAvailableThreads(out ntw, out ncp);
            //Thread.Sleep(600);
        }