Beispiel #1
0
        /// <summary>
        /// Execute scan
        /// </summary>
        /// <param name="project">Upload project folder</param>
        /// <param name="scanData"></param>
        /// <param name="scanId"></param>
        /// <returns></returns>
        private ProjectScanStatuses ExecuteScan(Project project, ref CxWSQueryVulnerabilityData[] scanData, ref long scanId)
        {
            Logger.Create().Debug("DoScan in");
            bool bCancel        = false;
            bool backgroundMode = _scan.LoginResult.AuthenticationData.IsRunScanInBackground == SimpleDecision.Yes;

            if (_dispatcher == null)
            {
                _dispatcher = ServiceLocators.ServiceLocator.GetDispatcher();
            }

            if (_dispatcher != null)
            {
                IScanView view    = null;
                var       waitEnd = new ManualResetEvent(false);

                //if was selected "always run in background" checkbox - hide dialog
                if (!backgroundMode)
                {
                    ICommandResult commandResult = _dispatcher.Dispatch(_scan);
                    view = ((ScanPresenter)commandResult).View;
                }

                _scan.ScanView = view;

                BackgroundWorkerHelper bg = new BackgroundWorkerHelper(_scan.LoginResult.AuthenticationData.ReconnectInterval * 1000, _scan.LoginResult.AuthenticationData.ReconnectCount);

                CxWebServiceClient client = new CxWebServiceClient(_scan.LoginResult.AuthenticationData);
                client.ServiceClient.Timeout = 1800000;

                bool isIISStoped    = false;
                bool isScanningEror = false;

                //User click cancel while info dialog was showed
                if (!bCancel)
                {
                    ShowScanProgressBar();

                    ConfigurationResult configuration = _configurationHelper.GetConfigurationList(_scan.LoginResult.SessionId, bg, client);

                    if (configuration == null)
                    {
                        _cancelPressed = true;
                    }

                    if (!configuration.IsSuccesfull)
                    {
                        LoginHelper.DoLogout();
                        if (client != null)
                        {
                            client.Close();
                        }
                        if (view != null)
                        {
                            view.CloseView();
                        }

                        _scan.InProcess = false;
                        return(ProjectScanStatuses.CanceledByUser);
                    }

                    //User click cancel while info dialog was showed
                    if (!bCancel)
                    {
                        byte[] zippedProject = ZipProject(_scan, project, bg);

                        if (!_scan.IsCancelPressed && zippedProject != null)
                        {
                            if (configuration.Configurations.Count > 0)
                            {
                                RunScanResult runScanResult = null;

                                if (!CommonData.IsProjectBound)
                                {
                                    if (_uploadSettings.IsPublic)
                                    {
                                        _scan.IsPublic = SetScanPrivacy();
                                    }

                                    runScanResult = RunScan(bg, client, configuration, zippedProject);
                                }
                                else
                                {
                                    if (_scan.UploadSettings.IsPublic)
                                    {
                                        _scan.IsPublic = SetScanPrivacy();
                                    }

                                    runScanResult = RunBoundedProjectScan(_scan, bg, client, zippedProject);
                                }

                                if (runScanResult == null || !runScanResult.IsSuccesfull)
                                {
                                    bCancel        = true;
                                    isIISStoped    = true;
                                    isScanningEror = true;
                                }

                                // Continue if project uploaded succesfull and cancel button while process wasn't pressed
                                if (runScanResult != null && runScanResult.IsSuccesfull)
                                {
                                    _scan.RunScanResult = runScanResult;

                                    //perform scan work in separated thread to improve UI responsibility
                                    System.Threading.ThreadPool.QueueUserWorkItem(delegate(object stateInfo)
                                    {
                                        try
                                        {
                                            // Wait while scan operation complete
                                            while (true)
                                            {
                                                StatusScanResult statusScan = UpdateScanStatus(ref bCancel, backgroundMode, view, bg, client, ref isIISStoped);

                                                // if scan complete with sucess or failure or cancel button was pressed
                                                // operation complete
                                                bCancel = bCancel ? bCancel : _scan.WaitForCancel();

                                                if (isIISStoped || bCancel ||
                                                    (statusScan != null && statusScan.RunStatus == CurrentStatusEnum.Finished) ||
                                                    (statusScan != null && statusScan.RunStatus == CurrentStatusEnum.Failed))
                                                {
                                                    break;
                                                }
                                            }

                                            waitEnd.Set();
                                        }
                                        catch (Exception err)
                                        {
                                            Logger.Create().Error(err.ToString());
                                            // show error
                                            waitEnd.Set();
                                            isIISStoped = true;
                                            Logger.Create().Debug(err);
                                        }

                                        if (_scan.ScanView == null || _scan.ScanView.Visibility == false)
                                        {
                                            var scanStatusBar = new ScanStatusBar(false, "", 0, 0, true);

                                            CommonActionsInstance.getInstance().UpdateScanProgress(scanStatusBar);

                                            //ObserversManager.Instance.Publish(typeof (ScanStatusBar), scanStatusBar);
                                        }
                                    });

                                    while (!waitEnd.WaitOne(0, false))
                                    {
                                        Application.DoEvents();
                                        Thread.Sleep(10);
                                    }
                                }
                            }

                            #region [Scan completed. Open perspective]

                            if (!bCancel && !isIISStoped)
                            {
                                ShowScanData(ref scanData, ref scanId, client);
                            }
                            else
                            {
                                #region [Stop scan in cancel pressed]
                                if (_scan.RunScanResult != null && !isIISStoped)
                                {
                                    bg.DoWorkFunc = delegate
                                    {
                                        if (!isIISStoped)
                                        {
                                            client.ServiceClient.CancelScan(_scan.LoginResult.SessionId, _scan.RunScanResult.ScanId);
                                        }
                                    };
                                    bg.DoWork("Stop scan...");
                                }
                                #endregion
                            }

                            #endregion

                            client.Close();
                        }
                        else
                        {
                            client.Close();
                            bCancel = true;
                        }
                    }
                    else
                    {
                    }
                }
                else
                {
                }
                if (!backgroundMode && view != null)
                {
                    view.CloseView();
                }

                if (isIISStoped)
                {
                    if (isScanningEror)
                    {
                        return(ProjectScanStatuses.Error);
                    }
                    else
                    {
                        return(ProjectScanStatuses.CanceledByUser);
                    }
                }

                if (!bCancel)
                {
                    return(ProjectScanStatuses.Success);
                }
                else
                {
                    if (isScanningEror)
                    {
                        return(ProjectScanStatuses.Error);
                    }
                    else
                    {
                        return(ProjectScanStatuses.CanceledByUser);
                    }
                }
            }

            return(ProjectScanStatuses.CanceledByUser);
        }
        /// <summary>
        /// Set upload params
        /// </summary>
        /// <param name="loginResult">Auth user data for uploading</param>
        /// <param name="project">Selected solution project data</param>
        /// <returns></returns>
        internal static Upload SetUploadSettings(LoginResult loginResult, Project project, bool cancelStatus)
        {
            cancelStatus = false;
            PresetResult presetResult = null;
            TeamResult   teamResult   = null;

            BackgroundWorkerHelper bg = new BackgroundWorkerHelper(delegate(object obj)
            {
                _client = InitCxClient(loginResult);
                if (_client == null)
                {
                    return;
                }

                presetResult = GetPresets(loginResult, presetResult);

                CxWSResponseGroupList teamXmlList = _client.ServiceClient.GetAssociatedGroupsList(loginResult.SessionId);

                teamResult = new TeamResult();
                teamResult.IsSuccesfull = teamXmlList.IsSuccesfull;
                teamResult.Teams        = new Dictionary <string, string>();
                if (teamXmlList.GroupList != null && teamXmlList.GroupList.Length > 0)
                {
                    for (int i = 0; i < teamXmlList.GroupList.Length; i++)
                    {
                        teamResult.Teams.Add(teamXmlList.GroupList[i].ID, teamXmlList.GroupList[i].GroupName);
                    }
                }

                if (_client != null)
                {
                    _client.Close();
                }
            }, loginResult.AuthenticationData.ReconnectInterval * 1000, loginResult.AuthenticationData.ReconnectCount);

            //Show wait dialog and perform server request in different thread to safe UI responsibility
            if (!bg.DoWork(PRESETS_LOADING_TEXT))
            {
                cancelStatus = true;
                return(null);
            }

            if (!presetResult.IsSuccesfull)
            {
                return(null);
            }

            var uploadData = new Upload(new EntityId(loginResult), project.ProjectName,
                                        string.Format("{0} Description", project.ProjectName), presetResult.Presets, 0,
                                        teamResult.Teams, Guid.Empty.ToString(), true);

            if (_dispatcher == null)
            {
                _dispatcher = ServiceLocators.ServiceLocator.GetDispatcher();
            }

            if (_dispatcher != null)
            {
                _dispatcher.Dispatch(uploadData);
            }

            return(uploadData);
        }
        public static LoginResult ExecuteLogin(LoginData login, out bool cancelPressed, bool relogin)
        {
            LoginResult loginResult = new LoginResult();

            cancelPressed = !login.IsLogging;

            if (login != null && login.IsLogging && (!_isLogged || relogin))
            {
                CxWebServiceClient client = null;

                BackgroundWorkerHelper bg = new BackgroundWorkerHelper(delegate
                {
                    try
                    {
                        client = new CxWebServiceClient(login);
                    }
                    catch (Exception e)
                    {
                        Logger.Create().Error(e.ToString());
                        System.Windows.Forms.MessageBox.Show(e.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK);
                        return;
                    }

                    if (client == null)
                    {
                        System.Windows.Forms.MessageBox.Show("Cannot connect to server", "Error", System.Windows.Forms.MessageBoxButtons.OK);
                        return;
                    }

                    // perform login
                    try
                    {
                        serverBaseUrl = login.ServerBaseUri;

                        bool loginSucceeded = DolLogin(login, client);
                        if (loginSucceeded)
                        {
                            loginResult.IsSuccesfull       = true;
                            loginResult.AuthenticationData = login;
                        }
                        _loginResult = loginResult;
                    }
                    catch (WebException ex)
                    {
                        Logger.Create().Error(ex.Message, ex);
                        loginResult.LoginResultType    = LoginResultType.UnknownServerName;
                        loginResult.LoginResultMessage = ex.Message;
                        loginResult.IsSuccesfull       = false;
                    }
                    catch (Exception ex)
                    {
                        Logger.Create().Error(ex.Message, ex);
                        loginResult.LoginResultType    = LoginResultType.UnknownError;
                        loginResult.LoginResultMessage = ex.Message;
                    }
                    finally
                    {
                        if (client != null)
                        {
                            client.Close();
                        }
                    }
                },
                                                                       login.ReconnectInterval * 1000, login.ReconnectCount);

                cancelPressed = !bg.DoWork(WAIT_DIALOG_PROGRESS_TEXT);
            }

            return(loginResult);
        }