1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-15 01:00:17 +00:00

Web UI code clean up

This commit is contained in:
Christophe Dumez 2011-09-25 11:42:03 +03:00
parent 4be897a4e5
commit 8e026e68d7
3 changed files with 66 additions and 57 deletions

View File

@ -62,8 +62,7 @@ HttpConnection::HttpConnection(QTcpSocket *socket, HttpServer *parent)
connect(m_socket, SIGNAL(disconnected()), SLOT(deleteLater())); connect(m_socket, SIGNAL(disconnected()), SLOT(deleteLater()));
} }
HttpConnection::~HttpConnection() HttpConnection::~HttpConnection() {
{
delete m_socket; delete m_socket;
} }

View File

@ -51,30 +51,35 @@ const int BAN_TIME = 3600000; // 1 hour
class UnbanTimer: public QTimer { class UnbanTimer: public QTimer {
public: public:
UnbanTimer(QObject *parent, QString peer_ip): QTimer(parent), peer_ip(peer_ip){ UnbanTimer(QObject *parent, const QString& peer_ip): QTimer(parent), m_peerIp(peer_ip){
setSingleShot(true); setSingleShot(true);
setInterval(BAN_TIME); setInterval(BAN_TIME);
} }
~UnbanTimer() { ~UnbanTimer() {
qDebug("||||||||||||Deleting ban timer|||||||||||||||"); qDebug("||||||||||||Deleting ban timer|||||||||||||||");
} }
QString peer_ip;
inline QString peerIp() const { return m_peerIp; }
private:
QString m_peerIp;
}; };
void HttpServer::UnbanTimerEvent() { void HttpServer::UnbanTimerEvent() {
UnbanTimer* ubantimer = static_cast<UnbanTimer*>(sender()); UnbanTimer* ubantimer = static_cast<UnbanTimer*>(sender());
qDebug("Ban period has expired for %s", qPrintable(ubantimer->peer_ip)); qDebug("Ban period has expired for %s", qPrintable(ubantimer->peerIp()));
client_failed_attempts.remove(ubantimer->peer_ip); m_clientFailedAttempts.remove(ubantimer->peerIp());
ubantimer->deleteLater(); ubantimer->deleteLater();
} }
int HttpServer::NbFailedAttemptsForIp(QString ip) const { int HttpServer::NbFailedAttemptsForIp(const QString& ip) const {
return client_failed_attempts.value(ip, 0); return m_clientFailedAttempts.value(ip, 0);
} }
void HttpServer::increaseNbFailedAttemptsForIp(QString ip) { void HttpServer::increaseNbFailedAttemptsForIp(const QString& ip) {
const int nb_fail = client_failed_attempts.value(ip, 0); const int nb_fail = m_clientFailedAttempts.value(ip, 0);
client_failed_attempts.insert(ip, nb_fail+1); m_clientFailedAttempts.insert(ip, nb_fail+1);
if(nb_fail == MAX_AUTH_FAILED_ATTEMPTS-1) { if(nb_fail == MAX_AUTH_FAILED_ATTEMPTS-1) {
// Max number of failed attempts reached // Max number of failed attempts reached
// Start ban period // Start ban period
@ -84,15 +89,20 @@ void HttpServer::increaseNbFailedAttemptsForIp(QString ip) {
} }
} }
void HttpServer::resetNbFailedAttemptsForIp(QString ip) { void HttpServer::resetNbFailedAttemptsForIp(const QString& ip) {
client_failed_attempts.remove(ip); m_clientFailedAttempts.remove(ip);
} }
HttpServer::HttpServer(int msec, QObject* parent) : QTcpServer(parent) { HttpServer::HttpServer(int msec, QObject* parent) : QTcpServer(parent),
m_eventManager(new EventManager(this)) {
const Preferences pref; const Preferences pref;
username = pref.getWebUiUsername().toLocal8Bit();
password_ha1 = pref.getWebUiPassword().toLocal8Bit(); m_username = pref.getWebUiUsername().toLocal8Bit();
m_passwordSha1 = pref.getWebUiPassword().toLocal8Bit();
m_localAuth = pref.isWebUiLocalAuthEnabled(); m_localAuth = pref.isWebUiLocalAuthEnabled();
// HTTPS-related
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
m_https = pref.isWebUiHttpsEnabled(); m_https = pref.isWebUiHttpsEnabled();
if (m_https) { if (m_https) {
@ -100,22 +110,25 @@ HttpServer::HttpServer(int msec, QObject* parent) : QTcpServer(parent) {
m_key = QSslKey(pref.getWebUiHttpsKey(), QSsl::Rsa); m_key = QSslKey(pref.getWebUiHttpsKey(), QSsl::Rsa);
} }
#endif #endif
manager = new EventManager(this);
//add torrents // Add torrents
std::vector<torrent_handle> torrents = QBtSession::instance()->getTorrents(); std::vector<torrent_handle> torrents = QBtSession::instance()->getTorrents();
std::vector<torrent_handle>::iterator torrentIT; std::vector<torrent_handle>::iterator torrentIT;
for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) { for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) {
QTorrentHandle h = QTorrentHandle(*torrentIT); QTorrentHandle h = QTorrentHandle(*torrentIT);
if(h.is_valid()) if(h.is_valid())
manager->addedTorrent(h); m_eventManager->addedTorrent(h);
} }
//connect QBtSession::instance() to manager //connect QBtSession::instance() to manager
connect(QBtSession::instance(), SIGNAL(addedTorrent(QTorrentHandle)), manager, SLOT(addedTorrent(QTorrentHandle))); connect(QBtSession::instance(), SIGNAL(addedTorrent(QTorrentHandle)), m_eventManager, SLOT(addedTorrent(QTorrentHandle)));
connect(QBtSession::instance(), SIGNAL(deletedTorrent(QString)), manager, SLOT(deletedTorrent(QString))); connect(QBtSession::instance(), SIGNAL(deletedTorrent(QString)), m_eventManager, SLOT(deletedTorrent(QString)));
//set timer //set timer
timer = new QTimer(this); m_timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(onTimer())); connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimer()));
timer->start(msec); m_timer->start(msec);
// Additional translations for Web UI // Additional translations for Web UI
QString a = tr("File"); QString a = tr("File");
a = tr("Edit"); a = tr("Edit");
@ -147,22 +160,20 @@ HttpServer::HttpServer(int msec, QObject* parent) : QTcpServer(parent) {
a = tr("Torrent name"); a = tr("Torrent name");
} }
HttpServer::~HttpServer() HttpServer::~HttpServer() {
{ delete m_timer;
delete timer; delete m_eventManager;
delete manager;
} }
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
void HttpServer::enableHttps(const QSslCertificate &certificate, const QSslKey &key) void HttpServer::enableHttps(const QSslCertificate &certificate,
{ const QSslKey &key) {
m_certificate = certificate; m_certificate = certificate;
m_key = key; m_key = key;
m_https = true; m_https = true;
} }
void HttpServer::disableHttps() void HttpServer::disableHttps() {
{
m_https = false; m_https = false;
m_certificate.clear(); m_certificate.clear();
m_key.clear(); m_key.clear();
@ -213,7 +224,7 @@ void HttpServer::onTimer() {
for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) { for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) {
QTorrentHandle h = QTorrentHandle(*torrentIT); QTorrentHandle h = QTorrentHandle(*torrentIT);
if(h.is_valid()) if(h.is_valid())
manager->modifiedTorrent(h); m_eventManager->modifiedTorrent(h);
} }
} }
@ -225,21 +236,23 @@ QString HttpServer::generateNonce() const {
return md5.result().toHex(); return md5.result().toHex();
} }
void HttpServer::setAuthorization(QString _username, QString _password_ha1) { void HttpServer::setAuthorization(const QString& username,
username = _username.toLocal8Bit(); const QString& password_sha1) {
password_ha1 = _password_ha1.toLocal8Bit(); m_username = username.toLocal8Bit();
m_passwordSha1 = password_sha1.toLocal8Bit();
} }
// Parse HTTP AUTH string // Parse HTTP AUTH string
// http://tools.ietf.org/html/rfc2617 // http://tools.ietf.org/html/rfc2617
bool HttpServer::isAuthorized(QByteArray auth, QString method) const { bool HttpServer::isAuthorized(const QByteArray& auth,
const QString& method) const {
//qDebug("AUTH string is %s", auth.data()); //qDebug("AUTH string is %s", auth.data());
// Get user name // Get user name
QRegExp regex_user(".*username=\"([^\"]+)\".*"); // Must be a quoted string QRegExp regex_user(".*username=\"([^\"]+)\".*"); // Must be a quoted string
if(regex_user.indexIn(auth) < 0) return false; if(regex_user.indexIn(auth) < 0) return false;
QString prop_user = regex_user.cap(1); QString prop_user = regex_user.cap(1);
//qDebug("AUTH: Proposed username is %s, real username is %s", prop_user.toLocal8Bit().data(), username.data()); //qDebug("AUTH: Proposed username is %s, real username is %s", prop_user.toLocal8Bit().data(), username.data());
if(prop_user != username) { if(prop_user != m_username) {
// User name is invalid, we can reject already // User name is invalid, we can reject already
qDebug("AUTH-PROB: Username is invalid"); qDebug("AUTH-PROB: Username is invalid");
return false; return false;
@ -308,28 +321,25 @@ bool HttpServer::isAuthorized(QByteArray auth, QString method) const {
} }
QByteArray prop_qop = regex_qop.cap(1).toLocal8Bit(); QByteArray prop_qop = regex_qop.cap(1).toLocal8Bit();
//qDebug("prop qop is: %s", prop_qop.data()); //qDebug("prop qop is: %s", prop_qop.data());
md5_ha.addData(password_ha1+":"+prop_nonce+":"+prop_nc+":"+prop_cnonce+":"+prop_qop+":"+ha2); md5_ha.addData(m_passwordSha1+":"+prop_nonce+":"+prop_nc+":"+prop_cnonce+":"+prop_qop+":"+ha2);
response = md5_ha.result().toHex(); response = md5_ha.result().toHex();
} else { } else {
QCryptographicHash md5_ha(QCryptographicHash::Md5); QCryptographicHash md5_ha(QCryptographicHash::Md5);
md5_ha.addData(password_ha1+":"+prop_nonce+":"+ha2); md5_ha.addData(m_passwordSha1+":"+prop_nonce+":"+ha2);
response = md5_ha.result().toHex(); response = md5_ha.result().toHex();
} }
//qDebug("AUTH: comparing reponses: (%d)", static_cast<int>(prop_response == response)); //qDebug("AUTH: comparing reponses: (%d)", static_cast<int>(prop_response == response));
return prop_response == response; return prop_response == response;
} }
EventManager* HttpServer::eventManager() const EventManager* HttpServer::eventManager() const {
{ return m_eventManager;
return manager;
} }
void HttpServer::setlocalAuthEnabled(bool enabled) void HttpServer::setlocalAuthEnabled(bool enabled) {
{
m_localAuth = enabled; m_localAuth = enabled;
} }
bool HttpServer::isLocalAuthEnabled() const bool HttpServer::isLocalAuthEnabled() const {
{
return m_localAuth; return m_localAuth;
} }

