Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / base / net / dnsupdater.cpp
blob2fcffcdf6408ed5aec8e5fc7dd420f43cc99675e
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2011 Christophe Dumez <chris@qbittorrent.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "dnsupdater.h"
31 #include <QDebug>
32 #include <QRegularExpression>
33 #include <QUrlQuery>
35 #include "base/global.h"
36 #include "base/logger.h"
37 #include "base/net/downloadmanager.h"
38 #include "base/version.h"
40 using namespace Net;
42 DNSUpdater::DNSUpdater(QObject *parent)
43 : QObject(parent)
44 , m_state(OK)
45 , m_service(DNS::Service::None)
47 updateCredentials();
49 // Load saved settings from previous session
50 const Preferences *const pref = Preferences::instance();
51 m_lastIPCheckTime = pref->getDNSLastUpd();
52 m_lastIP = QHostAddress(pref->getDNSLastIP());
54 // Start IP checking timer
55 m_ipCheckTimer.setInterval(IP_CHECK_INTERVAL_MS);
56 connect(&m_ipCheckTimer, &QTimer::timeout, this, &DNSUpdater::checkPublicIP);
57 m_ipCheckTimer.start();
59 // Check lastUpdate to avoid flooding
60 if (!m_lastIPCheckTime.isValid()
61 || (m_lastIPCheckTime.secsTo(QDateTime::currentDateTime()) * 1000 > IP_CHECK_INTERVAL_MS))
63 checkPublicIP();
67 DNSUpdater::~DNSUpdater()
69 // Save lastupdate time and last ip
70 Preferences *const pref = Preferences::instance();
71 pref->setDNSLastUpd(m_lastIPCheckTime);
72 pref->setDNSLastIP(m_lastIP.toString());
75 void DNSUpdater::checkPublicIP()
77 Q_ASSERT(m_state == OK);
79 DownloadManager::instance()->download(
80 DownloadRequest(u"http://checkip.dyndns.org"_qs).userAgent(QStringLiteral("qBittorrent/" QBT_VERSION_2))
81 , this, &DNSUpdater::ipRequestFinished);
83 m_lastIPCheckTime = QDateTime::currentDateTime();
86 void DNSUpdater::ipRequestFinished(const DownloadResult &result)
88 if (result.status != DownloadStatus::Success)
90 qWarning() << "IP request failed:" << result.errorString;
91 return;
94 // Parse response
95 const QRegularExpressionMatch ipRegexMatch = QRegularExpression(u"Current IP Address:\\s+([^<]+)</body>"_qs).match(QString::fromUtf8(result.data));
96 if (ipRegexMatch.hasMatch())
98 QString ipStr = ipRegexMatch.captured(1);
99 qDebug() << Q_FUNC_INFO << "Regular expression captured the following IP:" << ipStr;
100 QHostAddress newIp(ipStr);
101 if (!newIp.isNull())
103 if (m_lastIP != newIp)
105 qDebug() << Q_FUNC_INFO << "The IP address changed, report the change to DynDNS...";
106 qDebug() << m_lastIP.toString() << "->" << newIp.toString();
107 m_lastIP = newIp;
108 updateDNSService();
111 else
113 qWarning() << Q_FUNC_INFO << "Failed to construct a QHostAddress from the IP string";
116 else
118 qWarning() << Q_FUNC_INFO << "Regular expression failed to capture the IP address";
122 void DNSUpdater::updateDNSService()
124 qDebug() << Q_FUNC_INFO;
126 m_lastIPCheckTime = QDateTime::currentDateTime();
127 DownloadManager::instance()->download(
128 DownloadRequest(getUpdateUrl()).userAgent(QStringLiteral("qBittorrent/" QBT_VERSION_2))
129 , this, &DNSUpdater::ipUpdateFinished);
132 QString DNSUpdater::getUpdateUrl() const
134 QUrl url;
135 #ifdef QT_NO_OPENSSL
136 url.setScheme(u"http"_qs);
137 #else
138 url.setScheme(u"https"_qs);
139 #endif
140 url.setUserName(m_username);
141 url.setPassword(m_password);
143 Q_ASSERT(!m_lastIP.isNull());
144 // Service specific
145 switch (m_service)
147 case DNS::Service::DynDNS:
148 url.setHost(u"members.dyndns.org"_qs);
149 break;
150 case DNS::Service::NoIP:
151 url.setHost(u"dynupdate.no-ip.com"_qs);
152 break;
153 default:
154 qWarning() << "Unrecognized Dynamic DNS service!";
155 Q_ASSERT(false);
156 break;
158 url.setPath(u"/nic/update"_qs);
160 QUrlQuery urlQuery(url);
161 urlQuery.addQueryItem(u"hostname"_qs, m_domain);
162 urlQuery.addQueryItem(u"myip"_qs, m_lastIP.toString());
163 url.setQuery(urlQuery);
164 Q_ASSERT(url.isValid());
166 qDebug() << Q_FUNC_INFO << url.toString();
167 return url.toString();
170 void DNSUpdater::ipUpdateFinished(const DownloadResult &result)
172 if (result.status == DownloadStatus::Success)
173 processIPUpdateReply(QString::fromUtf8(result.data));
174 else
175 qWarning() << "IP update failed:" << result.errorString;
178 void DNSUpdater::processIPUpdateReply(const QString &reply)
180 Logger *const logger = Logger::instance();
181 qDebug() << Q_FUNC_INFO << reply;
182 const QString code = reply.split(u' ').first();
183 qDebug() << Q_FUNC_INFO << "Code:" << code;
185 if ((code == u"good") || (code == u"nochg"))
187 logger->addMessage(tr("Your dynamic DNS was successfully updated."), Log::INFO);
188 return;
191 if ((code == u"911") || (code == u"dnserr"))
193 logger->addMessage(tr("Dynamic DNS error: The service is temporarily unavailable, it will be retried in 30 minutes."), Log::CRITICAL);
194 m_lastIP.clear();
195 // It will retry in 30 minutes because the timer was not stopped
196 return;
199 // Everything below is an error, stop updating until the user updates something
200 m_ipCheckTimer.stop();
201 m_lastIP.clear();
202 if (code == u"nohost")
204 logger->addMessage(tr("Dynamic DNS error: hostname supplied does not exist under specified account."), Log::CRITICAL);
205 m_state = INVALID_CREDS;
206 return;
209 if (code == u"badauth")
211 logger->addMessage(tr("Dynamic DNS error: Invalid username/password."), Log::CRITICAL);
212 m_state = INVALID_CREDS;
213 return;
216 if (code == u"badagent")
218 logger->addMessage(tr("Dynamic DNS error: qBittorrent was blacklisted by the service, please submit a bug report at http://bugs.qbittorrent.org."),
219 Log::CRITICAL);
220 m_state = FATAL;
221 return;
224 if (code == u"!donator")
226 logger->addMessage(tr("Dynamic DNS error: %1 was returned by the service, please submit a bug report at http://bugs.qbittorrent.org.").arg(u"!donator"_qs),
227 Log::CRITICAL);
228 m_state = FATAL;
229 return;
232 if (code == u"abuse")
234 logger->addMessage(tr("Dynamic DNS error: Your username was blocked due to abuse."), Log::CRITICAL);
235 m_state = FATAL;
239 void DNSUpdater::updateCredentials()
241 if (m_state == FATAL) return;
242 Preferences *const pref = Preferences::instance();
243 Logger *const logger = Logger::instance();
244 bool change = false;
245 // Get DNS service information
246 if (m_service != pref->getDynDNSService())
248 m_service = pref->getDynDNSService();
249 change = true;
251 if (m_domain != pref->getDynDomainName())
253 m_domain = pref->getDynDomainName();
254 const QRegularExpressionMatch domainRegexMatch = QRegularExpression(u"^(?:(?!\\d|-)[a-zA-Z0-9\\-]{1,63}\\.)+[a-zA-Z]{2,}$"_qs).match(m_domain);
255 if (!domainRegexMatch.hasMatch())
257 logger->addMessage(tr("Dynamic DNS error: supplied domain name is invalid."), Log::CRITICAL);
258 m_lastIP.clear();
259 m_ipCheckTimer.stop();
260 m_state = INVALID_CREDS;
261 return;
263 change = true;
265 if (m_username != pref->getDynDNSUsername())
267 m_username = pref->getDynDNSUsername();
268 if (m_username.length() < 4)
270 logger->addMessage(tr("Dynamic DNS error: supplied username is too short."), Log::CRITICAL);
271 m_lastIP.clear();
272 m_ipCheckTimer.stop();
273 m_state = INVALID_CREDS;
274 return;
276 change = true;
278 if (m_password != pref->getDynDNSPassword())
280 m_password = pref->getDynDNSPassword();
281 if (m_password.length() < 4)
283 logger->addMessage(tr("Dynamic DNS error: supplied password is too short."), Log::CRITICAL);
284 m_lastIP.clear();
285 m_ipCheckTimer.stop();
286 m_state = INVALID_CREDS;
287 return;
289 change = true;
292 if ((m_state == INVALID_CREDS) && change)
294 m_state = OK; // Try again
295 m_ipCheckTimer.start();
296 checkPublicIP();
300 QUrl DNSUpdater::getRegistrationUrl(const DNS::Service service)
302 switch (service)
304 case DNS::Service::DynDNS:
305 return {u"https://account.dyn.com/entrance/"_qs};
306 case DNS::Service::NoIP:
307 return {u"https://www.noip.com/remote-access"_qs};
308 default:
309 Q_ASSERT(false);
310 break;
312 return {};