예제 #1
0
        private void Bgw1_DoWork(object sender, DoWorkEventArgs e)
        {
            this.ResultRichTextBox.Clear();
            ScanForm form = (ScanForm)e.Argument;

            long totalCount = form.ip_list.Count * form.port_list.Count;
            long count      = 0;

            Console.WriteLine("totalCount: {0}", totalCount);
            for (var ipIndex = 0; ipIndex < form.ip_list.Count; ipIndex++)
            {
                string ip = form.ip_list.ElementAt(ipIndex);
                for (var portIndex = 0; portIndex < form.port_list.Count; portIndex++)
                {
                    int port = form.port_list.ElementAt(portIndex);

                    // 判断是否可通
                    bool can_connect = Utils.CanConnect(ip, port, form.timeout);

                    if (can_connect)
                    {
                        // 写到结果框中
                        this.ResultRichTextBox.AppendText(ip + ":" + port + "\n");
                    }

                    // 获取比率更新进度条  感觉想复杂了
                    decimal A = count;
                    decimal B = totalCount;

                    decimal T = decimal.Parse((A / B).ToString("0.00"));

                    BackgroundWorker tmp = ((BackgroundWorker)sender);
                    tmp.ReportProgress(Convert.ToInt32(T * 100));

                    Console.WriteLine("percentage: {0}, CancellationPending: {1}", Convert.ToInt32(T * 100), tmp.CancellationPending);
                    this.ProcessLabel.Text = "当前:" + ip + ":" + port + "\n" + "总计: " + count + "/" + totalCount;
                    if (tmp.CancellationPending)
                    {
                        Console.WriteLine("按了停止按钮");
                        tmp.ReportProgress(0);
                        this.ProcessLabel.Text = " ";

                        Change_Search_Btn_Text("扫描", true);
                        return;
                    }

                    count++;
                }
            }
            // 循环完成
            ((BackgroundWorker)sender).ReportProgress(100);
            this.ProcessLabel.Text = "";

            Thread.Sleep(300);
            Change_Search_Btn_Text("扫描", true);
        }
예제 #2
0
        private void ScanBtn_Click(object sender, EventArgs e)
        {
            //testIpReg();
            // ip地址要求为ipv4地址
            // 起始ip是否合法
            bool IpStartValid = Utils.CheckIp(this.IpStartTextBox.Text);

            if (!IpStartValid)
            {
                Utils.Alert_Tips("起始IP不合法");
                this.IpStartTextBox.Focus();
                return;
            }
            // 结束ip是否合法
            bool IpEndValid = Utils.CheckIp(this.IpEndTextBox.Text);

            if (!IpEndValid)
            {
                Utils.Alert_Tips("结束IP不合法");
                this.IpEndTextBox.Focus();
                return;
            }
            if (!Check_Port(this.PortTextBox.Text))
            {
                Utils.Alert_Tips("端口号不合法");
                this.PortTextBox.Focus();
                return;
            }

            if (scanFlag)
            {
                this.ResultRichTextBox.Clear();
                ScanForm form      = new ScanForm();
                string   check_res = Prepare_Data(form);
                if (check_res != null)
                {
                    Utils.Alert_Tips(check_res);
                    return;
                }
                // 开始前 置为停止
                Change_Search_Btn_Text("停止", false);
                this.Start_Scan(form);
            }
            else
            {
                this.End_Scan();
                // 结束后 置为扫描
                Change_Search_Btn_Text("扫描", true);
            }
        }
예제 #3
0
        private string Get_Ip_List(ScanForm form)
        {
            List <string> ip_list   = new List <string>();
            string        startText = this.IpStartTextBox.Text;
            string        endText   = this.IpEndTextBox.Text;

            uint ustart = Utils.Ip2UInt(startText);
            uint uend   = Utils.Ip2UInt(endText);

            for (var tmp_ip = ustart; tmp_ip <= uend; tmp_ip++)
            {
                ip_list.Add(Utils.UIntToIP(tmp_ip));
            }

            form.ip_list = ip_list;
            return(null);
        }
예제 #4
0
        private string Prepare_Data(ScanForm form)
        {
            form.ip_start = this.IpStartTextBox.Text;
            form.ip_end   = this.IpEndTextBox.Text;

            string ip_res   = Get_Ip_List(form);
            string port_res = Get_Port_List(form);

            if (port_res != null)
            {
                return(port_res);
            }

            form.timeout     = int.Parse(this.TimeoutTextBox.Text);
            form.thread_nums = int.Parse(this.ThreadTextBox.Text);
            return(null);
        }
예제 #5
0
 private void Start_Scan(ScanForm scanForm)
 {
     this.backgroundWorker1.RunWorkerAsync(scanForm);
 }
예제 #6
0
        private string Get_Port_List(ScanForm form)
        {
            // 22,80,8080-8088
            List <int> port_list = new List <int>();

            string portText = this.PortTextBox.Text;

            // 检查portText的合法性
            char[] char_arr = portText.ToCharArray();
            // 上个字符是否为符号
            bool lastCharIsSymbol = false;

            for (var index = 0; index < char_arr.Length; index++)
            {
                char _char = char_arr[index];
                // 当前字符是否为符号
                bool isSymbol = !Char.IsDigit(_char);
                if (lastCharIsSymbol && isSymbol)
                {
                    return("端口号不合法");
                }
                else if (isSymbol)
                {
                    // 是符号 置true
                    lastCharIsSymbol = true;
                }
                else
                {
                    // 不是符号 置false
                    lastCharIsSymbol = false;
                }
            }

            string[] port_split_first = portText.Split(',');

            for (var i = 0; i < port_split_first.Length; i++)
            {
                string tmp_str = port_split_first[i];
                // 不包含 - 时直接添加跳过
                if (!tmp_str.Contains("-"))
                {
                    int tmp_port = int.Parse(tmp_str);
                    if (!Utils.CheckPort(tmp_port))
                    {
                        return("端口号不合法");
                    }
                    port_list.Add(tmp_port);
                    continue;
                }

                string[] port_split_twice = tmp_str.Split('-');
                int      t1 = int.Parse(port_split_twice[0]);
                int      t2 = int.Parse(port_split_twice[1]);
                if (!Utils.CheckPort(t1) || !Utils.CheckPort(t2) || t1 > t2)
                {
                    return("端口号不合法");
                }
                // 按照-分隔的要获取它中间的所有端口
                for (var j = t1; j <= t2; j++)
                {
                    // 添加端口
                    port_list.Add(j);
                }
            }
            //port_list.ForEach(item =>
            //{
            //    this.ResultRichTextBox.AppendText(item + "\n");
            //});
            form.port_list = port_list;
            return(null);
        }