qBittorrent/src/base/net/dnsupdater.cpp

301 lines
9.9 KiB
C++
Raw Normal View History

/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2011 Christophe Dumez <chris@qbittorrent.org>
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include <QDebug>
#include <QRegExp>
#include <QStringList>
#include <QUrlQuery>
2015-05-13 18:39:48 +03:00
2015-09-25 11:10:05 +03:00
#include "base/logger.h"
2015-12-19 15:35:45 +03:00
#include "base/net/downloadhandler.h"
#include "base/net/downloadmanager.h"
2015-05-13 18:39:48 +03:00
#include "dnsupdater.h"
2015-04-13 19:02:48 +03:00
using namespace Net;
2015-05-13 18:39:48 +03:00
DNSUpdater::DNSUpdater(QObject *parent)
: QObject(parent)
, m_state(OK)
, m_service(DNS::NONE)
{
2015-05-13 18:39:48 +03:00
updateCredentials();
2015-05-13 18:39:48 +03:00
// Load saved settings from previous session
const Preferences *const pref = Preferences::instance();
m_lastIPCheckTime = pref->getDNSLastUpd();
m_lastIP = QHostAddress(pref->getDNSLastIP());
2015-05-13 18:39:48 +03:00
// Start IP checking timer
m_ipCheckTimer.setInterval(IP_CHECK_INTERVAL_MS);
2018-04-18 16:59:41 +03:00
connect(&m_ipCheckTimer, &QTimer::timeout, this, &DNSUpdater::checkPublicIP);
2015-05-13 18:39:48 +03:00
m_ipCheckTimer.start();
2015-05-13 18:39:48 +03:00
// Check lastUpdate to avoid flooding
if (!m_lastIPCheckTime.isValid()
|| (m_lastIPCheckTime.secsTo(QDateTime::currentDateTime()) * 1000 > IP_CHECK_INTERVAL_MS)) {
checkPublicIP();
}
}
2015-05-13 18:39:48 +03:00
DNSUpdater::~DNSUpdater()
{
// Save lastupdate time and last ip
Preferences *const pref = Preferences::instance();
pref->setDNSLastUpd(m_lastIPCheckTime);
pref->setDNSLastIP(m_lastIP.toString());
}
void DNSUpdater::checkPublicIP()
{
2015-05-13 18:39:48 +03:00
Q_ASSERT(m_state == OK);
2015-12-19 15:35:45 +03:00
DownloadHandler *handler = DownloadManager::instance()->downloadUrl(
"http://checkip.dyndns.org", false, 0, false,
"qBittorrent/" QBT_VERSION_2);
2018-04-18 16:59:41 +03:00
connect(handler, static_cast<void (Net::DownloadHandler::*)(const QString &, const QByteArray &)>(&Net::DownloadHandler::downloadFinished)
, this, &DNSUpdater::ipRequestFinished);
connect(handler, &Net::DownloadHandler::downloadFailed, this, &DNSUpdater::ipRequestFailed);
2015-12-19 15:35:45 +03:00
2015-05-13 18:39:48 +03:00
m_lastIPCheckTime = QDateTime::currentDateTime();
}
2015-12-19 15:35:45 +03:00
void DNSUpdater::ipRequestFinished(const QString &url, const QByteArray &data)
{
2015-12-19 15:35:45 +03:00
Q_UNUSED(url);
// Parse response
QRegExp ipregex("Current IP Address:\\s+([^<]+)</body>");
if (ipregex.indexIn(data) >= 0) {
QString ipStr = ipregex.cap(1);
qDebug() << Q_FUNC_INFO << "Regular expression captured the following IP:" << ipStr;
QHostAddress newIp(ipStr);
if (!newIp.isNull()) {
if (m_lastIP != newIp) {
qDebug() << Q_FUNC_INFO << "The IP address changed, report the change to DynDNS...";
qDebug() << m_lastIP.toString() << "->" << newIp.toString();
m_lastIP = newIp;
updateDNSService();
2015-05-13 18:39:48 +03:00
}
}
else {
2015-12-19 15:35:45 +03:00
qWarning() << Q_FUNC_INFO << "Failed to construct a QHostAddress from the IP string";
}
}
2015-12-19 15:35:45 +03:00
else {
qWarning() << Q_FUNC_INFO << "Regular expression failed to capture the IP address";
}
}
void DNSUpdater::ipRequestFailed(const QString &url, const QString &error)
{
Q_UNUSED(url);
qWarning() << "IP request failed:" << error;
}
void DNSUpdater::updateDNSService()
{
2015-05-13 18:39:48 +03:00
qDebug() << Q_FUNC_INFO;
2015-12-19 15:35:45 +03:00
2015-05-13 18:39:48 +03:00
m_lastIPCheckTime = QDateTime::currentDateTime();
2015-12-19 15:35:45 +03:00
DownloadHandler *handler = DownloadManager::instance()->downloadUrl(
getUpdateUrl(), false, 0, false,
"qBittorrent/" QBT_VERSION_2);
2018-04-18 16:59:41 +03:00
connect(handler, static_cast<void (Net::DownloadHandler::*)(const QString &, const QByteArray &)>(&Net::DownloadHandler::downloadFinished)
, this, &DNSUpdater::ipUpdateFinished);
connect(handler, &Net::DownloadHandler::downloadFailed, this, &DNSUpdater::ipUpdateFailed);
}
2015-12-19 15:35:45 +03:00
QString DNSUpdater::getUpdateUrl() const
{
2015-05-13 18:39:48 +03:00
QUrl url;
#ifdef QT_NO_OPENSSL
2015-05-13 18:39:48 +03:00
url.setScheme("http");
#else
2015-05-13 18:39:48 +03:00
url.setScheme("https");
#endif
2015-05-13 18:39:48 +03:00
url.setUserName(m_username);
url.setPassword(m_password);
2015-05-13 18:39:48 +03:00
Q_ASSERT(!m_lastIP.isNull());
// Service specific
switch (m_service) {
2015-05-13 18:39:48 +03:00
case DNS::DYNDNS:
url.setHost("members.dyndns.org");
break;
case DNS::NOIP:
url.setHost("dynupdate.no-ip.com");
break;
default:
qWarning() << "Unrecognized Dynamic DNS service!";
Q_ASSERT(0);
}
url.setPath("/nic/update");
2015-05-13 18:39:48 +03:00
QUrlQuery urlQuery(url);
urlQuery.addQueryItem("hostname", m_domain);
urlQuery.addQueryItem("myip", m_lastIP.toString());
url.setQuery(urlQuery);
Q_ASSERT(url.isValid());
2015-05-13 18:39:48 +03:00
qDebug() << Q_FUNC_INFO << url.toString();
2015-12-19 15:35:45 +03:00
return url.toString();
}
2015-12-19 15:35:45 +03:00
void DNSUpdater::ipUpdateFinished(const QString &url, const QByteArray &data)
{
2015-12-19 15:35:45 +03:00
Q_UNUSED(url);
// Parse reply
processIPUpdateReply(data);
}
void DNSUpdater::ipUpdateFailed(const QString &url, const QString &error)
{
Q_UNUSED(url);
qWarning() << "IP update failed:" << error;
}
void DNSUpdater::processIPUpdateReply(const QString &reply)
{
2015-05-13 18:39:48 +03:00
Logger *const logger = Logger::instance();
qDebug() << Q_FUNC_INFO << reply;
QString code = reply.split(" ").first();
qDebug() << Q_FUNC_INFO << "Code:" << code;
2015-12-19 15:35:45 +03:00
if ((code == "good") || (code == "nochg")) {
2015-05-13 18:39:48 +03:00
logger->addMessage(tr("Your dynamic DNS was successfully updated."), Log::INFO);
return;
}
2015-12-19 15:35:45 +03:00
2015-05-13 18:39:48 +03:00
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);
m_lastIP.clear();
// It will retry in 30 minutes because the timer was not stopped
return;
}
2015-12-19 15:35:45 +03:00
2018-03-14 18:15:51 +03:00
// Everything below is an error, stop updating until the user updates something
2015-05-13 18:39:48 +03:00
m_ipCheckTimer.stop();
m_lastIP.clear();
2015-05-13 18:39:48 +03:00
if (code == "nohost") {
logger->addMessage(tr("Dynamic DNS error: hostname supplied does not exist under specified account."), Log::CRITICAL);
m_state = INVALID_CREDS;
return;
}
2015-12-19 15:35:45 +03:00
2015-05-13 18:39:48 +03:00
if (code == "badauth") {
logger->addMessage(tr("Dynamic DNS error: Invalid username/password."), Log::CRITICAL);
m_state = INVALID_CREDS;
return;
}
2015-12-19 15:35:45 +03:00
2015-05-13 18:39:48 +03:00
if (code == "badagent") {
logger->addMessage(tr("Dynamic DNS error: qBittorrent was blacklisted by the service, please report a bug at http://bugs.qbittorrent.org."),
Log::CRITICAL);
m_state = FATAL;
return;
}
2015-12-19 15:35:45 +03:00
2015-05-13 18:39:48 +03:00
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"),
Log::CRITICAL);
m_state = FATAL;
return;
}
2015-12-19 15:35:45 +03:00
2015-05-13 18:39:48 +03:00
if (code == "abuse") {
logger->addMessage(tr("Dynamic DNS error: Your username was blocked due to abuse."), Log::CRITICAL);
m_state = FATAL;
}
}
void DNSUpdater::updateCredentials()
{
2015-05-13 18:39:48 +03:00
if (m_state == FATAL) return;
Preferences *const pref = Preferences::instance();
Logger *const logger = Logger::instance();
bool change = false;
// Get DNS service information
if (m_service != pref->getDynDNSService()) {
m_service = pref->getDynDNSService();
change = true;
}
if (m_domain != pref->getDynDomainName()) {
m_domain = pref->getDynDomainName();
QRegExp domain_regex("^(?:(?!\\d|-)[a-zA-Z0-9\\-]{1,63}\\.)+[a-zA-Z]{2,}$");
if (domain_regex.indexIn(m_domain) < 0) {
logger->addMessage(tr("Dynamic DNS error: supplied domain name is invalid."), Log::CRITICAL);
m_lastIP.clear();
m_ipCheckTimer.stop();
m_state = INVALID_CREDS;
return;
}
change = true;
}
2015-05-13 18:39:48 +03:00
if (m_username != pref->getDynDNSUsername()) {
m_username = pref->getDynDNSUsername();
if (m_username.length() < 4) {
logger->addMessage(tr("Dynamic DNS error: supplied username is too short."), Log::CRITICAL);
m_lastIP.clear();
m_ipCheckTimer.stop();
m_state = INVALID_CREDS;
return;
}
change = true;
}
2015-05-13 18:39:48 +03:00
if (m_password != pref->getDynDNSPassword()) {
m_password = pref->getDynDNSPassword();
if (m_password.length() < 4) {
logger->addMessage(tr("Dynamic DNS error: supplied password is too short."), Log::CRITICAL);
m_lastIP.clear();
m_ipCheckTimer.stop();
m_state = INVALID_CREDS;
return;
}
change = true;
}
2015-05-13 18:39:48 +03:00
if ((m_state == INVALID_CREDS) && change) {
m_state = OK; // Try again
m_ipCheckTimer.start();
checkPublicIP();
}
}
QUrl DNSUpdater::getRegistrationUrl(int service)
{
switch (service) {
2015-05-13 18:39:48 +03:00
case DNS::DYNDNS:
return QUrl("https://www.dyndns.com/account/services/hosts/add.html");
case DNS::NOIP:
2016-03-07 02:48:45 +03:00
return QUrl("https://www.noip.com/remote-access");
2015-05-13 18:39:48 +03:00
default:
Q_ASSERT(0);
}
return QUrl();
}