private Upload GetUploadSettings(Project project, LoginResult loginResult) { Upload uploadSettings; if (!CommonData.IsProjectBound) { uploadSettings = UploadHelper.SetUploadSettings(loginResult, project, _cancelPressed); if (uploadSettings == null) { LoginHelper.DoLogout(); _scan.InProcess = false; } } else { uploadSettings = new Upload { ProjectName = CommonData.ProjectName, IsUploading = true, IsPublic = CommonData.IsProjectPublic }; } return(uploadSettings); }
static CxWebServiceClient InitCxClient(LoginResult loginResult) { CxWebServiceClient client; try { client = new CxWebServiceClient(loginResult.AuthenticationData); } catch (Exception e) { Common.Logger.Create().Error(e.ToString()); LoginHelper.DoLogout(); System.Windows.Forms.MessageBox.Show(e.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK); return(null); } if (client == null) { LoginHelper.DoLogout(); System.Windows.Forms.MessageBox.Show("Cannot connect to server", "Error", System.Windows.Forms.MessageBoxButtons.OK); } return(client); }
/// <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> /// Perform background job and show view while process executes /// </summary> /// <param name="message">View title message</param> /// <returns>If false - cancel buton in supported view was pressed</returns> public bool DoWork(string message) { if (_doWorkFunc == null) { return(false); } // Show perform operation dialog bool canceling = false; IWaitView waitView = new WaitFrm(message, delegate(object sender, EventArgs e) { canceling = true; }); int repeatNum = 1; string errorMessage = null; bool success = false; do { try { if (Invoke(message, waitView) || canceling) { success = true; break; } } catch (Exception ex) { Logger.Create().Error(ex.ToString()); LoginHelper.DoLogout(); errorMessage = ex.Message; message = string.Format("Reconnection attempt #{0}...", repeatNum.ToString()); DateTime waitTo = DateTime.Now.AddMilliseconds(_repeatIntervalOnException); while (!(canceling || DateTime.Now > waitTo)) { Application.DoEvents(); Thread.Sleep(10); } if (canceling) { break; } } }while (repeatNum++ <= _repeatCountOnException); if (success || canceling) { if (!string.IsNullOrEmpty(message)) { waitView.CloseView(); } return(!canceling ? success : false); } bool executionResult = false; while (true) { try { ErrorFrm frmError = new ErrorFrm(errorMessage, delegate(object o, EventArgs e) { executionResult = Invoke("Reconnection...", waitView); }, _doReloginFunc); DialogResult res = frmError.ShowDialog(); if (res == DialogResult.Cancel) { break; } if (res != DialogResult.OK) { if (_doReloginFunc != null) { IsReloginInvoked = true; _doReloginFunc.BeginInvoke(null, null, null, null); } break; } if (executionResult) { break; } } catch (Exception ex) { Logger.Create().Error(ex.ToString()); } } if (!string.IsNullOrEmpty(message)) { waitView.CloseView(); } return(executionResult); }