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"
32 #include <QRegularExpression>
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
;
43 const std::chrono::seconds IP_CHECK_INTERVAL
= 30min
;
45 DNSUpdater::DNSUpdater(QObject
*parent
)
48 , m_service(DNS::Service::None
)
52 // Load saved settings from previous session
53 const Preferences
*const pref
= Preferences::instance();
54 m_lastIPCheckTime
= pref
->getDNSLastUpd();
55 m_lastIP
= QHostAddress(pref
->getDNSLastIP());
57 // Start IP checking timer
58 m_ipCheckTimer
.setInterval(IP_CHECK_INTERVAL
);
59 connect(&m_ipCheckTimer
, &QTimer::timeout
, this, &DNSUpdater::checkPublicIP
);
60 m_ipCheckTimer
.start();
62 // Check lastUpdate to avoid flooding
63 if (!m_lastIPCheckTime
.isValid()
64 || (m_lastIPCheckTime
.secsTo(QDateTime::currentDateTime()) > IP_CHECK_INTERVAL
.count()))
70 DNSUpdater::~DNSUpdater()
72 // Save lastupdate time and last ip
73 Preferences
*const pref
= Preferences::instance();
74 pref
->setDNSLastUpd(m_lastIPCheckTime
);
75 pref
->setDNSLastIP(m_lastIP
.toString());
78 void DNSUpdater::checkPublicIP()
80 Q_ASSERT(m_state
== OK
);
82 DownloadManager::instance()->download(
83 DownloadRequest(u
"http://checkip.dyndns.org"_qs
).userAgent(QStringLiteral("qBittorrent/" QBT_VERSION_2
))
84 , this, &DNSUpdater::ipRequestFinished
);
86 m_lastIPCheckTime
= QDateTime::currentDateTime();
89 void DNSUpdater::ipRequestFinished(const DownloadResult
&result
)
91 if (result
.status
!= DownloadStatus::Success
)
93 qWarning() << "IP request failed:" << result
.errorString
;
98 const QRegularExpressionMatch ipRegexMatch
= QRegularExpression(u
"Current IP Address:\\s+([^<]+)</body>"_qs
).match(QString::fromUtf8(result
.data
));
99 if (ipRegexMatch
.hasMatch())
101 QString ipStr
= ipRegexMatch
.captured(1);
102 qDebug() << Q_FUNC_INFO
<< "Regular expression captured the following IP:" << ipStr
;
103 QHostAddress
newIp(ipStr
);
106 if (m_lastIP
!= newIp
)
108 qDebug() << Q_FUNC_INFO
<< "The IP address changed, report the change to DynDNS...";
109 qDebug() << m_lastIP
.toString() << "->" << newIp
.toString();
116 qWarning() << Q_FUNC_INFO
<< "Failed to construct a QHostAddress from the IP string";
121 qWarning() << Q_FUNC_INFO
<< "Regular expression failed to capture the IP address";
125 void DNSUpdater::updateDNSService()
127 qDebug() << Q_FUNC_INFO
;
129 m_lastIPCheckTime
= QDateTime::currentDateTime();
130 DownloadManager::instance()->download(
131 DownloadRequest(getUpdateUrl()).userAgent(QStringLiteral("qBittorrent/" QBT_VERSION_2
))
132 , this, &DNSUpdater::ipUpdateFinished
);
135 QString
DNSUpdater::getUpdateUrl() const
139 url
.setScheme(u
"http"_qs
);
141 url
.setScheme(u
"https"_qs
);
143 url
.setUserName(m_username
);
144 url
.setPassword(m_password
);
146 Q_ASSERT(!m_lastIP
.isNull());
150 case DNS::Service::DynDNS
:
151 url
.setHost(u
"members.dyndns.org"_qs
);
153 case DNS::Service::NoIP
:
154 url
.setHost(u
"dynupdate.no-ip.com"_qs
);
157 qWarning() << "Unrecognized Dynamic DNS service!";
161 url
.setPath(u
"/nic/update"_qs
);
163 QUrlQuery
urlQuery(url
);
164 urlQuery
.addQueryItem(u
"hostname"_qs
, m_domain
);
165 urlQuery
.addQueryItem(u
"myip"_qs
, m_lastIP
.toString());
166 url
.setQuery(urlQuery
);
167 Q_ASSERT(url
.isValid());
169 qDebug() << Q_FUNC_INFO
<< url
.toString();
170 return url
.toString();
173 void DNSUpdater::ipUpdateFinished(const DownloadResult
&result
)
175 if (result
.status
== DownloadStatus::Success
)
176 processIPUpdateReply(QString::fromUtf8(result
.data
));
178 qWarning() << "IP update failed:" << result
.errorString
;
181 void DNSUpdater::processIPUpdateReply(const QString
&reply
)
183 qDebug() << Q_FUNC_INFO
<< reply
;
184 const QString code
= reply
.split(u
' ').first();
185 qDebug() << Q_FUNC_INFO
<< "Code:" << code
;
187 if ((code
== u
"good") || (code
== u
"nochg"))
189 LogMsg(tr("Your dynamic DNS was successfully updated."), Log::INFO
);
193 if ((code
== u
"911") || (code
== u
"dnserr"))
195 LogMsg(tr("Dynamic DNS error: The service is temporarily unavailable, it will be retried in 30 minutes."), Log::CRITICAL
);
197 // It will retry in 30 minutes because the timer was not stopped
201 // Everything below is an error, stop updating until the user updates something
202 m_ipCheckTimer
.stop();
204 if (code
== u
"nohost")
206 LogMsg(tr("Dynamic DNS error: hostname supplied does not exist under specified account."), Log::CRITICAL
);
207 m_state
= INVALID_CREDS
;
211 if (code
== u
"badauth")
213 LogMsg(tr("Dynamic DNS error: Invalid username/password."), Log::CRITICAL
);
214 m_state
= INVALID_CREDS
;
218 if (code
== u
"badagent")
220 LogMsg(tr("Dynamic DNS error: qBittorrent was blacklisted by the service, please submit a bug report at http://bugs.qbittorrent.org."),
226 if (code
== u
"!donator")
228 LogMsg(tr("Dynamic DNS error: %1 was returned by the service, please submit a bug report at http://bugs.qbittorrent.org.").arg(u
"!donator"_qs
),
234 if (code
== u
"abuse")
236 LogMsg(tr("Dynamic DNS error: Your username was blocked due to abuse."), Log::CRITICAL
);
241 void DNSUpdater::updateCredentials()
243 if (m_state
== FATAL
) return;
244 Preferences
*const pref
= Preferences::instance();
246 // Get DNS service information
247 if (m_service
!= pref
->getDynDNSService())
249 m_service
= pref
->getDynDNSService();
252 if (m_domain
!= pref
->getDynDomainName())
254 m_domain
= pref
->getDynDomainName();
255 const QRegularExpressionMatch domainRegexMatch
= QRegularExpression(u
"^(?:(?!\\d|-)[a-zA-Z0-9\\-]{1,63}\\.)+[a-zA-Z]{2,}$"_qs
).match(m_domain
);
256 if (!domainRegexMatch
.hasMatch())
258 LogMsg(tr("Dynamic DNS error: supplied domain name is invalid."), Log::CRITICAL
);
260 m_ipCheckTimer
.stop();
261 m_state
= INVALID_CREDS
;
266 if (m_username
!= pref
->getDynDNSUsername())
268 m_username
= pref
->getDynDNSUsername();
269 if (m_username
.length() < 4)
271 LogMsg(tr("Dynamic DNS error: supplied username is too short."), Log::CRITICAL
);
273 m_ipCheckTimer
.stop();
274 m_state
= INVALID_CREDS
;
279 if (m_password
!= pref
->getDynDNSPassword())
281 m_password
= pref
->getDynDNSPassword();
282 if (m_password
.length() < 4)
284 LogMsg(tr("Dynamic DNS error: supplied password is too short."), Log::CRITICAL
);
286 m_ipCheckTimer
.stop();
287 m_state
= INVALID_CREDS
;
293 if ((m_state
== INVALID_CREDS
) && change
)
295 m_state
= OK
; // Try again
296 m_ipCheckTimer
.start();
301 QUrl
DNSUpdater::getRegistrationUrl(const DNS::Service service
)
305 case DNS::Service::DynDNS
:
306 return {u
"https://account.dyn.com/entrance/"_qs
};
307 case DNS::Service::NoIP
:
308 return {u
"https://www.noip.com/remote-access"_qs
};