mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-25 22:14:32 +00:00
Added signals for reporting the number of rules parsed
This commit is contained in:
parent
ab579e3449
commit
61d9f68bee
@ -54,44 +54,6 @@ using namespace std;
|
|||||||
class FilterParserThread : public QThread {
|
class FilterParserThread : public QThread {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
private:
|
|
||||||
libtorrent::session *s;
|
|
||||||
libtorrent::ip_filter filter;
|
|
||||||
bool abort;
|
|
||||||
QString filePath;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
QString cleanupIPAddress(QString _ip) {
|
|
||||||
QHostAddress ip(_ip.trimmed());
|
|
||||||
if(ip.isNull()) {
|
|
||||||
return QString();
|
|
||||||
}
|
|
||||||
return ip.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
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(".p2b", Qt::CaseInsensitive)) {
|
|
||||||
// PeerGuardian p2b file
|
|
||||||
parseP2BFilterFile(filePath);
|
|
||||||
} else {
|
|
||||||
// Default: eMule DAT format
|
|
||||||
parseDATFilterFile(filePath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s->set_ip_filter(filter);
|
|
||||||
qDebug("IP Filter thread: finished parsing, filter applied");
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
FilterParserThread(QObject* parent, libtorrent::session *s) : QThread(parent), s(s), abort(false) {
|
FilterParserThread(QObject* parent, libtorrent::session *s) : QThread(parent), s(s), abort(false) {
|
||||||
|
|
||||||
@ -103,12 +65,13 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parser for eMule ip filter in DAT format
|
// Parser for eMule ip filter in DAT format
|
||||||
void parseDATFilterFile(QString filePath) {
|
int parseDATFilterFile(QString filePath) {
|
||||||
|
int ruleCount = 0;
|
||||||
QFile file(filePath);
|
QFile file(filePath);
|
||||||
if (file.exists()){
|
if (file.exists()){
|
||||||
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
||||||
std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl;
|
std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl;
|
||||||
return;
|
return ruleCount;
|
||||||
}
|
}
|
||||||
unsigned int nbLine = 0;
|
unsigned int nbLine = 0;
|
||||||
while (!file.atEnd() && !abort) {
|
while (!file.atEnd() && !abort) {
|
||||||
@ -177,22 +140,24 @@ public:
|
|||||||
// Now Add to the filter
|
// Now Add to the filter
|
||||||
try {
|
try {
|
||||||
filter.add_rule(startAddr, endAddr, libtorrent::ip_filter::blocked);
|
filter.add_rule(startAddr, endAddr, libtorrent::ip_filter::blocked);
|
||||||
|
++ruleCount;
|
||||||
}catch(exception){
|
}catch(exception){
|
||||||
qDebug("Bad line in filter file, avoided crash...");
|
qDebug("Bad line in filter file, avoided crash...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
return ruleCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parser for PeerGuardian ip filter in p2p format
|
// Parser for PeerGuardian ip filter in p2p format
|
||||||
void parseP2PFilterFile(QString filePath) {
|
int parseP2PFilterFile(QString filePath) {
|
||||||
|
int ruleCount = 0;
|
||||||
QFile file(filePath);
|
QFile file(filePath);
|
||||||
QStringList IP;
|
|
||||||
if (file.exists()){
|
if (file.exists()){
|
||||||
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
||||||
std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl;
|
std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl;
|
||||||
return;
|
return ruleCount;
|
||||||
}
|
}
|
||||||
unsigned int nbLine = 0;
|
unsigned int nbLine = 0;
|
||||||
while (!file.atEnd() && !abort) {
|
while (!file.atEnd() && !abort) {
|
||||||
@ -246,6 +211,7 @@ public:
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
filter.add_rule(startAddr, endAddr, libtorrent::ip_filter::blocked);
|
filter.add_rule(startAddr, endAddr, libtorrent::ip_filter::blocked);
|
||||||
|
++ruleCount;
|
||||||
} catch(std::exception&) {
|
} catch(std::exception&) {
|
||||||
qDebug("p2p file: line %d is malformed.", nbLine);
|
qDebug("p2p file: line %d is malformed.", nbLine);
|
||||||
qDebug("Line was: %s", line.constData());
|
qDebug("Line was: %s", line.constData());
|
||||||
@ -254,6 +220,7 @@ public:
|
|||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
return ruleCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getlineInStream(QDataStream& stream, string& name, char delim) {
|
int getlineInStream(QDataStream& stream, string& name, char delim) {
|
||||||
@ -276,12 +243,13 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parser for PeerGuardian ip filter in p2p format
|
// Parser for PeerGuardian ip filter in p2p format
|
||||||
void parseP2BFilterFile(QString filePath) {
|
int parseP2BFilterFile(QString filePath) {
|
||||||
|
int ruleCount = 0;
|
||||||
QFile file(filePath);
|
QFile file(filePath);
|
||||||
if (file.exists()){
|
if (file.exists()){
|
||||||
if(!file.open(QIODevice::ReadOnly)){
|
if(!file.open(QIODevice::ReadOnly)){
|
||||||
std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl;
|
std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl;
|
||||||
return;
|
return ruleCount;
|
||||||
}
|
}
|
||||||
QDataStream stream(&file);
|
QDataStream stream(&file);
|
||||||
// Read header
|
// Read header
|
||||||
@ -293,7 +261,7 @@ public:
|
|||||||
!stream.readRawData((char*)&version, sizeof(version))
|
!stream.readRawData((char*)&version, sizeof(version))
|
||||||
) {
|
) {
|
||||||
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
|
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
|
||||||
return;
|
return ruleCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(version==1 || version==2) {
|
if(version==1 || version==2) {
|
||||||
@ -307,7 +275,7 @@ public:
|
|||||||
!stream.readRawData((char*)&end, sizeof(end))
|
!stream.readRawData((char*)&end, sizeof(end))
|
||||||
) {
|
) {
|
||||||
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
|
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
|
||||||
return;
|
return ruleCount;
|
||||||
}
|
}
|
||||||
// Network byte order to Host byte order
|
// Network byte order to Host byte order
|
||||||
// asio address_v4 contructor expects it
|
// asio address_v4 contructor expects it
|
||||||
@ -315,7 +283,10 @@ public:
|
|||||||
libtorrent::address_v4 first(ntohl(start));
|
libtorrent::address_v4 first(ntohl(start));
|
||||||
libtorrent::address_v4 last(ntohl(end));
|
libtorrent::address_v4 last(ntohl(end));
|
||||||
// Apply to bittorrent session
|
// Apply to bittorrent session
|
||||||
|
try {
|
||||||
filter.add_rule(first, last, libtorrent::ip_filter::blocked);
|
filter.add_rule(first, last, libtorrent::ip_filter::blocked);
|
||||||
|
++ruleCount;
|
||||||
|
} catch(std::exception&) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(version==3) {
|
else if(version==3) {
|
||||||
@ -323,7 +294,7 @@ public:
|
|||||||
unsigned int namecount;
|
unsigned int namecount;
|
||||||
if(!stream.readRawData((char*)&namecount, sizeof(namecount))) {
|
if(!stream.readRawData((char*)&namecount, sizeof(namecount))) {
|
||||||
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
|
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
|
||||||
return;
|
return ruleCount;
|
||||||
}
|
}
|
||||||
namecount=ntohl(namecount);
|
namecount=ntohl(namecount);
|
||||||
// Reading names although, we don't really care about them
|
// Reading names although, we don't really care about them
|
||||||
@ -331,15 +302,15 @@ public:
|
|||||||
string name;
|
string name;
|
||||||
if(!getlineInStream(stream, name, '\0')) {
|
if(!getlineInStream(stream, name, '\0')) {
|
||||||
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
|
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
|
||||||
return;
|
return ruleCount;
|
||||||
}
|
}
|
||||||
if(abort) return;
|
if(abort) return ruleCount;
|
||||||
}
|
}
|
||||||
// Reading the ranges
|
// Reading the ranges
|
||||||
unsigned int rangecount;
|
unsigned int rangecount;
|
||||||
if(!stream.readRawData((char*)&rangecount, sizeof(rangecount))) {
|
if(!stream.readRawData((char*)&rangecount, sizeof(rangecount))) {
|
||||||
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
|
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
|
||||||
return;
|
return ruleCount;
|
||||||
}
|
}
|
||||||
rangecount=ntohl(rangecount);
|
rangecount=ntohl(rangecount);
|
||||||
|
|
||||||
@ -352,7 +323,7 @@ public:
|
|||||||
!stream.readRawData((char*)&end, sizeof(end))
|
!stream.readRawData((char*)&end, sizeof(end))
|
||||||
) {
|
) {
|
||||||
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
|
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
|
||||||
return;
|
return ruleCount;
|
||||||
}
|
}
|
||||||
// Network byte order to Host byte order
|
// Network byte order to Host byte order
|
||||||
// asio address_v4 contructor expects it
|
// asio address_v4 contructor expects it
|
||||||
@ -360,15 +331,19 @@ public:
|
|||||||
libtorrent::address_v4 first(ntohl(start));
|
libtorrent::address_v4 first(ntohl(start));
|
||||||
libtorrent::address_v4 last(ntohl(end));
|
libtorrent::address_v4 last(ntohl(end));
|
||||||
// Apply to bittorrent session
|
// Apply to bittorrent session
|
||||||
|
try {
|
||||||
filter.add_rule(first, last, libtorrent::ip_filter::blocked);
|
filter.add_rule(first, last, libtorrent::ip_filter::blocked);
|
||||||
if(abort) return;
|
++ruleCount;
|
||||||
|
} catch(std::exception&) {}
|
||||||
|
if(abort) return ruleCount;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
|
std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl;
|
||||||
return;
|
return ruleCount;
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
return ruleCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process ip filter file
|
// Process ip filter file
|
||||||
@ -404,6 +379,49 @@ public:
|
|||||||
s->set_ip_filter(filter);
|
s->set_ip_filter(filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void IPFilterParsed(int ruleCount);
|
||||||
|
void IPFilterError();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
QString cleanupIPAddress(QString _ip) {
|
||||||
|
QHostAddress ip(_ip.trimmed());
|
||||||
|
if(ip.isNull()) {
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
return ip.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
void run(){
|
||||||
|
qDebug("Processing filter file");
|
||||||
|
int ruleCount = 0;
|
||||||
|
if(filePath.endsWith(".p2p", Qt::CaseInsensitive)) {
|
||||||
|
// PeerGuardian p2p file
|
||||||
|
ruleCount = parseP2PFilterFile(filePath);
|
||||||
|
} else {
|
||||||
|
if(filePath.endsWith(".p2b", Qt::CaseInsensitive)) {
|
||||||
|
// PeerGuardian p2b file
|
||||||
|
ruleCount = parseP2BFilterFile(filePath);
|
||||||
|
} else {
|
||||||
|
// Default: eMule DAT format
|
||||||
|
ruleCount = parseDATFilterFile(filePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
s->set_ip_filter(filter);
|
||||||
|
emit IPFilterParsed(ruleCount);
|
||||||
|
} catch(std::exception&){
|
||||||
|
emit IPFilterError();
|
||||||
|
}
|
||||||
|
qDebug("IP Filter thread: finished parsing, filter applied");
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
libtorrent::session *s;
|
||||||
|
libtorrent::ip_filter filter;
|
||||||
|
bool abort;
|
||||||
|
QString filePath;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user