Exemplo n.º 1
0
        private static RvSofOperation InstallOperation(RvSofOperation windowsOperation)
        {
            InstallSupportedData data = windowsOperation.InstallSupportedDataList[0];
            //string uri = data.Uris; //TODO: This must be corrected for agent updater to work..
            string filepath = Path.Combine(AgentUpdateDirectory, _fileName);

            try
            {
                InstallResult installResult = ExeInstall(filepath, data.CliOptions);

                if (!installResult.Success)
                {
                    var results = new RVsofResult();
                    results.Success = false.ToString();
                    results.AppId   = data.Id;
                    results.Error   = String.Format("Failed while installing RV Agent update: {0}. {1}. Exit code: {2}.",
                                                    filepath, installResult.ExitCodeMessage, installResult.ExitCode);
                    windowsOperation.AddResult(results);
                    return(windowsOperation);
                }

                return(null);
            }
            catch (Exception e)
            {
                Logger.Log("Could not install RV Agent Update: ", LogLevel.Error);
                Logger.LogException(e);

                var result = new RVsofResult();
                result.AppId = data.Id;
                result.Error = String.Format("Failed to update RV Agent:  {0}.", e.Message);
                windowsOperation.AddResult(result);
                return(windowsOperation);
            }
        }
        public static string CustomDataResults(RvSofOperation operation)
        {
            string jsonString = String.Empty;

            switch (operation.Type)
            {
                case RvOperationValue.WindowsRestoreInfo:
                    jsonString = RestoreInfoResult(operation);
                    break;
            }

            return jsonString;
        }
Exemplo n.º 3
0
        public static string CustomDataResults(RvSofOperation operation)
        {
            string jsonString = String.Empty;

            switch (operation.Type)
            {
            case RvOperationValue.WindowsRestoreInfo:
                jsonString = RestoreInfoResult(operation);
                break;
            }

            return(jsonString);
        }
