2012-02-29 18:33:50 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
|
2012-02-29 18:33:50 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2012-01-07 10:29:38 +01:00
|
|
|
#include "cabundleupdater.h"
|
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "networkmanager.h"
|
2014-02-19 22:07:21 +01:00
|
|
|
#include "browserwindow.h"
|
2014-03-09 21:51:42 +01:00
|
|
|
#include "datapaths.h"
|
2013-01-22 19:04:22 +01:00
|
|
|
#include "qztools.h"
|
2012-01-07 10:29:38 +01:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QTimer>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QFile>
|
|
|
|
|
2012-01-07 10:29:38 +01:00
|
|
|
CaBundleUpdater::CaBundleUpdater(NetworkManager* manager, QObject* parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_manager(manager)
|
|
|
|
, m_progress(Start)
|
2014-02-01 19:21:49 +01:00
|
|
|
, m_reply(0)
|
2012-01-07 10:29:38 +01:00
|
|
|
, m_latestBundleVersion(0)
|
|
|
|
{
|
2014-03-09 21:51:42 +01:00
|
|
|
m_bundleVersionFileName = DataPaths::path(DataPaths::Config) + "certificates/bundle_version";
|
|
|
|
m_bundleFileName = DataPaths::path(DataPaths::Config) + "certificates/ca-bundle.crt";
|
|
|
|
m_lastUpdateFileName = DataPaths::path(DataPaths::Config) + "certificates/last_update";
|
2012-01-07 10:29:38 +01:00
|
|
|
|
2013-03-11 17:09:28 +01:00
|
|
|
int updateTime = 30 * 1000;
|
|
|
|
|
|
|
|
// Check immediately on first run
|
|
|
|
if (!QFile(m_lastUpdateFileName).exists()) {
|
|
|
|
updateTime = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTimer::singleShot(updateTime, this, SLOT(start()));
|
2012-01-07 10:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CaBundleUpdater::start()
|
|
|
|
{
|
|
|
|
QFile updateFile(m_lastUpdateFileName);
|
|
|
|
bool updateNow = false;
|
|
|
|
|
|
|
|
if (updateFile.exists()) {
|
|
|
|
if (!updateFile.open(QFile::ReadOnly)) {
|
|
|
|
qWarning() << "CaBundleUpdater::start cannot open file for reading" << m_lastUpdateFileName;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
QDateTime updateTime = QDateTime::fromString(updateFile.readAll());
|
|
|
|
updateNow = updateTime.addDays(5) < QDateTime::currentDateTime();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
updateNow = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateNow) {
|
|
|
|
m_progress = CheckLastUpdate;
|
|
|
|
|
2014-02-19 22:07:21 +01:00
|
|
|
QUrl url = QUrl::fromEncoded(QString(Qz::WWWADDRESS + "/certs/bundle_version").toUtf8());
|
2012-01-31 21:10:22 +01:00
|
|
|
m_reply = m_manager->get(QNetworkRequest(url));
|
2012-01-07 10:29:38 +01:00
|
|
|
connect(m_reply, SIGNAL(finished()), this, SLOT(replyFinished()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CaBundleUpdater::replyFinished()
|
|
|
|
{
|
|
|
|
if (m_progress == CheckLastUpdate) {
|
|
|
|
QByteArray response = m_reply->readAll().trimmed();
|
|
|
|
m_reply->close();
|
|
|
|
m_reply->deleteLater();
|
|
|
|
|
|
|
|
if (m_reply->error() != QNetworkReply::NoError || response.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_latestBundleVersion = response.toInt();
|
2013-01-22 19:04:22 +01:00
|
|
|
int currentBundleVersion = QzTools::readAllFileContents(m_bundleVersionFileName).trimmed().toInt();
|
2012-01-07 10:29:38 +01:00
|
|
|
|
|
|
|
if (m_latestBundleVersion == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_latestBundleVersion > currentBundleVersion) {
|
|
|
|
m_progress = LoadBundle;
|
2012-01-31 21:10:22 +01:00
|
|
|
|
2014-02-19 22:07:21 +01:00
|
|
|
QUrl url = QUrl::fromEncoded(QString(Qz::WWWADDRESS + "/certs/ca-bundle.crt").toUtf8());
|
2012-01-31 21:10:22 +01:00
|
|
|
m_reply = m_manager->get(QNetworkRequest(url));
|
2012-01-07 10:29:38 +01:00
|
|
|
connect(m_reply, SIGNAL(finished()), this, SLOT(replyFinished()));
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile file(m_lastUpdateFileName);
|
|
|
|
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
|
|
|
|
qWarning() << "CaBundleUpdater::replyFinished cannot open file for writing" << m_lastUpdateFileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
file.write(QDateTime::currentDateTime().toString().toUtf8());
|
|
|
|
}
|
|
|
|
else if (m_progress == LoadBundle) {
|
|
|
|
QByteArray response = m_reply->readAll();
|
|
|
|
m_reply->close();
|
|
|
|
m_reply->deleteLater();
|
|
|
|
|
|
|
|
if (m_reply->error() != QNetworkReply::NoError || response.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile file(m_bundleVersionFileName);
|
|
|
|
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
|
|
|
|
qWarning() << "CaBundleUpdater::replyFinished cannot open file for writing" << m_bundleVersionFileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
file.write(QByteArray::number(m_latestBundleVersion));
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
file.setFileName(m_bundleFileName);
|
|
|
|
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
|
|
|
|
qWarning() << "CaBundleUpdater::replyFinished cannot open file for writing" << m_bundleFileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
file.write(response);
|
2013-03-11 15:47:03 +01:00
|
|
|
|
|
|
|
// Reload newly downloaded certificates
|
|
|
|
mApp->networkManager()->loadSettings();
|
2012-01-07 10:29:38 +01:00
|
|
|
}
|
|
|
|
}
|