Fixed typo in variable name.
[MUtilities.git] / src / UpdateChecker.cpp
blob671155d845b620360d19d95a17276df5b9f7d261
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2018 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
22 #include <MUtils/Global.h>
23 #include <MUtils/UpdateChecker.h>
24 #include <MUtils/OSSupport.h>
25 #include <MUtils/Exception.h>
27 #include <QStringList>
28 #include <QFile>
29 #include <QFileInfo>
30 #include <QDir>
31 #include <QProcess>
32 #include <QUrl>
33 #include <QEventLoop>
34 #include <QTimer>
35 #include <QElapsedTimer>
36 #include <QSet>
37 #include <QHash>
38 #include <QQueue>
40 #include "Mirrors.h"
42 ///////////////////////////////////////////////////////////////////////////////
43 // CONSTANTS
44 ///////////////////////////////////////////////////////////////////////////////
46 static const char *GLOBALHEADER_ID = "!Update";
48 static const char *MIRROR_URL_POSTFIX[] =
50 "update.ver",
51 "update_beta.ver",
52 NULL
55 static const int MIN_CONNSCORE = 5;
56 static const int QUICK_MIRRORS = 3;
57 static const int MAX_CONN_TIMEOUT = 16000;
58 static const int DOWNLOAD_TIMEOUT = 30000;
60 static const int VERSION_INFO_EXPIRES_MONTHS = 6;
61 static char *USER_AGENT_STR = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0"; /*use something innocuous*/
63 ////////////////////////////////////////////////////////////
64 // Utility Macros
65 ////////////////////////////////////////////////////////////
67 #define CHECK_CANCELLED() do \
68 { \
69 if(MUTILS_BOOLIFY(m_cancelled)) \
70 { \
71 m_success.fetchAndStoreOrdered(0); \
72 log("", "Update check has been cancelled by user!"); \
73 setProgress(m_maxProgress); \
74 setStatus(UpdateStatus_CancelledByUser); \
75 return; \
76 } \
77 } \
78 while(0)
80 #define LOG_MESSAGE_HELPER(X) do \
81 { \
82 if (!(X).isNull()) \
83 { \
84 emit messageLogged((X)); \
85 } \
86 } \
87 while(0)
89 #define STRICMP(X,Y) ((X).compare((Y), Qt::CaseInsensitive) == 0)
91 ////////////////////////////////////////////////////////////
92 // Helper Functions
93 ////////////////////////////////////////////////////////////
95 static QQueue<QString> buildRandomList(const char *const *values)
97 QQueue<QString> list;
98 while(*values)
100 list.insert(MUtils::next_rand_u32(list.size() + 1), QString::fromLatin1(*(values++)));
102 return list;
105 static const QHash<QString, QString> *initEnvVars(void)
107 QHash<QString, QString> *const environment = new QHash<QString, QString>();
108 environment->insert(QLatin1String("CURL_HOME"), QDir::toNativeSeparators(MUtils::temp_folder()));
109 return environment;
112 ////////////////////////////////////////////////////////////
113 // Update Info Class
114 ////////////////////////////////////////////////////////////
116 MUtils::UpdateCheckerInfo::UpdateCheckerInfo(void)
118 resetInfo();
121 void MUtils::UpdateCheckerInfo::resetInfo(void)
123 m_buildNo = 0;
124 m_buildDate.setDate(1900, 1, 1);
125 m_downloadSite.clear();
126 m_downloadAddress.clear();
127 m_downloadFilename.clear();
128 m_downloadFilecode.clear();
129 m_downloadChecksum.clear();
132 bool MUtils::UpdateCheckerInfo::isComplete(void)
134 return (this->m_buildNo > 0) &&
135 (this->m_buildDate.year() >= 2010) &&
136 (!this->m_downloadSite.isEmpty()) &&
137 (!this->m_downloadAddress.isEmpty()) &&
138 (!this->m_downloadFilename.isEmpty()) &&
139 (!this->m_downloadFilecode.isEmpty()) &&
140 (!this->m_downloadChecksum.isEmpty());
143 ////////////////////////////////////////////////////////////
144 // Constructor & Destructor
145 ////////////////////////////////////////////////////////////
147 MUtils::UpdateChecker::UpdateChecker(const QString &binCurl, const QString &binGnuPG, const QString &binKeys, const QString &applicationId, const quint32 &installedBuildNo, const bool betaUpdates, const bool testMode)
149 m_updateInfo(new UpdateCheckerInfo()),
150 m_binaryCurl(binCurl),
151 m_binaryGnuPG(binGnuPG),
152 m_binaryKeys(binKeys),
153 m_applicationId(applicationId),
154 m_installedBuildNo(installedBuildNo),
155 m_betaUpdates(betaUpdates),
156 m_testMode(testMode),
157 m_maxProgress(MIN_CONNSCORE + 5),
158 m_environment(initEnvVars())
160 m_status = UpdateStatus_NotStartedYet;
161 m_progress = 0;
163 if(m_binaryCurl.isEmpty() || m_binaryGnuPG.isEmpty() || m_binaryKeys.isEmpty())
165 MUTILS_THROW("Tools not initialized correctly!");
169 MUtils::UpdateChecker::~UpdateChecker(void)
173 ////////////////////////////////////////////////////////////
174 // Public slots
175 ////////////////////////////////////////////////////////////
177 void MUtils::UpdateChecker::start(Priority priority)
179 m_success.fetchAndStoreOrdered(0);
180 m_cancelled.fetchAndStoreOrdered(0);
181 QThread::start(priority);
184 ////////////////////////////////////////////////////////////
185 // Protected functions
186 ////////////////////////////////////////////////////////////
188 void MUtils::UpdateChecker::run(void)
190 qDebug("Update checker thread started!");
191 MUTILS_EXCEPTION_HANDLER(m_testMode ? testMirrorsList() : checkForUpdates());
192 qDebug("Update checker thread completed.");
195 void MUtils::UpdateChecker::checkForUpdates(void)
197 // ----- Initialization ----- //
199 m_updateInfo->resetInfo();
200 setProgress(0);
202 // ----- Test Internet Connection ----- //
204 log("Checking your Internet connection...", "");
205 setStatus(UpdateStatus_CheckingConnection);
207 const int networkStatus = OS::network_status();
208 if(networkStatus == OS::NETWORK_TYPE_NON)
210 if (!MUtils::OS::arguments().contains("ignore-network-status"))
212 log("Operating system reports that the computer is currently offline !!!");
213 setProgress(m_maxProgress);
214 setStatus(UpdateStatus_ErrorNoConnection);
215 return;
219 msleep(333);
220 setProgress(1);
222 // ----- Test Known Hosts Connectivity ----- //
224 int connectionScore = 0;
225 QQueue<QString> mirrorList = buildRandomList(known_hosts);
227 for(int connectionTimeout = 1000; connectionTimeout <= MAX_CONN_TIMEOUT; connectionTimeout *= 2)
229 QElapsedTimer elapsedTimer;
230 elapsedTimer.start();
231 const qint64 globalTimeout = 2 * MIN_CONNSCORE * connectionTimeout;
232 forever
234 if (mirrorList.isEmpty())
236 goto endLoop; /*depleted!*/
238 const QString hostName = mirrorList.dequeue();
239 if (tryContactHost(hostName, connectionTimeout))
241 setProgress(1 + (++connectionScore));
242 if (connectionScore >= MIN_CONNSCORE)
244 goto endLoop; /*success*/
247 else
249 mirrorList.enqueue(hostName);
250 if(elapsedTimer.hasExpired(globalTimeout))
252 break; /*timer expired*/
255 CHECK_CANCELLED();
259 endLoop:
260 if(connectionScore < MIN_CONNSCORE)
262 log("", "Connectivity test has failed: Internet connection appears to be broken!");
263 setProgress(m_maxProgress);
264 setStatus(UpdateStatus_ErrorConnectionTestFailed);
265 return;
268 // ----- Fetch Update Info From Server ----- //
270 log("----", "", "Internet connection is operational, checking for updates online...");
271 setStatus(UpdateStatus_FetchingUpdates);
273 int mirrorCount = 0;
274 mirrorList = buildRandomList(update_mirrors);
276 while(!mirrorList.isEmpty())
278 const QString currentMirror = mirrorList.takeFirst();
279 const bool isQuick = (mirrorCount++ < QUICK_MIRRORS);
280 if(tryUpdateMirror(m_updateInfo.data(), currentMirror, isQuick))
282 m_success.ref(); /*success*/
283 break;
285 if (isQuick)
287 mirrorList.append(currentMirror); /*re-schedule*/
289 CHECK_CANCELLED();
290 msleep(1);
293 msleep(333);
294 setProgress(MIN_CONNSCORE + 5);
296 // ----- Generate final result ----- //
298 if(MUTILS_BOOLIFY(m_success))
300 if(m_updateInfo->m_buildNo > m_installedBuildNo)
302 setStatus(UpdateStatus_CompletedUpdateAvailable);
304 else if(m_updateInfo->m_buildNo == m_installedBuildNo)
306 setStatus(UpdateStatus_CompletedNoUpdates);
308 else
310 setStatus(UpdateStatus_CompletedNewVersionOlder);
313 else
315 setStatus(UpdateStatus_ErrorFetchUpdateInfo);
319 void MUtils::UpdateChecker::testMirrorsList(void)
321 QStringList mirrorList;
322 for(int i = 0; update_mirrors[i]; i++)
324 mirrorList << QString::fromLatin1(update_mirrors[i]);
327 // ----- Test update mirrors ----- //
329 qDebug("\n[Mirror Sites]");
330 log("Testing all known mirror sites...", "", "---");
332 UpdateCheckerInfo updateInfo;
333 while (!mirrorList.isEmpty())
335 const QString currentMirror = mirrorList.takeFirst();
336 bool success = false;
337 qDebug("Testing: %s", MUTILS_L1STR(currentMirror));
338 log("", "Testing:", currentMirror, "");
339 for (quint8 attempt = 0; attempt < 3; ++attempt)
341 updateInfo.resetInfo();
342 if (tryUpdateMirror(&updateInfo, currentMirror, (!attempt)))
344 success = true;
345 break;
348 if (!success)
350 qWarning("\nUpdate mirror seems to be unavailable:\n%s\n", MUTILS_L1STR(currentMirror));
352 log("", "---");
355 // ----- Test known hosts ----- //
357 QStringList knownHostList;
358 for (int i = 0; known_hosts[i]; i++)
360 knownHostList << QString::fromLatin1(known_hosts[i]);
363 qDebug("\n[Known Hosts]");
364 log("Testing all known hosts...", "", "---");
366 while(!knownHostList.isEmpty())
368 const QString currentHost = knownHostList.takeFirst();
369 qDebug("Testing: %s", MUTILS_L1STR(currentHost));
370 log(QLatin1String(""), "Testing:", currentHost, "");
371 if (!tryContactHost(currentHost, DOWNLOAD_TIMEOUT))
373 qWarning("\nConnectivity test FAILED on the following host:\n%s\n", MUTILS_L1STR(currentHost));
375 log("---");
379 ////////////////////////////////////////////////////////////
380 // PRIVATE FUNCTIONS
381 ////////////////////////////////////////////////////////////
383 void MUtils::UpdateChecker::setStatus(const int status)
385 if(m_status != status)
387 m_status = status;
388 emit statusChanged(status);
392 void MUtils::UpdateChecker::setProgress(const int progress)
394 const int value = qBound(0, progress, m_maxProgress);
395 if(m_progress != value)
397 emit progressChanged(m_progress = value);
401 void MUtils::UpdateChecker::log(const QString &str1, const QString &str2, const QString &str3, const QString &str4)
403 LOG_MESSAGE_HELPER(str1);
404 LOG_MESSAGE_HELPER(str2);
405 LOG_MESSAGE_HELPER(str3);
406 LOG_MESSAGE_HELPER(str4);
409 bool MUtils::UpdateChecker::tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString &url, const bool &quick)
411 bool success = false;
412 log("", "Trying update mirror:", url, "");
414 if (quick)
416 setProgress(MIN_CONNSCORE + 1);
417 if (!tryContactHost(QUrl(url).host(), (MAX_CONN_TIMEOUT / 8)))
419 log("", "Mirror is too slow, skipping!");
420 return false;
424 const QString randPart = next_rand_str();
425 const QString outFileVers = QString("%1/%2.ver").arg(temp_folder(), randPart);
426 const QString outFileSign = QString("%1/%2.sig").arg(temp_folder(), randPart);
428 if (!getUpdateInfo(url, outFileVers, outFileSign))
430 log("", "Oops: Download of update information has failed!");
431 goto cleanUp;
434 log("Download completed, verifying signature:", "");
435 setProgress(MIN_CONNSCORE + 4);
436 if (!checkSignature(outFileVers, outFileSign))
438 log("", "Bad signature detected, take care !!!");
439 goto cleanUp;
442 log("", "Signature is valid, parsing update information:", "");
443 success = parseVersionInfo(outFileVers, updateInfo);
445 cleanUp:
446 QFile::remove(outFileVers);
447 QFile::remove(outFileSign);
448 return success;
451 bool MUtils::UpdateChecker::getUpdateInfo(const QString &url, const QString &outFileVers, const QString &outFileSign)
453 log("Downloading update information:", "");
454 setProgress(MIN_CONNSCORE + 2);
455 if(getFile(QUrl(QString("%1%2").arg(url, MIRROR_URL_POSTFIX[m_betaUpdates ? 1 : 0])), outFileVers))
457 if (!m_cancelled)
459 log( "Downloading signature file:", "");
460 setProgress(MIN_CONNSCORE + 3);
461 if (getFile(QUrl(QString("%1%2.sig2").arg(url, MIRROR_URL_POSTFIX[m_betaUpdates ? 1 : 0])), outFileSign))
463 return true; /*completed*/
467 return false;
470 //----------------------------------------------------------
471 // PARSE UPDATE INFO
472 //----------------------------------------------------------
474 #define _CHECK_HEADER(ID,NAME) \
475 if (STRICMP(name, (NAME))) \
477 sectionId = (ID); \
478 continue; \
481 #define _PARSE_TEXT(OUT,KEY) \
482 if (STRICMP(key, (KEY))) \
484 (OUT) = val; \
485 break; \
488 #define _PARSE_UINT(OUT,KEY) \
489 if (STRICMP(key, (KEY))) \
491 bool _ok = false; \
492 const unsigned int _tmp = val.toUInt(&_ok); \
493 if (_ok) \
495 (OUT) = _tmp; \
496 break; \
500 #define _PARSE_DATE(OUT,KEY) \
501 if (STRICMP(key, (KEY))) \
503 const QDate _tmp = QDate::fromString(val, Qt::ISODate); \
504 if (_tmp.isValid()) \
506 (OUT) = _tmp; \
507 break; \
511 bool MUtils::UpdateChecker::parseVersionInfo(const QString &file, UpdateCheckerInfo *const updateInfo)
513 updateInfo->resetInfo();
515 QFile data(file);
516 if(!data.open(QIODevice::ReadOnly))
518 qWarning("Cannot open update info file for reading!");
519 return false;
522 QDate updateInfoDate;
523 int sectionId = 0;
524 QRegExp regex_sec("^\\[(.+)\\]$"), regex_val("^([^=]+)=(.+)$");
526 while(!data.atEnd())
528 QString line = QString::fromLatin1(data.readLine()).trimmed();
529 if (regex_sec.indexIn(line) >= 0)
531 sectionId = 0; /*unknown section*/
532 const QString name = regex_sec.cap(1).trimmed();
533 log(QString("Sec: [%1]").arg(name));
534 _CHECK_HEADER(1, GLOBALHEADER_ID)
535 _CHECK_HEADER(2, m_applicationId)
536 continue;
538 if (regex_val.indexIn(line) >= 0)
540 const QString key = regex_val.cap(1).trimmed();
541 const QString val = regex_val.cap(2).trimmed();
542 log(QString("Val: \"%1\" = \"%2\"").arg(key, val));
543 switch (sectionId)
545 case 1:
546 _PARSE_DATE(updateInfoDate, "TimestampCreated")
547 break;
548 case 2:
549 _PARSE_UINT(updateInfo->m_buildNo, "BuildNo")
550 _PARSE_DATE(updateInfo->m_buildDate, "BuildDate")
551 _PARSE_TEXT(updateInfo->m_downloadSite, "DownloadSite")
552 _PARSE_TEXT(updateInfo->m_downloadAddress, "DownloadAddress")
553 _PARSE_TEXT(updateInfo->m_downloadFilename, "DownloadFilename")
554 _PARSE_TEXT(updateInfo->m_downloadFilecode, "DownloadFilecode")
555 _PARSE_TEXT(updateInfo->m_downloadChecksum, "DownloadChecksum")
556 break;
561 if (!updateInfo->isComplete())
563 log("", "WARNING: Update information is incomplete!");
564 goto failure;
567 if(updateInfoDate.isValid())
569 const QDate expiredDate = updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS);
570 if (expiredDate < OS::current_date())
572 log("", QString("WARNING: Update information has expired at %1!").arg(expiredDate.toString(Qt::ISODate)));
573 goto failure;
576 else
578 log("", "WARNING: Timestamp is missing from update information header!");
579 goto failure;
582 log("", "Success: Update information is complete.");
583 return true; /*success*/
585 failure:
586 updateInfo->resetInfo();
587 return false;
590 //----------------------------------------------------------
591 // EXTERNAL TOOLS
592 //----------------------------------------------------------
594 bool MUtils::UpdateChecker::getFile(const QUrl &url, const QString &outFile, const unsigned int maxRedir)
596 QFileInfo output(outFile);
597 output.setCaching(false);
599 if (output.exists())
601 QFile::remove(output.canonicalFilePath());
602 if (output.exists())
604 qWarning("Existing output file could not be found!");
605 return false;
609 QStringList args(QLatin1String("-vsSNqkfL"));
610 args << "-m" << QString::number(DOWNLOAD_TIMEOUT / 1000);
611 args << "--max-redirs" << QString::number(maxRedir);
612 args << "-A" << USER_AGENT_STR;
613 args << "-e" << QString("%1://%2/;auto").arg(url.scheme(), url.host());
614 args << "-o" << output.fileName() << url.toString();
616 return execCurl(args, output.absolutePath(), DOWNLOAD_TIMEOUT);
619 bool MUtils::UpdateChecker::tryContactHost(const QString &hostname, const int &timeoutMsec)
621 log(QString("Connecting to host: %1").arg(hostname), "");
623 QStringList args(QLatin1String("-vsSNqkI"));
624 args << "-m" << QString::number(qMax(1, timeoutMsec / 1000));
625 args << "-A" << USER_AGENT_STR;
626 args << "-o" << OS::null_device() << QString("http://%1/").arg(hostname);
628 return execCurl(args, temp_folder(), timeoutMsec);
631 bool MUtils::UpdateChecker::checkSignature(const QString &file, const QString &signature)
633 if (QFileInfo(file).absolutePath().compare(QFileInfo(signature).absolutePath(), Qt::CaseInsensitive) != 0)
635 qWarning("CheckSignature: File and signature should be in same folder!");
636 return false;
639 QString keyRingPath(m_binaryKeys);
640 bool removeKeyring = false;
641 if (QFileInfo(file).absolutePath().compare(QFileInfo(m_binaryKeys).absolutePath(), Qt::CaseInsensitive) != 0)
643 keyRingPath = make_temp_file(QFileInfo(file).absolutePath(), "gpg");
644 removeKeyring = true;
645 if (!QFile::copy(m_binaryKeys, keyRingPath))
647 qWarning("CheckSignature: Failed to copy the key-ring file!");
648 return false;
652 QStringList args;
653 args << QStringList() << "--homedir" << ".";
654 args << "--keyring" << QFileInfo(keyRingPath).fileName();
655 args << QFileInfo(signature).fileName();
656 args << QFileInfo(file).fileName();
658 const int exitCode = execProcess(m_binaryGnuPG, args, QFileInfo(file).absolutePath(), DOWNLOAD_TIMEOUT);
659 if (exitCode != INT_MAX)
661 log(QString().sprintf("Exited with code %d", exitCode));
664 if (removeKeyring)
666 remove_file(keyRingPath);
669 return (exitCode == 0); /*completed*/
672 bool MUtils::UpdateChecker::execCurl(const QStringList &args, const QString &workingDir, const int timeout)
674 const int exitCode = execProcess(m_binaryCurl, args, workingDir, timeout + (timeout / 2));
675 if (exitCode != INT_MAX)
677 switch (exitCode)
679 case 0: log(QLatin1String("DONE: Transfer completed successfully."), ""); break;
680 case 6: log(QLatin1String("ERROR: Remote host could not be resolved!"), ""); break;
681 case 7: log(QLatin1String("ERROR: Connection to remote host could not be established!"), ""); break;
682 case 22: log(QLatin1String("ERROR: Requested URL was not found or returned an error!"), ""); break;
683 case 28: log(QLatin1String("ERROR: Operation timed out !!!"), ""); break;
684 default: log(QString().sprintf("ERROR: Terminated with unknown code %d", exitCode), ""); break;
688 return (exitCode == 0); /*completed*/
691 int MUtils::UpdateChecker::execProcess(const QString &programFile, const QStringList &args, const QString &workingDir, const int timeout)
693 QProcess process;
694 init_process(process, workingDir, true, NULL, m_environment.data());
696 QEventLoop loop;
697 connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
698 connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), &loop, SLOT(quit()));
699 connect(&process, SIGNAL(readyRead()), &loop, SLOT(quit()));
701 QTimer timer;
702 timer.setSingleShot(true);
703 connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
705 process.start(programFile, args);
706 if (!process.waitForStarted())
708 log("PROCESS FAILED TO START !!!", "");
709 qWarning("WARNING: %s process could not be created!", MUTILS_UTF8(QFileInfo(programFile).fileName()));
710 return INT_MAX; /*failed to start*/
713 bool bAborted = false;
714 timer.start(qMax(timeout, 1500));
716 while (process.state() != QProcess::NotRunning)
718 loop.exec();
719 while (process.canReadLine())
721 const QString line = QString::fromLatin1(process.readLine()).simplified();
722 if (line.length() > 1)
724 log(line);
727 const bool bCancelled = MUTILS_BOOLIFY(m_cancelled);
728 if (bAborted = (bCancelled || ((!timer.isActive()) && (!process.waitForFinished(125)))))
730 log(bCancelled ? "CANCELLED BY USER !!!" : "PROCESS TIMEOUT !!!", "");
731 qWarning("WARNING: %s process %s!", MUTILS_UTF8(QFileInfo(programFile).fileName()), bCancelled ? "cancelled" : "timed out");
732 break; /*abort process*/
736 timer.stop();
737 timer.disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
739 if (bAborted)
741 process.kill();
742 process.waitForFinished(-1);
745 while (process.canReadLine())
747 const QString line = QString::fromLatin1(process.readLine()).simplified();
748 if (line.length() > 1)
750 log(line);
754 return bAborted ? INT_MAX : process.exitCode();
757 ////////////////////////////////////////////////////////////
758 // SLOTS
759 ////////////////////////////////////////////////////////////
761 /*NONE*/
763 ////////////////////////////////////////////////////////////
764 // EVENTS
765 ////////////////////////////////////////////////////////////
767 /*NONE*/