/// <summary> /// Convert XML string into StatusScanResult object /// </summary> /// <param name="response">xml string</param> /// <returns></returns> internal static StatusScanResult ParseStatusScanResult(string response) { StatusScanResult r = new StatusScanResult(); try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(response); foreach (XmlNode xmlNode in xmlDoc.ChildNodes[1].ChildNodes) { switch (xmlNode.Name) { case "IsSuccesfull": r.IsSuccesfull = bool.Parse(xmlNode.InnerText); break; case "ReturnValue": r.RunId = xmlNode.ChildNodes[0].Attributes["RunId"].Value; r.RunStatus = StatusScanResult.GetRunStatus(xmlNode.ChildNodes[0].Attributes["RunStatus"].Value); r.TotalPercent = int.Parse(xmlNode.ChildNodes[0].Attributes["TotalPercent"].Value); r.CurrentStage = int.Parse(xmlNode.ChildNodes[0].Attributes["CurrentStage"].Value); r.StageName = xmlNode.ChildNodes[0].Attributes["StageName"].Value; r.CurrentStagePercent = int.Parse(xmlNode.ChildNodes[0].Attributes["CurrentStagePercent"].Value); r.StageMessage = xmlNode.ChildNodes[0].Attributes["StageMessage"].Value; r.StepMessage = xmlNode.ChildNodes[0].Attributes["StepMessage"].Value; r.Details = xmlNode.ChildNodes[0].Attributes["Details"].Value; r.TimeStarted = xmlNode.ChildNodes[0].Attributes["TimeStarted"].Value; r.TimeFinished = xmlNode.ChildNodes[0].Attributes["TimeFinished"].Value; r.QueuePosition = int.Parse(xmlNode.ChildNodes[0].Attributes["QueuePosition"].Value); break; } } xmlDoc = null; } catch (Exception ex) { Common.Logger.Create().Error(ex.ToString()); } return(r); }
private StatusScanResult UpdateScanStatus(ref bool bCancel, bool backgroundMode, IScanView view, BackgroundWorkerHelper bg, CxWebServiceClient client, ref bool isIISStoped) { // Get current scan status CxWSResponseScanStatus cxWSResponseScanStatus = null; StatusScanResult statusScan = null; bg.DoWorkFunc = delegate(object obj) { cxWSResponseScanStatus = client.ServiceClient.GetStatusOfSingleScan(_scan.LoginResult.SessionId, _scan.RunScanResult.ScanId); statusScan = new StatusScanResult(); statusScan.CurrentStage = cxWSResponseScanStatus.CurrentStage; statusScan.CurrentStagePercent = cxWSResponseScanStatus.CurrentStagePercent; statusScan.Details = cxWSResponseScanStatus.StepDetails; statusScan.IsSuccesfull = cxWSResponseScanStatus.IsSuccesfull; statusScan.QueuePosition = cxWSResponseScanStatus.QueuePosition; statusScan.RunId = cxWSResponseScanStatus.RunId; statusScan.RunStatus = cxWSResponseScanStatus.CurrentStatus; statusScan.StageMessage = cxWSResponseScanStatus.StageMessage; statusScan.StageName = cxWSResponseScanStatus.StageName; statusScan.StepMessage = cxWSResponseScanStatus.StepMessage; statusScan.TimeFinished = cxWSResponseScanStatus.TimeFinished != null?cxWSResponseScanStatus.TimeFinished.ToString() : null; statusScan.TimeStarted = cxWSResponseScanStatus.TimeScheduled != null?cxWSResponseScanStatus.TimeScheduled.ToString() : null; statusScan.TotalPercent = cxWSResponseScanStatus.TotalPercent; }; bCancel = !bg.DoWork(null); if (!BackgroundWorkerHelper.IsReloginInvoked) { isIISStoped = !_scan.LoginResult.AuthenticationData.SaveSastScan; } else { BackgroundWorkerHelper.IsReloginInvoked = false; } if (!bCancel && cxWSResponseScanStatus != null && !isIISStoped) { ScanProgress progress = new ScanProgress( _scan.UploadSettings.ProjectName, statusScan.RunStatus.ToString(), statusScan.StageName, statusScan.StepMessage, statusScan.CurrentStagePercent, 0, 100, statusScan.TotalPercent); if (!backgroundMode) { view.Progress = progress; } try { CommonActionsInstance.getInstance().ScanProgressView.Progress = progress; } catch (Exception ex) { Logger.Create().Error(ex.ToString()); } if (statusScan.RunStatus == CurrentStatusEnum.Failed) { TopMostMessageBox.Show(statusScan.StageMessage, "Scan Error"); bCancel = true; } if (_scan.ScanView == null || _scan.ScanView.Visibility == false) { var scanStatusBar = new ScanStatusBar(true, string.Format("Scaning project {0}: {1} {2}", _scan.UploadSettings.ProjectName, statusScan.StepMessage, statusScan.RunStatus), statusScan.TotalPercent, 100); CommonActionsInstance.getInstance().UpdateScanProgress(scanStatusBar); } } return(statusScan); }
/// <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); }