@ -28,7 +28,6 @@
* Contact : chris @ qbittorrent . org
* Contact : chris @ qbittorrent . org
*/
*/
# include <QNetworkAccessManager>
# include <QDebug>
# include <QDebug>
# include <QRegExp>
# include <QRegExp>
# include <QStringList>
# include <QStringList>
@ -37,6 +36,8 @@
# endif
# endif
# include "base/logger.h"
# include "base/logger.h"
# include "base/net/downloadmanager.h"
# include "base/net/downloadhandler.h"
# include "dnsupdater.h"
# include "dnsupdater.h"
using namespace Net ;
using namespace Net ;
@ -76,35 +77,31 @@ DNSUpdater::~DNSUpdater()
void DNSUpdater : : checkPublicIP ( )
void DNSUpdater : : checkPublicIP ( )
{
{
Q_ASSERT ( m_state = = OK ) ;
Q_ASSERT ( m_state = = OK ) ;
QNetworkAccessManager * manager = new QNetworkAccessManager ( this ) ;
connect ( manager , SIGNAL ( finished ( QNetworkReply * ) ) , SLOT ( ipRequestFinished ( QNetworkReply * ) ) ) ;
DownloadHandler * handler = DownloadManager : : instance ( ) - > downloadUrl (
" http://checkip.dyndns.org " , false , 0 , false ,
QString ( " qBittorrent/%1 " ) . arg ( VERSION ) ) ;
connect ( handler , SIGNAL ( downloadFinished ( QString , QByteArray ) ) , SLOT ( ipRequestFinished ( QString , QByteArray ) ) ) ;
connect ( handler , SIGNAL ( downloadFailed ( QString , QString ) ) , SLOT ( ipRequestFailed ( QString , QString ) ) ) ;
m_lastIPCheckTime = QDateTime : : currentDateTime ( ) ;
m_lastIPCheckTime = QDateTime : : currentDateTime ( ) ;
QNetworkRequest request ;
request . setUrl ( QUrl ( " http://checkip.dyndns.org " ) ) ;
request . setRawHeader ( " User-Agent " , " qBittorrent/ " VERSION ) ;
manager - > get ( request ) ;
}
}
void DNSUpdater : : ipRequestFinished ( QNetworkReply * reply )
void DNSUpdater : : ipRequestFinished ( const QString & url , const QByteArray & data )
{
{
qDebug ( ) < < Q_FUNC_INFO ;
Q_UNUSED ( url ) ;
if ( reply - > error ( ) ) {
// Error
qWarning ( ) < < Q_FUNC_INFO < < " Error: " < < reply - > errorString ( ) ;
}
else {
// Parse response
// Parse response
QRegExp ipregex ( " Current IP Address: \\ s+([^<]+) < / body > " ) ;
QRegExp ipregex ( " Current IP Address: \\ s+([^<]+) < / body > " ) ;
QString ret = reply - > readAll ( ) ;
if ( ipregex . indexIn ( data ) > = 0 ) {
if ( ipregex . indexIn ( ret ) > = 0 ) {
QString ipStr = ipregex . cap ( 1 ) ;
QString ip_str = ipregex . cap ( 1 ) ;
qDebug ( ) < < Q_FUNC_INFO < < " Regular expression captured the following IP: " < < ipStr ;
qDebug ( ) < < Q_FUNC_INFO < < " Regular expression captured the following IP: " < < ip_str ;
QHostAddress newIp ( ipStr ) ;
QHostAddress new_ip ( ip_str ) ;
if ( ! newIp . isNull ( ) ) {
if ( ! new_ip . isNull ( ) ) {
if ( m_lastIP ! = newIp ) {
if ( m_lastIP ! = new_ip ) {
qDebug ( ) < < Q_FUNC_INFO < < " The IP address changed, report the change to DynDNS... " ;
qDebug ( ) < < Q_FUNC_INFO < < " The IP address changed, report the change to DynDNS... " ;
qDebug ( ) < < m_lastIP . toString ( ) < < " -> " < < new_i p . toString ( ) ;
qDebug ( ) < < m_lastIP . toString ( ) < < " -> " < < newI p . toString ( ) ;
m_lastIP = new_i p ;
m_lastIP = newI p ;
updateDNSService ( ) ;
updateDNSService ( ) ;
}
}
}
}
@ -116,25 +113,26 @@ void DNSUpdater::ipRequestFinished(QNetworkReply *reply)
qWarning ( ) < < Q_FUNC_INFO < < " Regular expression failed to capture the IP address " ;
qWarning ( ) < < Q_FUNC_INFO < < " Regular expression failed to capture the IP address " ;
}
}
}
}
// Clean up
reply - > deleteLater ( ) ;
void DNSUpdater : : ipRequestFailed ( const QString & url , const QString & error )
sender ( ) - > deleteLater ( ) ;
{
Q_UNUSED ( url ) ;
qWarning ( ) < < " IP request failed: " < < error ;
}
}
void DNSUpdater : : updateDNSService ( )
void DNSUpdater : : updateDNSService ( )
{
{
qDebug ( ) < < Q_FUNC_INFO ;
qDebug ( ) < < Q_FUNC_INFO ;
// Prepare request
QNetworkAccessManager * manager = new QNetworkAccessManager ( this ) ;
connect ( manager , SIGNAL ( finished ( QNetworkReply * ) ) , SLOT ( ipUpdateFinished ( QNetworkReply * ) ) ) ;
m_lastIPCheckTime = QDateTime : : currentDateTime ( ) ;
m_lastIPCheckTime = QDateTime : : currentDateTime ( ) ;
QNetworkRequest request ;
DownloadHandler * handler = DownloadManager : : instance ( ) - > downloadUrl (
request . setUrl ( getUpdateUrl ( ) ) ;
getUpdateUrl ( ) , false , 0 , false ,
request . setRawHeader ( " User-Agent " , " qBittorrent/ " VERSION ) ;
QString ( " qBittorrent/%1 " ) . arg ( VERSION ) ) ;
manager - > get ( request ) ;
connect ( handler , SIGNAL ( downloadFinished ( QString , QByteArray ) ) , SLOT ( ipUpdateFinished ( QString , QByteArray ) ) ) ;
connect ( handler , SIGNAL ( downloadFailed ( QString , QString ) ) , SLOT ( ipUpdateFailed ( QString , QString ) ) ) ;
}
}
QUrl DNSUpdater : : getUpdateUrl ( ) const
QString DNSUpdater : : getUpdateUrl ( ) const
{
{
QUrl url ;
QUrl url ;
# ifdef QT_NO_OPENSSL
# ifdef QT_NO_OPENSSL
@ -172,22 +170,20 @@ QUrl DNSUpdater::getUpdateUrl() const
Q_ASSERT ( url . isValid ( ) ) ;
Q_ASSERT ( url . isValid ( ) ) ;
qDebug ( ) < < Q_FUNC_INFO < < url . toString ( ) ;
qDebug ( ) < < Q_FUNC_INFO < < url . toString ( ) ;
return url ;
return url . toString ( ) ;
}
}
void DNSUpdater : : ipUpdateFinished ( QNetworkReply * reply )
void DNSUpdater : : ipUpdateFinished ( const QString & url , const QByteArray & data )
{
{
if ( reply - > error ( ) ) {
Q_UNUSED ( url ) ;
// Error
qWarning ( ) < < Q_FUNC_INFO < < " Error: " < < reply - > errorString ( ) ;
}
else {
// Parse reply
// Parse reply
processIPUpdateReply ( reply - > readAll ( ) ) ;
processIPUpdateReply ( data ) ;
}
}
// Clean up
reply - > deleteLater ( ) ;
void DNSUpdater : : ipUpdateFailed ( const QString & url , const QString & error )
sender ( ) - > deleteLater ( ) ;
{
Q_UNUSED ( url ) ;
qWarning ( ) < < " IP update failed: " < < error ;
}
}
void DNSUpdater : : processIPUpdateReply ( const QString & reply )
void DNSUpdater : : processIPUpdateReply ( const QString & reply )
@ -196,16 +192,19 @@ void DNSUpdater::processIPUpdateReply(const QString &reply)
qDebug ( ) < < Q_FUNC_INFO < < reply ;
qDebug ( ) < < Q_FUNC_INFO < < reply ;
QString code = reply . split ( " " ) . first ( ) ;
QString code = reply . split ( " " ) . first ( ) ;
qDebug ( ) < < Q_FUNC_INFO < < " Code: " < < code ;
qDebug ( ) < < Q_FUNC_INFO < < " Code: " < < code ;
if ( code = = " good " | | code = = " nochg " ) {
if ( ( code = = " good " ) | | ( code = = " nochg " ) ) {
logger - > addMessage ( tr ( " Your dynamic DNS was successfully updated. " ) , Log : : INFO ) ;
logger - > addMessage ( tr ( " Your dynamic DNS was successfully updated. " ) , Log : : INFO ) ;
return ;
return ;
}
}
if ( ( code = = " 911 " ) | | ( code = = " dnserr " ) ) {
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 ) ;
logger - > addMessage ( tr ( " Dynamic DNS error: The service is temporarily unavailable, it will be retried in 30 minutes. " ) , Log : : CRITICAL ) ;
m_lastIP . clear ( ) ;
m_lastIP . clear ( ) ;
// It will retry in 30 minutes because the timer was not stopped
// It will retry in 30 minutes because the timer was not stopped
return ;
return ;
}
}
// Everything bellow is an error, stop updating until the user updates something
// Everything bellow is an error, stop updating until the user updates something
m_ipCheckTimer . stop ( ) ;
m_ipCheckTimer . stop ( ) ;
m_lastIP . clear ( ) ;
m_lastIP . clear ( ) ;
@ -214,23 +213,27 @@ void DNSUpdater::processIPUpdateReply(const QString &reply)
m_state = INVALID_CREDS ;
m_state = INVALID_CREDS ;
return ;
return ;
}
}
if ( code = = " badauth " ) {
if ( code = = " badauth " ) {
logger - > addMessage ( tr ( " Dynamic DNS error: Invalid username/password. " ) , Log : : CRITICAL ) ;
logger - > addMessage ( tr ( " Dynamic DNS error: Invalid username/password. " ) , Log : : CRITICAL ) ;
m_state = INVALID_CREDS ;
m_state = INVALID_CREDS ;
return ;
return ;
}
}
if ( code = = " badagent " ) {
if ( code = = " badagent " ) {
logger - > addMessage ( tr ( " Dynamic DNS error: qBittorrent was blacklisted by the service, please report a bug at http://bugs.qbittorrent.org. " ) ,
logger - > addMessage ( tr ( " Dynamic DNS error: qBittorrent was blacklisted by the service, please report a bug at http://bugs.qbittorrent.org. " ) ,
Log : : CRITICAL ) ;
Log : : CRITICAL ) ;
m_state = FATAL ;
m_state = FATAL ;
return ;
return ;
}
}
if ( code = = " !donator " ) {
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 " ) ,
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 ) ;
Log : : CRITICAL ) ;
m_state = FATAL ;
m_state = FATAL ;
return ;
return ;
}
}
if ( code = = " abuse " ) {
if ( code = = " abuse " ) {
logger - > addMessage ( tr ( " Dynamic DNS error: Your username was blocked due to abuse. " ) , Log : : CRITICAL ) ;
logger - > addMessage ( tr ( " Dynamic DNS error: Your username was blocked due to abuse. " ) , Log : : CRITICAL ) ;
m_state = FATAL ;
m_state = FATAL ;