예제 #1
0
        /// <summary>
        /// Attach the OnExit event handler, to do some clean up before exiting
        /// </summary>
        public static void AttachOnExitEventHandler(IView view)
        {
            Application.ApplicationExit += (object sender, EventArgs e) =>
            {
                //Terminate the Blocker/Redirector
                Blocker_Redirector.CLoseBR();

                //Terminate the scanner
                Scanner.CloseAllCaptures(view);

                //Clean all notifications
                ToastAPI.ClearNotificationHistory();

                //Toast notifications service cleanup
                ToastAPI.DestroyAPI();
            };
        }
예제 #2
0
        public async Task <bool> SubItemCheckingHandler(CheckState CurrentValue, CheckState NewValue, int ColumnIndex, object RowObject)
        {
            try
            {
                //Don't allow blocking / redirection while the sniffer is active or any other operation.
                if (OperationIsInProgress)
                {
                    MetroMessageBox.Show(this, "The Speed Limiter can't be used while the sniffer is active!", "Error", MessageBoxButtons.OK,
                                         MessageBoxIcon.Error);

                    return(false);
                }

                //Get the device in the selected row
                DeviceList.SelectObject(RowObject);
                Device device = RowObject as Device;

                if (device.IsGateway || device.IsLocalDevice)
                {
                    MetroMessageBox.Show(this, "This operation can not target the gateway or your own ip address!", "Error", MessageBoxButtons.OK,
                                         MessageBoxIcon.Error);

                    return(false);
                }

                if (NewValue == CheckState.Checked && ColumnIndex == 6 && !device.Blocked && !device.Redirected)
                {
                    //Update device state in list
                    var listDevice = Devices.FirstOrDefault(D => D.Value.MAC == device.MAC);
                    if (listDevice.Equals(default(KeyValuePair <IPAddress, Device>)))
                    {
                        throw new CustomExceptions.DeviceNotInListException("Device was not found in the list of targeted devices.");
                    }

                    listDevice.Value.Blocked = true;

                    //Update device state in UI
                    device.Blocked      = true;
                    device.DeviceStatus = "Offline";
                    DeviceList.UpdateObject(device);
                    pictureBox1.Image = NetStalker.Properties.Resources.icons8_ok_red;

                    //Activate the BR if it's not already active
                    if (!Blocker_Redirector.BRMainSwitch)
                    {
                        if (Blocker_Redirector.BRTask != null && Blocker_Redirector.BRTask.Status == TaskStatus.Running)
                        {
                            Blocker_Redirector.BRTask.Wait();
                        }

                        Blocker_Redirector.BRMainSwitch = true;
                        Blocker_Redirector.BlockAndRedirect();
                    }
                }
                else if (NewValue == CheckState.Checked && ColumnIndex == 5 && !device.Blocked && !device.Redirected)
                {
                    //Update device state in list
                    var listDevice = Devices.FirstOrDefault(D => D.Value.MAC == device.MAC);
                    if (listDevice.Equals(default(KeyValuePair <IPAddress, Device>)))
                    {
                        throw new CustomExceptions.DeviceNotInListException("Device was not found in the list of targeted devices.");
                    }

                    listDevice.Value.Blocked    = true;
                    listDevice.Value.Redirected = true;

                    //Update device state in UI
                    device.Blocked     = true;
                    device.Redirected  = true;
                    device.DownloadCap = 0;
                    device.UploadCap   = 0;
                    DeviceList.UpdateObject(device);
                    pictureBox1.Image = NetStalker.Properties.Resources.icons8_ok_red;

                    //Activate the BR if it's not already active
                    if (!Blocker_Redirector.BRMainSwitch)
                    {
                        if (Blocker_Redirector.BRTask != null && Blocker_Redirector.BRTask.Status == TaskStatus.Running)
                        {
                            Blocker_Redirector.BRTask.Wait();
                        }

                        Blocker_Redirector.BRMainSwitch = true;
                        Blocker_Redirector.BlockAndRedirect();
                    }

                    //Start value counter if it's not already started
                    if (!ValuesTimer.Enabled)
                    {
                        ValuesTimer.Enabled = true;
                    }
                }
                else if (NewValue == CheckState.Unchecked && ColumnIndex == 6 && device.Blocked && !device.Redirected)
                {
                    //Update device state in list
                    var listDevice = Devices.FirstOrDefault(D => D.Value.MAC == device.MAC);
                    if (listDevice.Equals(default(KeyValuePair <IPAddress, Device>)))
                    {
                        throw new CustomExceptions.DeviceNotInListException("Device was not found in the list of targeted devices.");
                    }

                    listDevice.Value.Blocked = false;

                    //Update device state in UI
                    device.Blocked      = false;
                    device.DeviceStatus = "Online";
                    DeviceList.UpdateObject(device);

                    await Scanner.RestoreDevice(device);

                    //Checks if there are any devices left with active targeting
                    if (!Devices.Any(D => D.Value.Blocked == true))
                    {
                        pictureBox1.Image = NetStalker.Properties.Resources.color_ok;
                        Blocker_Redirector.BRMainSwitch = false;
                    }
                }
                else if (NewValue == CheckState.Unchecked && ColumnIndex == 5 && device.Redirected)
                {
                    //Update device state in list
                    var listDevice = Devices.FirstOrDefault(D => D.Value.MAC == device.MAC);
                    if (listDevice.Equals(default(KeyValuePair <IPAddress, Device>)))
                    {
                        throw new CustomExceptions.DeviceNotInListException("Device was not found in the list of targeted devices.");
                    }

                    listDevice.Value.Blocked     = false;
                    listDevice.Value.Redirected  = false;
                    listDevice.Value.Limited     = false;
                    listDevice.Value.DownloadCap = 0;
                    listDevice.Value.UploadCap   = 0;

                    device.Blocked       = false;
                    device.Redirected    = false;
                    device.Limited       = false;
                    device.DownloadCap   = 0;
                    device.UploadCap     = 0;
                    device.DownloadSpeed = "";
                    device.UploadSpeed   = "";
                    DeviceList.UpdateObject(device);

                    await Scanner.RestoreDevice(device);

                    //Checks if there are any devices left with the Redirected switch
                    if (!Devices.Any(D => D.Value.Redirected == true))
                    {
                        pictureBox1.Image = NetStalker.Properties.Resources.color_ok;
                        Blocker_Redirector.BRMainSwitch = false;
                        ValuesTimer.Enabled             = false;
                        ValuesTimer.Stop();
                    }
                }
                else
                {
                    //The user action didn't hit any of our conditions so we cancel it and reset the value
                    return(false);
                }
            }
            catch (CustomExceptions.DeviceNotInListException)
            {
                MetroMessageBox.Show(this, "The selected device was not found in the list or targets", "Error", MessageBoxButtons.OK,
                                     MessageBoxIcon.Error);

                return(false);
            }

            return(true);
        }