• R/O
  • HTTP
  • SSH
  • HTTPS

MUtilities: 提交

MUtilities development repository


Commit MetaInfo

修订版ba7fa5aea592ce7e103c3d977b263b73b3944e99 (tree)
时间2018-10-16 06:26:36
作者LoRd_MuldeR <mulder2@gmx....>
CommiterLoRd_MuldeR

Log Message

Updated the UpdateChecker class to use cURL instead of Wget.

更改概述

差异

--- a/include/MUtils/UpdateChecker.h
+++ b/include/MUtils/UpdateChecker.h
@@ -81,7 +81,7 @@ namespace MUtils
8181 }
8282 update_status_t;
8383
84- UpdateChecker(const QString &binWGet, const QString &binMCat, const QString &binGnuPG, const QString &binKeys, const QString &applicationId, const quint32 &installedBuildNo, const bool betaUpdates, const bool testMode = false);
84+ UpdateChecker(const QString &binCurl, const QString &binGnuPG, const QString &binKeys, const QString &applicationId, const quint32 &installedBuildNo, const bool betaUpdates, const bool testMode = false);
8585 ~UpdateChecker(void);
8686
8787 const int getUpdateStatus(void) const { return m_status; }
@@ -115,11 +115,12 @@ namespace MUtils
115115 const QString m_applicationId;
116116 const quint32 m_installedBuildNo;
117117
118- const QString m_binaryWGet;
119- const QString m_binaryMCat;
118+ const QString m_binaryCurl;
120119 const QString m_binaryGnuPG;
121120 const QString m_binaryKeys;
122121
122+ const QScopedPointer<const QHash<QString, QString>> m_environment;
123+
123124 QAtomicInt m_success;
124125 QAtomicInt m_cancelled;
125126
@@ -131,11 +132,10 @@ namespace MUtils
131132 inline void log(const QString &str1, const QString &str2 = QString(), const QString &str3 = QString(), const QString &str4 = QString());
132133
133134 bool getUpdateInfo(const QString &url, const QString &outFileVers, const QString &outFileSign);
134- bool tryContactHost(const QString &hostname, const int &timeoutMsec, quint32 *const ipAddrOut = NULL);
135+ bool tryContactHost(const QString &hostname, const int &timeoutMsec);
135136 bool parseVersionInfo(const QString &file, UpdateCheckerInfo *updateInfo);
136137
137- bool getFile(const QString &url, const QString &outFile, const unsigned int maxRedir = 5U);
138- bool getFile(const QString &url, const bool forceIp4, const QString &outFile, const unsigned int maxRedir = 5U);
138+ bool getFile(const QString &url, const QString &outFile, const unsigned int maxRedir = 8U);
139139 bool checkSignature(const QString &file, const QString &signature);
140140 bool tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString &url, const bool &quick);
141141 };
--- a/src/UpdateChecker.cpp
+++ b/src/UpdateChecker.cpp
@@ -27,12 +27,14 @@
2727 #include <QStringList>
2828 #include <QFile>
2929 #include <QFileInfo>
30+#include <QDir>
3031 #include <QProcess>
3132 #include <QUrl>
3233 #include <QEventLoop>
3334 #include <QTimer>
3435 #include <QElapsedTimer>
3536 #include <QSet>
37+#include <QHash>
3638
3739 #include "Mirrors.h"
3840
@@ -55,7 +57,7 @@ static const int MAX_CONN_TIMEOUT = 16000;
5557 static const int DOWNLOAD_TIMEOUT = 30000;
5658
5759 static const int VERSION_INFO_EXPIRES_MONTHS = 6;
58-static char *USER_AGENT_STR = "Mozilla/5.0 (X11; Linux i686; rv:10.0) Gecko/20100101 Firefox/10.0"; /*use something innocuous*/
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*/
5961
6062 #define CHECK_CANCELLED() do \
6163 { \
@@ -96,6 +98,13 @@ static QStringList buildRandomList(const char *const values[])
9698 return list;
9799 }
98100
101+static const QHash<QString, QString> *initEnvVars(void)
102+{
103+ QHash<QString, QString> *const environment = new QHash<QString, QString>();
104+ environment->insert(QLatin1String("CURL_HOME"), QDir::toNativeSeparators(MUtils::temp_folder()));
105+ return environment;
106+}
107+
99108 ////////////////////////////////////////////////////////////
100109 // Update Info Class
101110 ////////////////////////////////////////////////////////////
@@ -133,23 +142,23 @@ bool MUtils::UpdateCheckerInfo::isComplete(void)
133142 // Constructor & Destructor
134143 ////////////////////////////////////////////////////////////
135144
136-MUtils::UpdateChecker::UpdateChecker(const QString &binWGet, const QString &binMCat, const QString &binGnuPG, const QString &binKeys, const QString &applicationId, const quint32 &installedBuildNo, const bool betaUpdates, const bool testMode)
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)
137146 :
138147 m_updateInfo(new UpdateCheckerInfo()),
139- m_binaryWGet(binWGet),
140- m_binaryMCat(binMCat),
148+ m_binaryCurl(binCurl),
141149 m_binaryGnuPG(binGnuPG),
142150 m_binaryKeys(binKeys),
143151 m_applicationId(applicationId),
144152 m_installedBuildNo(installedBuildNo),
145153 m_betaUpdates(betaUpdates),
146154 m_testMode(testMode),
147- m_maxProgress(getMaxProgress())
155+ m_maxProgress(getMaxProgress()),
156+ m_environment(initEnvVars())
148157 {
149158 m_status = UpdateStatus_NotStartedYet;
150159 m_progress = 0;
151160
152- if(m_binaryWGet.isEmpty() || m_binaryGnuPG.isEmpty() || m_binaryKeys.isEmpty())
161+ if(m_binaryCurl.isEmpty() || m_binaryGnuPG.isEmpty() || m_binaryKeys.isEmpty())
153162 {
154163 MUTILS_THROW("Tools not initialized correctly!");
155164 }
@@ -209,7 +218,7 @@ void MUtils::UpdateChecker::checkForUpdates(void)
209218 int connectionScore = 0;
210219 QStringList mirrorList = buildRandomList(known_hosts);
211220
212- for(int connectionTimout = 500; connectionTimout <= MAX_CONN_TIMEOUT; connectionTimout *= 2)
221+ for(int connectionTimout = 1000; connectionTimout <= MAX_CONN_TIMEOUT; connectionTimout *= 2)
213222 {
214223 QElapsedTimer elapsedTimer;
215224 elapsedTimer.start();
@@ -348,25 +357,12 @@ void MUtils::UpdateChecker::testMirrorsList(void)
348357 qDebug("\n[Known Hosts]");
349358 log("Testing all known hosts...", "", "---");
350359
351- QSet<quint32> ipAddrSet;
352- quint32 ipAddr;
353360 while(!knownHostList.isEmpty())
354361 {
355362 const QString currentHost = knownHostList.takeFirst();
356363 qDebug("Testing: %s", MUTILS_L1STR(currentHost));
357364 log(QLatin1String(""), "Testing:", currentHost, "");
358- if (tryContactHost(currentHost, DOWNLOAD_TIMEOUT, &ipAddr))
359- {
360- if (ipAddrSet.contains(ipAddr))
361- {
362- qWarning("Duplicate IP-address 0x%08X was encountered!", ipAddr);
363- }
364- else
365- {
366- ipAddrSet << ipAddr; /*not encountered yet*/
367- }
368- }
369- else
365+ if (!tryContactHost(currentHost, DOWNLOAD_TIMEOUT))
370366 {
371367 qWarning("\nConnectivity test FAILED on the following host:\n%s\n", MUTILS_L1STR(currentHost));
372368 }
@@ -572,22 +568,6 @@ bool MUtils::UpdateChecker::parseVersionInfo(const QString &file, UpdateCheckerI
572568
573569 bool MUtils::UpdateChecker::getFile(const QString &url, const QString &outFile, const unsigned int maxRedir)
574570 {
575- for (int i = 0; i < 2; i++)
576- {
577- if (getFile(url, (i > 0), outFile, maxRedir))
578- {
579- return true;
580- }
581- if (MUTILS_BOOLIFY(m_cancelled))
582- {
583- break; /*cancelled*/
584- }
585- }
586- return false;
587-}
588-
589-bool MUtils::UpdateChecker::getFile(const QString &url, const bool forceIp4, const QString &outFile, const unsigned int maxRedir)
590-{
591571 QFileInfo output(outFile);
592572 output.setCaching(false);
593573
@@ -601,18 +581,13 @@ bool MUtils::UpdateChecker::getFile(const QString &url, const bool forceIp4, con
601581 }
602582
603583 QProcess process;
604- init_process(process, output.absolutePath());
584+ init_process(process, output.absolutePath(), true, NULL, m_environment.data());
605585
606- QStringList args;
607- if (forceIp4)
608- {
609- args << "-4";
610- }
611-
612- args << "--no-config" << "--no-cache" << "--no-dns-cache" << "--no-check-certificate" << "--no-hsts";
613- args << QString().sprintf("--max-redirect=%u", maxRedir) << QString().sprintf("--timeout=%u", DOWNLOAD_TIMEOUT / 1000);
614- args << QString("--referer=%1://%2/").arg(QUrl(url).scheme(), QUrl(url).host()) << "-U" << USER_AGENT_STR;
615- args << "-O" << output.fileName() << url;
586+ QStringList args(QLatin1String("-vsSqkfL"));
587+ args << "-m" << QString::number(DOWNLOAD_TIMEOUT / 1000);
588+ args << "--max-redirs" << QString::number(maxRedir);
589+ args << "-U" << USER_AGENT_STR;
590+ args << "-o" << output.fileName() << url;
616591
617592 QEventLoop loop;
618593 connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
@@ -623,16 +598,13 @@ bool MUtils::UpdateChecker::getFile(const QString &url, const bool forceIp4, con
623598 timer.setSingleShot(true);
624599 connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
625600
626- const QRegExp httpResponseOK("200 OK$");
627-
628- process.start(m_binaryWGet, args);
629-
601+ process.start(m_binaryCurl, args);
630602 if (!process.waitForStarted())
631603 {
632604 return false;
633605 }
634606
635- timer.start(DOWNLOAD_TIMEOUT);
607+ timer.start(2 * DOWNLOAD_TIMEOUT);
636608
637609 while (process.state() != QProcess::NotRunning)
638610 {
@@ -641,14 +613,17 @@ bool MUtils::UpdateChecker::getFile(const QString &url, const bool forceIp4, con
641613 while (process.canReadLine())
642614 {
643615 const QString line = QString::fromLatin1(process.readLine()).simplified();
644- log(line);
616+ if (!line.isEmpty())
617+ {
618+ log(line);
619+ }
645620 }
646621 if (bTimeOut || MUTILS_BOOLIFY(m_cancelled))
647622 {
648- qWarning("WGet process timed out <-- killing!");
623+ qWarning("cURL process timed out <-- killing!");
649624 process.kill();
650625 process.waitForFinished();
651- log(bTimeOut ? "!!! TIMEOUT !!!": "!!! CANCELLED !!!");
626+ log(bTimeOut ? "PROCESS TIMEOUT !!!" : "CANCELLED BY USER !!!", "");
652627 return false;
653628 }
654629 }
@@ -660,16 +635,18 @@ bool MUtils::UpdateChecker::getFile(const QString &url, const bool forceIp4, con
660635 return (process.exitCode() == 0) && output.exists() && output.isFile();
661636 }
662637
663-bool MUtils::UpdateChecker::tryContactHost(const QString &hostname, const int &timeoutMsec, quint32 *const ipAddrOut)
638+bool MUtils::UpdateChecker::tryContactHost(const QString &hostname, const int &timeoutMsec)
664639 {
665640 log(QString("Connecting to host: %1").arg(hostname), "");
666641
667642 QProcess process;
668- init_process(process, temp_folder());
669-
670- QStringList args;
671- args << "--retry" << QString::number(3) << hostname << QString::number(80);
643+ init_process(process, temp_folder(), true, NULL, m_environment.data());
672644
645+ QStringList args(QLatin1String("-vsSqkI"));
646+ args << "-m" << QString::number(qMax(timeoutMsec, 1000) / 1000);
647+ args << "-U" << USER_AGENT_STR;
648+ args << "-o" << OS::null_device() << QString("http://%1/").arg(hostname);
649+
673650 QEventLoop loop;
674651 connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
675652 connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), &loop, SLOT(quit()));
@@ -678,22 +655,14 @@ bool MUtils::UpdateChecker::tryContactHost(const QString &hostname, const int &t
678655 QTimer timer;
679656 timer.setSingleShot(true);
680657 connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
681-
682- QScopedPointer<QRegExp> ipAddr;
683- if (ipAddrOut)
684- {
685- *ipAddrOut = 0;
686- ipAddr.reset(new QRegExp("Connecting\\s+to\\s+(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+):(\\d+)", Qt::CaseInsensitive));
687- }
688658
689- process.start(m_binaryMCat, args);
690-
659+ process.start(m_binaryCurl, args);
691660 if (!process.waitForStarted())
692661 {
693662 return false;
694663 }
695664
696- timer.start(timeoutMsec);
665+ timer.start(2 * qMax(timeoutMsec, 1000));
697666
698667 while (process.state() != QProcess::NotRunning)
699668 {
@@ -702,28 +671,17 @@ bool MUtils::UpdateChecker::tryContactHost(const QString &hostname, const int &t
702671 while (process.canReadLine())
703672 {
704673 const QString line = QString::fromLatin1(process.readLine()).simplified();
705- if (!ipAddr.isNull())
674+ if (!line.isEmpty())
706675 {
707- if (ipAddr->indexIn(line) >= 0)
708- {
709- quint32 values[4];
710- if (MUtils::regexp_parse_uint32((*ipAddr), values, 4))
711- {
712- *ipAddrOut |= ((values[0] & 0xFF) << 0x18);
713- *ipAddrOut |= ((values[1] & 0xFF) << 0x10);
714- *ipAddrOut |= ((values[2] & 0xFF) << 0x08);
715- *ipAddrOut |= ((values[3] & 0xFF) << 0x00);
716- }
717- }
676+ log(line);
718677 }
719- log(line);
720678 }
721679 if (bTimeOut || MUTILS_BOOLIFY(m_cancelled))
722680 {
723- qWarning("MCat process timed out <-- killing!");
681+ qWarning("cURL process timed out <-- killing!");
724682 process.kill();
725683 process.waitForFinished();
726- log(bTimeOut ? "!!! TIMEOUT !!!" : "!!! CANCELLED !!!");
684+ log(bTimeOut ? "PROCESS TIMEOUT !!!" : "CANCELLED BY USER !!!", "");
727685 return false;
728686 }
729687 }
@@ -785,7 +743,11 @@ bool MUtils::UpdateChecker::checkSignature(const QString &file, const QString &s
785743 loop.exec();
786744 while (process.canReadLine())
787745 {
788- log(QString::fromLatin1(process.readLine()).simplified());
746+ const QString line = QString::fromLatin1(process.readLine()).simplified();
747+ if (!line.isEmpty())
748+ {
749+ log(line);
750+ }
789751 }
790752 }
791753
Show on old repository browser