Пример #1
0
        private void ExecutePing()
        {
            lProgress.Invoke((MethodInvoker) delegate {
                lProgress.Text = "Initializing ping scan...";
            });
            pBar.Invoke((MethodInvoker) delegate {
                pBar.Maximum = 0;
                if ((PingType.Loopback & currentType) == PingType.Loopback)
                {
                    pBar.Maximum += LOOPBACK_PING_AMOUNT;
                }
                if ((PingType.Localhost & currentType) == PingType.Localhost)
                {
                    pBar.Maximum += LOCALHOST_PING_AMOUNT;
                }
                if ((PingType.Dns & currentType) == PingType.Dns)
                {
                    pBar.Maximum += byte.Parse(Settings.GetSetting("DNS_COUNT")) * DNS_PING_AMOUNT;
                }
                if ((PingType.Targets & currentType) == PingType.Targets)
                {
                    pBar.Maximum += FormSession.TARGETS.Count * TARGET_PING_AMOUNT;
                }
            });

            token.ThrowIfCancellationRequested();


            if (!tokenSource.IsCancellationRequested)
            {
                lProgress.Invoke((MethodInvoker) delegate {
                    lProgress.Text = $"Pinging {currentIretation}/{pBar.Maximum}...";
                });
            }


            if ((PingType.Loopback & currentType) == PingType.Loopback)
            {
                uint currentGood = 0, currentBad = 0;
                for (int i = 1; i <= LOOPBACK_PING_AMOUNT; i++)
                {
                    PingReply reply = GetPingReply(IPAddress.Loopback);
                    if (reply.Status == IPStatus.Success)
                    {
                        pingGood++;
                        currentGood++;
                    }
                    else
                    {
                        pingBad++;
                        currentBad++;
                    }
                    if (!tokenSource.IsCancellationRequested)
                    {
                        lbInfo.Invoke((MethodInvoker) delegate {
                            lbInfo.Items.Add($"Loopback ping {i}/{LOOPBACK_PING_AMOUNT} : {reply.Status.ToString()} \t| Round Trip Time : {reply.RoundtripTime}ms");
                        });
                    }

                    if (i != LOOPBACK_PING_AMOUNT)
                    {
                        NextAction();
                    }
                }
                if (!tokenSource.IsCancellationRequested)
                {
                    LogHandler.AddLog($"Loopback Address ping(s) completed. Successful pings : {currentGood}, Unsuccessful pings : {currentBad}");
                }
            }
            if ((PingType.Localhost & currentType) == PingType.Localhost)
            {
                uint currentGood = 0, currentBad = 0;
                for (int i = 1; i <= LOCALHOST_PING_AMOUNT; i++)
                {
                    PingReply reply = GetPingReply(StaticUtilities.GetLocalIPv4Address());
                    if (reply.Status == IPStatus.Success)
                    {
                        pingGood++;
                        currentGood++;
                    }
                    else
                    {
                        pingBad++;
                        currentBad++;
                    }
                    if (!tokenSource.IsCancellationRequested)
                    {
                        lbInfo.Invoke((MethodInvoker) delegate {
                            lbInfo.Items.Add($"Localhost ping {i}/{LOCALHOST_PING_AMOUNT} : {reply.Status.ToString()} \t| Round Trip Time : {reply.RoundtripTime}ms");
                        });
                    }
                    if (i != LOCALHOST_PING_AMOUNT)
                    {
                        NextAction();
                    }
                }
                if (!tokenSource.IsCancellationRequested)
                {
                    LogHandler.AddLog($"Localhost Address ping(s) completed. Successful pings : {currentGood}, Unsuccessful pings : {currentBad}");
                }
            }
            if ((PingType.Dns & currentType) == PingType.Dns)
            {
                uint currentGood = 0, currentBad = 0;
                foreach (var dns in Settings.GetSettingsDnsAddresses())
                {
                    int i = 1;
                    for (; i <= DNS_PING_AMOUNT; i++)
                    {
                        PingReply reply = GetPingReply(dns);
                        if (reply.Status == IPStatus.Success)
                        {
                            pingGood++;
                            currentGood++;
                        }
                        else
                        {
                            pingBad++;
                            currentBad++;
                        }
                        if (!tokenSource.IsCancellationRequested)
                        {
                            lbInfo.Invoke((MethodInvoker) delegate {
                                lbInfo.Items.Add($"DNS ping ({dns}) {i}/{DNS_PING_AMOUNT} : {reply.Status.ToString()} \t| Round Trip Time : {reply.RoundtripTime}ms");
                            });
                        }
                        if (i != DNS_PING_AMOUNT)
                        {
                            NextAction();
                        }
                    }
                    if (i != DNS_PING_AMOUNT)
                    {
                        NextAction();
                    }
                }
                if (!tokenSource.IsCancellationRequested)
                {
                    LogHandler.AddLog($"DNS Addresses ping(s) completed. Successful pings : {currentGood}, Unsuccessful pings : {currentBad}");
                }
            }
            if ((PingType.Targets & currentType) == PingType.Targets)
            {
                uint currentGood = 0, currentBad = 0;
                foreach (var target in FormSession.TARGETS)
                {
                    if (tokenSource.IsCancellationRequested)
                    {
                        break;
                    }
                    int i = 1;
                    for (; i <= TARGET_PING_AMOUNT; i++)
                    {
                        if (tokenSource.IsCancellationRequested)
                        {
                            break;
                        }

                        PingReply reply = GetPingReply(target);
                        if (reply.Status == IPStatus.Success)
                        {
                            pingGood++;
                            currentGood++;
                        }
                        else
                        {
                            pingBad++;
                            currentBad++;
                        }
                        if (!tokenSource.IsCancellationRequested)
                        {
                            lbInfo.Invoke((MethodInvoker) delegate {
                                lbInfo.Items.Add($"Target ping ({target})\t{i}/{TARGET_PING_AMOUNT} : {reply.Status.ToString()} \t| Round Trip Time : {reply.RoundtripTime}ms");
                            });
                        }
                        if (i != TARGET_PING_AMOUNT)
                        {
                            NextAction();
                        }
                    }
                    if (i != TARGET_PING_AMOUNT)
                    {
                        NextAction();
                    }
                }

                if (!tokenSource.IsCancellationRequested)
                {
                    LogHandler.AddLog($"Target Addresses ping(s) completed. Successful pings : {currentGood}, Unsuccessful pings : {currentBad}");
                }
            }

            if (!tokenSource.IsCancellationRequested)
            {
                lbInfo.Invoke((MethodInvoker) delegate {
                    lProgress.Text   = "Pings completed";
                    btnStart.Enabled = false;
                    btnExit.Text     = "Exit Test";
                });
                completed             = true;
                FormSession.TEST_DATA = GenerateData();
            }
        }