Small fix.
[MUtilities.git] / src / UpdateChecker.cpp
blob33dd516f47787219e14c6a1abc8be7142f1da355
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>
39 #include "Mirrors.h"
41 ///////////////////////////////////////////////////////////////////////////////
42 // CONSTANTS
43 ///////////////////////////////////////////////////////////////////////////////
45 static const char *GLOBALHEADER_ID = "!Update";
47 static const char *MIRROR_URL_POSTFIX[] =
49 "update.ver",
50 "update_beta.ver",
51 NULL
54 static const int MIN_CONNSCORE = 5;
55 static const int QUICK_MIRRORS = 3;
56 static const int MAX_CONN_TIMEOUT = 16000;
57 static const int DOWNLOAD_TIMEOUT = 30000;
59 static const int VERSION_INFO_EXPIRES_MONTHS = 6;
60 static char *USER_AGENT_STR = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0"; /*use something innocuous*/
62 ////////////////////////////////////////////////////////////
63 // Utility Macros
64 ////////////////////////////////////////////////////////////
66 #define CHECK_CANCELLED() do \
67 { \
68 if(MUTILS_BOOLIFY(m_cancelled)) \
69 { \
70 m_success.fetchAndStoreOrdered(0); \
71 log("", "Update check has been cancelled by user!"); \
72 setProgress(m_maxProgress); \
73 setStatus(UpdateStatus_CancelledByUser); \
74 return; \
75 } \
76 } \
77 while(0)
79 #define LOG_MESSAGE_HELPER(X) do \
80 { \
81 if (!(X).isNull()) \
82 { \
83 emit messageLogged((X)); \
84 } \
85 } \
86 while(0)
88 #define STRICMP(X,Y) ((X).compare((Y), Qt::CaseInsensitive) == 0)
90 ////////////////////////////////////////////////////////////
91 // Helper Functions
92 ////////////////////////////////////////////////////////////
94 static QStringList buildRandomList(const char *const values[])
96 QStringList list;
97 for (int index = 0; values[index]; index++)
99 const int pos = MUtils::next_rand_u32() % (index + 1);
100 list.insert(pos, QString::fromLatin1(values[index]));
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 QStringList mirrorList = buildRandomList(known_hosts);
227 for(int connectionTimout = 1000; connectionTimout <= MAX_CONN_TIMEOUT; connectionTimout *= 2)
229 QElapsedTimer elapsedTimer;
230 elapsedTimer.start();
231 const int globalTimout = 2 * MIN_CONNSCORE * connectionTimout;
232 while (!elapsedTimer.hasExpired(globalTimout))
234 const QString hostName = mirrorList.takeFirst();
235 if (tryContactHost(hostName, connectionTimout))
237 setProgress(1 + (connectionScore += 1));
238 elapsedTimer.restart();
239 if (connectionScore >= MIN_CONNSCORE)
241 goto endLoop; /*success*/
244 else
246 mirrorList.append(hostName); /*re-schedule*/
248 CHECK_CANCELLED();
249 msleep(1);
253 endLoop:
254 if(connectionScore < MIN_CONNSCORE)
256 log("", "Connectivity test has failed: Internet connection appears to be broken!");
257 setProgress(m_maxProgress);
258 setStatus(UpdateStatus_ErrorConnectionTestFailed);
259 return;
262 // ----- Fetch Update Info From Server ----- //
264 log("----", "", "Internet connection is operational, checking for updates online...");
265 setStatus(UpdateStatus_FetchingUpdates);
267 int mirrorCount = 0;
268 mirrorList = buildRandomList(update_mirrors);
270 while(!mirrorList.isEmpty())
272 const QString currentMirror = mirrorList.takeFirst();
273 const bool isQuick = (mirrorCount++ < QUICK_MIRRORS);
274 if(tryUpdateMirror(m_updateInfo.data(), currentMirror, isQuick))
276 m_success.ref(); /*success*/
277 break;
279 if (isQuick)
281 mirrorList.append(currentMirror); /*re-schedule*/
283 CHECK_CANCELLED();
284 msleep(1);
287 msleep(333);
288 setProgress(MIN_CONNSCORE + 5);
290 // ----- Generate final result ----- //
292 if(MUTILS_BOOLIFY(m_success))
294 if(m_updateInfo->m_buildNo > m_installedBuildNo)
296 setStatus(UpdateStatus_CompletedUpdateAvailable);
298 else if(m_updateInfo->m_buildNo == m_installedBuildNo)
300 setStatus(UpdateStatus_CompletedNoUpdates);
302 else
304 setStatus(UpdateStatus_CompletedNewVersionOlder);
307 else
309 setStatus(UpdateStatus_ErrorFetchUpdateInfo);
313 void MUtils::UpdateChecker::testMirrorsList(void)
315 QStringList mirrorList;
316 for(int i = 0; update_mirrors[i]; i++)
318 mirrorList << QString::fromLatin1(update_mirrors[i]);
321 // ----- Test update mirrors ----- //
323 qDebug("\n[Mirror Sites]");
324 log("Testing all known mirror sites...", "", "---");
326 UpdateCheckerInfo updateInfo;
327 while (!mirrorList.isEmpty())
329 const QString currentMirror = mirrorList.takeFirst();
330 bool success = false;
331 qDebug("Testing: %s", MUTILS_L1STR(currentMirror));
332 log("", "Testing:", currentMirror, "");
333 for (quint8 attempt = 0; attempt < 3; ++attempt)
335 updateInfo.resetInfo();
336 if (tryUpdateMirror(&updateInfo, currentMirror, (!attempt)))
338 success = true;
339 break;
342 if (!success)
344 qWarning("\nUpdate mirror seems to be unavailable:\n%s\n", MUTILS_L1STR(currentMirror));
346 log("", "---");
349 // ----- Test known hosts ----- //
351 QStringList knownHostList;
352 for (int i = 0; known_hosts[i]; i++)
354 knownHostList << QString::fromLatin1(known_hosts[i]);
357 qDebug("\n[Known Hosts]");
358 log("Testing all known hosts...", "", "---");
360 while(!knownHostList.isEmpty())
362 const QString currentHost = knownHostList.takeFirst();
363 qDebug("Testing: %s", MUTILS_L1STR(currentHost));
364 log(QLatin1String(""), "Testing:", currentHost, "");
365 if (!tryContactHost(currentHost, DOWNLOAD_TIMEOUT))
367 qWarning("\nConnectivity test FAILED on the following host:\n%s\n", MUTILS_L1STR(currentHost));
369 log("---");
373 ////////////////////////////////////////////////////////////
374 // PRIVATE FUNCTIONS
375 ////////////////////////////////////////////////////////////
377 void MUtils::UpdateChecker::setStatus(const int status)
379 if(m_status != status)
381 m_status = status;
382 emit statusChanged(status);
386 void MUtils::UpdateChecker::setProgress(const int progress)
388 const int value = qBound(0, progress, m_maxProgress);
389 if(m_progress != value)
391 emit progressChanged(m_progress = value);
395 void MUtils::UpdateChecker::log(const QString &str1, const QString &str2, const QString &str3, const QString &str4)
397 LOG_MESSAGE_HELPER(str1);
398 LOG_MESSAGE_HELPER(str2);
399 LOG_MESSAGE_HELPER(str3);
400 LOG_MESSAGE_HELPER(str4);
403 bool MUtils::UpdateChecker::tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString &url, const bool &quick)
405 bool success = false;
406 log("", "Trying update mirror:", url, "");
408 if (quick)
410 setProgress(MIN_CONNSCORE + 1);
411 if (!tryContactHost(QUrl(url).host(), (MAX_CONN_TIMEOUT / 8)))
413 log("", "Mirror is too slow, skipping!");
414 return false;
418 const QString randPart = next_rand_str();
419 const QString outFileVers = QString("%1/%2.ver").arg(temp_folder(), randPart);
420 const QString outFileSign = QString("%1/%2.sig").arg(temp_folder(), randPart);
422 if (!getUpdateInfo(url, outFileVers, outFileSign))
424 log("", "Oops: Download of update information has failed!");
425 goto cleanUp;
428 log("Download completed, verifying signature:", "");
429 setProgress(MIN_CONNSCORE + 4);
430 if (!checkSignature(outFileVers, outFileSign))
432 log("", "Bad signature detected, take care !!!");
433 goto cleanUp;
436 log("", "Signature is valid, parsing update information:", "");
437 success = parseVersionInfo(outFileVers, updateInfo);
439 cleanUp:
440 QFile::remove(outFileVers);
441 QFile::remove(outFileSign);
442 return success;
445 bool MUtils::UpdateChecker::getUpdateInfo(const QString &url, const QString &outFileVers, const QString &outFileSign)
447 log("Downloading update information:", "");
448 setProgress(MIN_CONNSCORE + 2);
449 if(getFile(QUrl(QString("%1%2").arg(url, MIRROR_URL_POSTFIX[m_betaUpdates ? 1 : 0])), outFileVers))
451 if (!m_cancelled)
453 log( "Downloading signature file:", "");
454 setProgress(MIN_CONNSCORE + 3);
455 if (getFile(QUrl(QString("%1%2.sig2").arg(url, MIRROR_URL_POSTFIX[m_betaUpdates ? 1 : 0])), outFileSign))
457 return true; /*completed*/
461 return false;
464 //----------------------------------------------------------
465 // PARSE UPDATE INFO
466 //----------------------------------------------------------
468 #define _CHECK_HEADER(ID,NAME) \
469 if (STRICMP(name, (NAME))) \
471 sectionId = (ID); \
472 continue; \
475 #define _PARSE_TEXT(OUT,KEY) \
476 if (STRICMP(key, (KEY))) \
478 (OUT) = val; \
479 break; \
482 #define _PARSE_UINT(OUT,KEY) \
483 if (STRICMP(key, (KEY))) \
485 bool _ok = false; \
486 const unsigned int _tmp = val.toUInt(&_ok); \
487 if (_ok) \
489 (OUT) = _tmp; \
490 break; \
494 #define _PARSE_DATE(OUT,KEY) \
495 if (STRICMP(key, (KEY))) \
497 const QDate _tmp = QDate::fromString(val, Qt::ISODate); \
498 if (_tmp.isValid()) \
500 (OUT) = _tmp; \
501 break; \
505 bool MUtils::UpdateChecker::parseVersionInfo(const QString &file, UpdateCheckerInfo *const updateInfo)
507 updateInfo->resetInfo();
509 QFile data(file);
510 if(!data.open(QIODevice::ReadOnly))
512 qWarning("Cannot open update info file for reading!");
513 return false;
516 QDate updateInfoDate;
517 int sectionId = 0;
518 QRegExp regex_sec("^\\[(.+)\\]$"), regex_val("^([^=]+)=(.+)$");
520 while(!data.atEnd())
522 QString line = QString::fromLatin1(data.readLine()).trimmed();
523 if (regex_sec.indexIn(line) >= 0)
525 sectionId = 0; /*unknown section*/
526 const QString name = regex_sec.cap(1).trimmed();
527 log(QString("Sec: [%1]").arg(name));
528 _CHECK_HEADER(1, GLOBALHEADER_ID)
529 _CHECK_HEADER(2, m_applicationId)
530 continue;
532 if (regex_val.indexIn(line) >= 0)
534 const QString key = regex_val.cap(1).trimmed();
535 const QString val = regex_val.cap(2).trimmed();
536 log(QString("Val: \"%1\" = \"%2\"").arg(key, val));
537 switch (sectionId)
539 case 1:
540 _PARSE_DATE(updateInfoDate, "TimestampCreated")
541 break;
542 case 2:
543 _PARSE_UINT(updateInfo->m_buildNo, "BuildNo")
544 _PARSE_DATE(updateInfo->m_buildDate, "BuildDate")
545 _PARSE_TEXT(updateInfo->m_downloadSite, "DownloadSite")
546 _PARSE_TEXT(updateInfo->m_downloadAddress, "DownloadAddress")
547 _PARSE_TEXT(updateInfo->m_downloadFilename, "DownloadFilename")
548 _PARSE_TEXT(updateInfo->m_downloadFilecode, "DownloadFilecode")
549 _PARSE_TEXT(updateInfo->m_downloadChecksum, "DownloadChecksum")
550 break;
555 if (!updateInfo->isComplete())
557 log("", "WARNING: Update information is incomplete!");
558 goto failure;
561 if(updateInfoDate.isValid())
563 const QDate expiredDate = updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS);
564 if (expiredDate < OS::current_date())
566 log("", QString("WARNING: Update information has expired at %1!").arg(expiredDate.toString(Qt::ISODate)));
567 goto failure;
570 else
572 log("", "WARNING: Timestamp is missing from update information header!");
573 goto failure;
576 log("", "Success: Update information is complete.");
577 return true; /*success*/
579 failure:
580 updateInfo->resetInfo();
581 return false;
584 //----------------------------------------------------------
585 // EXTERNAL TOOLS
586 //----------------------------------------------------------
588 bool MUtils::UpdateChecker::getFile(const QUrl &url, const QString &outFile, const unsigned int maxRedir)
590 QFileInfo output(outFile);
591 output.setCaching(false);
593 if (output.exists())
595 QFile::remove(output.canonicalFilePath());
596 if (output.exists())
598 qWarning("Existing output file could not be found!");
599 return false;
603 QStringList args(QLatin1String("-vsSNqkfL"));
604 args << "-m" << QString::number(DOWNLOAD_TIMEOUT / 1000);
605 args << "--max-redirs" << QString::number(maxRedir);
606 args << "-A" << USER_AGENT_STR;
607 args << "-e" << QString("%1://%2/;auto").arg(url.scheme(), url.host());
608 args << "-o" << output.fileName() << url.toString();
610 return execCurl(args, output.absolutePath(), DOWNLOAD_TIMEOUT);
613 bool MUtils::UpdateChecker::tryContactHost(const QString &hostname, const int &timeoutMsec)
615 log(QString("Connecting to host: %1").arg(hostname), "");
617 QStringList args(QLatin1String("-vsSNqkI"));
618 args << "-m" << QString::number(qMax(1, timeoutMsec / 1000));
619 args << "-A" << USER_AGENT_STR;
620 args << "-o" << OS::null_device() << QString("http://%1/").arg(hostname);
622 return execCurl(args, temp_folder(), timeoutMsec);
625 bool MUtils::UpdateChecker::checkSignature(const QString &file, const QString &signature)
627 if (QFileInfo(file).absolutePath().compare(QFileInfo(signature).absolutePath(), Qt::CaseInsensitive) != 0)
629 qWarning("CheckSignature: File and signature should be in same folder!");
630 return false;
633 QString keyRingPath(m_binaryKeys);
634 bool removeKeyring = false;
635 if (QFileInfo(file).absolutePath().compare(QFileInfo(m_binaryKeys).absolutePath(), Qt::CaseInsensitive) != 0)
637 keyRingPath = make_temp_file(QFileInfo(file).absolutePath(), "gpg");
638 removeKeyring = true;
639 if (!QFile::copy(m_binaryKeys, keyRingPath))
641 qWarning("CheckSignature: Failed to copy the key-ring file!");
642 return false;
646 QStringList args;
647 args << QStringList() << "--homedir" << ".";
648 args << "--keyring" << QFileInfo(keyRingPath).fileName();
649 args << QFileInfo(signature).fileName();
650 args << QFileInfo(file).fileName();
652 const int exitCode = execProcess(m_binaryGnuPG, args, QFileInfo(file).absolutePath(), DOWNLOAD_TIMEOUT);
653 if (exitCode != INT_MAX)
655 log(QString().sprintf("Exited with code %d", exitCode));
658 if (removeKeyring)
660 remove_file(keyRingPath);
663 return (exitCode == 0); /*completed*/
666 bool MUtils::UpdateChecker::execCurl(const QStringList &args, const QString &workingDir, const int timeout)
668 const int exitCode = execProcess(m_binaryCurl, args, workingDir, timeout + (timeout / 2));
669 if (exitCode != INT_MAX)
671 switch (exitCode)
673 case 0: log(QLatin1String("DONE: Transfer completed successfully."), ""); break;
674 case 6: log(QLatin1String("ERROR: Remote host could not be resolved!"), ""); break;
675 case 7: log(QLatin1String("ERROR: Connection to remote host could not be established!"), ""); break;
676 case 22: log(QLatin1String("ERROR: Requested URL was not found or returned an error!"), ""); break;
677 case 28: log(QLatin1String("ERROR: Operation timed out !!!"), ""); break;
678 default: log(QString().sprintf("ERROR: Terminated with unknown code %d", exitCode), ""); break;
682 return (exitCode == 0); /*completed*/
685 int MUtils::UpdateChecker::execProcess(const QString &programFile, const QStringList &args, const QString &workingDir, const int timeout)
687 QProcess process;
688 init_process(process, workingDir, true, NULL, m_environment.data());
690 QEventLoop loop;
691 connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
692 connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), &loop, SLOT(quit()));
693 connect(&process, SIGNAL(readyRead()), &loop, SLOT(quit()));
695 QTimer timer;
696 timer.setSingleShot(true);
697 connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
699 process.start(programFile, args);
700 if (!process.waitForStarted())
702 log("PROCESS FAILED TO START !!!", "");
703 qWarning("WARNING: %s process could not be created!", MUTILS_UTF8(QFileInfo(programFile).fileName()));
704 return INT_MAX; /*failed to start*/
707 bool bAborted = false;
708 timer.start(qMax(timeout, 1500));
710 while (process.state() != QProcess::NotRunning)
712 loop.exec();
713 while (process.canReadLine())
715 const QString line = QString::fromLatin1(process.readLine()).simplified();
716 if (line.length() > 1)
718 log(line);
721 const bool bCancelled = MUTILS_BOOLIFY(m_cancelled);
722 if (bAborted = (bCancelled || ((!timer.isActive()) && (!process.waitForFinished(125)))))
724 log(bCancelled ? "CANCELLED BY USER !!!" : "PROCESS TIMEOUT !!!", "");
725 qWarning("WARNING: %s process %s!", MUTILS_UTF8(QFileInfo(programFile).fileName()), bCancelled ? "cancelled" : "timed out");
726 break; /*abort process*/
730 timer.stop();
731 timer.disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
733 if (bAborted)
735 process.kill();
736 process.waitForFinished(-1);
739 while (process.canReadLine())
741 const QString line = QString::fromLatin1(process.readLine()).simplified();
742 if (line.length() > 1)
744 log(line);
748 return bAborted ? INT_MAX : process.exitCode();
751 ////////////////////////////////////////////////////////////
752 // SLOTS
753 ////////////////////////////////////////////////////////////
755 /*NONE*/
757 ////////////////////////////////////////////////////////////
758 // EVENTS
759 ////////////////////////////////////////////////////////////
761 /*NONE*/