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>
41 ///////////////////////////////////////////////////////////////////////////////
43 ///////////////////////////////////////////////////////////////////////////////
45 static const char *header_id = "!Update";
47 static const char *mirror_url_postfix[] =
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 #define CHECK_CANCELLED() do \
64 if(MUTILS_BOOLIFY(m_cancelled)) \
66 m_success.fetchAndStoreOrdered(0); \
67 log("", "Update check has been cancelled by user!"); \
68 setProgress(m_maxProgress); \
69 setStatus(UpdateStatus_CancelledByUser); \
75 ////////////////////////////////////////////////////////////
77 ////////////////////////////////////////////////////////////
79 static int getMaxProgress(void)
82 while (update_mirrors[counter])
86 counter += MIN_CONNSCORE + QUICK_MIRRORS + 2;
90 static QStringList buildRandomList(const char *const values[])
93 for (int index = 0; values[index]; index++)
95 const int pos = MUtils::next_rand_u32() % (index + 1);
96 list.insert(pos, QString::fromLatin1(values[index]));
101 static const QHash<QString, QString> *initEnvVars(void)
103 QHash<QString, QString> *const environment = new QHash<QString, QString>();
104 environment->insert(QLatin1String("CURL_HOME"), QDir::toNativeSeparators(MUtils::temp_folder()));
108 ////////////////////////////////////////////////////////////
110 ////////////////////////////////////////////////////////////
112 MUtils::UpdateCheckerInfo::UpdateCheckerInfo(void)
117 void MUtils::UpdateCheckerInfo::resetInfo(void)
120 m_buildDate.setDate(1900, 1, 1);
121 m_downloadSite.clear();
122 m_downloadAddress.clear();
123 m_downloadFilename.clear();
124 m_downloadFilecode.clear();
125 m_downloadChecksum.clear();
128 bool MUtils::UpdateCheckerInfo::isComplete(void)
130 if(this->m_buildNo < 1) return false;
131 if(this->m_buildDate.year() < 2010) return false;
132 if(this->m_downloadSite.isEmpty()) return false;
133 if(this->m_downloadAddress.isEmpty()) return false;
134 if(this->m_downloadFilename.isEmpty()) return false;
135 if(this->m_downloadFilecode.isEmpty()) return false;
136 if(this->m_downloadChecksum.isEmpty()) return false;
141 ////////////////////////////////////////////////////////////
142 // Constructor & Destructor
143 ////////////////////////////////////////////////////////////
145 MUtils::UpdateChecker::UpdateChecker(const QString &binCurl, const QString &binGnuPG, const QString &binKeys, const QString &applicationId, const quint32 &installedBuildNo, const bool betaUpdates, const bool testMode)
147 m_updateInfo(new UpdateCheckerInfo()),
148 m_binaryCurl(binCurl),
149 m_binaryGnuPG(binGnuPG),
150 m_binaryKeys(binKeys),
151 m_applicationId(applicationId),
152 m_installedBuildNo(installedBuildNo),
153 m_betaUpdates(betaUpdates),
154 m_testMode(testMode),
155 m_maxProgress(getMaxProgress()),
156 m_environment(initEnvVars())
158 m_status = UpdateStatus_NotStartedYet;
161 if(m_binaryCurl.isEmpty() || m_binaryGnuPG.isEmpty() || m_binaryKeys.isEmpty())
163 MUTILS_THROW("Tools not initialized correctly!");
167 MUtils::UpdateChecker::~UpdateChecker(void)
171 ////////////////////////////////////////////////////////////
173 ////////////////////////////////////////////////////////////
175 void MUtils::UpdateChecker::start(Priority priority)
177 m_success.fetchAndStoreOrdered(0);
178 m_cancelled.fetchAndStoreOrdered(0);
179 QThread::start(priority);
182 ////////////////////////////////////////////////////////////
183 // Protected functions
184 ////////////////////////////////////////////////////////////
186 void MUtils::UpdateChecker::run(void)
188 qDebug("Update checker thread started!");
189 MUTILS_EXCEPTION_HANDLER(m_testMode ? testMirrorsList() : checkForUpdates());
190 qDebug("Update checker thread completed.");
193 void MUtils::UpdateChecker::checkForUpdates(void)
195 // ----- Initialization ----- //
197 m_updateInfo->resetInfo();
200 // ----- Test Internet Connection ----- //
202 log("Checking your Internet connection...", "");
203 setStatus(UpdateStatus_CheckingConnection);
205 const int networkStatus = OS::network_status();
206 if(networkStatus == OS::NETWORK_TYPE_NON)
208 if (!MUtils::OS::arguments().contains("ignore-network-status"))
210 log("Operating system reports that the computer is currently offline !!!");
211 setProgress(m_maxProgress);
212 setStatus(UpdateStatus_ErrorNoConnection);
220 // ----- Test Known Hosts Connectivity ----- //
222 int connectionScore = 0;
223 QStringList mirrorList = buildRandomList(known_hosts);
225 for(int connectionTimout = 1000; connectionTimout <= MAX_CONN_TIMEOUT; connectionTimout *= 2)
227 QElapsedTimer elapsedTimer;
228 elapsedTimer.start();
229 const int globalTimout = 2 * MIN_CONNSCORE * connectionTimout;
230 while (!elapsedTimer.hasExpired(globalTimout))
232 const QString hostName = mirrorList.takeFirst();
233 if (tryContactHost(hostName, connectionTimout))
235 connectionScore += 1;
236 setProgress(qBound(1, connectionScore + 1, MIN_CONNSCORE + 1));
237 elapsedTimer.restart();
238 if (connectionScore >= MIN_CONNSCORE)
240 goto endLoop; /*success*/
245 mirrorList.append(hostName); /*re-schedule*/
253 if(connectionScore < MIN_CONNSCORE)
255 log("", "Connectivity test has failed: Internet connection appears to be broken!");
256 setProgress(m_maxProgress);
257 setStatus(UpdateStatus_ErrorConnectionTestFailed);
261 // ----- Fetch Update Info From Server ----- //
263 log("----", "", "Internet connection is operational, checking for updates online...");
264 setStatus(UpdateStatus_FetchingUpdates);
267 mirrorList = buildRandomList(update_mirrors);
269 while(!mirrorList.isEmpty())
271 setProgress(m_progress + 1);
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*/
281 mirrorList.append(currentMirror); /*re-schedule*/
287 while (m_progress < m_maxProgress)
290 setProgress(m_progress + 1);
294 // ----- Generate final result ----- //
296 if(MUTILS_BOOLIFY(m_success))
298 if(m_updateInfo->m_buildNo > m_installedBuildNo)
300 setStatus(UpdateStatus_CompletedUpdateAvailable);
302 else if(m_updateInfo->m_buildNo == m_installedBuildNo)
304 setStatus(UpdateStatus_CompletedNoUpdates);
308 setStatus(UpdateStatus_CompletedNewVersionOlder);
313 setStatus(UpdateStatus_ErrorFetchUpdateInfo);
317 void MUtils::UpdateChecker::testMirrorsList(void)
319 // ----- Test update mirrors ----- //
321 QStringList mirrorList;
322 for(int i = 0; update_mirrors[i]; i++)
324 mirrorList << QString::fromLatin1(update_mirrors[i]);
327 qDebug("\n[Mirror Sites]");
328 log("Testing all known mirror sites...", "", "---");
330 UpdateCheckerInfo updateInfo;
331 while (!mirrorList.isEmpty())
333 const QString currentMirror = mirrorList.takeFirst();
334 bool success = false;
335 qDebug("Testing: %s", MUTILS_L1STR(currentMirror));
336 log("", "Testing:", currentMirror, "");
337 for (quint8 attempt = 0; attempt < 3; ++attempt)
339 updateInfo.resetInfo();
340 if (tryUpdateMirror(&updateInfo, currentMirror, (!attempt)))
348 qWarning("\nUpdate mirror seems to be unavailable:\n%s\n", MUTILS_L1STR(currentMirror));
353 // ----- Test known hosts ----- //
355 QStringList knownHostList;
356 for (int i = 0; known_hosts[i]; i++)
358 knownHostList << QString::fromLatin1(known_hosts[i]);
361 qDebug("\n[Known Hosts]");
362 log("Testing all known hosts...", "", "---");
364 while(!knownHostList.isEmpty())
366 const QString currentHost = knownHostList.takeFirst();
367 qDebug("Testing: %s", MUTILS_L1STR(currentHost));
368 log(QLatin1String(""), "Testing:", currentHost, "");
369 if (!tryContactHost(currentHost, DOWNLOAD_TIMEOUT))
371 qWarning("\nConnectivity test FAILED on the following host:\n%s\n", MUTILS_L1STR(currentHost));
377 ////////////////////////////////////////////////////////////
379 ////////////////////////////////////////////////////////////
381 void MUtils::UpdateChecker::setStatus(const int status)
383 if(m_status != status)
386 emit statusChanged(status);
390 void MUtils::UpdateChecker::setProgress(const int progress)
392 if(m_progress != progress)
394 m_progress = progress;
395 emit progressChanged(progress);
399 void MUtils::UpdateChecker::log(const QString &str1, const QString &str2, const QString &str3, const QString &str4)
401 if(!str1.isNull()) emit messageLogged(str1);
402 if(!str2.isNull()) emit messageLogged(str2);
403 if(!str3.isNull()) emit messageLogged(str3);
404 if(!str4.isNull()) emit messageLogged(str4);
407 bool MUtils::UpdateChecker::tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString &url, const bool &quick)
409 bool success = false;
410 log("", "Trying mirror:", url, "");
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("Download completed, verifying signature:", "");
428 if (checkSignature(outFileVers, outFileSign))
430 log("", "Signature is valid, parsing info:", "");
431 success = parseVersionInfo(outFileVers, updateInfo);
435 log("", "Bad signature, take care !!!");
440 log("", "Download has failed!");
443 QFile::remove(outFileVers);
444 QFile::remove(outFileSign);
449 bool MUtils::UpdateChecker::getUpdateInfo(const QString &url, const QString &outFileVers, const QString &outFileSign)
451 log("Downloading update info:", "");
452 if(getFile(QUrl(QString("%1%2").arg(url, mirror_url_postfix[m_betaUpdates ? 1 : 0])), outFileVers))
456 log( "Downloading signature file:", "");
457 if (getFile(QUrl(QString("%1%2.sig2").arg(url, mirror_url_postfix[m_betaUpdates ? 1 : 0])), outFileSign))
466 bool MUtils::UpdateChecker::parseVersionInfo(const QString &file, UpdateCheckerInfo *updateInfo)
468 QRegExp value("^(\\w+)=(.+)$");
469 QRegExp section("^\\[(.+)\\]$");
471 QDate updateInfoDate;
472 updateInfo->resetInfo();
475 if(!data.open(QIODevice::ReadOnly))
477 qWarning("Cannot open update info file for reading!");
486 QString line = QString::fromLatin1(data.readLine()).trimmed();
487 if(section.indexIn(line) >= 0)
489 log(QString("Sec: [%1]").arg(section.cap(1)));
490 inSec = (section.cap(1).compare(m_applicationId, Qt::CaseInsensitive) == 0);
491 inHdr = (section.cap(1).compare(QString::fromLatin1(header_id), Qt::CaseInsensitive) == 0);
494 if(inSec && (value.indexIn(line) >= 0))
496 log(QString("Val: '%1' ==> '%2").arg(value.cap(1), value.cap(2)));
497 if(value.cap(1).compare("BuildNo", Qt::CaseInsensitive) == 0)
500 const unsigned int temp = value.cap(2).toUInt(&ok);
501 if(ok) updateInfo->m_buildNo = temp;
503 else if(value.cap(1).compare("BuildDate", Qt::CaseInsensitive) == 0)
505 const QDate temp = QDate::fromString(value.cap(2).trimmed(), Qt::ISODate);
506 if(temp.isValid()) updateInfo->m_buildDate = temp;
508 else if(value.cap(1).compare("DownloadSite", Qt::CaseInsensitive) == 0)
510 updateInfo->m_downloadSite = value.cap(2).trimmed();
512 else if(value.cap(1).compare("DownloadAddress", Qt::CaseInsensitive) == 0)
514 updateInfo->m_downloadAddress = value.cap(2).trimmed();
516 else if(value.cap(1).compare("DownloadFilename", Qt::CaseInsensitive) == 0)
518 updateInfo->m_downloadFilename = value.cap(2).trimmed();
520 else if(value.cap(1).compare("DownloadFilecode", Qt::CaseInsensitive) == 0)
522 updateInfo->m_downloadFilecode = value.cap(2).trimmed();
524 else if(value.cap(1).compare("DownloadChecksum", Qt::CaseInsensitive) == 0)
526 updateInfo->m_downloadChecksum = value.cap(2).trimmed();
529 if(inHdr && (value.indexIn(line) >= 0))
531 log(QString("Val: '%1' ==> '%2").arg(value.cap(1), value.cap(2)));
532 if(value.cap(1).compare("TimestampCreated", Qt::CaseInsensitive) == 0)
534 QDate temp = QDate::fromString(value.cap(2).trimmed(), Qt::ISODate);
535 if(temp.isValid()) updateInfoDate = temp;
540 if(!updateInfoDate.isValid())
542 updateInfo->resetInfo();
543 log("WARNING: Version info timestamp is missing!");
547 const QDate currentDate = OS::current_date();
548 if(updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS) < currentDate)
550 updateInfo->resetInfo();
551 log(QString::fromLatin1("WARNING: This version info has expired at %1!").arg(updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS).toString(Qt::ISODate)));
554 else if(currentDate < updateInfoDate)
556 log("Version info is from the future, take care!");
557 qWarning("Version info is from the future, take care!");
560 if(!updateInfo->isComplete())
562 log("WARNING: Version info is incomplete!");
569 //----------------------------------------------------------
571 //----------------------------------------------------------
573 bool MUtils::UpdateChecker::getFile(const QUrl &url, const QString &outFile, const unsigned int maxRedir)
575 QFileInfo output(outFile);
576 output.setCaching(false);
580 QFile::remove(output.canonicalFilePath());
583 qWarning("Existing output file could not be found!");
588 QStringList args(QLatin1String("-vsSNqkfL"));
589 args << "-m" << QString::number(DOWNLOAD_TIMEOUT / 1000);
590 args << "--max-redirs" << QString::number(maxRedir);
591 args << "-A" << USER_AGENT_STR;
592 args << "-e" << QString("%1://%2/;auto").arg(url.scheme(), url.host());
593 args << "-o" << output.fileName() << url.toString();
595 return execCurl(args, output.absolutePath(), DOWNLOAD_TIMEOUT);
598 bool MUtils::UpdateChecker::tryContactHost(const QString &hostname, const int &timeoutMsec)
600 log(QString("Connecting to host: %1").arg(hostname), "");
602 QStringList args(QLatin1String("-vsSNqkI"));
603 args << "-m" << QString::number(qMax(1, timeoutMsec / 1000));
604 args << "-A" << USER_AGENT_STR;
605 args << "-o" << OS::null_device() << QString("http://%1/").arg(hostname);
607 return execCurl(args, temp_folder(), timeoutMsec);
610 bool MUtils::UpdateChecker::checkSignature(const QString &file, const QString &signature)
612 if (QFileInfo(file).absolutePath().compare(QFileInfo(signature).absolutePath(), Qt::CaseInsensitive) != 0)
614 qWarning("CheckSignature: File and signature should be in same folder!");
618 QString keyRingPath(m_binaryKeys);
619 bool removeKeyring = false;
620 if (QFileInfo(file).absolutePath().compare(QFileInfo(m_binaryKeys).absolutePath(), Qt::CaseInsensitive) != 0)
622 keyRingPath = make_temp_file(QFileInfo(file).absolutePath(), "gpg");
623 removeKeyring = true;
624 if (!QFile::copy(m_binaryKeys, keyRingPath))
626 qWarning("CheckSignature: Failed to copy the key-ring file!");
632 args << QStringList() << "--homedir" << ".";
633 args << "--keyring" << QFileInfo(keyRingPath).fileName();
634 args << QFileInfo(signature).fileName();
635 args << QFileInfo(file).fileName();
637 const int exitCode = execProcess(m_binaryGnuPG, args, QFileInfo(file).absolutePath(), DOWNLOAD_TIMEOUT);
638 if (exitCode != INT_MAX)
640 log(QString().sprintf("Exited with code %d", exitCode));
645 remove_file(keyRingPath);
648 return (exitCode == 0); /*completed*/
651 bool MUtils::UpdateChecker::execCurl(const QStringList &args, const QString &workingDir, const int timeout)
653 const int exitCode = execProcess(m_binaryCurl, args, workingDir, timeout + (timeout / 2));
654 if (exitCode != INT_MAX)
659 case 0: log(QLatin1String("DONE: Transfer completed successfully."), ""); break;
660 case 6: log(QLatin1String("ERROR: Remote host could not be resolved!"), ""); break;
661 case 7: log(QLatin1String("ERROR: Connection to remote host could not be established!"), ""); break;
662 case 22: log(QLatin1String("ERROR: Requested URL was not found or returned an error!"), ""); break;
663 case 28: log(QLatin1String("ERROR: Operation timed out !!!"), ""); break;
664 default: log(QString().sprintf("ERROR: Terminated with unknown code %d", exitCode), ""); break;
668 return (exitCode == 0); /*completed*/
671 int MUtils::UpdateChecker::execProcess(const QString &programFile, const QStringList &args, const QString &workingDir, const int timeout)
674 init_process(process, workingDir, true, NULL, m_environment.data());
677 connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
678 connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), &loop, SLOT(quit()));
679 connect(&process, SIGNAL(readyRead()), &loop, SLOT(quit()));
682 timer.setSingleShot(true);
683 connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
685 process.start(programFile, args);
686 if (!process.waitForStarted())
688 log("PROCESS FAILED TO START !!!", "");
689 qWarning("WARNING: %s process could not be created!", MUTILS_UTF8(QFileInfo(programFile).fileName()));
690 return INT_MAX; /*failed to start*/
693 bool bAborted = false;
694 timer.start(qMax(timeout, 1500));
696 while (process.state() != QProcess::NotRunning)
699 while (process.canReadLine())
701 const QString line = QString::fromLatin1(process.readLine()).simplified();
702 if ((!line.isEmpty()) && line.compare(QLatin1String("<")) && line.compare(QLatin1String(">")))
707 const bool bCancelled = MUTILS_BOOLIFY(m_cancelled);
708 if (bAborted = (bCancelled || ((!timer.isActive()) && (!process.waitForFinished(125)))))
710 log(bCancelled ? "CANCELLED BY USER !!!" : "PROCESS TIMEOUT !!!", "");
711 qWarning("WARNING: %s process %s!", MUTILS_UTF8(QFileInfo(programFile).fileName()), bCancelled ? "cancelled" : "timed out");
712 break; /*abort process*/
717 timer.disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
722 process.waitForFinished(-1);
725 while (process.canReadLine())
727 const QString line = QString::fromLatin1(process.readLine()).simplified();
728 if ((!line.isEmpty()) && line.compare(QLatin1String("<")) && line.compare(QLatin1String(">")))
734 return bAborted ? INT_MAX : process.exitCode();
737 ////////////////////////////////////////////////////////////
739 ////////////////////////////////////////////////////////////
743 ////////////////////////////////////////////////////////////
745 ////////////////////////////////////////////////////////////