Enable customizing the save statistics time interval
[qBittorrent.git] / src / base / net / dnsupdater.cpp
blob4aa385668cd385ec5d420ae25674e57c7c4d4293
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 std::chrono_literals;
41 using namespace Net;
43 const std::chrono::seconds IP_CHECK_INTERVAL = 30min;
45 DNSUpdater::DNSUpdater(QObject *parent)
46 : QObject(parent)
48 updateCredentials();
50 // Load saved settings from previous session
51 const Preferences *const pref = Preferences::instance();
52 m_lastIPCheckTime = pref->getDNSLastUpd();
53 m_lastIP = QHostAddress(pref->getDNSLastIP());
55 // Start IP checking timer
56 m_ipCheckTimer.setInterval(IP_CHECK_INTERVAL);
57 connect(&m_ipCheckTimer, &QTimer::timeout, this, &DNSUpdater::checkPublicIP);
58 m_ipCheckTimer.start();
60 // Check lastUpdate to avoid flooding
61 if (!m_lastIPCheckTime.isValid()
62 || (m_lastIPCheckTime.secsTo(QDateTime::currentDateTime()) > IP_CHECK_INTERVAL.count()))
64 checkPublicIP();
68 DNSUpdater::~DNSUpdater()
70 // Save lastupdate time and last ip
71 Preferences *const pref = Preferences::instance();
72 pref->setDNSLastUpd(m_lastIPCheckTime);
73 pref->setDNSLastIP(m_lastIP.toString());
76 void DNSUpdater::checkPublicIP()
78 Q_ASSERT(m_state == OK);
80 DownloadManager::instance()->download(
81 DownloadRequest(u"http://checkip.dyndns.org"_s).userAgent(QStringLiteral("qBittorrent/" QBT_VERSION_2))
82 , Preferences::instance()->useProxyForGeneralPurposes(), this, &DNSUpdater::ipRequestFinished);
84 m_lastIPCheckTime = QDateTime::currentDateTime();
87 void DNSUpdater::ipRequestFinished(const DownloadResult &result)
89 if (result.status != DownloadStatus::Success)
91 qWarning() << "IP request failed:" << result.errorString;
92 return;
95 // Parse response
96 const QRegularExpressionMatch ipRegexMatch = QRegularExpression(u"Current IP Address:\\s+([^<]+)</body>"_s).match(QString::fromUtf8(result.data));
97 if (ipRegexMatch.hasMatch())
99 QString ipStr = ipRegexMatch.captured(1);
100 qDebug() << Q_FUNC_INFO << "Regular expression captured the following IP:" << ipStr;
101 QHostAddress newIp(ipStr);
102 if (!newIp.isNull())
104 if (m_lastIP != newIp)
106 qDebug() << Q_FUNC_INFO << "The IP address changed, report the change to DynDNS...";
107 qDebug() << m_lastIP.toString() << "->" << newIp.toString();
108 m_lastIP = newIp;
109 updateDNSService();
112 else
114 qWarning() << Q_FUNC_INFO << "Failed to construct a QHostAddress from the IP string";
117 else
119 qWarning() << Q_FUNC_INFO << "Regular expression failed to capture the IP address";
123 void DNSUpdater::updateDNSService()
125 qDebug() << Q_FUNC_INFO;
127 m_lastIPCheckTime = QDateTime::currentDateTime();
128 DownloadManager::instance()->download(
129 DownloadRequest(getUpdateUrl()).userAgent(QStringLiteral("qBittorrent/" QBT_VERSION_2))
130 , Preferences::instance()->useProxyForGeneralPurposes(), this, &DNSUpdater::ipUpdateFinished);
133 QString DNSUpdater::getUpdateUrl() const
135 QUrl url;
136 #ifdef QT_NO_OPENSSL
137 url.setScheme(u"http"_s);
138 #else
139 url.setScheme(u"https"_s);
140 #endif
141 url.setUserName(m_username);
142 url.setPassword(m_password);
144 Q_ASSERT(!m_lastIP.isNull());
145 // Service specific
146 switch (m_service)
148 case DNS::Service::DynDNS:
149 url.setHost(u"members.dyndns.org"_s);
150 break;
151 case DNS::Service::NoIP:
152 url.setHost(u"dynupdate.no-ip.com"_s);
153 break;
154 default:
155 qWarning() << "Unrecognized Dynamic DNS service!";
156 Q_ASSERT(false);
157 break;
159 url.setPath(u"/nic/update"_s);
161 QUrlQuery urlQuery(url);
162 urlQuery.addQueryItem(u"hostname"_s, m_domain);
163 urlQuery.addQueryItem(u"myip"_s, m_lastIP.toString());
164 url.setQuery(urlQuery);
165 Q_ASSERT(url.isValid());
167 qDebug() << Q_FUNC_INFO << url.toString();
168 return url.toString();
171 void DNSUpdater::ipUpdateFinished(const DownloadResult &result)
173 if (result.status == DownloadStatus::Success)
174 processIPUpdateReply(QString::fromUtf8(result.data));
175 else
176 qWarning() << "IP update failed:" << result.errorString;
179 void DNSUpdater::processIPUpdateReply(const QString &reply)
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 LogMsg(tr("Your dynamic DNS was successfully updated."), Log::INFO);
188 return;
191 if ((code == u"911") || (code == u"dnserr"))
193 LogMsg(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 LogMsg(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 LogMsg(tr("Dynamic DNS error: Invalid username/password."), Log::CRITICAL);
212 m_state = INVALID_CREDS;
213 return;
216 if (code == u"badagent")
218 LogMsg(tr("Dynamic DNS error: qBittorrent was blacklisted by the service, please submit a bug report at https://bugs.qbittorrent.org."),
219 Log::CRITICAL);
220 m_state = FATAL;
221 return;
224 if (code == u"!donator")
226 LogMsg(tr("Dynamic DNS error: %1 was returned by the service, please submit a bug report at https://bugs.qbittorrent.org.").arg(u"!donator"_s),
227 Log::CRITICAL);
228 m_state = FATAL;
229 return;
232 if (code == u"abuse")
234 LogMsg(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 bool change = false;
244 // Get DNS service information
245 if (m_service != pref->getDynDNSService())
247 m_service = pref->getDynDNSService();
248 change = true;
250 if (m_domain != pref->getDynDomainName())
252 m_domain = pref->getDynDomainName();
253 const QRegularExpressionMatch domainRegexMatch = QRegularExpression(u"^(?:(?!\\d|-)[a-zA-Z0-9\\-]{1,63}\\.)+[a-zA-Z]{2,}$"_s).match(m_domain);
254 if (!domainRegexMatch.hasMatch())
256 LogMsg(tr("Dynamic DNS error: supplied domain name is invalid."), Log::CRITICAL);
257 m_lastIP.clear();
258 m_ipCheckTimer.stop();
259 m_state = INVALID_CREDS;
260 return;
262 change = true;
264 if (m_username != pref->getDynDNSUsername())
266 m_username = pref->getDynDNSUsername();
267 if (m_username.length() < 4)
269 LogMsg(tr("Dynamic DNS error: supplied username is too short."), Log::CRITICAL);
270 m_lastIP.clear();
271 m_ipCheckTimer.stop();
272 m_state = INVALID_CREDS;
273 return;
275 change = true;
277 if (m_password != pref->getDynDNSPassword())
279 m_password = pref->getDynDNSPassword();
280 if (m_password.length() < 4)
282 LogMsg(tr("Dynamic DNS error: supplied password is too short."), Log::CRITICAL);
283 m_lastIP.clear();
284 m_ipCheckTimer.stop();
285 m_state = INVALID_CREDS;
286 return;
288 change = true;
291 if ((m_state == INVALID_CREDS) && change)
293 m_state = OK; // Try again
294 m_ipCheckTimer.start();
295 checkPublicIP();
299 QUrl DNSUpdater::getRegistrationUrl(const DNS::Service service)
301 switch (service)
303 case DNS::Service::DynDNS:
304 return {u"https://account.dyn.com/entrance/"_s};
305 case DNS::Service::NoIP:
306 return {u"https://www.noip.com/remote-access"_s};
307 default:
308 Q_ASSERT(false);
309 break;
311 return {};