Exemplo n.º 4
0
        private static string RestoreInfoResult(RvSofOperation operation)
        {
            var json      = new JObject();
            var jsonArray = new JArray();

            json.Add(OperationKey.Operation, RvOperationValue.WindowsRestoreInfo);
            json.Add(OperationKey.OperationId, operation.Id);
            json.Add(OperationKey.AgentId, Settings.AgentId);

            // If there are no system restores, then return empty list.
            if (operation.Restores == null)
            {
                JObject jObject = JObject.Parse(String.Empty);
                jsonArray.Add(jObject);
            }
            else
            {
                foreach (WindowsRestore.WindowsRestoreData data in operation.Restores)
                {
                    var builder = new StringBuilder("{");

                    builder.AppendFormat((string)@" ""description"" : ""{0}"", ", (object)data.Description);
                    builder.AppendFormat((string)@" ""creation_time"" : ""{0}"", ", (object)data.CreationTime);
                    builder.AppendFormat((string)@" ""sequence_number"" : ""{0}"", ", (object)data.SequenceNumber);

                    builder.Append("}");

                    JObject jObject = JObject.Parse(builder.ToString());
                    jsonArray.Add(jObject);
                }
            }

            json.Add(OperationKey.Data, jsonArray);

            return(json.ToString());
        }
        /// <summary>
        /// Determines whether to perform a system restore or send data back to the server. It use WMI.
        /// </summary>
        /// <param name="operation">Variable of type RvSofOperation</param>
        /// <returns>Returns updated RvSofOperation after processing.</returns>
        public static RvSofOperation Restore(RvSofOperation operation)
        {
            var results = new RVsofResult();
            var restoreClass = new ManagementClass("\\\\.\\root\\default", "systemrestore", new ObjectGetOptions());
            ManagementObjectCollection restoreCollection = restoreClass.GetInstances();

            foreach (ManagementObject restoreItem in restoreCollection)
            {
                // Possible properties. See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa378925(v=vs.85).aspx
                //(string)restoreItem["Description"]
                //(uint)restoreItem["RestorePointType"]).ToString()
                //(uint)restoreItem["EventType"]).ToString()
                //(uint)restoreItem["SequenceNumber"]).ToString()
                //(string)restoreItem["CreationTime"]

                // Crazy way to call a method for a WMI class through System.Management.
                // See: http://msdn.microsoft.com/en-us/library/ms257364(v=vs.80).aspx
                if (((uint)restoreItem["SequenceNumber"]) != operation.WindowsRestoreSequence) continue;

                ManagementBaseObject inputParameters = restoreClass.GetMethodParameters("Restore");
                inputParameters["SequenceNumber"] = operation.WindowsRestoreSequence;

                try
                {
                    ManagementBaseObject outputParameters = restoreClass.InvokeMethod("Restore", inputParameters, null);
                    if (outputParameters != null && Convert.ToInt32(outputParameters["returnValue"]) == 0)
                    {
                        // Success! Restart system for restore point can take affect.
                        RvUtils.RestartSystem();
                        return null;
                    }

                    // Failed...
                    results.AppId = String.Empty;
                    results.Success = false.ToString();
                    results.RebootRequired = false.ToString();
                    results.Data.Name = String.Empty;

                    // Ummmm from the docs: "If the method succeeds, the return value is S_OK (0).
                    // Otherwise, the method returns one of the COM error codes defined in WinError.h."
                    if (outputParameters != null)
                    {
                        var exitCode = Convert.ToInt32(outputParameters["returnValue"]);
                        results.Error = "Win32 Error: " + new Win32Exception(exitCode).Message;
                    }

                    operation.AddResult(results);
                }
                catch (ManagementException e)
                {
                    Logger.Log("Exception: {0}", LogLevel.Error, e.Message);
                    if (e.InnerException != null)
                        Logger.Log("Inner exception: {0}", LogLevel.Error, e.InnerException.Message);

                    Logger.Log("Failed to perform a system restore.", LogLevel.Error);
                    results.AppId = String.Empty;
                    results.Success = false.ToString();
                    results.RebootRequired = false.ToString();
                    results.Data.Name = String.Empty;
                    results.Error = String.Format("ManagementException Error: {0}", e);
                }

                operation.AddResult(results);
                return operation;
            }

            results.AppId = String.Empty;
            results.Success = false.ToString();
            results.RebootRequired = false.ToString();
            results.Error= String.Format("No restore point with sequence number {0} was found.", operation.WindowsRestoreSequence);
            results.Data.Name = String.Empty;

            operation.AddResult(results);
            return operation;
        }
        private static string RestoreInfoResult(RvSofOperation operation)
        {
            var json = new JObject();
            var jsonArray = new JArray();

            json.Add(OperationKey.Operation, RvOperationValue.WindowsRestoreInfo);
            json.Add(OperationKey.OperationId, operation.Id);
            json.Add(OperationKey.AgentId, Settings.AgentId);

            // If there are no system restores, then return empty list.
            if (operation.Restores == null)
            {
                JObject jObject = JObject.Parse(String.Empty);
                jsonArray.Add(jObject);
            }
            else
            {
                foreach (WindowsRestore.WindowsRestoreData data in operation.Restores)
                {
                    var builder = new StringBuilder("{");

                    builder.AppendFormat((string) @" ""description"" : ""{0}"", ", (object) data.Description);
                    builder.AppendFormat((string) @" ""creation_time"" : ""{0}"", ", (object) data.CreationTime);
                    builder.AppendFormat((string) @" ""sequence_number"" : ""{0}"", ", (object) data.SequenceNumber);

                    builder.Append("}");

                    JObject jObject = JObject.Parse(builder.ToString());
                    jsonArray.Add(jObject);
                }
            }

            json.Add(OperationKey.Data, jsonArray);

            return json.ToString();
        }
