1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2018 LoRd_MuldeR <MuldeR2@GMX.de>
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.
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>
35 #include <QElapsedTimer>
42 ///////////////////////////////////////////////////////////////////////////////
44 ///////////////////////////////////////////////////////////////////////////////
46 static const char *GLOBALHEADER_ID = "!Update";
48 static const char *MIRROR_URL_POSTFIX[] =
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 ////////////////////////////////////////////////////////////
65 ////////////////////////////////////////////////////////////
67 #define CHECK_CANCELLED() do \
69 if(MUTILS_BOOLIFY(m_cancelled)) \
71 m_success.fetchAndStoreOrdered(0); \
72 log("", "Update check has been cancelled by user!"); \
73 setProgress(m_maxProgress); \
74 setStatus(UpdateStatus_CancelledByUser); \
80 #define LOG_MESSAGE_HELPER(X) do \
84 emit messageLogged((X)); \
89 #define STRICMP(X,Y) ((X).compare((Y), Qt::CaseInsensitive) == 0)
91 ////////////////////////////////////////////////////////////
93 ////////////////////////////////////////////////////////////
95 static QQueue<QString> buildRandomList(const char *const *values)
100 list.insert(MUtils::next_rand_u32(list.size() + 1), QString::fromLatin1(*(values++)));
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()));
112 ////////////////////////////////////////////////////////////
114 ////////////////////////////////////////////////////////////
116 MUtils::UpdateCheckerInfo::UpdateCheckerInfo(void)
121 void MUtils::UpdateCheckerInfo::resetInfo(void)
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;
163 if(m_binaryCurl.isEmpty() || m_binaryGnuPG.isEmpty() || m_binaryKeys.isEmpty())
165 MUTILS_THROW("Tools not initialized correctly!");
169 MUtils::UpdateChecker::~UpdateChecker(void)
173 ////////////////////////////////////////////////////////////
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();
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);
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 int globalTimeout = 2 * MIN_CONNSCORE * connectionTimeout, count = mirrorList.count();
232 for(int i = 0; i < count; ++i)
234 Q_ASSERT(!mirrorList.isEmpty());
235 const QString hostName = mirrorList.dequeue();
236 if (tryContactHost(hostName, connectionTimeout))
238 setProgress(1 + (++connectionScore));
239 if (connectionScore >= MIN_CONNSCORE)
241 goto endLoop; /*success*/
246 mirrorList.enqueue(hostName);
247 if(elapsedTimer.hasExpired(globalTimeout))
249 break; /*timer expired*/
257 if(connectionScore < MIN_CONNSCORE)
259 log("", "Connectivity test has failed: Internet connection appears to be broken!");
260 setProgress(m_maxProgress);
261 setStatus(UpdateStatus_ErrorConnectionTestFailed);
265 // ----- Fetch Update Info From Server ----- //
267 log("----", "", "Internet connection is operational, checking for updates online...");
268 setStatus(UpdateStatus_FetchingUpdates);
271 mirrorList = buildRandomList(update_mirrors);
273 while(!mirrorList.isEmpty())
275 const QString currentMirror = mirrorList.takeFirst();
276 const bool isQuick = (mirrorCount++ < QUICK_MIRRORS);
277 if(tryUpdateMirror(m_updateInfo.data(), currentMirror, isQuick))
279 m_success.ref(); /*success*/
284 mirrorList.append(currentMirror); /*re-schedule*/
291 setProgress(MIN_CONNSCORE + 5);
293 // ----- Generate final result ----- //
295 if(MUTILS_BOOLIFY(m_success))
297 if(m_updateInfo->m_buildNo > m_installedBuildNo)
299 setStatus(UpdateStatus_CompletedUpdateAvailable);
301 else if(m_updateInfo->m_buildNo == m_installedBuildNo)
303 setStatus(UpdateStatus_CompletedNoUpdates);
307 setStatus(UpdateStatus_CompletedNewVersionOlder);
312 setStatus(UpdateStatus_ErrorFetchUpdateInfo);
316 void MUtils::UpdateChecker::testMirrorsList(void)
318 QQueue<QString> mirrorList;
319 for(int i = 0; update_mirrors[i]; i++)
321 mirrorList.enqueue(QString::fromLatin1(update_mirrors[i]));
324 // ----- Test update mirrors ----- //
326 qDebug("\n[Mirror Sites]");
327 log("Testing all known mirror sites...", "", "---");
329 UpdateCheckerInfo updateInfo;
330 while (!mirrorList.isEmpty())
332 const QString currentMirror = mirrorList.dequeue();
333 bool success = false;
334 qDebug("Testing: %s", MUTILS_L1STR(currentMirror));
335 log("", "Testing mirror:", currentMirror, "");
336 for (quint8 attempt = 0; attempt < 3; ++attempt)
338 updateInfo.resetInfo();
339 if (tryUpdateMirror(&updateInfo, currentMirror, (!attempt)))
347 qWarning("\nUpdate mirror seems to be unavailable:\n%s\n", MUTILS_L1STR(currentMirror));
352 // ----- Test known hosts ----- //
355 for (int i = 0; known_hosts[i]; i++)
357 mirrorList.enqueue(QString::fromLatin1(known_hosts[i]));
360 qDebug("\n[Known Hosts]");
361 log("Testing all known hosts...", "", "---");
363 while(!mirrorList.isEmpty())
365 const QString currentHost = mirrorList.dequeue();
366 qDebug("Testing: %s", MUTILS_L1STR(currentHost));
367 log(QLatin1String(""), "Testing host:", currentHost, "");
368 if (!tryContactHost(currentHost, DOWNLOAD_TIMEOUT))
370 qWarning("\nConnectivity test FAILED on the following host:\n%s\n", MUTILS_L1STR(currentHost));
376 ////////////////////////////////////////////////////////////
378 ////////////////////////////////////////////////////////////
380 void MUtils::UpdateChecker::setStatus(const int status)
382 if(m_status != status)
385 emit statusChanged(status);
389 void MUtils::UpdateChecker::setProgress(const int progress)
391 const int value = qBound(0, progress, m_maxProgress);
392 if(m_progress != value)
394 emit progressChanged(m_progress = value);
398 void MUtils::UpdateChecker::log(const QString &str1, const QString &str2, const QString &str3, const QString &str4)
400 LOG_MESSAGE_HELPER(str1);
401 LOG_MESSAGE_HELPER(str2);
402 LOG_MESSAGE_HELPER(str3);
403 LOG_MESSAGE_HELPER(str4);
406 bool MUtils::UpdateChecker::tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString &url, const bool &quick)
408 bool success = false;
409 log("", "Trying update mirror:", url, "");
413 setProgress(MIN_CONNSCORE + 1);
414 if (!tryContactHost(QUrl(url).host(), (MAX_CONN_TIMEOUT / 8)))
416 log("", "Mirror is too slow, skipping!");
421 const QString randPart = next_rand_str();
422 const QString outFileVers = QString("%1/%2.ver").arg(temp_folder(), randPart);
423 const QString outFileSign = QString("%1/%2.sig").arg(temp_folder(), randPart);
425 if (!getUpdateInfo(url, outFileVers, outFileSign))
427 log("", "Oops: Download of update information has failed!");
431 log("Download completed, verifying signature:", "");
432 setProgress(MIN_CONNSCORE + 4);
433 if (!checkSignature(outFileVers, outFileSign))
435 log("", "Bad signature detected, take care !!!");
439 log("", "Signature is valid, parsing update information:", "");
440 success = parseVersionInfo(outFileVers, updateInfo);
443 QFile::remove(outFileVers);
444 QFile::remove(outFileSign);
448 bool MUtils::UpdateChecker::getUpdateInfo(const QString &url, const QString &outFileVers, const QString &outFileSign)
450 log("Downloading update information:", "");
451 setProgress(MIN_CONNSCORE + 2);
452 if(getFile(QUrl(QString("%1%2").arg(url, MIRROR_URL_POSTFIX[m_betaUpdates ? 1 : 0])), outFileVers))
456 log( "Downloading signature file:", "");
457 setProgress(MIN_CONNSCORE + 3);
458 if (getFile(QUrl(QString("%1%2.sig2").arg(url, MIRROR_URL_POSTFIX[m_betaUpdates ? 1 : 0])), outFileSign))
460 return true; /*completed*/
467 //----------------------------------------------------------
469 //----------------------------------------------------------
471 #define _CHECK_HEADER(ID,NAME) \
472 if (STRICMP(name, (NAME))) \
478 #define _PARSE_TEXT(OUT,KEY) \
479 if (STRICMP(key, (KEY))) \
485 #define _PARSE_UINT(OUT,KEY) \
486 if (STRICMP(key, (KEY))) \
489 const unsigned int _tmp = val.toUInt(&_ok); \
497 #define _PARSE_DATE(OUT,KEY) \
498 if (STRICMP(key, (KEY))) \
500 const QDate _tmp = QDate::fromString(val, Qt::ISODate); \
501 if (_tmp.isValid()) \
508 bool MUtils::UpdateChecker::parseVersionInfo(const QString &file, UpdateCheckerInfo *const updateInfo)
510 updateInfo->resetInfo();
513 if(!data.open(QIODevice::ReadOnly))
515 qWarning("Cannot open update info file for reading!");
519 QDate updateInfoDate;
521 QRegExp regex_sec("^\\[(.+)\\]$"), regex_val("^([^=]+)=(.+)$");
525 QString line = QString::fromLatin1(data.readLine()).trimmed();
526 if (regex_sec.indexIn(line) >= 0)
528 sectionId = 0; /*unknown section*/
529 const QString name = regex_sec.cap(1).trimmed();
530 log(QString("Sec: [%1]").arg(name));
531 _CHECK_HEADER(1, GLOBALHEADER_ID)
532 _CHECK_HEADER(2, m_applicationId)
535 if (regex_val.indexIn(line) >= 0)
537 const QString key = regex_val.cap(1).trimmed();
538 const QString val = regex_val.cap(2).trimmed();
539 log(QString("Val: \"%1\" = \"%2\"").arg(key, val));
543 _PARSE_DATE(updateInfoDate, "TimestampCreated")
546 _PARSE_UINT(updateInfo->m_buildNo, "BuildNo")
547 _PARSE_DATE(updateInfo->m_buildDate, "BuildDate")
548 _PARSE_TEXT(updateInfo->m_downloadSite, "DownloadSite")
549 _PARSE_TEXT(updateInfo->m_downloadAddress, "DownloadAddress")
550 _PARSE_TEXT(updateInfo->m_downloadFilename, "DownloadFilename")
551 _PARSE_TEXT(updateInfo->m_downloadFilecode, "DownloadFilecode")
552 _PARSE_TEXT(updateInfo->m_downloadChecksum, "DownloadChecksum")
558 if (!updateInfo->isComplete())
560 log("", "WARNING: Update information is incomplete!");
564 if(updateInfoDate.isValid())
566 const QDate expiredDate = updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS);
567 if (expiredDate < OS::current_date())
569 log("", QString("WARNING: Update information has expired at %1!").arg(expiredDate.toString(Qt::ISODate)));
575 log("", "WARNING: Timestamp is missing from update information header!");
579 log("", "Success: Update information is complete.");
580 return true; /*success*/
583 updateInfo->resetInfo();
587 //----------------------------------------------------------
589 //----------------------------------------------------------
591 bool MUtils::UpdateChecker::getFile(const QUrl &url, const QString &outFile, const unsigned int maxRedir)
593 QFileInfo output(outFile);
594 output.setCaching(false);
598 QFile::remove(output.canonicalFilePath());
601 qWarning("Existing output file could not be found!");
606 QStringList args(QLatin1String("-vsSNqkfL"));
607 args << "-m" << QString::number(DOWNLOAD_TIMEOUT / 1000);
608 args << "--max-redirs" << QString::number(maxRedir);
609 args << "-A" << USER_AGENT_STR;
610 args << "-e" << QString("%1://%2/;auto").arg(url.scheme(), url.host());
611 args << "-o" << output.fileName() << url.toString();
613 return execCurl(args, output.absolutePath(), DOWNLOAD_TIMEOUT);
616 bool MUtils::UpdateChecker::tryContactHost(const QString &hostname, const int &timeoutMsec)
618 log(QString("Connecting to host: %1").arg(hostname), "");
620 QStringList args(QLatin1String("-vsSNqkI"));
621 args << "-m" << QString::number(qMax(1, timeoutMsec / 1000));
622 args << "-A" << USER_AGENT_STR;
623 args << "-o" << OS::null_device() << QString("http://%1/").arg(hostname);
625 return execCurl(args, temp_folder(), timeoutMsec);
628 bool MUtils::UpdateChecker::checkSignature(const QString &file, const QString &signature)
630 if (QFileInfo(file).absolutePath().compare(QFileInfo(signature).absolutePath(), Qt::CaseInsensitive) != 0)
632 qWarning("CheckSignature: File and signature should be in same folder!");
636 QString keyRingPath(m_binaryKeys);
637 bool removeKeyring = false;
638 if (QFileInfo(file).absolutePath().compare(QFileInfo(m_binaryKeys).absolutePath(), Qt::CaseInsensitive) != 0)
640 keyRingPath = make_temp_file(QFileInfo(file).absolutePath(), "gpg");
641 removeKeyring = true;
642 if (!QFile::copy(m_binaryKeys, keyRingPath))
644 qWarning("CheckSignature: Failed to copy the key-ring file!");
650 args << QStringList() << "--homedir" << ".";
651 args << "--keyring" << QFileInfo(keyRingPath).fileName();
652 args << QFileInfo(signature).fileName();
653 args << QFileInfo(file).fileName();
655 const int exitCode = execProcess(m_binaryGnuPG, args, QFileInfo(file).absolutePath(), DOWNLOAD_TIMEOUT);
656 if (exitCode != INT_MAX)
658 log(QString().sprintf("Exited with code %d", exitCode));
663 remove_file(keyRingPath);
666 return (exitCode == 0); /*completed*/
669 bool MUtils::UpdateChecker::execCurl(const QStringList &args, const QString &workingDir, const int timeout)
671 const int exitCode = execProcess(m_binaryCurl, args, workingDir, timeout + (timeout / 2));
672 if (exitCode != INT_MAX)
676 case 0: log(QLatin1String("DONE: Transfer completed successfully."), ""); break;
677 case 6: log(QLatin1String("ERROR: Remote host could not be resolved!"), ""); break;
678 case 7: log(QLatin1String("ERROR: Connection to remote host could not be established!"), ""); break;
679 case 22: log(QLatin1String("ERROR: Requested URL was not found or returned an error!"), ""); break;
680 case 28: log(QLatin1String("ERROR: Operation timed out !!!"), ""); break;
681 default: log(QString().sprintf("ERROR: Terminated with unknown code %d", exitCode), ""); break;
685 return (exitCode == 0); /*completed*/
688 int MUtils::UpdateChecker::execProcess(const QString &programFile, const QStringList &args, const QString &workingDir, const int timeout)
691 init_process(process, workingDir, true, NULL, m_environment.data());
694 connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
695 connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), &loop, SLOT(quit()));
696 connect(&process, SIGNAL(readyRead()), &loop, SLOT(quit()));
699 timer.setSingleShot(true);
700 connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
702 process.start(programFile, args);
703 if (!process.waitForStarted())
705 log("PROCESS FAILED TO START !!!", "");
706 qWarning("WARNING: %s process could not be created!", MUTILS_UTF8(QFileInfo(programFile).fileName()));
707 return INT_MAX; /*failed to start*/
710 bool bAborted = false;
711 timer.start(qMax(timeout, 1500));
713 while (process.state() != QProcess::NotRunning)
716 while (process.canReadLine())
718 const QString line = QString::fromLatin1(process.readLine()).simplified();
719 if (line.length() > 1)
724 const bool bCancelled = MUTILS_BOOLIFY(m_cancelled);
725 if (bAborted = (bCancelled || ((!timer.isActive()) && (!process.waitForFinished(125)))))
727 log(bCancelled ? "CANCELLED BY USER !!!" : "PROCESS TIMEOUT !!!", "");
728 qWarning("WARNING: %s process %s!", MUTILS_UTF8(QFileInfo(programFile).fileName()), bCancelled ? "cancelled" : "timed out");
729 break; /*abort process*/
734 timer.disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
739 process.waitForFinished(-1);
742 while (process.canReadLine())
744 const QString line = QString::fromLatin1(process.readLine()).simplified();
745 if (line.length() > 1)
751 return bAborted ? INT_MAX : process.exitCode();
754 ////////////////////////////////////////////////////////////
756 ////////////////////////////////////////////////////////////
760 ////////////////////////////////////////////////////////////
762 ////////////////////////////////////////////////////////////