View File

@ -59,15 +59,15 @@ class HttpServer : public QTcpServer {
public: public:
HttpServer(int msec, QObject* parent = 0); HttpServer(int msec, QObject* parent = 0);
~HttpServer(); ~HttpServer();
void setAuthorization(QString username, QString password_ha1); void setAuthorization(const QString& username, const QString& password_sha1);
bool isAuthorized(QByteArray auth, QString method) const; bool isAuthorized(const QByteArray& auth, const QString& method) const;
void setlocalAuthEnabled(bool enabled); void setlocalAuthEnabled(bool enabled);
bool isLocalAuthEnabled() const; bool isLocalAuthEnabled() const;
EventManager *eventManager() const; EventManager *eventManager() const;
QString generateNonce() const; QString generateNonce() const;
int NbFailedAttemptsForIp(QString ip) const; int NbFailedAttemptsForIp(const QString& ip) const;
void increaseNbFailedAttemptsForIp(QString ip); void increaseNbFailedAttemptsForIp(const QString& ip);
void resetNbFailedAttemptsForIp(QString ip); void resetNbFailedAttemptsForIp(const QString& ip);
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
void enableHttps(const QSslCertificate &certificate, const QSslKey &key); void enableHttps(const QSslCertificate &certificate, const QSslKey &key);
@ -85,11 +85,11 @@ private:
void handleNewConnection(QTcpSocket *socket); void handleNewConnection(QTcpSocket *socket);
private: private:
QByteArray username; QByteArray m_username;
QByteArray password_ha1; QByteArray m_passwordSha1;
EventManager *manager; EventManager *m_eventManager;
QTimer *timer; QTimer *m_timer;
QHash<QString, int> client_failed_attempts; QHash<QString, int> m_clientFailedAttempts;
bool m_localAuth; bool m_localAuth;
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
bool m_https; bool m_https;