Exemplo n.º 7
0
        public static RvSofOperation DownloadUpdate(RvSofOperation operation)
        {
            var url = operation.InstallAgentUpdateDataList[0].ToString();
            var split = url.Split(new[] { '/' });
            var filename = split[split.Length - 1];
            var filepath = Path.Combine(AgentUpdateDirectory, filename);
            int fileSize;

            _fileName = filename;

            if (!Directory.Exists(Settings.BackupDirectory))
                Directory.CreateDirectory(Settings.BackupDirectory);

            if (!Directory.Exists(AgentUpdateDirectory))
                Directory.CreateDirectory(AgentUpdateDirectory);

            if (File.Exists(filepath))
                File.Delete(filepath);

                  try
                  {
                     using (WebClient)
                     {
                        if (Settings.Proxy != null)
                            WebClient.Proxy = Settings.Proxy;

                        WebClient.OpenRead(url);
                        fileSize = Convert.ToInt32(WebClient.ResponseHeaders["Content-Length"]);
                        WebClient.DownloadFile(new Uri(url), filepath);
                     }

                  }
                  catch (Exception e)
                  {
                      Logger.Log("Could not download file {0}. Please check network settings and File URI.", LogLevel.Error, filename);
                      Logger.LogException(e);
                      var data = operation.InstallSupportedDataList[0];
                      var result = new RVsofResult();
                      result.AppId = data.Id;
                      result.Error = "RV Agent Update did not Download, check network and/or Download URI.";
                      operation.AddResult(result);
                      return operation;
                  }

                  if (File.Exists(filepath))
                  {
                      var downloadedAgent = new FileInfo(filepath);
                      var downloadedAgentSize = Convert.ToInt32(downloadedAgent.Length);

                      if (fileSize == downloadedAgentSize)
                      {
                          //Install Operation for the Update
                          var updateResults = InstallOperation(operation);
                          if (updateResults != null)
                              return updateResults;

                          //Installation of RV Agent Update Failed while Installing.
                          InstallSupportedData data = operation.InstallSupportedDataList[0];
                          var result = new RVsofResult();
                          result.AppId = data.Id;
                          result.Error = "RV Agent Update Failed while installing.";
                          result.Success = false.ToString();
                          operation.AddResult(result);
                          return operation;
                      }
                      else
                      {
                          //Downloaded failed, send back results
                          Logger.Log("RV Agent Update File download corrupted, unable to install the update.");
                          InstallSupportedData data = operation.InstallSupportedDataList[0];
                          var result = new RVsofResult();
                          result.AppId = data.Id;
                          result.Error = "File download corrupted, unable to install update.";
                          operation.AddResult(result);
                          return operation;
                      }

                  }
                  else
                  {
                      //Downloaded failed, send back results
                      Logger.Log("RV Agent Update did not Download, check network and/or Download URI.", LogLevel.Info);
                      InstallSupportedData data = operation.InstallSupportedDataList[0];
                      var result = new RVsofResult();
                      result.AppId = data.Id;
                      result.Error = "RV Agent Update did not Download, check network and/or Download URI.";
                      operation.AddResult(result);
                      return operation;

                  }
        }
