Use DownloadManager by DNSUpdater

This commit is contained in:
Vladimir Golovnev (qlassez) 2015-12-19 15:35:45 +03:00
parent e378a65508
commit 881108057d
2 changed files with 77 additions and 74 deletions

View file

@ -28,7 +28,6 @@
* Contact : chris@qbittorrent.org * Contact : chris@qbittorrent.org
*/ */
#include <QNetworkAccessManager>
#include <QDebug> #include <QDebug>
#include <QRegExp> #include <QRegExp>
#include <QStringList> #include <QStringList>
@ -37,6 +36,8 @@
#endif #endif
#include "base/logger.h" #include "base/logger.h"
#include "base/net/downloadmanager.h"
#include "base/net/downloadhandler.h"
#include "dnsupdater.h" #include "dnsupdater.h"
using namespace Net; using namespace Net;
@ -76,65 +77,62 @@ DNSUpdater::~DNSUpdater()
void DNSUpdater::checkPublicIP() void DNSUpdater::checkPublicIP()
{ {
Q_ASSERT(m_state == OK); Q_ASSERT(m_state == OK);
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply *)), SLOT(ipRequestFinished(QNetworkReply *))); DownloadHandler *handler = DownloadManager::instance()->downloadUrl(
"http://checkip.dyndns.org", false, 0, false,
QString("qBittorrent/%1").arg(VERSION));
connect(handler, SIGNAL(downloadFinished(QString, QByteArray)), SLOT(ipRequestFinished(QString, QByteArray)));
connect(handler, SIGNAL(downloadFailed(QString, QString)), SLOT(ipRequestFailed(QString, QString)));
m_lastIPCheckTime = QDateTime::currentDateTime(); m_lastIPCheckTime = QDateTime::currentDateTime();
QNetworkRequest request;
request.setUrl(QUrl("http://checkip.dyndns.org"));
request.setRawHeader("User-Agent", "qBittorrent/" VERSION);
manager->get(request);
} }
void DNSUpdater::ipRequestFinished(QNetworkReply *reply) void DNSUpdater::ipRequestFinished(const QString &url, const QByteArray &data)
{ {
qDebug() << Q_FUNC_INFO; Q_UNUSED(url);
if (reply->error()) {
// Error // Parse response
qWarning() << Q_FUNC_INFO << "Error:" << reply->errorString(); QRegExp ipregex("Current IP Address:\\s+([^<]+)</body>");
} if (ipregex.indexIn(data) >= 0) {
else { QString ipStr = ipregex.cap(1);
// Parse response qDebug() << Q_FUNC_INFO << "Regular expression captured the following IP:" << ipStr;
QRegExp ipregex("Current IP Address:\\s+([^<]+)</body>"); QHostAddress newIp(ipStr);
QString ret = reply->readAll(); if (!newIp.isNull()) {
if (ipregex.indexIn(ret) >= 0) { if (m_lastIP != newIp) {
QString ip_str = ipregex.cap(1); qDebug() << Q_FUNC_INFO << "The IP address changed, report the change to DynDNS...";
qDebug() << Q_FUNC_INFO << "Regular expression captured the following IP:" << ip_str; qDebug() << m_lastIP.toString() << "->" << newIp.toString();
QHostAddress new_ip(ip_str); m_lastIP = newIp;
if (!new_ip.isNull()) { updateDNSService();
if (m_lastIP != new_ip) {
qDebug() << Q_FUNC_INFO << "The IP address changed, report the change to DynDNS...";
qDebug() << m_lastIP.toString() << "->" << new_ip.toString();
m_lastIP = new_ip;
updateDNSService();
}
}
else {
qWarning() << Q_FUNC_INFO << "Failed to construct a QHostAddress from the IP string";
} }
} }
else { else {
qWarning() << Q_FUNC_INFO << "Regular expression failed to capture the IP address"; qWarning() << Q_FUNC_INFO << "Failed to construct a QHostAddress from the IP string";
} }
} }
// Clean up else {
reply->deleteLater(); qWarning() << Q_FUNC_INFO << "Regular expression failed to capture the IP address";
sender()->deleteLater(); }
}
void DNSUpdater::ipRequestFailed(const QString &url, const QString &error)
{
Q_UNUSED(url);
qWarning() << "IP request failed:" << error;
} }
void DNSUpdater::updateDNSService() void DNSUpdater::updateDNSService()
{ {
qDebug() << Q_FUNC_INFO; qDebug() << Q_FUNC_INFO;
// Prepare request
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply *)), SLOT(ipUpdateFinished(QNetworkReply *)));
m_lastIPCheckTime = QDateTime::currentDateTime(); m_lastIPCheckTime = QDateTime::currentDateTime();
QNetworkRequest request; DownloadHandler *handler = DownloadManager::instance()->downloadUrl(
request.setUrl(getUpdateUrl()); getUpdateUrl(), false, 0, false,
request.setRawHeader("User-Agent", "qBittorrent/" VERSION); QString("qBittorrent/%1").arg(VERSION));
manager->get(request); connect(handler, SIGNAL(downloadFinished(QString, QByteArray)), SLOT(ipUpdateFinished(QString, QByteArray)));
connect(handler, SIGNAL(downloadFailed(QString, QString)), SLOT(ipUpdateFailed(QString, QString)));
} }
QUrl DNSUpdater::getUpdateUrl() const QString DNSUpdater::getUpdateUrl() const
{ {
QUrl url; QUrl url;
#ifdef QT_NO_OPENSSL #ifdef QT_NO_OPENSSL
@ -172,22 +170,20 @@ QUrl DNSUpdater::getUpdateUrl() const
Q_ASSERT(url.isValid()); Q_ASSERT(url.isValid());
qDebug() << Q_FUNC_INFO << url.toString(); qDebug() << Q_FUNC_INFO << url.toString();
return url; return url.toString();
} }
void DNSUpdater::ipUpdateFinished(QNetworkReply *reply) void DNSUpdater::ipUpdateFinished(const QString &url, const QByteArray &data)
{ {
if (reply->error()) { Q_UNUSED(url);
// Error // Parse reply
qWarning() << Q_FUNC_INFO << "Error:" << reply->errorString(); processIPUpdateReply(data);
} }
else {
// Parse reply void DNSUpdater::ipUpdateFailed(const QString &url, const QString &error)
processIPUpdateReply(reply->readAll()); {
} Q_UNUSED(url);
// Clean up qWarning() << "IP update failed:" << error;
reply->deleteLater();
sender()->deleteLater();
} }
void DNSUpdater::processIPUpdateReply(const QString &reply) void DNSUpdater::processIPUpdateReply(const QString &reply)
@ -196,16 +192,19 @@ void DNSUpdater::processIPUpdateReply(const QString &reply)
qDebug() << Q_FUNC_INFO << reply; qDebug() << Q_FUNC_INFO << reply;
QString code = reply.split(" ").first(); QString code = reply.split(" ").first();
qDebug() << Q_FUNC_INFO << "Code:" << code; qDebug() << Q_FUNC_INFO << "Code:" << code;
if (code == "good" || code == "nochg") {
if ((code == "good") || (code == "nochg")) {
logger->addMessage(tr("Your dynamic DNS was successfully updated."), Log::INFO); logger->addMessage(tr("Your dynamic DNS was successfully updated."), Log::INFO);
return; return;
} }
if ((code == "911") || (code == "dnserr")) { if ((code == "911") || (code == "dnserr")) {
logger->addMessage(tr("Dynamic DNS error: The service is temporarily unavailable, it will be retried in 30 minutes."), Log::CRITICAL); logger->addMessage(tr("Dynamic DNS error: The service is temporarily unavailable, it will be retried in 30 minutes."), Log::CRITICAL);
m_lastIP.clear(); m_lastIP.clear();
// It will retry in 30 minutes because the timer was not stopped // It will retry in 30 minutes because the timer was not stopped
return; return;
} }
// Everything bellow is an error, stop updating until the user updates something // Everything bellow is an error, stop updating until the user updates something
m_ipCheckTimer.stop(); m_ipCheckTimer.stop();
m_lastIP.clear(); m_lastIP.clear();
@ -214,23 +213,27 @@ void DNSUpdater::processIPUpdateReply(const QString &reply)
m_state = INVALID_CREDS; m_state = INVALID_CREDS;
return; return;
} }
if (code == "badauth") { if (code == "badauth") {
logger->addMessage(tr("Dynamic DNS error: Invalid username/password."), Log::CRITICAL); logger->addMessage(tr("Dynamic DNS error: Invalid username/password."), Log::CRITICAL);
m_state = INVALID_CREDS; m_state = INVALID_CREDS;
return; return;
} }
if (code == "badagent") { if (code == "badagent") {
logger->addMessage(tr("Dynamic DNS error: qBittorrent was blacklisted by the service, please report a bug at http://bugs.qbittorrent.org."), logger->addMessage(tr("Dynamic DNS error: qBittorrent was blacklisted by the service, please report a bug at http://bugs.qbittorrent.org."),
Log::CRITICAL); Log::CRITICAL);
m_state = FATAL; m_state = FATAL;
return; return;
} }
if (code == "!donator") { if (code == "!donator") {
logger->addMessage(tr("Dynamic DNS error: %1 was returned by the service, please report a bug at http://bugs.qbittorrent.org.").arg("!donator"), logger->addMessage(tr("Dynamic DNS error: %1 was returned by the service, please report a bug at http://bugs.qbittorrent.org.").arg("!donator"),
Log::CRITICAL); Log::CRITICAL);
m_state = FATAL; m_state = FATAL;
return; return;
} }
if (code == "abuse") { if (code == "abuse") {
logger->addMessage(tr("Dynamic DNS error: Your username was blocked due to abuse."), Log::CRITICAL); logger->addMessage(tr("Dynamic DNS error: Your username was blocked due to abuse."), Log::CRITICAL);
m_state = FATAL; m_state = FATAL;

View file

@ -33,15 +33,15 @@
#include <QObject> #include <QObject>
#include <QHostAddress> #include <QHostAddress>
#include <QNetworkReply>
#include <QDateTime> #include <QDateTime>
#include <QTimer> #include <QTimer>
#include "base/preferences.h" #include "base/preferences.h"
namespace Net namespace Net
{ {
// Based on http://www.dyndns.com/developers/specs/ // Based on http://www.dyndns.com/developers/specs/
class DNSUpdater : public QObject class DNSUpdater: public QObject
{ {
Q_OBJECT Q_OBJECT
@ -56,15 +56,25 @@ namespace Net
private slots: private slots:
void checkPublicIP(); void checkPublicIP();
void ipRequestFinished(QNetworkReply *reply); void ipRequestFinished(const QString &url, const QByteArray &data);
void ipRequestFailed(const QString &url, const QString &error);
void updateDNSService(); void updateDNSService();
void ipUpdateFinished(QNetworkReply *reply); void ipUpdateFinished(const QString &url, const QByteArray &data);
void ipUpdateFailed(const QString &url, const QString &error);
private: private:
QUrl getUpdateUrl() const; enum State
{
OK,
INVALID_CREDS,
FATAL
};
static const int IP_CHECK_INTERVAL_MS = 1800000; // 30 min
QString getUpdateUrl() const;
void processIPUpdateReply(const QString &reply); void processIPUpdateReply(const QString &reply);
private:
QHostAddress m_lastIP; QHostAddress m_lastIP;
QDateTime m_lastIPCheckTime; QDateTime m_lastIPCheckTime;
QTimer m_ipCheckTimer; QTimer m_ipCheckTimer;
@ -74,16 +84,6 @@ namespace Net
QString m_domain; QString m_domain;
QString m_username; QString m_username;
QString m_password; QString m_password;
private:
static const int IP_CHECK_INTERVAL_MS = 1800000; // 30 min
enum State
{
OK,
INVALID_CREDS,
FATAL
};
}; };
} }