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
)
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()))
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
;
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
);
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();
114 qWarning() << Q_FUNC_INFO
<< "Failed to construct a QHostAddress from the IP string";
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
137 url
.setScheme(u
"http"_s
);
139 url
.setScheme(u
"https"_s
);
141 url
.setUserName(m_username
);
142 url
.setPassword(m_password
);
144 Q_ASSERT(!m_lastIP
.isNull());
148 case DNS::Service::DynDNS
:
149 url
.setHost(u
"members.dyndns.org"_s
);
151 case DNS::Service::NoIP
:
152 url
.setHost(u
"dynupdate.no-ip.com"_s
);
155 qWarning() << "Unrecognized Dynamic DNS service!";
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
));
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
);
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
);
195 // It will retry in 30 minutes because the timer was not stopped
199 // Everything below is an error, stop updating until the user updates something
200 m_ipCheckTimer
.stop();
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
;
209 if (code
== u
"badauth")
211 LogMsg(tr("Dynamic DNS error: Invalid username/password."), Log::CRITICAL
);
212 m_state
= INVALID_CREDS
;
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."),
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
),
232 if (code
== u
"abuse")
234 LogMsg(tr("Dynamic DNS error: Your username was blocked due to abuse."), Log::CRITICAL
);
239 void DNSUpdater::updateCredentials()
241 if (m_state
== FATAL
) return;
242 Preferences
*const pref
= Preferences::instance();
244 // Get DNS service information
245 if (m_service
!= pref
->getDynDNSService())
247 m_service
= pref
->getDynDNSService();
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
);
258 m_ipCheckTimer
.stop();
259 m_state
= INVALID_CREDS
;
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
);
271 m_ipCheckTimer
.stop();
272 m_state
= INVALID_CREDS
;
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
);
284 m_ipCheckTimer
.stop();
285 m_state
= INVALID_CREDS
;
291 if ((m_state
== INVALID_CREDS
) && change
)
293 m_state
= OK
; // Try again
294 m_ipCheckTimer
.start();
299 QUrl
DNSUpdater::getRegistrationUrl(const DNS::Service 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
};