1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2016 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 using namespace MUtils
;
37 ///////////////////////////////////////////////////////////////////////////////
39 ///////////////////////////////////////////////////////////////////////////////
41 static const char *header_id
= "!Update";
43 static const char *mirror_url_postfix
[] =
50 static const char *update_mirrors
[] =
52 "http://muldersoft.com/",
53 "http://mulder.bplaced.net/", //"http://mulder.cwsurf.de/",
54 "http://mulder.6te.net/",
55 "http://mulder.webuda.com/", //"http://mulder.byethost13.com/",
56 "http://mulder.pe.hu/", //"http://muldersoft.kilu.de/",
57 "http://muldersoft.square7.ch/", //"http://muldersoft.zxq.net/",
58 "http://muldersoft.co.nf/",
59 "http://muldersoft.eu.pn/",
60 "http://muldersoft.lima-city.de/",
61 "http://www.muldersoft.keepfree.de/",
62 "http://lamexp.sourceforge.net/",
63 "http://lordmulder.github.io/LameXP/",
64 "http://lord_mulder.bitbucket.org/",
65 "http://www.tricksoft.de/",
69 static const char *known_hosts
[] = //Taken form: http://www.alexa.com/topsites !!!
74 "clbianco.altervista.org",
75 "status.aws.amazon.com",
83 "web.audacityteam.org",
84 "status.automattic.com",
92 "www.bingeandgrab.com",
93 "www.bucketheadpikes.com",
94 "www.buckethead-coop.com",
97 "www.citizeninsomniac.com",
101 "www.codeproject.com",
102 "www.der-postillon.com",
121 "www.huffingtonpost.co.uk",
127 "www.jiscdigitalmedia.ac.uk",
128 "kannmanumdieuhrzeitschonnbierchentrinken.de",
129 "mirrors.kernel.org",
131 "lame.sourceforge.net",
136 "www.livejournal.com",
141 "www.mod-technologies.com",
145 "wiki.multimedia.cx",
147 "mirror.netcologne.de",
157 "www.portablefreeware.com",
158 "support.proboards.com",
163 "www.seamonkey-project.org",
170 "www.sputnikmusic.com",
174 "tdm-gcc.tdragon.net",
181 "blog.virustotal.com",
182 "www.vkgoeswild.com",
185 "status.wikimedia.org",
195 "ffmpeg.zeranoe.com",
199 static const int MIN_CONNSCORE
= 5;
200 static const int VERSION_INFO_EXPIRES_MONTHS
= 6;
201 static char *USER_AGENT_STR
= "Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20111106 IceCat/7.0.1";
203 ////////////////////////////////////////////////////////////
205 ////////////////////////////////////////////////////////////
207 static int getMaxProgress(void)
209 int counter
= MIN_CONNSCORE
+ 2;
210 for(int i
= 0; update_mirrors
[i
]; i
++) counter
++;
214 static QStringList
buildRandomList(const char *const values
[])
217 for (int index
= 0; values
[index
]; index
++)
219 const int pos
= next_rand32() % (index
+ 1);
220 list
.insert(pos
, QString::fromLatin1(values
[index
]));
225 ////////////////////////////////////////////////////////////
227 ////////////////////////////////////////////////////////////
229 UpdateCheckerInfo::UpdateCheckerInfo(void)
234 void UpdateCheckerInfo::resetInfo(void)
237 m_buildDate
.setDate(1900, 1, 1);
238 m_downloadSite
.clear();
239 m_downloadAddress
.clear();
240 m_downloadFilename
.clear();
241 m_downloadFilecode
.clear();
242 m_downloadChecksum
.clear();
245 bool UpdateCheckerInfo::isComplete(void)
247 if(this->m_buildNo
< 1) return false;
248 if(this->m_buildDate
.year() < 2010) return false;
249 if(this->m_downloadSite
.isEmpty()) return false;
250 if(this->m_downloadAddress
.isEmpty()) return false;
251 if(this->m_downloadFilename
.isEmpty()) return false;
252 if(this->m_downloadFilecode
.isEmpty()) return false;
253 if(this->m_downloadChecksum
.isEmpty()) return false;
258 ////////////////////////////////////////////////////////////
259 // Constructor & Destructor
260 ////////////////////////////////////////////////////////////
262 UpdateChecker::UpdateChecker(const QString
&binWGet
, const QString
&binNC
, const QString
&binGnuPG
, const QString
&binKeys
, const QString
&applicationId
, const quint32
&installedBuildNo
, const bool betaUpdates
, const bool testMode
)
264 m_updateInfo(new UpdateCheckerInfo()),
265 m_binaryWGet(binWGet
),
267 m_binaryGnuPG(binGnuPG
),
268 m_binaryKeys(binKeys
),
269 m_applicationId(applicationId
),
270 m_installedBuildNo(installedBuildNo
),
271 m_betaUpdates(betaUpdates
),
272 m_testMode(testMode
),
273 m_maxProgress(getMaxProgress())
276 m_status
= UpdateStatus_NotStartedYet
;
279 if(m_binaryWGet
.isEmpty() || m_binaryGnuPG
.isEmpty() || m_binaryKeys
.isEmpty())
281 MUTILS_THROW("Tools not initialized correctly!");
285 UpdateChecker::~UpdateChecker(void)
289 ////////////////////////////////////////////////////////////
290 // Protected functions
291 ////////////////////////////////////////////////////////////
293 void UpdateChecker::run(void)
295 qDebug("Update checker thread started!");
296 MUTILS_EXCEPTION_HANDLER(m_testMode
? testKnownHosts() : checkForUpdates());
297 qDebug("Update checker thread completed.");
300 void UpdateChecker::checkForUpdates(void)
302 // ----- Initialization ----- //
305 m_updateInfo
->resetInfo();
309 // ----- Test Internet Connection ----- //
311 log("Checking internet connection...", "");
312 setStatus(UpdateStatus_CheckingConnection
);
314 const int networkStatus
= OS::network_status();
315 if(networkStatus
== OS::NETWORK_TYPE_NON
)
317 log("Operating system reports that the computer is currently offline !!!");
318 setProgress(m_maxProgress
);
319 setStatus(UpdateStatus_ErrorNoConnection
);
325 // ----- Test Known Hosts Connectivity ----- //
327 int connectionScore
= 0, connectionRetry
= MIN_CONNSCORE
;
329 QStringList hostList
= buildRandomList(known_hosts
);
330 while((!hostList
.isEmpty()) && (connectionScore
< MIN_CONNSCORE
) && (connectionRetry
> 0))
333 if(tryContactHost(hostList
.takeFirst()))
335 connectionScore
+= 1;
336 connectionRetry
= MIN_CONNSCORE
;
338 setProgress(qBound(1, connectionScore
+ 1, MIN_CONNSCORE
+ 1));
342 if(connectionScore
< MIN_CONNSCORE
)
344 log("", "Connectivity test has failed: Internet connection appears to be broken!");
345 setProgress(m_maxProgress
);
346 setStatus(UpdateStatus_ErrorConnectionTestFailed
);
350 // ----- Fetch Update Info From Server ----- //
352 log("----", "", "Checking for updates online...");
353 setStatus(UpdateStatus_FetchingUpdates
);
355 QStringList mirrorList
= buildRandomList(update_mirrors
);
356 while(!mirrorList
.isEmpty())
358 QString currentMirror
= mirrorList
.takeFirst();
359 setProgress(m_progress
+ 1);
362 if(tryUpdateMirror(m_updateInfo
.data(), currentMirror
))
373 setProgress(m_maxProgress
);
377 if(m_updateInfo
->m_buildNo
> m_installedBuildNo
)
379 setStatus(UpdateStatus_CompletedUpdateAvailable
);
381 else if(m_updateInfo
->m_buildNo
== m_installedBuildNo
)
383 setStatus(UpdateStatus_CompletedNoUpdates
);
387 setStatus(UpdateStatus_CompletedNewVersionOlder
);
392 setStatus(UpdateStatus_ErrorFetchUpdateInfo
);
396 void UpdateChecker::testKnownHosts(void)
398 QStringList hostList
;
399 for(int i
= 0; known_hosts
[i
]; i
++)
401 hostList
<< QString::fromLatin1(known_hosts
[i
]);
404 qDebug("\n[Known Hosts]");
405 log("Testing all known hosts...", "", "---");
407 int hostCount
= hostList
.count();
408 while(!hostList
.isEmpty())
410 QString currentHost
= hostList
.takeFirst();
411 qDebug("Testing: %s", currentHost
.toLatin1().constData());
412 log("", "Testing:", currentHost
, "");
413 if (!tryContactHost(currentHost
))
415 qWarning("\nConnectivity test FAILED on the following host:\n%s\n", currentHost
.toLatin1().constData());
421 ////////////////////////////////////////////////////////////
423 ////////////////////////////////////////////////////////////
425 void UpdateChecker::setStatus(const int status
)
427 if(m_status
!= status
)
430 emit
statusChanged(status
);
434 void UpdateChecker::setProgress(const int progress
)
436 if(m_progress
!= progress
)
438 m_progress
= progress
;
439 emit
progressChanged(progress
);
443 void UpdateChecker::log(const QString
&str1
, const QString
&str2
, const QString
&str3
, const QString
&str4
)
445 if(!str1
.isNull()) emit
messageLogged(str1
);
446 if(!str2
.isNull()) emit
messageLogged(str2
);
447 if(!str3
.isNull()) emit
messageLogged(str3
);
448 if(!str4
.isNull()) emit
messageLogged(str4
);
451 bool UpdateChecker::tryUpdateMirror(UpdateCheckerInfo
*updateInfo
, const QString
&url
)
453 bool success
= false;
454 log("", "Trying mirror:", url
);
456 const QString randPart
= rand_str();
457 const QString outFileVers
= QString("%1/%2.ver").arg(temp_folder(), randPart
);
458 const QString outFileSign
= QString("%1/%2.sig").arg(temp_folder(), randPart
);
460 if(getUpdateInfo(url
, outFileVers
, outFileSign
))
462 log("", "Download okay, checking signature:");
463 if(checkSignature(outFileVers
, outFileSign
))
465 log("", "Signature okay, parsing info:");
466 success
= parseVersionInfo(outFileVers
, updateInfo
);
470 log("", "Bad signature, take care!");
475 log("", "Download has failed!");
478 QFile::remove(outFileVers
);
479 QFile::remove(outFileSign
);
484 bool UpdateChecker::getUpdateInfo(const QString
&url
, const QString
&outFileVers
, const QString
&outFileSign
)
486 log("", "Downloading update info:");
487 if(!getFile(QString("%1%2" ).arg(url
, mirror_url_postfix
[m_betaUpdates
? 1 : 0]), outFileVers
))
492 log("", "Downloading signature:");
493 if(!getFile(QString("%1%2.sig2").arg(url
, mirror_url_postfix
[m_betaUpdates
? 1 : 0]), outFileSign
))
501 bool UpdateChecker::parseVersionInfo(const QString
&file
, UpdateCheckerInfo
*updateInfo
)
503 QRegExp
value("^(\\w+)=(.+)$");
504 QRegExp
section("^\\[(.+)\\]$");
506 QDate updateInfoDate
;
507 updateInfo
->resetInfo();
510 if(!data
.open(QIODevice::ReadOnly
))
512 qWarning("Cannot open update info file for reading!");
521 QString line
= QString::fromLatin1(data
.readLine()).trimmed();
522 if(section
.indexIn(line
) >= 0)
524 log(QString("Sec: [%1]").arg(section
.cap(1)));
525 inSec
= (section
.cap(1).compare(m_applicationId
, Qt::CaseInsensitive
) == 0);
526 inHdr
= (section
.cap(1).compare(QString::fromLatin1(header_id
), Qt::CaseInsensitive
) == 0);
529 if(inSec
&& (value
.indexIn(line
) >= 0))
531 log(QString("Val: '%1' ==> '%2").arg(value
.cap(1), value
.cap(2)));
532 if(value
.cap(1).compare("BuildNo", Qt::CaseInsensitive
) == 0)
535 const unsigned int temp
= value
.cap(2).toUInt(&ok
);
536 if(ok
) updateInfo
->m_buildNo
= temp
;
538 else if(value
.cap(1).compare("BuildDate", Qt::CaseInsensitive
) == 0)
540 const QDate temp
= QDate::fromString(value
.cap(2).trimmed(), Qt::ISODate
);
541 if(temp
.isValid()) updateInfo
->m_buildDate
= temp
;
543 else if(value
.cap(1).compare("DownloadSite", Qt::CaseInsensitive
) == 0)
545 updateInfo
->m_downloadSite
= value
.cap(2).trimmed();
547 else if(value
.cap(1).compare("DownloadAddress", Qt::CaseInsensitive
) == 0)
549 updateInfo
->m_downloadAddress
= value
.cap(2).trimmed();
551 else if(value
.cap(1).compare("DownloadFilename", Qt::CaseInsensitive
) == 0)
553 updateInfo
->m_downloadFilename
= value
.cap(2).trimmed();
555 else if(value
.cap(1).compare("DownloadFilecode", Qt::CaseInsensitive
) == 0)
557 updateInfo
->m_downloadFilecode
= value
.cap(2).trimmed();
559 else if(value
.cap(1).compare("DownloadChecksum", Qt::CaseInsensitive
) == 0)
561 updateInfo
->m_downloadChecksum
= value
.cap(2).trimmed();
564 if(inHdr
&& (value
.indexIn(line
) >= 0))
566 log(QString("Val: '%1' ==> '%2").arg(value
.cap(1), value
.cap(2)));
567 if(value
.cap(1).compare("TimestampCreated", Qt::CaseInsensitive
) == 0)
569 QDate temp
= QDate::fromString(value
.cap(2).trimmed(), Qt::ISODate
);
570 if(temp
.isValid()) updateInfoDate
= temp
;
575 if(!updateInfoDate
.isValid())
577 updateInfo
->resetInfo();
578 log("WARNING: Version info timestamp is missing!");
582 const QDate currentDate
= OS::current_date();
583 if(updateInfoDate
.addMonths(VERSION_INFO_EXPIRES_MONTHS
) < currentDate
)
585 updateInfo
->resetInfo();
586 log(QString::fromLatin1("WARNING: This version info has expired at %1!").arg(updateInfoDate
.addMonths(VERSION_INFO_EXPIRES_MONTHS
).toString(Qt::ISODate
)));
589 else if(currentDate
< updateInfoDate
)
591 log("Version info is from the future, take care!");
592 qWarning("Version info is from the future, take care!");
595 if(!updateInfo
->isComplete())
597 log("WARNING: Version info is incomplete!");
604 //----------------------------------------------------------
606 //----------------------------------------------------------
608 bool UpdateChecker::getFile(const QString
&url
, const QString
&outFile
, const unsigned int maxRedir
, bool *httpOk
)
610 for (int i
= 0; i
< 2; i
++)
612 if (getFile(url
, (i
> 0), outFile
, maxRedir
, httpOk
))
620 bool UpdateChecker::getFile(const QString
&url
, const bool forceIp4
, const QString
&outFile
, const unsigned int maxRedir
, bool *httpOk
)
622 QFileInfo
output(outFile
);
623 output
.setCaching(false);
624 if (httpOk
) *httpOk
= false;
628 QFile::remove(output
.canonicalFilePath());
636 init_process(process
, output
.absolutePath());
644 args
<< "-T" << "15" << "--no-cache" << "--no-dns-cache" << QString().sprintf("--max-redirect=%u", maxRedir
);
645 args
<< QString("--referer=%1://%2/").arg(QUrl(url
).scheme(), QUrl(url
).host()) << "-U" << USER_AGENT_STR
;
646 args
<< "-O" << output
.fileName() << url
;
649 connect(&process
, SIGNAL(error(QProcess::ProcessError
)), &loop
, SLOT(quit()));
650 connect(&process
, SIGNAL(finished(int, QProcess::ExitStatus
)), &loop
, SLOT(quit()));
651 connect(&process
, SIGNAL(readyRead()), &loop
, SLOT(quit()));
654 timer
.setSingleShot(true);
655 connect(&timer
, SIGNAL(timeout()), &loop
, SLOT(quit()));
657 const QRegExp
httpResponseOK("200 OK$");
659 process
.start(m_binaryWGet
, args
);
661 if (!process
.waitForStarted())
668 while (process
.state() != QProcess::NotRunning
)
671 const bool bTimeOut
= (!timer
.isActive());
672 while (process
.canReadLine())
674 QString line
= QString::fromLatin1(process
.readLine()).simplified();
675 if (line
.contains(httpResponseOK
))
677 line
.append(" [OK]");
678 if (httpOk
) *httpOk
= true;
684 qWarning("WGet process timed out <-- killing!");
686 process
.waitForFinished();
687 log("!!! TIMEOUT !!!");
693 timer
.disconnect(&timer
, SIGNAL(timeout()), &loop
, SLOT(quit()));
695 log(QString().sprintf("Exited with code %d", process
.exitCode()));
696 return (process
.exitCode() == 0) && output
.exists() && output
.isFile();
699 bool UpdateChecker::tryContactHost(const QString
&hostname
)
701 log(QString("Connecting to host: %1").arg(hostname
));
704 init_process(process
, temp_folder());
707 args
<< "-z" << hostname
<< QString::number(80);
710 connect(&process
, SIGNAL(error(QProcess::ProcessError
)), &loop
, SLOT(quit()));
711 connect(&process
, SIGNAL(finished(int, QProcess::ExitStatus
)), &loop
, SLOT(quit()));
712 connect(&process
, SIGNAL(readyRead()), &loop
, SLOT(quit()));
715 timer
.setSingleShot(true);
716 connect(&timer
, SIGNAL(timeout()), &loop
, SLOT(quit()));
718 process
.start(m_binaryNC
, args
);
720 if (!process
.waitForStarted())
727 while (process
.state() != QProcess::NotRunning
)
730 const bool bTimeOut
= (!timer
.isActive());
731 while (process
.canReadLine())
733 QString line
= QString::fromLatin1(process
.readLine()).simplified();
738 qWarning("NC process timed out <-- killing!");
740 process
.waitForFinished();
741 log("!!! TIMEOUT !!!");
747 timer
.disconnect(&timer
, SIGNAL(timeout()), &loop
, SLOT(quit()));
749 if (process
.exitCode() != 0)
751 log("Connection has failed!");
754 log(QString().sprintf("Exited with code %d", process
.exitCode()), "");
755 return (process
.exitCode() == 0);
758 bool UpdateChecker::checkSignature(const QString
&file
, const QString
&signature
)
760 if (QFileInfo(file
).absolutePath().compare(QFileInfo(signature
).absolutePath(), Qt::CaseInsensitive
) != 0)
762 qWarning("CheckSignature: File and signature should be in same folder!");
766 QString
keyRingPath(m_binaryKeys
);
767 bool removeKeyring
= false;
768 if (QFileInfo(file
).absolutePath().compare(QFileInfo(m_binaryKeys
).absolutePath(), Qt::CaseInsensitive
) != 0)
770 keyRingPath
= make_temp_file(QFileInfo(file
).absolutePath(), "gpg");
771 removeKeyring
= true;
772 if (!QFile::copy(m_binaryKeys
, keyRingPath
))
774 qWarning("CheckSignature: Failed to copy the key-ring file!");
780 init_process(process
, QFileInfo(file
).absolutePath());
783 connect(&process
, SIGNAL(error(QProcess::ProcessError
)), &loop
, SLOT(quit()));
784 connect(&process
, SIGNAL(finished(int, QProcess::ExitStatus
)), &loop
, SLOT(quit()));
785 connect(&process
, SIGNAL(readyRead()), &loop
, SLOT(quit()));
787 process
.start(m_binaryGnuPG
, QStringList() << "--homedir" << "." << "--keyring" << QFileInfo(keyRingPath
).fileName() << QFileInfo(signature
).fileName() << QFileInfo(file
).fileName());
789 if (!process
.waitForStarted())
793 remove_file(keyRingPath
);
798 while (process
.state() == QProcess::Running
)
801 while (process
.canReadLine())
803 log(QString::fromLatin1(process
.readLine()).simplified());
809 remove_file(keyRingPath
);
812 log(QString().sprintf("Exited with code %d", process
.exitCode()));
813 return (process
.exitCode() == 0);
816 ////////////////////////////////////////////////////////////
818 ////////////////////////////////////////////////////////////
822 ////////////////////////////////////////////////////////////
824 ////////////////////////////////////////////////////////////