Exemplo n.º 8
0
        private static RvSofOperation InstallOperation(RvSofOperation windowsOperation)
        {
            InstallSupportedData data = windowsOperation.InstallSupportedDataList[0];
            //string uri = data.Uris; //TODO: This must be corrected for agent updater to work..
            string filepath = Path.Combine(AgentUpdateDirectory, _fileName);

            try
            {
                 InstallResult installResult = ExeInstall(filepath, data.CliOptions);

                 if (!installResult.Success)
                 {
                     var results = new RVsofResult();
                     results.Success = false.ToString();
                     results.AppId = data.Id;
                     results.Error = String.Format("Failed while installing RV Agent update: {0}. {1}. Exit code: {2}.",
                                                                filepath, installResult.ExitCodeMessage, installResult.ExitCode);
                     windowsOperation.AddResult(results);
                     return windowsOperation;
                 }

                 return null;
            }
            catch (Exception e)
            {
                    Logger.Log("Could not install RV Agent Update: ", LogLevel.Error);
                    Logger.LogException(e);

                    var result = new RVsofResult();
                    result.AppId = data.Id;
                    result.Error = String.Format("Failed to update RV Agent:  {0}.", e.Message);
                    windowsOperation.AddResult(result);
                    return windowsOperation;
            }
        }
        public static RvSofOperation InstallCustomAppsOperation(RvSofOperation operation)
        {
            var foundApplications = new List <InstallCustomData>();
            var registryOpen      = new RegistryReader();

            //Load all
            foreach (InstallCustomData installData in operation.InstallCustomDataList)
            {
                string appDir = Path.Combine(Settings.UpdateDirectory, installData.Id);
                bool   found  = false;

                foreach (string item in installData.Uris)
                {
                    string[] split    = item.Split(new[] { '/' });
                    string   filename = split[split.Length - 1];
                    string   filepath = Path.Combine(appDir, filename);

                    if (File.Exists(filepath))
                    {
                        found = true;
                    }
                    else
                    {
                        found = false;
                        break;
                    }
                }

                if (!found)
                {
                    var result = new RVsofResult();
                    result.AppId = installData.Id;
                    result.Error = "Update files did not Download: " + installData.Name;
                    Logger.Log("Update files did not Download for: " + installData.Name);
                    operation.AddResult(result);
                }
                else
                {
                    Logger.Log("Update Files for {0} : Downloaded OK", LogLevel.Info, installData.Name);
                    foundApplications.Add(installData);
                }
            }

            foreach (InstallCustomData id in foundApplications)
            {
                try
                {
                    string   appDirectory = Path.Combine(Settings.UpdateDirectory, id.Id);
                    string[] appFiles     = Directory.GetFiles(appDirectory);

                    foreach (string file in appFiles)
                    {
                        var    extension = Path.GetExtension(file);
                        Result result;

                        switch (extension)
                        {
                        case Extension.Exe:

                            Logger.Log("Installing: {0}", LogLevel.Info, id.Name);
                            result = ExeInstall(file, id.CliOptions);
                            break;

                        case Extension.Msi:

                            Logger.Log("Installing: {0}", LogLevel.Info, id.Name);
                            result = MsiInstall(file, id.CliOptions);
                            break;

                        case Extension.Msp:

                            Logger.Log("Installing: {0}", LogLevel.Info, id.Name);
                            result = MspInstall(file, id.CliOptions);
                            break;

                        default:
                            throw new Exception(String.Format("{0} is not a supported file format.", extension));
                        }

                        if (!result.Success)
                        {
                            var results = new RVsofResult();
                            results.Success = false.ToString();
                            results.AppId   = id.Id;
                            results.Error   = String.Format("Failed to install {0}. {1}. Exit code: {2}.", file, result.ExitCodeMessage, result.ExitCode);
                            Logger.Log("Failed to install: {0}", LogLevel.Info, file);
                            operation.AddResult(results);
                            break;
                        }

                        // If the last file was reached without issues, all should be good.
                        if (appFiles[appFiles.Length - 1].Equals(file))
                        {
                            var results = new RVsofResult();

                            //Get new list of installed applications after finishing installing applications.
                            operation.ListOfAppsAfterInstall = registryOpen.GetRegistryInstalledApplicationNames();

                            results.Success        = true.ToString();
                            results.AppId          = id.Id;
                            results.RebootRequired = result.Restart.ToString();
                            results.Data.Name      = registryOpen.GetSetFromTwoLists(operation.ListOfInstalledApps, operation.ListOfAppsAfterInstall); //TODO: keep an eye on this, no need for it??
                            Logger.Log("Update Success: {0}", LogLevel.Debug, file);
                            operation.AddResult(results);
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.Log("Could not install {0}.", LogLevel.Error, id.Name);
                    Logger.LogException(e);

                    var result = new RVsofResult();
                    result.AppId = id.Id;
                    result.Error = String.Format("Failed to install. {0}.", e.Message);
                    operation.AddResult(result);
                }
            }

            return(operation);
        }
        public static bool DownloadThirdPartyApp(RvSofOperation operation)
        {
            if (!Directory.Exists(Settings.UpdateDirectory))
            {
                Directory.CreateDirectory(Settings.UpdateDirectory);
            }

            using (var client = new WebClient())
            {
                foreach (var installData in operation.InstallUpdateDataList)
                {
                    var appDir = Path.Combine(Settings.UpdateDirectory, installData.Id);

                    if (Directory.Exists(appDir))
                    {
                        Directory.Delete(appDir, true);
                    }

                    Directory.CreateDirectory(appDir);

                    // Just in case the web server is using a self-signed cert.
                    // Webclient won't validate the SSL/TLS cerficate if it's not trusted.
                    var tempCallback = ServicePointManager.ServerCertificateValidationCallback;
                    ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); };

                    foreach (var file in installData.Uris)
                    {
                        var filepath = Path.Combine(appDir, file.FileName);

                        try
                        {
                            if (Settings.proxy != null)
                            {
                                client.Proxy = Settings.proxy;
                            }

                            client.OpenRead(file.Uri);
                            var fileSize = Convert.ToInt32(client.ResponseHeaders["Content-Length"]);
                            client.DownloadFile(file.Uri, filepath);

                            if (!File.Exists(filepath))
                            {
                                return(false);
                            }

                            var downloadedUri       = new FileInfo(filepath);
                            var downloadedAgentSize = Convert.ToInt32(downloadedUri.Length);

                            if (!fileSize.Equals(downloadedAgentSize) && downloadedAgentSize != file.FileSize)
                            {
                                return(false);
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.Log("Could not download file {0}.", LogLevel.Error, file.FileName);
                            Logger.LogException(e);
                            return(false);
                        }
                    }
                    ServicePointManager.ServerCertificateValidationCallback = tempCallback;
                }
            }

            return(true);
        }
Exemplo n.º 11
0
        /// <summary>
        ///     Determines whether to perform a system restore or send data back to the server. It use WMI.
        /// </summary>
        /// <param name="operation"></param>
        /// <returns></returns>
        public static RvSofOperation Restore(RvSofOperation operation)
        {
            var results      = new RVsofResult();
            var restoreClass = new ManagementClass("\\\\.\\root\\default", "systemrestore", new ObjectGetOptions());
            ManagementObjectCollection restoreCollection = restoreClass.GetInstances();

            foreach (ManagementObject restoreItem in restoreCollection)
            {
                // Possible properties. See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa378925(v=vs.85).aspx
                //(string)restoreItem["Description"]
                //(uint)restoreItem["RestorePointType"]).ToString()
                //(uint)restoreItem["EventType"]).ToString()
                //(uint)restoreItem["SequenceNumber"]).ToString()
                //(string)restoreItem["CreationTime"]

                // Crazy way to call a method for a WMI class through System.Management.
                // See: http://msdn.microsoft.com/en-us/library/ms257364(v=vs.80).aspx
                if (((uint)restoreItem["SequenceNumber"]) != operation.WindowsRestoreSequence)
                {
                    continue;
                }

                ManagementBaseObject inputParameters = restoreClass.GetMethodParameters("Restore");
                inputParameters["SequenceNumber"] = operation.WindowsRestoreSequence;

                try
                {
                    ManagementBaseObject outputParameters = restoreClass.InvokeMethod("Restore", inputParameters, null);
                    if (outputParameters != null && Convert.ToInt32(outputParameters["returnValue"]) == 0)
                    {
                        // Success! Restart system for restore point can take affect.
                        RvUtils.RestartSystem();
                        return(null);
                    }

                    // Failed...
                    results.AppId          = String.Empty;
                    results.Success        = false.ToString();
                    results.RebootRequired = false.ToString();
                    results.Data.Name      = String.Empty;

                    // Ummmm from the docs: "If the method succeeds, the return value is S_OK (0).
                    // Otherwise, the method returns one of the COM error codes defined in WinError.h."
                    if (outputParameters != null)
                    {
                        var exitCode = Convert.ToInt32(outputParameters["returnValue"]);
                        results.Error = "Win32 Error: " + new Win32Exception(exitCode).Message;
                    }

                    operation.AddResult(results);
                }
                catch (ManagementException e)
                {
                    Logger.Log("Exception: {0}", LogLevel.Error, e.Message);
                    if (e.InnerException != null)
                    {
                        Logger.Log("Inner exception: {0}", LogLevel.Error, e.InnerException.Message);
                    }

                    Logger.Log("Failed to perform a system restore.", LogLevel.Error);
                    results.AppId          = String.Empty;
                    results.Success        = false.ToString();
                    results.RebootRequired = false.ToString();
                    results.Data.Name      = String.Empty;
                    results.Error          = String.Format("ManagementException Error: {0}", e);
                }

                operation.AddResult(results);
                return(operation);
            }

            results.AppId          = String.Empty;
            results.Success        = false.ToString();
            results.RebootRequired = false.ToString();
            results.Error          = String.Format("No restore point with sequence number {0} was found.", operation.WindowsRestoreSequence);
            results.Data.Name      = String.Empty;

            operation.AddResult(results);
            return(operation);
        }
        public static RvSofOperation InstallSupportedAppsOperation(RvSofOperation operation)
        {
            var foundApplications = new List<InstallSupportedData>();
            var registryOpen = new RegistryReader();

            //Load all
            foreach (var installData in operation.InstallSupportedDataList)
            {
                var appDir = Path.Combine(Settings.UpdateDirectory, installData.Id);
                var found = false;

                foreach (var item in installData.Uris)
                {
                    var split = item.Split(new[] { '/' });
                    var filename = split[split.Length - 1];
                    var filepath = Path.Combine(appDir, filename);

                    if (File.Exists(filepath))
                        found = true;
                    else
                    {
                        found = false;
                        break;
                    }
                }

                if (!found)
                {
                    var result = new RVsofResult();
                    result.AppId = installData.Id;
                    result.Error = "Update did not Download: " + Environment.NewLine
                                                + installData.Name + Environment.NewLine;
                    operation.AddResult(result);
                }
                else
                {
                    Logger.Log("Update Files: Downloaded OK");
                    foundApplications.Add(installData);
                }
            }

            foreach (InstallSupportedData id in foundApplications)
            {
                try
                {
                    var appDirectory = Path.Combine(Settings.UpdateDirectory, id.Id);
                    var appFiles = Directory.GetFiles(appDirectory);

                    foreach (string file in appFiles)
                    {
                        var extension = Path.GetExtension(file);
                        Result result;

                        switch (extension)
                        {
                            case Extension.Exe:

                                Logger.Log("Installing: {0}", LogLevel.Info, id.Name);
                                result = ExeInstall(file, id.CliOptions);

                                break;

                            case Extension.Msi:

                                Logger.Log("Installing: {0}", LogLevel.Info, id.Name);
                                result = MsiInstall(file, id.CliOptions);

                                break;

                            case Extension.Msp:

                                Logger.Log("Installing: {0}", LogLevel.Info, id.Name);
                                result = MspInstall(file, id.CliOptions);

                                break;

                            default:

                                throw new Exception(String.Format("{0} is not a supported file format.", extension));
                        }

                        if (!result.Success)
                        {
                            var results = new RVsofResult();
                            results.Success = false.ToString();
                            results.AppId = id.Id;
                            results.Error = String.Format("Failed to install {0}. {1}. Exit code: {2}.", file, result.ExitCodeMessage, result.ExitCode);
                            Logger.Log("Failed to install: {0}", LogLevel.Info, file);
                            operation.AddResult(results);
                            break;
                        }

                        // If the last file was reached without issues, all should be good.
                        if (appFiles[appFiles.Length - 1].Equals(file))
                        {
                            var results = new RVsofResult();

                            //Get new list of installed applications after finishing installing applications.
                            operation.ListOfAppsAfterInstall = registryOpen.GetRegistryInstalledApplicationNames();

                            results.Success = true.ToString();
                            results.AppId = id.Id;
                            results.RebootRequired = result.Restart.ToString();
                            results.Data.Name = registryOpen.GetSetFromTwoLists(operation.ListOfInstalledApps, operation.ListOfAppsAfterInstall); //TODO: Keep eye on this, possibly not needed anymore.
                            Logger.Log("Update Success: {0}", LogLevel.Debug, file);
                            operation.AddResult(results);
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.Log("Could not install {0}.", LogLevel.Error, id.Name);
                    Logger.LogException(e);

                    var result = new RVsofResult();
                    result.AppId = id.Id;
                    result.Error = String.Format("Failed to install. {0}.", e.Message);
                    operation.AddResult(result);
                }
            }

            return operation;
        }
        public static bool DownloadThirdPartyApp(RvSofOperation operation)
        {
            if (!Directory.Exists(Settings.UpdateDirectory))
                Directory.CreateDirectory(Settings.UpdateDirectory);

            using (var client = new WebClient())
            {
                foreach (var installData in operation.InstallUpdateDataList)
                {
                    var appDir = Path.Combine(Settings.UpdateDirectory, installData.Id);

                    if (Directory.Exists(appDir))
                        Directory.Delete(appDir, true);

                    Directory.CreateDirectory(appDir);

                    // Just in case the web server is using a self-signed cert.
                    // Webclient won't validate the SSL/TLS cerficate if it's not trusted.
                    var tempCallback = ServicePointManager.ServerCertificateValidationCallback;
                    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

                    foreach (var file in installData.Uris)
                    {
                        var filepath = Path.Combine(appDir, file.FileName);

                        try
                        {
                            if (Settings.proxy != null)
                                client.Proxy = Settings.proxy;

                            client.OpenRead(file.Uri);
                            var fileSize = Convert.ToInt32(client.ResponseHeaders["Content-Length"]);
                            client.DownloadFile(file.Uri, filepath);

                            if (!File.Exists(filepath))
                                return false;

                            var downloadedUri = new FileInfo(filepath);
                            var downloadedAgentSize = Convert.ToInt32(downloadedUri.Length);

                            if (!fileSize.Equals(downloadedAgentSize) && downloadedAgentSize != file.FileSize)
                                return false;
                        }
                        catch (Exception e)
                        {
                            Logger.Log("Could not download file {0}.", LogLevel.Error, file.FileName);
                            Logger.LogException(e);
                            return false;
                        }
                    }
                    ServicePointManager.ServerCertificateValidationCallback = tempCallback;
                }
            }

            return true;
        }
Exemplo n.º 14
0
        public static RvSofOperation DownloadUpdate(RvSofOperation operation)
        {
            var url      = operation.InstallAgentUpdateDataList[0].ToString();
            var split    = url.Split(new[] { '/' });
            var filename = split[split.Length - 1];
            var filepath = Path.Combine(AgentUpdateDirectory, filename);
            int fileSize;

            _fileName = filename;


            if (!Directory.Exists(Settings.BackupDirectory))
            {
                Directory.CreateDirectory(Settings.BackupDirectory);
            }

            if (!Directory.Exists(AgentUpdateDirectory))
            {
                Directory.CreateDirectory(AgentUpdateDirectory);
            }

            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }

            try
            {
                using (WebClient)
                {
                    if (Settings.Proxy != null)
                    {
                        WebClient.Proxy = Settings.Proxy;
                    }

                    WebClient.OpenRead(url);
                    fileSize = Convert.ToInt32(WebClient.ResponseHeaders["Content-Length"]);
                    WebClient.DownloadFile(new Uri(url), filepath);
                }
            }
            catch (Exception e)
            {
                Logger.Log("Could not download file {0}. Please check network settings and File URI.", LogLevel.Error, filename);
                Logger.LogException(e);
                var data   = operation.InstallSupportedDataList[0];
                var result = new RVsofResult();
                result.AppId = data.Id;
                result.Error = "RV Agent Update did not Download, check network and/or Download URI.";
                operation.AddResult(result);
                return(operation);
            }


            if (File.Exists(filepath))
            {
                var downloadedAgent     = new FileInfo(filepath);
                var downloadedAgentSize = Convert.ToInt32(downloadedAgent.Length);

                if (fileSize == downloadedAgentSize)
                {
                    //Install Operation for the Update
                    var updateResults = InstallOperation(operation);
                    if (updateResults != null)
                    {
                        return(updateResults);
                    }

                    //Installation of RV Agent Update Failed while Installing.
                    InstallSupportedData data = operation.InstallSupportedDataList[0];
                    var result = new RVsofResult();
                    result.AppId   = data.Id;
                    result.Error   = "RV Agent Update Failed while installing.";
                    result.Success = false.ToString();
                    operation.AddResult(result);
                    return(operation);
                }
                else
                {
                    //Downloaded failed, send back results
                    Logger.Log("RV Agent Update File download corrupted, unable to install the update.");
                    InstallSupportedData data = operation.InstallSupportedDataList[0];
                    var result = new RVsofResult();
                    result.AppId = data.Id;
                    result.Error = "File download corrupted, unable to install update.";
                    operation.AddResult(result);
                    return(operation);
                }
            }
            else
            {
                //Downloaded failed, send back results
                Logger.Log("RV Agent Update did not Download, check network and/or Download URI.", LogLevel.Info);
                InstallSupportedData data = operation.InstallSupportedDataList[0];
                var result = new RVsofResult();
                result.AppId = data.Id;
                result.Error = "RV Agent Update did not Download, check network and/or Download URI.";
                operation.AddResult(result);
                return(operation);
            }
        }