mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2024-11-22 01:06:03 +03:00
- BUGFIX: Threadified IP filter file parser to avoid GUI freeze
This commit is contained in:
parent
ec5325ed3c
commit
7463552dcd
7 changed files with 407 additions and 330 deletions
|
@ -12,6 +12,7 @@
|
|||
- FEATURE: Added support for PeerGuardian p2p filters (text)
|
||||
- FEATURE: Added support for PeerGuardian p2b filters (binary)
|
||||
- BUGFIX: Do not display seeds number in seeding list (always 0)
|
||||
- BUGFIX: Threadified IP filter file parser to avoid GUI freeze
|
||||
- COSMETIC: Do not display progress bar in seeding list (always 100%)
|
||||
- COSMETIC: Added a progress bar for torrent creation
|
||||
- COSMETIC: Display tracker errors in a cleaner way
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include "misc.h"
|
||||
#include "downloadThread.h"
|
||||
#include "deleteThread.h"
|
||||
#include "filterParserThread.h"
|
||||
|
||||
#include <libtorrent/extensions/metadata_transfer.hpp>
|
||||
#include <libtorrent/extensions/ut_pex.hpp>
|
||||
|
@ -62,7 +63,8 @@ bittorrent::bittorrent() : timerScan(0), DHTEnabled(false), preAllocateAll(false
|
|||
connect(downloader, SIGNAL(downloadFailure(QString, QString)), this, SLOT(handleDownloadFailure(QString, QString)));
|
||||
// File deleter (thread)
|
||||
deleter = new deleteThread(this);
|
||||
BigRatioTimer = 0;
|
||||
BigRatioTimer = 0;
|
||||
filterParser = 0;
|
||||
qDebug("* BTSession constructed");
|
||||
}
|
||||
|
||||
|
@ -85,6 +87,8 @@ bittorrent::~bittorrent() {
|
|||
delete timerAlerts;
|
||||
if(BigRatioTimer != 0)
|
||||
delete BigRatioTimer;
|
||||
if(filterParser != 0)
|
||||
delete filterParser;
|
||||
delete downloader;
|
||||
// Delete BT session
|
||||
qDebug("Deleting session");
|
||||
|
@ -1119,15 +1123,25 @@ void bittorrent::setDHTPort(int dht_port) {
|
|||
}
|
||||
|
||||
// Enable IP Filtering
|
||||
void bittorrent::enableIPFilter(ip_filter filter) {
|
||||
void bittorrent::enableIPFilter(QString filter) {
|
||||
qDebug("Enabling IPFiler");
|
||||
s->set_ip_filter(filter);
|
||||
if(!filterParser) {
|
||||
filterParser = new FilterParserThread(this, s);
|
||||
}
|
||||
if(filterPath.isEmpty() || filterPath != filter) {
|
||||
filterPath = filter;
|
||||
filterParser->processFilterFile(filter);
|
||||
}
|
||||
}
|
||||
|
||||
// Disable IP Filtering
|
||||
void bittorrent::disableIPFilter() {
|
||||
qDebug("Disabling IPFilter");
|
||||
s->set_ip_filter(ip_filter());
|
||||
if(filterParser) {
|
||||
delete filterParser;
|
||||
}
|
||||
filterPath = "";
|
||||
}
|
||||
|
||||
// Set BT session settings (user_agent)
|
||||
|
|
|
@ -36,6 +36,7 @@ using namespace libtorrent;
|
|||
class downloadThread;
|
||||
class deleteThread;
|
||||
class QTimer;
|
||||
class FilterParserThread;
|
||||
|
||||
class bittorrent : public QObject{
|
||||
Q_OBJECT
|
||||
|
@ -66,6 +67,8 @@ class bittorrent : public QObject{
|
|||
bool UPnPEnabled;
|
||||
bool NATPMPEnabled;
|
||||
bool LSDEnabled;
|
||||
FilterParserThread *filterParser;
|
||||
QString filterPath;
|
||||
|
||||
protected:
|
||||
QString getSavePath(QString hash);
|
||||
|
@ -108,7 +111,7 @@ class bittorrent : public QObject{
|
|||
void enableDirectoryScanning(QString scan_dir);
|
||||
void disableDirectoryScanning();
|
||||
void enablePeerExchange();
|
||||
void enableIPFilter(ip_filter filter);
|
||||
void enableIPFilter(QString filter);
|
||||
void disableIPFilter();
|
||||
void resumeUnfinishedTorrents();
|
||||
void saveTorrentSpeedLimits(QString hash);
|
||||
|
|
381
src/filterParserThread.h
Normal file
381
src/filterParserThread.h
Normal file
|
@ -0,0 +1,381 @@
|
|||
/*
|
||||
* Bittorrent Client using Qt4 and libtorrent.
|
||||
* Copyright (C) 2006 Christophe Dumez
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
* Contact : chris@qbittorrent.org
|
||||
*/
|
||||
|
||||
#ifndef FILTERPARSERTHREAD_H
|
||||
#define FILTERPARSERTHREAD_H
|
||||
|
||||
#include <QThread>
|
||||
#include <QFile>
|
||||
#include <QDataStream>
|
||||
#include <QMessageBox>
|
||||
|
||||
#include <libtorrent/session.hpp>
|
||||
#include <libtorrent/ip_filter.hpp>
|
||||
|
||||
using namespace libtorrent;
|
||||
using namespace std;
|
||||
|
||||
// P2B Stuff
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
// End of P2B stuff
|
||||
|
||||
class FilterParserThread : public QThread {
|
||||
private:
|
||||
session *s;
|
||||
ip_filter filter;
|
||||
bool abort;
|
||||
QString filePath;
|
||||
|
||||
protected:
|
||||
void run(){
|
||||
qDebug("Processing filter file");
|
||||
if(filePath.endsWith(".dat", Qt::CaseInsensitive)) {
|
||||
// eMule DAT file
|
||||
parseDATFilterFile(filePath);
|
||||
} else {
|
||||
if(filePath.endsWith(".p2p", Qt::CaseInsensitive)) {
|
||||
// PeerGuardian p2p file
|
||||
parseP2PFilterFile(filePath);
|
||||
} else {
|
||||
if(filePath.endsWith(".p2p", Qt::CaseInsensitive)) {
|
||||
// PeerGuardian p2p file
|
||||
parseP2BFilterFile(filePath);
|
||||
} else {
|
||||
// Default: eMule DAT format
|
||||
parseDATFilterFile(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
s->set_ip_filter(filter);
|
||||
qDebug("IP Filter thread: finished parsing, filter applied");
|
||||
}
|
||||
|
||||
public:
|
||||
FilterParserThread(QObject* parent, session *s) : QThread(parent), s(s), abort(false) {
|
||||
|
||||
}
|
||||
|
||||
~FilterParserThread(){
|
||||
abort = true;
|
||||
wait();
|
||||
}
|
||||
|
||||
// Parser for eMule ip filter in DAT format
|
||||
void parseDATFilterFile(QString filePath) {
|
||||
const QRegExp is_ipv6(QString::fromUtf8("^[0-9a-f]{4}(:[0-9a-f]{4}){7}$"), Qt::CaseInsensitive, QRegExp::RegExp);
|
||||
const QRegExp is_ipv4(QString::fromUtf8("^(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))(\\.(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))){3}$"), Qt::CaseInsensitive, QRegExp::RegExp);
|
||||
QString strStartIP, strEndIP;
|
||||
bool IPv4 = true;
|
||||
QFile file(filePath);
|
||||
if (file.exists()){
|
||||
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("Couldn't open %1 in read mode.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
unsigned int nbLine = 0;
|
||||
while (!file.atEnd() && !abort) {
|
||||
++nbLine;
|
||||
QByteArray line = file.readLine();
|
||||
// Ignoring empty lines
|
||||
line = line.trimmed();
|
||||
if(line.isEmpty()) continue;
|
||||
// Ignoring commented lines
|
||||
if(line.startsWith('#') || line.startsWith("//")) continue;
|
||||
// Line is not commented
|
||||
QList<QByteArray> partsList = line.split(',');
|
||||
unsigned int nbElem = partsList.size();
|
||||
// IP Range can be splitted by a dash or a comma...
|
||||
// Check if there is a dash in first part
|
||||
QByteArray firstPart = partsList.at(0);
|
||||
int nbAccess = 0;
|
||||
if(firstPart.contains('-')) {
|
||||
// Range is splitted by a dash
|
||||
QList<QByteArray> IPs = firstPart.split('-');
|
||||
if(IPs.size() != 2) {
|
||||
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
|
||||
continue;
|
||||
}
|
||||
strStartIP = IPs.at(0).trimmed();
|
||||
strEndIP = IPs.at(1).trimmed();
|
||||
// Check if IPs are correct
|
||||
if(strStartIP.contains(is_ipv4) && strEndIP.contains(is_ipv4)) {
|
||||
IPv4 = true;
|
||||
} else {
|
||||
if(strStartIP.contains(is_ipv6) && strEndIP.contains(is_ipv6)) {
|
||||
IPv4 = false;
|
||||
} else {
|
||||
// Could not determine IP format
|
||||
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// Check if there is an access value (apparently not mandatory)
|
||||
if(nbElem > 1) {
|
||||
// There is possibly one
|
||||
bool ok;
|
||||
nbAccess = partsList.at(1).trimmed().toInt(&ok);
|
||||
if(!ok){
|
||||
nbAccess = 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Range is probably splitted by a comma
|
||||
unsigned int nbElem = partsList.size();
|
||||
if(nbElem > 1) {
|
||||
strStartIP = firstPart.trimmed();
|
||||
strEndIP = partsList.at(1).trimmed();
|
||||
// Check if IPs are correct
|
||||
if(strStartIP.contains(is_ipv4) && strEndIP.contains(is_ipv4)) {
|
||||
IPv4 = true;
|
||||
} else {
|
||||
if(strStartIP.contains(is_ipv6) && strEndIP.contains(is_ipv6)) {
|
||||
IPv4 = false;
|
||||
} else {
|
||||
// Could not determine IP format
|
||||
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// Check if there is an access value (apparently not mandatory)
|
||||
if(nbElem > 2) {
|
||||
// There is possibly one
|
||||
bool ok;
|
||||
nbAccess = partsList.at(2).trimmed().toInt(&ok);
|
||||
if(!ok){
|
||||
nbAccess = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(nbAccess > 127) {
|
||||
// Ignoring this rule because access value is too high
|
||||
continue;
|
||||
}
|
||||
// Now Add to the filter
|
||||
QStringList IP;
|
||||
if(IPv4) {
|
||||
//IPv4 addresses
|
||||
IP = strStartIP.split('.');
|
||||
address_v4 start((IP.at(0).toInt() << 24) + (IP.at(1).toInt() << 16) + (IP.at(2).toInt() << 8) + IP.at(3).toInt());
|
||||
IP = strEndIP.split('.');
|
||||
address_v4 last((IP.at(0).toInt() << 24) + (IP.at(1).toInt() << 16) + (IP.at(2).toInt() << 8) + IP.at(3).toInt());
|
||||
// Apply to bittorrent session
|
||||
filter.add_rule(start, last, ip_filter::blocked);
|
||||
} else {
|
||||
// IPv6, ex : 1fff:0000:0a88:85a3:0000:0000:ac1f:8001
|
||||
IP = strStartIP.split(':');
|
||||
address_v6 start = address_v6::from_string(strStartIP.remove(':', 0).toUtf8().data());
|
||||
IP = strEndIP.split(':');
|
||||
address_v6 last = address_v6::from_string(strEndIP.remove(':', 0).toUtf8().data());
|
||||
// Apply to bittorrent session
|
||||
filter.add_rule(start, last, ip_filter::blocked);
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Parser for PeerGuardian ip filter in p2p format
|
||||
void parseP2PFilterFile(QString filePath) {
|
||||
const QRegExp is_ipv4(QString::fromUtf8("^(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))(\\.(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))){3}$"), Qt::CaseInsensitive, QRegExp::RegExp);
|
||||
QFile file(filePath);
|
||||
QStringList IP;
|
||||
if (file.exists()){
|
||||
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("Couldn't open %1 in read mode.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
unsigned int nbLine = 0;
|
||||
while (!file.atEnd() && !abort) {
|
||||
++nbLine;
|
||||
QByteArray line = file.readLine();
|
||||
// Ignoring empty lines
|
||||
line = line.trimmed();
|
||||
if(line.isEmpty()) continue;
|
||||
// Ignoring commented lines
|
||||
if(line.startsWith('#') || line.startsWith("//")) continue;
|
||||
// Line is not commented
|
||||
QList<QByteArray> partsList = line.split(':');
|
||||
if(partsList.size() < 2){
|
||||
qDebug("p2p file: line %d is malformed.", nbLine);
|
||||
continue;
|
||||
}
|
||||
// Get IP range
|
||||
QList<QByteArray> IPs = partsList.at(1).split('-');
|
||||
if(IPs.size() != 2) {
|
||||
qDebug("p2p file: line %d is malformed.", nbLine);
|
||||
continue;
|
||||
}
|
||||
QString strStartIP = IPs.at(0).trimmed();
|
||||
QString strEndIP = IPs.at(1).trimmed();
|
||||
// Check IPs format (IPv4 only)
|
||||
if(strStartIP.contains(is_ipv4) && strEndIP.contains(is_ipv4)) {
|
||||
// IPv4
|
||||
IP = strStartIP.split('.');
|
||||
address_v4 start((IP.at(0).toInt() << 24) + (IP.at(1).toInt() << 16) + (IP.at(2).toInt() << 8) + IP.at(3).toInt());
|
||||
IP = strEndIP.split('.');
|
||||
address_v4 last((IP.at(0).toInt() << 24) + (IP.at(1).toInt() << 16) + (IP.at(2).toInt() << 8) + IP.at(3).toInt());
|
||||
// Apply to bittorrent session
|
||||
filter.add_rule(start, last, ip_filter::blocked);
|
||||
} else {
|
||||
qDebug("p2p file: line %d is malformed.", nbLine);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
int getlineInStream(QDataStream& stream, string& name, char delim) {
|
||||
char c;
|
||||
int total_read = 0;
|
||||
do {
|
||||
int read = stream.readRawData(&c, 1);
|
||||
total_read += read;
|
||||
if(read > 0) {
|
||||
if(c != delim) {
|
||||
name += c;
|
||||
} else {
|
||||
// Delim found
|
||||
return total_read;
|
||||
}
|
||||
}
|
||||
} while(read > 0);
|
||||
return total_read;
|
||||
}
|
||||
|
||||
// Parser for PeerGuardian ip filter in p2p format
|
||||
void parseP2BFilterFile(QString filePath) {
|
||||
QFile file(filePath);
|
||||
if (file.exists()){
|
||||
if(!file.open(QIODevice::ReadOnly)){
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("Couldn't open %1 in read mode.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
QDataStream stream(&file);
|
||||
// Read header
|
||||
char buf[7];
|
||||
unsigned char version;
|
||||
if(
|
||||
!stream.readRawData(buf, sizeof(buf)) ||
|
||||
memcmp(buf, "\xFF\xFF\xFF\xFFP2B", 7) ||
|
||||
!stream.readRawData((char*)&version, sizeof(version))
|
||||
) {
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("%1 is not a valid PeerGuardian P2B file.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
|
||||
if(version==1 || version==2) {
|
||||
qDebug ("p2b version 1 or 2");
|
||||
unsigned int start, end;
|
||||
|
||||
string name;
|
||||
while(getlineInStream(stream, name, '\0') && !abort) {
|
||||
if(
|
||||
!stream.readRawData((char*)&start, sizeof(start)) ||
|
||||
!stream.readRawData((char*)&end, sizeof(end))
|
||||
) {
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("%1 is not a valid PeerGuardian P2B file.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
// Network byte order to Host byte order
|
||||
// asio address_v4 contructor expects it
|
||||
// that way
|
||||
address_v4 first(ntohl(start));
|
||||
address_v4 last(ntohl(end));
|
||||
// Apply to bittorrent session
|
||||
filter.add_rule(first, last, ip_filter::blocked);
|
||||
}
|
||||
}
|
||||
else if(version==3) {
|
||||
qDebug ("p2b version 3");
|
||||
unsigned int namecount;
|
||||
if(!stream.readRawData((char*)&namecount, sizeof(namecount))) {
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("%1 is not a valid PeerGuardian P2B file.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
namecount=ntohl(namecount);
|
||||
// Reading names although, we don't really care about them
|
||||
for(unsigned int i=0; i<namecount; i++) {
|
||||
string name;
|
||||
if(!getlineInStream(stream, name, '\0')) {
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("%1 is not a valid PeerGuardian P2B file.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
if(abort) return;
|
||||
}
|
||||
// Reading the ranges
|
||||
unsigned int rangecount;
|
||||
if(!stream.readRawData((char*)&rangecount, sizeof(rangecount))) {
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("%1 is not a valid PeerGuardian P2B file.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
rangecount=ntohl(rangecount);
|
||||
|
||||
unsigned int name, start, end;
|
||||
|
||||
for(unsigned int i=0; i<rangecount; i++) {
|
||||
if(
|
||||
!stream.readRawData((char*)&name, sizeof(name)) ||
|
||||
!stream.readRawData((char*)&start, sizeof(start)) ||
|
||||
!stream.readRawData((char*)&end, sizeof(end))
|
||||
) {
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("%1 is not a valid PeerGuardian P2B file.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
// Network byte order to Host byte order
|
||||
// asio address_v4 contructor expects it
|
||||
// that way
|
||||
address_v4 first(ntohl(start));
|
||||
address_v4 last(ntohl(end));
|
||||
// Apply to bittorrent session
|
||||
filter.add_rule(first, last, ip_filter::blocked);
|
||||
if(abort) return;
|
||||
}
|
||||
} else {
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("%1 is not a valid PeerGuardian P2B file.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Process ip filter file
|
||||
// Supported formats:
|
||||
// * eMule IP list (DAT): http://wiki.phoenixlabs.org/wiki/DAT_Format
|
||||
// * PeerGuardian Text (P2P): http://wiki.phoenixlabs.org/wiki/P2P_Format
|
||||
// * PeerGuardian Binary (P2B): http://wiki.phoenixlabs.org/wiki/P2B_Format
|
||||
void processFilterFile(QString _filePath){
|
||||
if(isRunning()) {
|
||||
// Already parsing a filter, abort first
|
||||
abort = true;
|
||||
wait();
|
||||
}
|
||||
abort = false;
|
||||
filePath = _filePath;
|
||||
// Run it
|
||||
start();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -39,10 +39,6 @@
|
|||
#include <QMacStyle>
|
||||
#endif
|
||||
|
||||
// P2B Stuff
|
||||
#include <arpa/inet.h>
|
||||
// End of P2B stuff
|
||||
|
||||
#include "options_imp.h"
|
||||
#include "misc.h"
|
||||
|
||||
|
@ -594,7 +590,6 @@ void options_imp::loadOptions(){
|
|||
if(isFilteringEnabled()) {
|
||||
enableFilter(2); // Enable
|
||||
textFilterPath->setText(settings.value(QString::fromUtf8("File"), QString()).toString());
|
||||
processFilterFile(textFilterPath->text());
|
||||
} else {
|
||||
enableFilter(0); // Disable
|
||||
}
|
||||
|
@ -1044,7 +1039,6 @@ void options_imp::on_browseFilterButton_clicked() {
|
|||
QString ipfilter = QFileDialog::getOpenFileName(this, tr("Choose an ip filter file"), QDir::homePath(), tr("Filters (*.dat *.p2p *.p2b)"));
|
||||
if(!ipfilter.isNull()){
|
||||
textFilterPath->setText(ipfilter);
|
||||
processFilterFile(ipfilter);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1056,313 +1050,9 @@ void options_imp::on_browseSaveDirButton_clicked(){
|
|||
}
|
||||
}
|
||||
|
||||
// Parser for eMule ip filter in DAT format
|
||||
void options_imp::parseDATFilterFile(QString filePath) {
|
||||
const QRegExp is_ipv6(QString::fromUtf8("^[0-9a-f]{4}(:[0-9a-f]{4}){7}$"), Qt::CaseInsensitive, QRegExp::RegExp);
|
||||
const QRegExp is_ipv4(QString::fromUtf8("^(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))(\\.(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))){3}$"), Qt::CaseInsensitive, QRegExp::RegExp);
|
||||
QString strStartIP, strEndIP;
|
||||
bool IPv4 = true;
|
||||
QFile file(filePath);
|
||||
if (file.exists()){
|
||||
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("Couldn't open %1 in read mode.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
unsigned int nbLine = 0;
|
||||
while (!file.atEnd()) {
|
||||
++nbLine;
|
||||
QByteArray line = file.readLine();
|
||||
// Ignoring empty lines
|
||||
line = line.trimmed();
|
||||
if(line.isEmpty()) continue;
|
||||
// Ignoring commented lines
|
||||
if(line.startsWith('#') || line.startsWith("//")) continue;
|
||||
// Line is not commented
|
||||
QList<QByteArray> partsList = line.split(',');
|
||||
unsigned int nbElem = partsList.size();
|
||||
// IP Range can be splitted by a dash or a comma...
|
||||
// Check if there is a dash in first part
|
||||
QByteArray firstPart = partsList.at(0);
|
||||
int nbAccess = 0;
|
||||
if(firstPart.contains('-')) {
|
||||
// Range is splitted by a dash
|
||||
QList<QByteArray> IPs = firstPart.split('-');
|
||||
if(IPs.size() != 2) {
|
||||
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
|
||||
continue;
|
||||
}
|
||||
strStartIP = IPs.at(0).trimmed();
|
||||
strEndIP = IPs.at(1).trimmed();
|
||||
// Check if IPs are correct
|
||||
if(strStartIP.contains(is_ipv4) && strEndIP.contains(is_ipv4)) {
|
||||
IPv4 = true;
|
||||
} else {
|
||||
if(strStartIP.contains(is_ipv6) && strEndIP.contains(is_ipv6)) {
|
||||
IPv4 = false;
|
||||
} else {
|
||||
// Could not determine IP format
|
||||
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// Check if there is an access value (apparently not mandatory)
|
||||
if(nbElem > 1) {
|
||||
// There is possibly one
|
||||
bool ok;
|
||||
nbAccess = partsList.at(1).trimmed().toInt(&ok);
|
||||
if(!ok){
|
||||
nbAccess = 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Range is probably splitted by a comma
|
||||
unsigned int nbElem = partsList.size();
|
||||
if(nbElem > 1) {
|
||||
strStartIP = firstPart.trimmed();
|
||||
strEndIP = partsList.at(1).trimmed();
|
||||
// Check if IPs are correct
|
||||
if(strStartIP.contains(is_ipv4) && strEndIP.contains(is_ipv4)) {
|
||||
IPv4 = true;
|
||||
} else {
|
||||
if(strStartIP.contains(is_ipv6) && strEndIP.contains(is_ipv6)) {
|
||||
IPv4 = false;
|
||||
} else {
|
||||
// Could not determine IP format
|
||||
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// Check if there is an access value (apparently not mandatory)
|
||||
if(nbElem > 2) {
|
||||
// There is possibly one
|
||||
bool ok;
|
||||
nbAccess = partsList.at(2).trimmed().toInt(&ok);
|
||||
if(!ok){
|
||||
nbAccess = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(nbAccess > 127) {
|
||||
// Ignoring this rule because access value is too high
|
||||
continue;
|
||||
}
|
||||
// Now Add to the filter
|
||||
QStringList IP;
|
||||
if(IPv4) {
|
||||
//IPv4 addresses
|
||||
IP = strStartIP.split('.');
|
||||
address_v4 start((IP.at(0).toInt() << 24) + (IP.at(1).toInt() << 16) + (IP.at(2).toInt() << 8) + IP.at(3).toInt());
|
||||
IP = strEndIP.split('.');
|
||||
address_v4 last((IP.at(0).toInt() << 24) + (IP.at(1).toInt() << 16) + (IP.at(2).toInt() << 8) + IP.at(3).toInt());
|
||||
// Apply to bittorrent session
|
||||
filter.add_rule(start, last, ip_filter::blocked);
|
||||
} else {
|
||||
// IPv6, ex : 1fff:0000:0a88:85a3:0000:0000:ac1f:8001
|
||||
IP = strStartIP.split(':');
|
||||
address_v6 start = address_v6::from_string(strStartIP.remove(':', 0).toUtf8().data());
|
||||
IP = strEndIP.split(':');
|
||||
address_v6 last = address_v6::from_string(strEndIP.remove(':', 0).toUtf8().data());
|
||||
// Apply to bittorrent session
|
||||
filter.add_rule(start, last, ip_filter::blocked);
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Parser for PeerGuardian ip filter in p2p format
|
||||
void options_imp::parseP2PFilterFile(QString filePath) {
|
||||
const QRegExp is_ipv4(QString::fromUtf8("^(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))(\\.(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))){3}$"), Qt::CaseInsensitive, QRegExp::RegExp);
|
||||
QFile file(filePath);
|
||||
QStringList IP;
|
||||
if (file.exists()){
|
||||
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("Couldn't open %1 in read mode.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
unsigned int nbLine = 0;
|
||||
while (!file.atEnd()) {
|
||||
++nbLine;
|
||||
QByteArray line = file.readLine();
|
||||
// Ignoring empty lines
|
||||
line = line.trimmed();
|
||||
if(line.isEmpty()) continue;
|
||||
// Ignoring commented lines
|
||||
if(line.startsWith('#') || line.startsWith("//")) continue;
|
||||
// Line is not commented
|
||||
QList<QByteArray> partsList = line.split(':');
|
||||
if(partsList.size() < 2){
|
||||
qDebug("p2p file: line %d is malformed.", nbLine);
|
||||
continue;
|
||||
}
|
||||
// Get IP range
|
||||
QList<QByteArray> IPs = partsList.at(1).split('-');
|
||||
if(IPs.size() != 2) {
|
||||
qDebug("p2p file: line %d is malformed.", nbLine);
|
||||
continue;
|
||||
}
|
||||
QString strStartIP = IPs.at(0).trimmed();
|
||||
QString strEndIP = IPs.at(1).trimmed();
|
||||
// Check IPs format (IPv4 only)
|
||||
if(strStartIP.contains(is_ipv4) && strEndIP.contains(is_ipv4)) {
|
||||
// IPv4
|
||||
IP = strStartIP.split('.');
|
||||
address_v4 start((IP.at(0).toInt() << 24) + (IP.at(1).toInt() << 16) + (IP.at(2).toInt() << 8) + IP.at(3).toInt());
|
||||
IP = strEndIP.split('.');
|
||||
address_v4 last((IP.at(0).toInt() << 24) + (IP.at(1).toInt() << 16) + (IP.at(2).toInt() << 8) + IP.at(3).toInt());
|
||||
// Apply to bittorrent session
|
||||
filter.add_rule(start, last, ip_filter::blocked);
|
||||
} else {
|
||||
qDebug("p2p file: line %d is malformed.", nbLine);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
int options_imp::getlineInStream(QDataStream& stream, string& name, char delim) {
|
||||
char c;
|
||||
int total_read = 0;
|
||||
do {
|
||||
int read = stream.readRawData(&c, 1);
|
||||
total_read += read;
|
||||
if(read > 0) {
|
||||
if(c != delim) {
|
||||
name += c;
|
||||
} else {
|
||||
// Delim found
|
||||
return total_read;
|
||||
}
|
||||
}
|
||||
} while(read > 0);
|
||||
return total_read;
|
||||
}
|
||||
|
||||
// Parser for PeerGuardian ip filter in p2p format
|
||||
void options_imp::parseP2BFilterFile(QString filePath) {
|
||||
QFile file(filePath);
|
||||
if (file.exists()){
|
||||
if(!file.open(QIODevice::ReadOnly)){
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("Couldn't open %1 in read mode.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
QDataStream stream(&file);
|
||||
// Read header
|
||||
char buf[7];
|
||||
unsigned char version;
|
||||
if(
|
||||
!stream.readRawData(buf, sizeof(buf)) ||
|
||||
memcmp(buf, "\xFF\xFF\xFF\xFFP2B", 7) ||
|
||||
!stream.readRawData((char*)&version, sizeof(version))
|
||||
) {
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("%1 is not a valid PeerGuardian P2B file.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
|
||||
if(version==1 || version==2) {
|
||||
qDebug ("p2b version 1 or 2");
|
||||
unsigned int start, end;
|
||||
|
||||
string name;
|
||||
while(getlineInStream(stream, name, '\0')) {
|
||||
if(
|
||||
!stream.readRawData((char*)&start, sizeof(start)) ||
|
||||
!stream.readRawData((char*)&end, sizeof(end))
|
||||
) {
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("%1 is not a valid PeerGuardian P2B file.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
// Network byte order to Host byte order
|
||||
// asio address_v4 contructor expects it
|
||||
// that way
|
||||
address_v4 first(ntohl(start));
|
||||
address_v4 last(ntohl(end));
|
||||
// Apply to bittorrent session
|
||||
filter.add_rule(first, last, ip_filter::blocked);
|
||||
}
|
||||
}
|
||||
else if(version==3) {
|
||||
qDebug ("p2b version 3");
|
||||
unsigned int namecount;
|
||||
if(!stream.readRawData((char*)&namecount, sizeof(namecount))) {
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("%1 is not a valid PeerGuardian P2B file.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
namecount=ntohl(namecount);
|
||||
// Reading names although, we don't really care about them
|
||||
for(unsigned int i=0; i<namecount; i++) {
|
||||
string name;
|
||||
if(!getlineInStream(stream, name, '\0')) {
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("%1 is not a valid PeerGuardian P2B file.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Reading the ranges
|
||||
unsigned int rangecount;
|
||||
if(!stream.readRawData((char*)&rangecount, sizeof(rangecount))) {
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("%1 is not a valid PeerGuardian P2B file.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
rangecount=ntohl(rangecount);
|
||||
|
||||
unsigned int name, start, end;
|
||||
|
||||
for(unsigned int i=0; i<rangecount; i++) {
|
||||
if(
|
||||
!stream.readRawData((char*)&name, sizeof(name)) ||
|
||||
!stream.readRawData((char*)&start, sizeof(start)) ||
|
||||
!stream.readRawData((char*)&end, sizeof(end))
|
||||
) {
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("%1 is not a valid PeerGuardian P2B file.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
// Network byte order to Host byte order
|
||||
// asio address_v4 contructor expects it
|
||||
// that way
|
||||
address_v4 first(ntohl(start));
|
||||
address_v4 last(ntohl(end));
|
||||
// Apply to bittorrent session
|
||||
filter.add_rule(first, last, ip_filter::blocked);
|
||||
}
|
||||
} else {
|
||||
QMessageBox::critical(0, tr("I/O Error", "Input/Output Error"), tr("%1 is not a valid PeerGuardian P2B file.").arg(filePath));
|
||||
return;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Process ip filter file
|
||||
// Supported formats:
|
||||
// * eMule IP list (DAT): http://wiki.phoenixlabs.org/wiki/DAT_Format
|
||||
// * PeerGuardian Text (P2P): http://wiki.phoenixlabs.org/wiki/P2P_Format
|
||||
// * PeerGuardian Binary (P2B): http://wiki.phoenixlabs.org/wiki/P2B_Format
|
||||
void options_imp::processFilterFile(QString filePath){
|
||||
qDebug("Processing filter files");
|
||||
if(filePath.endsWith(".dat", Qt::CaseInsensitive)) {
|
||||
// eMule DAT file
|
||||
parseDATFilterFile(filePath);
|
||||
} else {
|
||||
if(filePath.endsWith(".p2p", Qt::CaseInsensitive)) {
|
||||
// PeerGuardian p2p file
|
||||
parseP2PFilterFile(filePath);
|
||||
} else {
|
||||
if(filePath.endsWith(".p2p", Qt::CaseInsensitive)) {
|
||||
// PeerGuardian p2p file
|
||||
parseP2BFilterFile(filePath);
|
||||
} else {
|
||||
// Default: eMule DAT format
|
||||
parseDATFilterFile(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return Filter object to apply to BT session
|
||||
ip_filter options_imp::getFilter() const{
|
||||
return filter;
|
||||
QString options_imp::getFilter() const{
|
||||
return textFilterPath->text();
|
||||
}
|
||||
|
||||
// Web UI
|
||||
|
|
|
@ -24,11 +24,6 @@
|
|||
|
||||
#include "ui_options.h"
|
||||
#include <libtorrent/ip_filter.hpp>
|
||||
#include <QDataStream>
|
||||
|
||||
// P2B Stuff
|
||||
#include <string.h>
|
||||
// End of P2B stuff
|
||||
|
||||
#define HTTP 1
|
||||
#define SOCKS5 2
|
||||
|
@ -41,7 +36,6 @@
|
|||
#define SHOW_PROPERTIES 2
|
||||
|
||||
using namespace libtorrent;
|
||||
using namespace std;
|
||||
|
||||
class QCloseEvent;
|
||||
|
||||
|
@ -50,7 +44,6 @@ class options_imp : public QDialog, private Ui::Dialog {
|
|||
|
||||
private:
|
||||
QButtonGroup choiceLanguage;
|
||||
ip_filter filter;
|
||||
QStringList locales;
|
||||
QAbstractButton *applyButton;
|
||||
|
||||
|
@ -110,12 +103,11 @@ class options_imp : public QDialog, private Ui::Dialog {
|
|||
float getDeleteRatio() const;
|
||||
// IP Filter
|
||||
bool isFilteringEnabled() const;
|
||||
ip_filter getFilter() const;
|
||||
QString getFilter() const;
|
||||
bool isWebUiEnabled() const;
|
||||
quint16 webUiPort() const;
|
||||
QString webUiUsername() const;
|
||||
QString webUiPassword() const;
|
||||
int getlineInStream(QDataStream& stream, string& name, char delim);
|
||||
|
||||
protected slots:
|
||||
void enableUploadLimit(int checkBoxValue);
|
||||
|
@ -137,16 +129,12 @@ class options_imp : public QDialog, private Ui::Dialog {
|
|||
void on_browseScanDirButton_clicked();
|
||||
void on_browseFilterButton_clicked();
|
||||
void on_browseSaveDirButton_clicked();
|
||||
void processFilterFile(QString filePath=QString());
|
||||
void enableApplyButton();
|
||||
void checkPortsLogic();
|
||||
void enableSystrayOptions();
|
||||
void disableSystrayOptions();
|
||||
void setSystrayOptionsState(int checkBoxValue);
|
||||
void enableWebUi(bool checkBoxValue);
|
||||
void parseDATFilterFile(QString filePath);
|
||||
void parseP2PFilterFile(QString filePath);
|
||||
void parseP2BFilterFile(QString filePath);
|
||||
|
||||
public slots:
|
||||
void setLocale(QString locale);
|
||||
|
|
|
@ -147,7 +147,7 @@ HEADERS += GUI.h misc.h options_imp.h about_imp.h \
|
|||
realprogressbarthread.h qrealarray.h \
|
||||
httpserver.h httpconnection.h \
|
||||
httprequestparser.h httpresponsegenerator.h \
|
||||
json.h eventmanager.h
|
||||
json.h eventmanager.h filterParserThread.h
|
||||
FORMS += MainWindow.ui options.ui about.ui \
|
||||
properties.ui createtorrent.ui preview.ui \
|
||||
login.ui downloadFromURL.ui addTorrentDialog.ui \
|
||||
|
|
Loading…
Reference in a new issue