1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2015 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_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/", //"http://mulder.byethost13.com/",
57 "http://mulder.pe.hu/", //"http://muldersoft.kilu.de/",
58 "http://muldersoft.square7.ch/", //"http://muldersoft.zxq.net/",
59 "http://muldersoft.co.nf/",
60 "http://muldersoft.eu.pn/",
61 "http://lamexp.sourceforge.net/",
62 "http://lordmulder.github.io/LameXP/",
63 "http://lord_mulder.bitbucket.org/",
64 "http://www.tricksoft.de/",
68 static const char *update_mirrors_back
[] =
70 "http://mplayer.savedonthe.net/",
74 static const char *known_hosts
[] = //Taken form: http://www.alexa.com/topsites !!!
76 "http://www.163.com/",
77 "http://www.7-zip.org/",
78 "http://www.ac3filter.net/",
79 "http://clbianco.altervista.org/",
80 "http://status.aws.amazon.com/",
81 "http://antergos.com/",
82 "http://www.aol.com/",
83 "http://www.apache.org/",
84 "http://www.apple.com/",
85 "http://www.adobe.com/",
86 "http://archive.org/web/",
87 "http://www.artlebedev.ru/",
88 "http://web.audacityteam.org/",
89 "http://status.automattic.com/",
90 "http://www.avidemux.org/",
91 "http://www.babylon.com/",
92 "http://www.baidu.com/",
93 "http://bandcamp.com/",
94 "http://www.bbc.co.uk/",
95 "http://www.berlios.de/",
96 "http://www.bing.com/",
97 "http://www.bingeandgrab.com/",
98 "http://www.bucketheadpikes.com/",
99 "http://www.buckethead-coop.com/",
100 "http://www.ccc.de/",
101 "http://www.citizeninsomniac.com/WMV/",
102 "http://www.cnet.com/",
104 "http://www.codeplex.com/",
105 "http://www.codeproject.com/",
106 "http://www.der-postillon.com/",
107 "http://www.ebay.com/",
108 "http://www.equation.com/",
109 "http://www.farbrausch.de/",
111 "http://fedoraproject.org/wiki/Fedora_Project_Wiki",
112 "http://blog.fefe.de/",
113 "http://www.ffmpeg.org/",
114 "http://blog.flickr.net/en",
115 "http://free-codecs.com/",
116 "http://git-scm.com/",
117 "http://doc.gitlab.com/",
118 "http://www.gmx.net/",
119 "http://news.gnome.org/",
120 "http://www.gnu.org/",
122 "http://code.google.com/",
123 "http://haali.su/mkv/",
124 "http://www.heise.de/",
125 "http://www.huffingtonpost.co.uk/",
126 "http://www.iana.org/",
127 "http://www.imdb.com/",
128 "http://www.imgburn.com/",
130 "http://www.jd.com/contact/",
131 "http://www.jiscdigitalmedia.ac.uk/",
132 "http://kannmanumdieuhrzeitschonnbierchentrinken.de/",
133 "http://mirrors.kernel.org/",
134 "http://komisar.gin.by/",
135 "http://lame.sourceforge.net/",
136 "http://www.libav.org/",
137 "http://blog.linkedin.com/",
138 "http://www.linuxmint.com/",
139 "http://www.livedoor.com/",
140 "http://www.livejournal.com/",
141 "http://longplayer.org/",
142 "http://go.mail.ru/",
143 "http://marknelson.us/",
144 "http://www.mediafire.com/about/",
145 "http://www.mod-technologies.com/",
146 "http://ftp.mozilla.org/",
147 "http://mplayerhq.hu/",
148 "http://www.msn.com/en-us/",
149 "http://wiki.multimedia.cx/",
150 "http://www.nch.com.au/",
151 "http://oss.netfarm.it/",
152 "http://netrenderer.de/",
153 "http://www.nytimes.com/",
154 "http://www.opera.com/",
155 "http://www.partha.com/",
156 "http://pastebin.com/",
157 "http://pastie.org/",
158 "http://portableapps.com/about",
159 "http://www.portablefreeware.com/",
161 "http://www.quakelive.com/",
162 "http://rationalqm.us/mine.html",
163 "http://www.seamonkey-project.org/",
164 "http://selfhtml.org/",
165 "http://www.shspvr.com/",
166 "http://www.sina.com.cn/",
167 "http://www.sohu.com/",
168 "http://www.sogou.com/",
169 "http://sourceforge.net/",
170 "http://www.spiegel.de/",
171 "http://www.sputnikmusic.com/",
172 "http://stackoverflow.com/",
173 "http://tdm-gcc.tdragon.net/",
174 "http://www.tdrsmusic.com/",
175 "http://www.ubuntu.com/",
176 "http://www.uol.com.br/",
177 "http://www.videohelp.com/",
178 "http://www.videolan.org/",
179 "http://virtualdub.org/",
180 "http://blog.virustotal.com/",
181 "http://www.vkgoeswild.com/",
182 "http://www.warr.org/WAhere.html",
183 "http://www.weibo.com/login.php",
184 "http://status.wikimedia.org/",
185 "http://www.winamp.com/",
186 "http://www.winhoros.de/",
189 "http://xhmikosr.1f0.de/",
191 "http://us.mail.yahoo.com/",
192 "http://www.youtube.com/yt/about/",
193 "http://www.zedo.com/",
194 "http://ffmpeg.zeranoe.com/",
198 static const int MIN_CONNSCORE
= 8;
199 static const int VERSION_INFO_EXPIRES_MONTHS
= 6;
200 static char *USER_AGENT_STR
= "Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20111106 IceCat/7.0.1";
203 static int getMaxProgress(void)
205 int counter
= MIN_CONNSCORE
+ 2;
206 for(int i
= 0; update_mirrors_prim
[i
]; i
++) counter
++;
207 for(int i
= 0; update_mirrors_back
[i
]; i
++) counter
++;
211 ////////////////////////////////////////////////////////////
213 ////////////////////////////////////////////////////////////
215 UpdateCheckerInfo::UpdateCheckerInfo(void)
220 void UpdateCheckerInfo::resetInfo(void)
223 m_buildDate
.setDate(1900, 1, 1);
224 m_downloadSite
.clear();
225 m_downloadAddress
.clear();
226 m_downloadFilename
.clear();
227 m_downloadFilecode
.clear();
228 m_downloadChecksum
.clear();
231 bool UpdateCheckerInfo::isComplete(void)
233 if(this->m_buildNo
< 1) return false;
234 if(this->m_buildDate
.year() < 2010) return false;
235 if(this->m_downloadSite
.isEmpty()) return false;
236 if(this->m_downloadAddress
.isEmpty()) return false;
237 if(this->m_downloadFilename
.isEmpty()) return false;
238 if(this->m_downloadFilecode
.isEmpty()) return false;
239 if(this->m_downloadChecksum
.isEmpty()) return false;
244 ////////////////////////////////////////////////////////////
245 // Constructor & Destructor
246 ////////////////////////////////////////////////////////////
248 UpdateChecker::UpdateChecker(const QString
&binWGet
, const QString
&binGnuPG
, const QString
&binKeys
, const QString
&applicationId
, const quint32
&installedBuildNo
, const bool betaUpdates
, const bool testMode
)
250 m_updateInfo(new UpdateCheckerInfo()),
251 m_binaryWGet(binWGet
),
252 m_binaryGnuPG(binGnuPG
),
253 m_binaryKeys(binKeys
),
254 m_applicationId(applicationId
),
255 m_installedBuildNo(installedBuildNo
),
256 m_betaUpdates(betaUpdates
),
257 m_testMode(testMode
),
258 m_maxProgress(getMaxProgress())
261 m_status
= UpdateStatus_NotStartedYet
;
264 if(m_binaryWGet
.isEmpty() || m_binaryGnuPG
.isEmpty() || m_binaryKeys
.isEmpty())
266 MUTILS_THROW("Tools not initialized correctly!");
270 UpdateChecker::~UpdateChecker(void)
274 ////////////////////////////////////////////////////////////
275 // Protected functions
276 ////////////////////////////////////////////////////////////
278 void UpdateChecker::run(void)
280 qDebug("Update checker thread started!");
281 MUTILS_EXCEPTION_HANDLER(m_testMode
? testKnownHosts() : checkForUpdates());
282 qDebug("Update checker thread completed.");
285 void UpdateChecker::checkForUpdates(void)
287 // ----- Initialization ----- //
290 m_updateInfo
->resetInfo();
294 // ----- Test Internet Connection ----- //
296 int connectionScore
= 0;
297 int maxConnectTries
= (3 * MIN_CONNSCORE
) / 2;
299 log("Checking internet connection...");
300 setStatus(UpdateStatus_CheckingConnection
);
302 const int networkStatus
= OS::network_status();
303 if(networkStatus
== OS::NETWORK_TYPE_NON
)
305 log("", "Operating system reports that the computer is currently offline !!!");
306 setProgress(m_maxProgress
);
307 setStatus(UpdateStatus_ErrorNoConnection
);
313 // ----- Test Known Hosts Connectivity ----- //
315 QStringList hostList
;
316 for(int i
= 0; known_hosts
[i
]; i
++)
318 hostList
<< QString::fromLatin1(known_hosts
[i
]);
321 while(!(hostList
.isEmpty() || (connectionScore
>= MIN_CONNSCORE
) || (maxConnectTries
< 1)))
323 switch(tryContactHost(hostList
.takeAt(next_rand32() % hostList
.count())))
325 case 01: connectionScore
+= 1; break;
326 case 02: connectionScore
+= 2; break;
327 default: maxConnectTries
-= 1; break;
329 setProgress(qBound(1, connectionScore
+ 1, MIN_CONNSCORE
+ 1));
333 if(connectionScore
< MIN_CONNSCORE
)
335 log("", "Connectivity test has failed: Internet connection appears to be broken!");
336 setProgress(m_maxProgress
);
337 setStatus(UpdateStatus_ErrorConnectionTestFailed
);
341 // ----- Build Mirror List ----- //
343 log("", "----", "", "Checking for updates online...");
344 setStatus(UpdateStatus_FetchingUpdates
);
346 QStringList mirrorList
;
347 for(int index
= 0; update_mirrors_prim
[index
]; index
++)
349 mirrorList
<< QString::fromLatin1(update_mirrors_prim
[index
]);
352 if(const int len
= mirrorList
.count())
354 const int rounds
= len
* 1097;
355 for(int i
= 0; i
< rounds
; i
++)
357 mirrorList
.swap(i
% len
, next_rand32() % len
);
361 for(int index
= 0; update_mirrors_back
[index
]; index
++)
363 mirrorList
<< QString::fromLatin1(update_mirrors_back
[index
]);
366 // ----- Fetch Update Info From Server ----- //
368 while(!mirrorList
.isEmpty())
370 QString currentMirror
= mirrorList
.takeFirst();
371 setProgress(m_progress
+ 1);
374 if(tryUpdateMirror(m_updateInfo
.data(), currentMirror
))
385 setProgress(m_maxProgress
);
389 if(m_updateInfo
->m_buildNo
> m_installedBuildNo
)
391 setStatus(UpdateStatus_CompletedUpdateAvailable
);
393 else if(m_updateInfo
->m_buildNo
== m_installedBuildNo
)
395 setStatus(UpdateStatus_CompletedNoUpdates
);
399 setStatus(UpdateStatus_CompletedNewVersionOlder
);
404 setStatus(UpdateStatus_ErrorFetchUpdateInfo
);
408 void UpdateChecker::testKnownHosts(void)
410 QStringList hostList
;
411 for(int i
= 0; known_hosts
[i
]; i
++)
413 hostList
<< QString::fromLatin1(known_hosts
[i
]);
416 qDebug("\n[Known Hosts]");
417 log("Testing all known hosts...", "", "---");
419 int hostCount
= hostList
.count();
420 while(!hostList
.isEmpty())
422 QString currentHost
= hostList
.takeFirst();
423 qDebug("Testing: %s", currentHost
.toLatin1().constData());
424 log("", "Testing:", currentHost
, "");
425 QString outFile
= QString("%1/%2.htm").arg(temp_folder(), rand_str());
427 if(!getFile(currentHost
, outFile
, 0, &httpOk
))
431 qWarning("\nConnectivity test was SLOW on the following site:\n%s\n", currentHost
.toLatin1().constData());
435 qWarning("\nConnectivity test FAILED on the following site:\n%s\n", currentHost
.toLatin1().constData());
439 QFile::remove(outFile
);
443 ////////////////////////////////////////////////////////////
445 ////////////////////////////////////////////////////////////
447 void UpdateChecker::setStatus(const int status
)
449 if(m_status
!= status
)
452 emit
statusChanged(status
);
456 void UpdateChecker::setProgress(const int progress
)
458 if(m_progress
!= progress
)
460 m_progress
= progress
;
461 emit
progressChanged(progress
);
465 void UpdateChecker::log(const QString
&str1
, const QString
&str2
, const QString
&str3
, const QString
&str4
)
467 if(!str1
.isNull()) emit
messageLogged(str1
);
468 if(!str2
.isNull()) emit
messageLogged(str2
);
469 if(!str3
.isNull()) emit
messageLogged(str3
);
470 if(!str4
.isNull()) emit
messageLogged(str4
);
473 int UpdateChecker::tryContactHost(const QString
&url
)
475 int result
= -1; bool httpOkay
= false;
476 const QString outFile
= QString("%1/%2.htm").arg(temp_folder(), rand_str());
477 log("", "Testing host:", url
);
479 if(getFile(url
, outFile
, 0, &httpOkay
))
481 log("Connection to host was established successfully.");
488 log("Connection to host timed out after HTTP OK was received.");
493 log("Connection failed: The host could not be reached!");
498 QFile::remove(outFile
);
502 bool UpdateChecker::tryUpdateMirror(UpdateCheckerInfo
*updateInfo
, const QString
&url
)
504 bool success
= false;
505 log("", "Trying mirror:", url
);
507 const QString randPart
= rand_str();
508 const QString outFileVers
= QString("%1/%2.ver").arg(temp_folder(), randPart
);
509 const QString outFileSign
= QString("%1/%2.sig").arg(temp_folder(), randPart
);
511 if(getUpdateInfo(url
, outFileVers
, outFileSign
))
513 log("", "Download okay, checking signature:");
514 if(checkSignature(outFileVers
, outFileSign
))
516 log("", "Signature okay, parsing info:");
517 success
= parseVersionInfo(outFileVers
, updateInfo
);
521 log("", "Bad signature, take care!");
526 log("", "Download has failed!");
529 QFile::remove(outFileVers
);
530 QFile::remove(outFileSign
);
535 bool UpdateChecker::getUpdateInfo(const QString
&url
, const QString
&outFileVers
, const QString
&outFileSign
)
537 log("", "Downloading update info:");
538 if(!getFile(QString("%1%2" ).arg(url
, mirror_url_postfix
[m_betaUpdates
? 1 : 0]), outFileVers
))
543 log("", "Downloading signature:");
544 if(!getFile(QString("%1%2.sig2").arg(url
, mirror_url_postfix
[m_betaUpdates
? 1 : 0]), outFileSign
))
552 bool UpdateChecker::getFile(const QString
&url
, const QString
&outFile
, const unsigned int maxRedir
, bool *httpOk
)
554 for(int i
= 0; i
< 2; i
++)
556 if(getFile(url
, (i
> 0), outFile
, maxRedir
, httpOk
))
564 bool UpdateChecker::getFile(const QString
&url
, const bool forceIp4
, const QString
&outFile
, const unsigned int maxRedir
, bool *httpOk
)
566 QFileInfo
output(outFile
);
567 output
.setCaching(false);
568 if(httpOk
) *httpOk
= false;
572 QFile::remove(output
.canonicalFilePath());
580 init_process(process
, output
.absolutePath());
588 args
<< "-T" << "15" << "--no-cache" << "--no-dns-cache" << QString().sprintf("--max-redirect=%u", maxRedir
);
589 args
<< QString("--referer=%1://%2/").arg(QUrl(url
).scheme(), QUrl(url
).host()) << "-U" << USER_AGENT_STR
;
590 args
<< "-O" << output
.fileName() << url
;
593 connect(&process
, SIGNAL(error(QProcess::ProcessError
)), &loop
, SLOT(quit()));
594 connect(&process
, SIGNAL(finished(int,QProcess::ExitStatus
)), &loop
, SLOT(quit()));
595 connect(&process
, SIGNAL(readyRead()), &loop
, SLOT(quit()));
598 timer
.setSingleShot(true);
599 connect(&timer
, SIGNAL(timeout()), &loop
, SLOT(quit()));
601 const QRegExp
httpResponseOK("200 OK$");
603 process
.start(m_binaryWGet
, args
);
605 if(!process
.waitForStarted())
612 while(process
.state() != QProcess::NotRunning
)
615 const bool bTimeOut
= (!timer
.isActive());
616 while(process
.canReadLine())
618 QString line
= QString::fromLatin1(process
.readLine()).simplified();
619 if(line
.contains(httpResponseOK
))
621 line
.append(" [OK]");
622 if(httpOk
) *httpOk
= true;
628 qWarning("WGet process timed out <-- killing!");
630 process
.waitForFinished();
631 log("!!! TIMEOUT !!!");
637 timer
.disconnect(&timer
, SIGNAL(timeout()), &loop
, SLOT(quit()));
639 log(QString().sprintf("Exited with code %d", process
.exitCode()));
640 return (process
.exitCode() == 0) && output
.exists() && output
.isFile();
643 bool UpdateChecker::checkSignature(const QString
&file
, const QString
&signature
)
645 if(QFileInfo(file
).absolutePath().compare(QFileInfo(signature
).absolutePath(), Qt::CaseInsensitive
) != 0)
647 qWarning("CheckSignature: File and signature should be in same folder!");
651 if(QFileInfo(file
).absolutePath().compare(QFileInfo(m_binaryKeys
).absolutePath(), Qt::CaseInsensitive
) != 0)
653 qWarning("CheckSignature: File and keyring should be in same folder!");
658 init_process(process
, QFileInfo(file
).absolutePath());
661 connect(&process
, SIGNAL(error(QProcess::ProcessError
)), &loop
, SLOT(quit()));
662 connect(&process
, SIGNAL(finished(int,QProcess::ExitStatus
)), &loop
, SLOT(quit()));
663 connect(&process
, SIGNAL(readyRead()), &loop
, SLOT(quit()));
665 process
.start(m_binaryGnuPG
, QStringList() << "--homedir" << "." << "--keyring" << QFileInfo(m_binaryKeys
).fileName() << QFileInfo(signature
).fileName() << QFileInfo(file
).fileName());
667 if(!process
.waitForStarted())
672 while(process
.state() == QProcess::Running
)
675 while(process
.canReadLine())
677 log(QString::fromLatin1(process
.readLine()).simplified());
681 log(QString().sprintf("Exited with code %d", process
.exitCode()));
682 return (process
.exitCode() == 0);
685 bool UpdateChecker::parseVersionInfo(const QString
&file
, UpdateCheckerInfo
*updateInfo
)
687 QRegExp
value("^(\\w+)=(.+)$");
688 QRegExp
section("^\\[(.+)\\]$");
690 QDate updateInfoDate
;
691 updateInfo
->resetInfo();
694 if(!data
.open(QIODevice::ReadOnly
))
696 qWarning("Cannot open update info file for reading!");
705 QString line
= QString::fromLatin1(data
.readLine()).trimmed();
706 if(section
.indexIn(line
) >= 0)
708 log(QString("Sec: [%1]").arg(section
.cap(1)));
709 inSec
= (section
.cap(1).compare(m_applicationId
, Qt::CaseInsensitive
) == 0);
710 inHdr
= (section
.cap(1).compare(QString::fromLatin1(header_id
), Qt::CaseInsensitive
) == 0);
713 if(inSec
&& (value
.indexIn(line
) >= 0))
715 log(QString("Val: '%1' ==> '%2").arg(value
.cap(1), value
.cap(2)));
716 if(value
.cap(1).compare("BuildNo", Qt::CaseInsensitive
) == 0)
719 const unsigned int temp
= value
.cap(2).toUInt(&ok
);
720 if(ok
) updateInfo
->m_buildNo
= temp
;
722 else if(value
.cap(1).compare("BuildDate", Qt::CaseInsensitive
) == 0)
724 const QDate temp
= QDate::fromString(value
.cap(2).trimmed(), Qt::ISODate
);
725 if(temp
.isValid()) updateInfo
->m_buildDate
= temp
;
727 else if(value
.cap(1).compare("DownloadSite", Qt::CaseInsensitive
) == 0)
729 updateInfo
->m_downloadSite
= value
.cap(2).trimmed();
731 else if(value
.cap(1).compare("DownloadAddress", Qt::CaseInsensitive
) == 0)
733 updateInfo
->m_downloadAddress
= value
.cap(2).trimmed();
735 else if(value
.cap(1).compare("DownloadFilename", Qt::CaseInsensitive
) == 0)
737 updateInfo
->m_downloadFilename
= value
.cap(2).trimmed();
739 else if(value
.cap(1).compare("DownloadFilecode", Qt::CaseInsensitive
) == 0)
741 updateInfo
->m_downloadFilecode
= value
.cap(2).trimmed();
743 else if(value
.cap(1).compare("DownloadChecksum", Qt::CaseInsensitive
) == 0)
745 updateInfo
->m_downloadChecksum
= value
.cap(2).trimmed();
748 if(inHdr
&& (value
.indexIn(line
) >= 0))
750 log(QString("Val: '%1' ==> '%2").arg(value
.cap(1), value
.cap(2)));
751 if(value
.cap(1).compare("TimestampCreated", Qt::CaseInsensitive
) == 0)
753 QDate temp
= QDate::fromString(value
.cap(2).trimmed(), Qt::ISODate
);
754 if(temp
.isValid()) updateInfoDate
= temp
;
759 if(!updateInfoDate
.isValid())
761 updateInfo
->resetInfo();
762 log("WARNING: Version info timestamp is missing!");
766 const QDate currentDate
= OS::current_date();
767 if(updateInfoDate
.addMonths(VERSION_INFO_EXPIRES_MONTHS
) < currentDate
)
769 updateInfo
->resetInfo();
770 log(QString::fromLatin1("WARNING: This version info has expired at %1!").arg(updateInfoDate
.addMonths(VERSION_INFO_EXPIRES_MONTHS
).toString(Qt::ISODate
)));
773 else if(currentDate
< updateInfoDate
)
775 log("Version info is from the future, take care!");
776 qWarning("Version info is from the future, take care!");
779 if(!updateInfo
->isComplete())
781 log("WARNING: Version info is incomplete!");
788 ////////////////////////////////////////////////////////////
790 ////////////////////////////////////////////////////////////
794 ////////////////////////////////////////////////////////////
796 ////////////////////////////////////////////////////////////