Updated list of known hosts.
[MUtilities.git] / src / UpdateChecker.cpp
blobe361804f9ddc05259f99889a2ac3203141b60a30
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2015 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 <QProcess>
31 #include <QUrl>
32 #include <QEventLoop>
33 #include <QTimer>
35 using namespace MUtils;
37 ///////////////////////////////////////////////////////////////////////////////
38 // CONSTANTS
39 ///////////////////////////////////////////////////////////////////////////////
41 static const char *header_id = "!Update";
43 static const char *mirror_url_postfix[] =
45 "update.ver",
46 "update_beta.ver",
47 NULL
50 static const char *update_mirrors_prim[] =
52 "http://muldersoft.com/",
53 "http://mulder.bplaced.net/",
54 "http://mulder.cwsurf.de/",
55 "http://mulder.6te.net/",
56 "http://mulder.webuda.com/",
57 "http://mulder.byethost13.com/",
58 "http://muldersoft.kilu.de/",
59 "http://mulder.pe.hu/",
60 "http://muldersoft.square7.ch/", //"http://muldersoft.zxq.net/",
61 "http://muldersoft.co.nf/",
62 "http://muldersoft.eu.pn/",
63 "http://lamexp.sourceforge.net/",
64 "http://lordmulder.github.io/LameXP/",
65 "http://lord_mulder.bitbucket.org/",
66 "http://www.tricksoft.de/",
67 NULL
70 static const char *update_mirrors_back[] =
72 "http://mplayer.savedonthe.net/",
73 NULL
76 static const char *known_hosts[] = //Taken form: http://www.alexa.com/topsites !!!
78 "http://www.163.com/",
79 "http://www.7-zip.org/",
80 "http://www.ac3filter.net/",
81 "http://www.amazon.com/",
82 "http://antergos.com/",
83 "http://www.aol.com/",
84 "http://www.apache.org/",
85 "http://www.apple.com/",
86 "http://www.adobe.com/",
87 "http://web.audacityteam.org/",
88 "http://automattic.com/",
89 "http://www.avidemux.org/",
90 "http://www.babylon.com/",
91 "http://www.baidu.com/",
92 "http://bandcamp.com/",
93 "http://www.bbc.co.uk/",
94 "http://www.berlios.de/",
95 "http://www.bing.com/",
96 "http://www.bucketheadpikes.com/",
97 "http://www.ccc.de/",
98 "http://www.citizeninsomniac.com/WMV/",
99 "http://www.cnet.com/",
100 "http://cnzz.com/",
101 "http://www.codeplex.com/",
102 "http://www.der-postillon.com/",
103 "http://www.ebay.com/",
104 "http://www.equation.com/",
105 "http://fc2.com/",
106 "http://fedoraproject.org/wiki/Fedora_Project_Wiki",
107 "http://blog.fefe.de/",
108 "http://www.ffmpeg.org/",
109 "http://blog.flickr.net/en",
110 "http://free-codecs.com/",
111 "http://git-scm.com/",
112 "http://status.gitlab.com/",
113 "http://www.gmx.net/",
114 "http://news.gnome.org/",
115 "http://www.gnu.org/",
116 "http://go.com/",
117 "http://code.google.com/",
118 "http://haali.su/mkv/",
119 "http://www.heise.de/",
120 "http://www.huffingtonpost.co.uk/",
121 "http://www.iana.org/",
122 "http://www.imdb.com/",
123 "http://www.imgburn.com/",
124 "http://imgur.com/",
125 "http://www.jd.com/contact/",
126 "http://kannmanumdieuhrzeitschonnbierchentrinken.de/",
127 "http://mirrors.kernel.org/",
128 "http://komisar.gin.by/",
129 "http://lame.sourceforge.net/",
130 "http://www.libav.org/",
131 "http://blog.linkedin.com/",
132 "http://www.linuxmint.com/",
133 "http://www.livedoor.com/",
134 "http://www.livejournal.com/",
135 "http://go.mail.ru/",
136 "http://www.mediafire.com/about/",
137 "http://ftp.mozilla.org/",
138 "http://mplayerhq.hu/",
139 "http://www.msn.com/en-us/",
140 "http://wiki.multimedia.cx/",
141 "http://www.nch.com.au/",
142 "http://oss.netfarm.it/",
143 "http://www.nytimes.com/",
144 "http://www.opera.com/",
145 "http://www.partha.com/",
146 "http://pastie.org/",
147 "http://www.portablefreeware.com/",
148 "http://www.qt.io/",
149 "http://www.quakelive.com/",
150 "http://www.seamonkey-project.org/",
151 "http://www.shspvr.com/",
152 "http://www.sina.com.cn/",
153 "http://www.sohu.com/",
154 "http://www.sogou.com/",
155 "http://sourceforge.net/",
156 "http://www.spiegel.de/",
157 "http://stackoverflow.com/",
158 "http://tdm-gcc.tdragon.net/",
159 "http://www.tdrsmusic.com/",
160 "http://www.ubuntu.com/",
161 "http://www.uol.com.br/",
162 "http://www.videohelp.com/",
163 "http://www.videolan.org/",
164 "http://virtualdub.org/",
165 "http://blog.virustotal.com/",
166 "http://www.warr.org/buckethead.html",
167 "http://www.weibo.com/login.php",
168 "http://status.wikimedia.org/",
169 "http://www.winamp.com/",
170 "http://wpde.org/",
171 "http://x265.org/",
172 "http://xhmikosr.1f0.de/",
173 "http://xiph.org/",
174 "http://us.mail.yahoo.com/",
175 "http://www.yandex.ru/",
176 "http://www.youtube.com/yt/about/",
177 "http://www.zedo.com/",
178 "http://ffmpeg.zeranoe.com/",
179 NULL
182 static const int MIN_CONNSCORE = 8;
183 static const int VERSION_INFO_EXPIRES_MONTHS = 6;
184 static char *USER_AGENT_STR = "Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20111106 IceCat/7.0.1";
186 //Helper function
187 static int getMaxProgress(void)
189 int counter = MIN_CONNSCORE + 2;
190 for(int i = 0; update_mirrors_prim[i]; i++) counter++;
191 for(int i = 0; update_mirrors_back[i]; i++) counter++;
192 return counter;
195 ////////////////////////////////////////////////////////////
196 // Update Info Class
197 ////////////////////////////////////////////////////////////
199 UpdateCheckerInfo::UpdateCheckerInfo(void)
201 resetInfo();
204 void UpdateCheckerInfo::resetInfo(void)
206 m_buildNo = 0;
207 m_buildDate.setDate(1900, 1, 1);
208 m_downloadSite.clear();
209 m_downloadAddress.clear();
210 m_downloadFilename.clear();
211 m_downloadFilecode.clear();
214 ////////////////////////////////////////////////////////////
215 // Constructor & Destructor
216 ////////////////////////////////////////////////////////////
218 UpdateChecker::UpdateChecker(const QString &binWGet, const QString &binGnuPG, const QString &binKeys, const QString &applicationId, const quint32 &installedBuildNo, const bool betaUpdates, const bool testMode)
220 m_updateInfo(new UpdateCheckerInfo()),
221 m_binaryWGet(binWGet),
222 m_binaryGnuPG(binGnuPG),
223 m_binaryKeys(binKeys),
224 m_applicationId(applicationId),
225 m_installedBuildNo(installedBuildNo),
226 m_betaUpdates(betaUpdates),
227 m_testMode(testMode),
228 m_maxProgress(getMaxProgress())
230 m_success = false;
231 m_status = UpdateStatus_NotStartedYet;
232 m_progress = 0;
234 if(m_binaryWGet.isEmpty() || m_binaryGnuPG.isEmpty() || m_binaryKeys.isEmpty())
236 MUTILS_THROW("Tools not initialized correctly!");
240 UpdateChecker::~UpdateChecker(void)
242 delete m_updateInfo;
245 ////////////////////////////////////////////////////////////
246 // Protected functions
247 ////////////////////////////////////////////////////////////
249 void UpdateChecker::run(void)
251 qDebug("Update checker thread started!");
252 MUTILS_EXCEPTION_HANDLER(m_testMode ? testKnownHosts() : checkForUpdates());
253 qDebug("Update checker thread completed.");
256 void UpdateChecker::checkForUpdates(void)
258 // ----- Initialization ----- //
260 m_success = false;
261 m_updateInfo->resetInfo();
262 seed_rand();
263 setProgress(0);
265 // ----- Test Internet Connection ----- //
267 int connectionScore = 0;
268 int maxConnectTries = (3 * MIN_CONNSCORE) / 2;
270 log("Checking internet connection...");
271 setStatus(UpdateStatus_CheckingConnection);
273 const int networkStatus = OS::network_status();
274 if(networkStatus == OS::NETWORK_TYPE_NON)
276 log("", "Operating system reports that the computer is currently offline !!!");
277 setProgress(m_maxProgress);
278 setStatus(UpdateStatus_ErrorNoConnection);
279 return;
282 setProgress(1);
284 // ----- Test Known Hosts Connectivity ----- //
286 QStringList hostList;
287 for(int i = 0; known_hosts[i]; i++)
289 hostList << QString::fromLatin1(known_hosts[i]);
292 while(!(hostList.isEmpty() || (connectionScore >= MIN_CONNSCORE) || (maxConnectTries < 1)))
294 switch(tryContactHost(hostList.takeAt(next_rand32() % hostList.count())))
296 case 01: connectionScore += 1; break;
297 case 02: connectionScore += 2; break;
298 default: maxConnectTries -= 1; break;
300 setProgress(qBound(1, connectionScore + 1, MIN_CONNSCORE + 1));
301 msleep(64);
304 if(connectionScore < MIN_CONNSCORE)
306 log("", "Connectivity test has failed: Internet connection appears to be broken!");
307 setProgress(m_maxProgress);
308 setStatus(UpdateStatus_ErrorConnectionTestFailed);
309 return;
312 // ----- Build Mirror List ----- //
314 log("", "----", "", "Checking for updates online...");
315 setStatus(UpdateStatus_FetchingUpdates);
317 QStringList mirrorList;
318 for(int index = 0; update_mirrors_prim[index]; index++)
320 mirrorList << QString::fromLatin1(update_mirrors_prim[index]);
323 if(const int len = mirrorList.count())
325 const int rounds = len * 1097;
326 for(int i = 0; i < rounds; i++)
328 mirrorList.swap(i % len, next_rand32() % len);
332 for(int index = 0; update_mirrors_back[index]; index++)
334 mirrorList << QString::fromLatin1(update_mirrors_back[index]);
337 // ----- Fetch Update Info From Server ----- //
339 while(!mirrorList.isEmpty())
341 QString currentMirror = mirrorList.takeFirst();
342 setProgress(m_progress + 1);
343 if(!m_success)
345 if(tryUpdateMirror(m_updateInfo, currentMirror))
347 m_success = true;
350 else
352 msleep(64);
356 setProgress(m_maxProgress);
358 if(m_success)
360 if(m_updateInfo->m_buildNo > m_installedBuildNo)
362 setStatus(UpdateStatus_CompletedUpdateAvailable);
364 else if(m_updateInfo->m_buildNo == m_installedBuildNo)
366 setStatus(UpdateStatus_CompletedNoUpdates);
368 else
370 setStatus(UpdateStatus_CompletedNewVersionOlder);
373 else
375 setStatus(UpdateStatus_ErrorFetchUpdateInfo);
379 void UpdateChecker::testKnownHosts(void)
381 QStringList hostList;
382 for(int i = 0; known_hosts[i]; i++)
384 hostList << QString::fromLatin1(known_hosts[i]);
387 qDebug("\n[Known Hosts]");
388 log("Testing all known hosts...", "", "---");
390 int hostCount = hostList.count();
391 while(!hostList.isEmpty())
393 QString currentHost = hostList.takeFirst();
394 qDebug("Testing: %s", currentHost.toLatin1().constData());
395 log("", "Testing:", currentHost, "");
396 QString outFile = QString("%1/%2.htm").arg(temp_folder(), rand_str());
397 bool httpOk = false;
398 if(!getFile(currentHost, outFile, 0, &httpOk))
400 if(httpOk)
402 qWarning("\nConnectivity test was SLOW on the following site:\n%s\n", currentHost.toLatin1().constData());
404 else
406 qWarning("\nConnectivity test FAILED on the following site:\n%s\n", currentHost.toLatin1().constData());
409 log("", "---");
410 QFile::remove(outFile);
414 ////////////////////////////////////////////////////////////
415 // PRIVATE FUNCTIONS
416 ////////////////////////////////////////////////////////////
418 void UpdateChecker::setStatus(const int status)
420 if(m_status != status)
422 m_status = status;
423 emit statusChanged(status);
427 void UpdateChecker::setProgress(const int progress)
429 if(m_progress != progress)
431 m_progress = progress;
432 emit progressChanged(progress);
436 void UpdateChecker::log(const QString &str1, const QString &str2, const QString &str3, const QString &str4)
438 if(!str1.isNull()) emit messageLogged(str1);
439 if(!str2.isNull()) emit messageLogged(str2);
440 if(!str3.isNull()) emit messageLogged(str3);
441 if(!str4.isNull()) emit messageLogged(str4);
444 int UpdateChecker::tryContactHost(const QString &url)
446 int result = -1; bool httpOkay = false;
447 const QString outFile = QString("%1/%2.htm").arg(temp_folder(), rand_str());
448 log("", "Testing host:", url);
450 if(getFile(url, outFile, 0, &httpOkay))
452 log("Connection to host was established successfully.");
453 result = 2;
455 else
457 if(httpOkay)
459 log("Connection to host timed out after HTTP OK was received.");
460 result = 1;
462 else
464 log("Connection failed: The host could not be reached!");
465 result = 0;
469 QFile::remove(outFile);
470 return result;
473 bool UpdateChecker::tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString &url)
475 bool success = false;
476 log("", "Trying mirror:", url);
478 const QString randPart = rand_str();
479 const QString outFileVers = QString("%1/%2.ver").arg(temp_folder(), randPart);
480 const QString outFileSign = QString("%1/%2.sig").arg(temp_folder(), randPart);
482 if(getUpdateInfo(url, outFileVers, outFileSign))
484 log("", "Download okay, checking signature:");
485 if(checkSignature(outFileVers, outFileSign))
487 log("", "Signature okay, parsing info:");
488 success = parseVersionInfo(outFileVers, updateInfo);
490 else
492 log("", "Bad signature, take care!");
495 else
497 log("", "Download has failed!");
500 QFile::remove(outFileVers);
501 QFile::remove(outFileSign);
503 return success;
506 bool UpdateChecker::getUpdateInfo(const QString &url, const QString &outFileVers, const QString &outFileSign)
508 log("", "Downloading update info:");
509 if(!getFile(QString("%1%2" ).arg(url, mirror_url_postfix[m_betaUpdates ? 1 : 0]), outFileVers))
511 return false;
514 log("", "Downloading signature:");
515 if(!getFile(QString("%1%2.sig").arg(url, mirror_url_postfix[m_betaUpdates ? 1 : 0]), outFileSign))
517 return false;
520 return true;
523 bool UpdateChecker::getFile(const QString &url, const QString &outFile, unsigned int maxRedir, bool *httpOk)
525 QFileInfo output(outFile);
526 output.setCaching(false);
527 if(httpOk) *httpOk = false;
529 if(output.exists())
531 QFile::remove(output.canonicalFilePath());
532 if(output.exists())
534 return false;
538 QProcess process;
539 init_process(process, output.absolutePath());
541 QStringList args;
542 args << "-T" << "15" << "--no-cache" << "--no-dns-cache" << QString().sprintf("--max-redirect=%u", maxRedir);
543 args << QString("--referer=%1://%2/").arg(QUrl(url).scheme(), QUrl(url).host()) << "-U" << USER_AGENT_STR;
544 args << "-O" << output.fileName() << url;
546 QEventLoop loop;
547 connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
548 connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), &loop, SLOT(quit()));
549 connect(&process, SIGNAL(readyRead()), &loop, SLOT(quit()));
551 QTimer timer;
552 timer.setSingleShot(true);
553 timer.setInterval(25000);
554 connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
556 const QRegExp httpResponseOK("200 OK$");
558 process.start(m_binaryWGet, args);
560 if(!process.waitForStarted())
562 return false;
565 timer.start();
567 while(process.state() != QProcess::NotRunning)
569 loop.exec();
570 const bool bTimeOut = (!timer.isActive());
571 while(process.canReadLine())
573 QString line = QString::fromLatin1(process.readLine()).simplified();
574 if(line.contains(httpResponseOK))
576 line.append(" [OK]");
577 if(httpOk) *httpOk = true;
579 log(line);
581 if(bTimeOut)
583 qWarning("WGet process timed out <-- killing!");
584 process.kill();
585 process.waitForFinished();
586 log("!!! TIMEOUT !!!");
587 return false;
591 timer.stop();
592 timer.disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
594 log(QString().sprintf("Exited with code %d", process.exitCode()));
595 return (process.exitCode() == 0) && output.exists() && output.isFile();
598 bool UpdateChecker::checkSignature(const QString &file, const QString &signature)
600 if(QFileInfo(file).absolutePath().compare(QFileInfo(signature).absolutePath(), Qt::CaseInsensitive) != 0)
602 qWarning("CheckSignature: File and signature should be in same folder!");
603 return false;
606 if(QFileInfo(file).absolutePath().compare(QFileInfo(m_binaryKeys).absolutePath(), Qt::CaseInsensitive) != 0)
608 qWarning("CheckSignature: File and keyring should be in same folder!");
609 return false;
612 QProcess process;
613 init_process(process, QFileInfo(file).absolutePath());
615 QEventLoop loop;
616 connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
617 connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), &loop, SLOT(quit()));
618 connect(&process, SIGNAL(readyRead()), &loop, SLOT(quit()));
620 process.start(m_binaryGnuPG, QStringList() << "--homedir" << "." << "--keyring" << QFileInfo(m_binaryKeys).fileName() << QFileInfo(signature).fileName() << QFileInfo(file).fileName());
622 if(!process.waitForStarted())
624 return false;
627 while(process.state() == QProcess::Running)
629 loop.exec();
630 while(process.canReadLine())
632 log(QString::fromLatin1(process.readLine()).simplified());
636 log(QString().sprintf("Exited with code %d", process.exitCode()));
637 return (process.exitCode() == 0);
640 bool UpdateChecker::parseVersionInfo(const QString &file, UpdateCheckerInfo *updateInfo)
642 QRegExp value("^(\\w+)=(.+)$");
643 QRegExp section("^\\[(.+)\\]$");
645 QDate updateInfoDate;
646 updateInfo->resetInfo();
648 QFile data(file);
649 if(!data.open(QIODevice::ReadOnly))
651 qWarning("Cannot open update info file for reading!");
652 return false;
655 bool inHdr = false;
656 bool inSec = false;
658 while(!data.atEnd())
660 QString line = QString::fromLatin1(data.readLine()).trimmed();
661 if(section.indexIn(line) >= 0)
663 log(QString("Sec: [%1]").arg(section.cap(1)));
664 inSec = (section.cap(1).compare(m_applicationId, Qt::CaseInsensitive) == 0);
665 inHdr = (section.cap(1).compare(QString::fromLatin1(header_id), Qt::CaseInsensitive) == 0);
666 continue;
668 if(inSec && (value.indexIn(line) >= 0))
670 log(QString("Val: '%1' ==> '%2").arg(value.cap(1), value.cap(2)));
671 if(value.cap(1).compare("BuildNo", Qt::CaseInsensitive) == 0)
673 bool ok = false;
674 unsigned int temp = value.cap(2).toUInt(&ok);
675 if(ok) updateInfo->m_buildNo = temp;
677 else if(value.cap(1).compare("BuildDate", Qt::CaseInsensitive) == 0)
679 QDate temp = QDate::fromString(value.cap(2).trimmed(), Qt::ISODate);
680 if(temp.isValid()) updateInfo->m_buildDate = temp;
682 else if(value.cap(1).compare("DownloadSite", Qt::CaseInsensitive) == 0)
684 updateInfo->m_downloadSite = value.cap(2).trimmed();
686 else if(value.cap(1).compare("DownloadAddress", Qt::CaseInsensitive) == 0)
688 updateInfo->m_downloadAddress = value.cap(2).trimmed();
690 else if(value.cap(1).compare("DownloadFilename", Qt::CaseInsensitive) == 0)
692 updateInfo->m_downloadFilename = value.cap(2).trimmed();
694 else if(value.cap(1).compare("DownloadFilecode", Qt::CaseInsensitive) == 0)
696 updateInfo->m_downloadFilecode = value.cap(2).trimmed();
699 if(inHdr && (value.indexIn(line) >= 0))
701 log(QString("Val: '%1' ==> '%2").arg(value.cap(1), value.cap(2)));
702 if(value.cap(1).compare("TimestampCreated", Qt::CaseInsensitive) == 0)
704 QDate temp = QDate::fromString(value.cap(2).trimmed(), Qt::ISODate);
705 if(temp.isValid()) updateInfoDate = temp;
710 if(!updateInfoDate.isValid())
712 updateInfo->resetInfo();
713 log("WARNING: Version info timestamp is missing!");
714 return false;
717 const QDate currentDate = OS::current_date();
718 if(updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS) < currentDate)
720 updateInfo->resetInfo();
721 log(QString::fromLatin1("WARNING: This version info has expired at %1!").arg(updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS).toString(Qt::ISODate)));
722 return false;
724 else if(currentDate < updateInfoDate)
726 log("Version info is from the future, take care!");
727 qWarning("Version info is from the future, take care!");
730 bool complete = true;
732 if(!(updateInfo->m_buildNo > 0)) complete = false;
733 if(!(updateInfo->m_buildDate.year() >= 2010)) complete = false;
734 if(updateInfo->m_downloadSite.isEmpty()) complete = false;
735 if(updateInfo->m_downloadAddress.isEmpty()) complete = false;
736 if(updateInfo->m_downloadFilename.isEmpty()) complete = false;
737 if(updateInfo->m_downloadFilecode.isEmpty()) complete = false;
739 if(!complete)
741 log("WARNING: Version info is incomplete!");
744 return complete;
747 ////////////////////////////////////////////////////////////
748 // SLOTS
749 ////////////////////////////////////////////////////////////
751 /*NONE*/
753 ////////////////////////////////////////////////////////////
754 // EVENTS
755 ////////////////////////////////////////////////////////////
757 /*NONE*/