Ejemplo n.º 1
0
        private static async Task <bool> DownloadInstaller()
        {
            try
            {
                ProgressManager.SetTotalBytes(0);
                bool shouldRetry = true;
                int  retryCount  = 0;

                while (shouldRetry && !_ShouldAbort)                  //While we should retry and not abort
                {
                    if (retryCount < SwiftUpdateConstants.RetryCount) //If we've not exceeded the retry count
                    {
                        ProgressManager.progressDescription = "Downloading latest version of installer for updating.";
                        _Log.WriteEntry("Downloading installer from server directory " + _ServerFolder + "Installer/" + _Application + " Installer.md5");

                        await DownloadFile(_ServerFolder + "Installer/" + _Application + " Installer.md5", _UpdateFolder + "Installer//Installer.md5");                                        //Downloading installer checksum
                        await DownloadFile(_ServerFolder + "Installer/" + _Application + " Installer.exe", _UpdateFolder + "Installer//Installer.exe");                                        //Downloading installer

                        if (SwiftUpdateUtilities.ReadMD5FromFile(_UpdateFolder + "Installer//Installer.md5") == SwiftUpdateUtilities.CalculateMD5(_UpdateFolder + "Installer//Installer.exe")) //Compare the downloaded md5 to the md5 of the downloaded installer
                        {
                            _Log.WriteEntry("Installer checksums matched.");
                            return(true);
                        }

                        _Log.WriteEntry("Installer checksums did not match, retrying...");

                        retryCount++;
                    }

                    else    //We've exceeded the retry count, return false
                    {
                        Directory.Delete(_TempFolder, true);
                        shouldRetry = false;
                        _Log.WriteEntry("Retried max amount of times to download installer.");
                        MessageBox.Show("Failed to download installer. Please ensure you have a stable internet connection. Otherwise, try again later. If the issue persists, please contact support and provide your Update.log.", "Download Failed!", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }

                return(false);
            }

            catch (Exception ex)
            {
                Directory.Delete(_TempFolder, true);
                _Log.WriteEntry("Exception when downloading the installer: " + ex);
                MessageBox.Show("Failed to download the installer. Please ensure you have a stable internet connection. Otherwise, try again later. If the issue persists, please contact support and provide your Update.log.", "Update Failed!", MessageBoxButton.OK, MessageBoxImage.Error);
                return(false);
            }
        }
Ejemplo n.º 2
0
        private static async Task <bool> VerifyUpdateIntegrity(List <FileEntry> filesToVerify)
        {
            _Log.WriteEntry("Verifying integrity of files.");

            try
            {
                List <FileEntry> badFiles  = new List <FileEntry>();
                bool             isFileBad = true;

                foreach (var newFile in filesToVerify)                                                                  //For all of our new files
                {
                    foreach (var downloadedFile in Directory.GetFiles(_TempFolder, "*.*", SearchOption.AllDirectories)) //For all of the downloaded files in the temp folder
                    {
                        if (Path.GetFileName(newFile.Path) == Path.GetFileName(downloadedFile))                         //If the two filenames match
                        {
                            if (newFile.Checksum == SwiftUpdateUtilities.CalculateMD5(downloadedFile))                  //If the checksums match
                            {
                                isFileBad = false;                                                                      //The file integrity is guarenteed
                            }
                        }
                    }

                    if (isFileBad)  //If the file is bad, add it to the list of bad files to redownload
                    {
                        _Log.WriteEntry("The file " + newFile.Path + " is bad.");
                        badFiles.Add(newFile);
                    }

                    isFileBad = true;
                }

                if (badFiles.Count > 0) //If we have bad files, redownload them
                {
                    _Log.WriteEntry("We have bad files.");
                    bool shouldRetry = true;

                    while (shouldRetry && !_ShouldAbort)    //While we should retry and not abort
                    {
                        if (_BadFilesReattemptCount < SwiftUpdateConstants.RetryCount)
                        {
                            if (!await Task.Run(() => RedownloadBadFiles(badFiles)))    //If we did not successfully redownloaded the bad files/experienced an error, return false
                            {
                                return(false);
                            }

                            else    //Otherwise the redownload was successful, so now we validate them
                            {
                                _BadFilesReattemptCount++;

                                if (!await Task.Run(() => VerifyUpdateIntegrity(badFiles)))    //If the integrity was bad again, we retry again
                                {
                                    _Log.WriteEntry("Failed to redownload bad files. Attempt " + (_BadFilesReattemptCount + 1) + " of " + SwiftUpdateConstants.RetryCount);
                                }

                                else    //Integrity was good this time
                                {
                                    _Log.WriteEntry("Successfully redownloaded bad files, returning true.");
                                    return(true);
                                }
                            }
                        }

                        else    //We've exceeded the attempt count
                        {
                            shouldRetry = false;
                            Directory.Delete(_TempFolder, true);
                            MessageBox.Show("Failed to verify the integrity of the update. Please ensure you have a stable internet connection. Otherwise, try again later. If the issue persists, please contact support and provide your Update.log.", "Update Failed!", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }

                    return(false);
                }

                else    //No bad files, great, return true
                {
                    _Log.WriteEntry("No bad files, returning true.");
                    return(true);
                }
            }

            catch (Exception ex)
            {
                _Log.WriteEntry("Exception when verifying update integrity: " + ex);
                Directory.Delete(_TempFolder);
                MessageBox.Show("Failed to verify the integrity of the update. Please ensure you have a stable internet connection. Otherwise, try again later. If the issue persists, please contact support and provide your Update.log.", "Update Failed!", MessageBoxButton.OK, MessageBoxImage.Error);
                return(false);
            }
        }
Ejemplo n.º 3
0
        public static async Task <bool> CheckForUpdates(string appName, string serverFolder)
        {
            _Application  = appName;
            _ServerFolder = serverFolder;
            _UpdateFolder = Environment.GetEnvironmentVariable("LocalAppData") + "//" + appName + "//";
            _TempFolder   = _UpdateFolder + "TemporaryDownloads//"; // Set private variables
            _Log          = new Logger(_UpdateFolder + "\\Update.log");
            _NewFiles     = new List <FileEntry>();

            Directory.CreateDirectory(_TempFolder);
            Directory.CreateDirectory(_TempFolder + "\\" + _Application + "\\");   //Create directories in preparation for download

            bool installerVerified = false;
            bool shouldRetry       = true;
            int  retryCount        = 0;

            if (!Directory.Exists(_UpdateFolder))
            {
                Directory.CreateDirectory(_UpdateFolder);
                _Log.WriteEntry("Update folder \"" + _UpdateFolder + "\" didn't exist, creating it and returning true (an update is required)");
                return(true);
            }

            else if (!Directory.Exists(_UpdateFolder + "Installer"))
            {
                Directory.CreateDirectory(_UpdateFolder + "Installer");
                _Log.WriteEntry("Installer folder didn't exist, creating directory " + _UpdateFolder + "Installer, and forcing an update.");
                return(true);
            }

            _Log.WriteEntry("Downloading Version.md5 from server, saving in \"" + _TempFolder + "\" as " + SwiftUpdateConstants.newVersionMD5Name);
            await DownloadFile(_ServerFolder + SwiftUpdateConstants.versionMD5Name, _TempFolder + SwiftUpdateConstants.newVersionMD5Name); //Download the Version.md5 from the server

            while (shouldRetry && !_ShouldAbort)                                                                                           //While we should reattempt
            {
                if (retryCount < SwiftUpdateConstants.RetryCount)                                                                          //If we've not exceeded the max reattempts
                {
                    if (!installerVerified)                                                                                                //If we haven't verified the installer's version, download it
                    {
                        _Log.WriteEntry("Downloading installer.md5.");
                        await DownloadFile(_ServerFolder + "Installer/" + _Application + " Installer.md5", _UpdateFolder + "Installer//Installer.new.md5");                                        //Downloading installer checksum

                        if (SwiftUpdateUtilities.ReadMD5FromFile(_UpdateFolder + "Installer//Installer.new.md5") != SwiftUpdateUtilities.CalculateMD5(_UpdateFolder + "Installer//Installer.exe")) //Comparing md5 of the downloaded checksum to the local installer
                        {
                            _Log.WriteEntry("Local md5 did not match download, downloading new installer.");
                            await DownloadFile(_ServerFolder + "Installer/" + _Application + " Installer.exe", _UpdateFolder + "Installer//Installer.new.exe");                                            //Downloading installer exe

                            if (SwiftUpdateUtilities.ReadMD5FromFile(_UpdateFolder + "Installer//Installer.new.md5") == SwiftUpdateUtilities.CalculateMD5(_UpdateFolder + "Installer//Installer.new.exe")) //If the checksum of the new installer matches the md5 we downloaded earlier
                            {
                                _Log.WriteEntry("Downloaded checksums matched, new installer verified.");
                                File.Delete(_UpdateFolder + "Installer//Installer.md5");
                                File.Delete(_UpdateFolder + "Installer//Installer.exe");                                               //Delete the old installer files
                                File.Move(_UpdateFolder + "Installer//Installer.new.md5", _UpdateFolder + "Installer//Installer.md5");
                                File.Move(_UpdateFolder + "Installer//Installer.new.exe", _UpdateFolder + "Installer//Installer.exe"); //Copy over the new installer files
                                installerVerified = true;
                                retryCount        = 0;                                                                                 //We've verified the new installer, so we reset our reattempts for the update files to 0
                            }

                            else   //The new installer's checksum does NOT match the one from earlier, we begin the process again
                            {
                                _Log.WriteEntry("Downloaded installer checksums did not match, retrying...");
                            }
                        }

                        else   //If the checksum we downloaded DOES match our local copy, no new installer required
                        {
                            _Log.WriteEntry("No new installer available.");
                            File.Delete(_UpdateFolder + "Installer//Installer.new.md5");
                            installerVerified = true;
                            retryCount        = 0; //We've verified the new installer, so we delete the md5 we downloaded and reset our reattempts for the update files to 0
                        }
                    }

                    else                                                                       //The installer is verified, so we move on to the update files
                    {
                        if (!File.Exists(_UpdateFolder + SwiftUpdateConstants.versionMD5Name)) //If our local copy doesn't exist
                        {
                            _Log.WriteEntry("Our local Version.md5 at directory \"" + _UpdateFolder + SwiftUpdateConstants.versionMD5Name + "\" didn't exist, update required.");
                            return(true);    //We redownload the update
                        }

                        else
                        {
                            if (SwiftUpdateUtilities.ReadMD5FromFile(_UpdateFolder + SwiftUpdateConstants.versionMD5Name) == SwiftUpdateUtilities.ReadMD5FromFile(_TempFolder + SwiftUpdateConstants.newVersionMD5Name))    //If the Version.md5 we downloaded matches our local copy
                            {
                                _Log.WriteEntry("Compared md5s, no update is required, deleting temp.");
                                Directory.Delete(_TempFolder, true);
                                return(false);   //No update is available
                            }

                            else    //Our downloaded Version.md5 does NOT match our local copy, possibly an update available
                            {
                                _Log.WriteEntry("Our downloaded Version.md5 did not match our local copy. Downloading Version.xml from server, saving in \"" + _TempFolder + "\" as " + SwiftUpdateConstants.newVersionXMLName);
                                await DownloadFile(_ServerFolder + SwiftUpdateConstants.versionXMLName, _TempFolder + SwiftUpdateConstants.newVersionXMLName);  //We download the new Version.xml file from the server and store in in a temp folder

                                _Log.WriteEntry("Checksum comparison. " + SwiftUpdateUtilities.ReadMD5FromFile(_TempFolder + SwiftUpdateConstants.newVersionMD5Name) + " | " + SwiftUpdateUtilities.CalculateMD5(_TempFolder + SwiftUpdateConstants.newVersionXMLName));
                                if (SwiftUpdateUtilities.ReadMD5FromFile(_TempFolder + SwiftUpdateConstants.newVersionMD5Name) == SwiftUpdateUtilities.CalculateMD5(_TempFolder + SwiftUpdateConstants.newVersionXMLName))  //If the checksum we downloaded earlier matches the calculated checksum of the xml file (integrity guarenteed)
                                {
                                    _Log.WriteEntry("Checksum of Version.xml matches our previously downloaded Version.md5, checking versions now.");
                                    if (SwiftUpdateXML.GetVersionNumber(_TempFolder + SwiftUpdateConstants.newVersionXMLName).CompareTo(SwiftUpdateXML.GetVersionNumber(_UpdateFolder + SwiftUpdateConstants.versionXMLName)) > 0) //If the xml file we downloaded has a greater version number than our local copy
                                    {
                                        _Log.WriteEntry("Our new file has a greater version than our local, an update is available, returning true.");
                                        return(true);    //We definitely have an update
                                    }

                                    else    //The version number on the server is the same or earlier than us
                                    {
                                        _Log.WriteEntry("We have the same or a later version than the server, returning false, deleting temp.");
                                        Directory.Delete(_TempFolder, true);
                                        return(false);
                                    }
                                }

                                _Log.WriteEntry("The downloaded Version.xml didn't match the downloaded checksum, retrying...");
                            }
                        }
                    }

                    retryCount++;
                }

                else
                {
                    _Log.WriteEntry("Retried max amount of times.");
                    MessageBox.Show("Failed to verify if an update was available. Please ensure that you have a stable internet connection. Otherwise, try again later. If the issue persists, contact support and provide the Update.log.", "Update Check Failed!", MessageBoxButton.OK, MessageBoxImage.Error);
                    shouldRetry = false;
                }
            }

            return(false);
        }
Ejemplo n.º 4
0
        private static async Task <bool> StartUpdate()
        {
            bool shouldRetry = true;
            int  retryCount  = 0;

            while (shouldRetry && !_ShouldAbort)                  //While we should retry and not abort
            {
                if (retryCount < SwiftUpdateConstants.RetryCount) //If we've not exceeded the retry count
                {
                    _Log.WriteEntry("Downloading the Version xml and md5 files.");
                    await DownloadFile(_ServerFolder + SwiftUpdateConstants.versionMD5Name, _TempFolder + SwiftUpdateConstants.newFileListMD5Name); //Downloading new file list checksum file
                    await DownloadFile(_ServerFolder + SwiftUpdateConstants.versionXMLName, _TempFolder + SwiftUpdateConstants.newVersionXMLName);  //Download new file list xml file

                    _Log.WriteEntry("Downloading the file list xml and md5 files.");
                    await DownloadFile(_ServerFolder + SwiftUpdateConstants.fileListMD5Name, _TempFolder + SwiftUpdateConstants.newFileListMD5Name);                                                             //Downloading new file list checksum file
                    await DownloadFile(_ServerFolder + SwiftUpdateConstants.fileListXMLName, _TempFolder + SwiftUpdateConstants.newFileListXMLName);                                                             //Download new file list xml file

                    if (SwiftUpdateUtilities.ReadMD5FromFile(_TempFolder + SwiftUpdateConstants.newFileListMD5Name) == SwiftUpdateUtilities.CalculateMD5(_TempFolder + SwiftUpdateConstants.newFileListXMLName)) //Comparing md5 of new file list to local checksum of new file list
                    {
                        _Log.WriteEntry("The checksums of the file list match, downloading new update files.");
                        if (await Task.Run(() => GetNewFiles()))    //If we've got the list of new files
                        {
                            _Log.WriteEntry("Got a list of new files.");
                            ProgressManager.SetTotalBytes(GetTotalSizeOfFiles(_NewFiles));
                            if (await Task.Run(() => DownloadNewFiles()))      //If we've downloaded the new files
                            {
                                if (await Task.Run(() => DownloadInstaller())) //If we've downloaded the installer
                                {
                                    _Log.WriteEntry("Returning true from StartUpdate");
                                    ProgressManager.progressDescription = "Download completed.";
                                    return(true);
                                }
                            }
                        }

                        //Download abort/exception if we reach here, so abort the update process
                        _ShouldAbort = true;
                        shouldRetry  = false;
                        Reset();
                        _Log.WriteEntry("Experienced a download error, aborting...");
                        return(false);
                    }

                    else    //The checksum we downloaded does not match the checksum of the new file list
                    {
                        _Log.WriteEntry("The checksums of the file list didn't match, retrying...");
                    }

                    retryCount++;
                }

                else    //We've exceeded the count
                {
                    Reset();
                    shouldRetry = false;
                    _Log.WriteEntry("Retried max amount of times to get file list and Version.");
                    MessageBox.Show("Failed to download initial update files. Please ensure that you have a stable internet connection. Otherwise, try again later. If the issue persists, contact support and provide your Update.log.", "Update Failed!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

            return(false);
        }