mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2025-12-29 19:58:06 -06:00
Branched v2.2.x
This commit is contained in:
@@ -37,6 +37,48 @@
|
||||
#include <QCryptographicHash>
|
||||
#include <QTime>
|
||||
#include <QRegExp>
|
||||
#include <QTimer>
|
||||
|
||||
const int BAN_TIME = 3600000; // 1 hour
|
||||
|
||||
class UnbanTimer: public QTimer {
|
||||
public:
|
||||
UnbanTimer(QObject *parent, QString peer_ip): QTimer(parent), peer_ip(peer_ip){
|
||||
setSingleShot(true);
|
||||
setInterval(BAN_TIME);
|
||||
}
|
||||
~UnbanTimer() {
|
||||
qDebug("||||||||||||Deleting ban timer|||||||||||||||");
|
||||
}
|
||||
QString peer_ip;
|
||||
};
|
||||
|
||||
void HttpServer::UnbanTimerEvent() {
|
||||
UnbanTimer* ubantimer = static_cast<UnbanTimer*>(sender());
|
||||
qDebug("Ban period has expired for %s", qPrintable(ubantimer->peer_ip));
|
||||
client_failed_attempts.remove(ubantimer->peer_ip);
|
||||
ubantimer->deleteLater();
|
||||
}
|
||||
|
||||
int HttpServer::NbFailedAttemptsForIp(QString ip) const {
|
||||
return client_failed_attempts.value(ip, 0);
|
||||
}
|
||||
|
||||
void HttpServer::increaseNbFailedAttemptsForIp(QString ip) {
|
||||
const int nb_fail = client_failed_attempts.value(ip, 0);
|
||||
client_failed_attempts.insert(ip, nb_fail+1);
|
||||
if(nb_fail == MAX_AUTH_FAILED_ATTEMPTS-1) {
|
||||
// Max number of failed attempts reached
|
||||
// Start ban period
|
||||
UnbanTimer* ubantimer = new UnbanTimer(this, ip);
|
||||
connect(ubantimer, SIGNAL(timeout()), this, SLOT(UnbanTimerEvent()));
|
||||
ubantimer->start();
|
||||
}
|
||||
}
|
||||
|
||||
void HttpServer::resetNbFailedAttemptsForIp(QString ip) {
|
||||
client_failed_attempts.remove(ip);
|
||||
}
|
||||
|
||||
HttpServer::HttpServer(Bittorrent *_BTSession, int msec, QObject* parent) : QTcpServer(parent) {
|
||||
username = Preferences::getWebUiUsername().toLocal8Bit();
|
||||
@@ -132,16 +174,12 @@ void HttpServer::setAuthorization(QString _username, QString _password_ha1) {
|
||||
password_ha1 = _password_ha1.toLocal8Bit();
|
||||
}
|
||||
|
||||
// AUTH string is: Digest username="chris",
|
||||
// realm="Web UI Access",
|
||||
// nonce="570d04de93444b7fd3eaeaecb00e635e",
|
||||
// uri="/", algorithm=MD5,
|
||||
// response="ba886766d19b45313c0e2195e4344264",
|
||||
// qop=auth, nc=00000001, cnonce="e8ac970779c17075"
|
||||
// Parse HTTP AUTH string
|
||||
// http://tools.ietf.org/html/rfc2617
|
||||
bool HttpServer::isAuthorized(QByteArray auth, QString method) const {
|
||||
qDebug("AUTH string is %s", auth.data());
|
||||
// Get user name
|
||||
QRegExp regex_user(".*username=\"([^\"]+)\".*");
|
||||
QRegExp regex_user(".*username=\"([^\"]+)\".*"); // Must be a quoted string
|
||||
if(regex_user.indexIn(auth) < 0) return false;
|
||||
QString prop_user = regex_user.cap(1);
|
||||
qDebug("AUTH: Proposed username is %s, real username is %s", prop_user.toLocal8Bit().data(), username.data());
|
||||
@@ -151,7 +189,7 @@ bool HttpServer::isAuthorized(QByteArray auth, QString method) const {
|
||||
return false;
|
||||
}
|
||||
// Get realm
|
||||
QRegExp regex_realm(".*realm=\"([^\"]+)\".*");
|
||||
QRegExp regex_realm(".*realm=\"([^\"]+)\".*"); // Must be a quoted string
|
||||
if(regex_realm.indexIn(auth) < 0) {
|
||||
qDebug("AUTH-PROB: Missing realm");
|
||||
return false;
|
||||
@@ -162,7 +200,7 @@ bool HttpServer::isAuthorized(QByteArray auth, QString method) const {
|
||||
return false;
|
||||
}
|
||||
// get nonce
|
||||
QRegExp regex_nonce(".*nonce=\"([^\"]+)\".*");
|
||||
QRegExp regex_nonce(".*nonce=[\"]?([\\w=]+)[\"]?.*");
|
||||
if(regex_nonce.indexIn(auth) < 0) {
|
||||
qDebug("AUTH-PROB: missing nonce");
|
||||
return false;
|
||||
@@ -178,7 +216,7 @@ bool HttpServer::isAuthorized(QByteArray auth, QString method) const {
|
||||
QByteArray prop_uri = regex_uri.cap(1).toLocal8Bit();
|
||||
qDebug("prop uri is: %s", prop_uri.data());
|
||||
// get response
|
||||
QRegExp regex_response(".*response=\"([^\"]+)\".*");
|
||||
QRegExp regex_response(".*response=[\"]?([\\w=]+)[\"]?.*");
|
||||
if(regex_response.indexIn(auth) < 0) {
|
||||
qDebug("AUTH-PROB: Missing response");
|
||||
return false;
|
||||
@@ -193,14 +231,14 @@ bool HttpServer::isAuthorized(QByteArray auth, QString method) const {
|
||||
if(auth.contains("qop=")) {
|
||||
QCryptographicHash md5_ha(QCryptographicHash::Md5);
|
||||
// Get nc
|
||||
QRegExp regex_nc(".*nc=(\\w+).*");
|
||||
QRegExp regex_nc(".*nc=[\"]?([\\w=]+)[\"]?.*");
|
||||
if(regex_nc.indexIn(auth) < 0) {
|
||||
qDebug("AUTH-PROB: qop but missing nc");
|
||||
return false;
|
||||
}
|
||||
QByteArray prop_nc = regex_nc.cap(1).toLocal8Bit();
|
||||
qDebug("prop nc is: %s", prop_nc.data());
|
||||
QRegExp regex_cnonce(".*cnonce=\"([^\"]+)\".*");
|
||||
QRegExp regex_cnonce(".*cnonce=[\"]?([\\w=]+)[\"]?.*");
|
||||
if(regex_cnonce.indexIn(auth) < 0) {
|
||||
qDebug("AUTH-PROB: qop but missing cnonce");
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user