mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-19 12:59:56 -07:00
Improved IPv6 support
This commit is contained in:
parent
8276c301c2
commit
da95d5e0df
2 changed files with 311 additions and 360 deletions
|
@ -34,7 +34,6 @@
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QDataStream>
|
#include <QDataStream>
|
||||||
#include <QRegExp>
|
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
|
|
||||||
#include <libtorrent/session.hpp>
|
#include <libtorrent/session.hpp>
|
||||||
|
@ -97,10 +96,6 @@ class FilterParserThread : public QThread {
|
||||||
|
|
||||||
// Parser for eMule ip filter in DAT format
|
// Parser for eMule ip filter in DAT format
|
||||||
void parseDATFilterFile(QString filePath) {
|
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);
|
QFile file(filePath);
|
||||||
if (file.exists()){
|
if (file.exists()){
|
||||||
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
||||||
|
@ -116,100 +111,54 @@ class FilterParserThread : public QThread {
|
||||||
if(line.isEmpty()) continue;
|
if(line.isEmpty()) continue;
|
||||||
// Ignoring commented lines
|
// Ignoring commented lines
|
||||||
if(line.startsWith('#') || line.startsWith("//")) continue;
|
if(line.startsWith('#') || line.startsWith("//")) continue;
|
||||||
// Line is not commented
|
|
||||||
|
// Line should be splitted by commas
|
||||||
QList<QByteArray> partsList = line.split(',');
|
QList<QByteArray> partsList = line.split(',');
|
||||||
unsigned int nbElem = partsList.size();
|
const uint nbElem = partsList.size();
|
||||||
// IP Range can be splitted by a dash or a comma...
|
|
||||||
// Check if there is a dash in first part
|
// IP Range should be splitted by a dash
|
||||||
QByteArray firstPart = partsList.at(0);
|
QList<QByteArray> IPs = partsList.first().split('-');
|
||||||
int nbAccess = 0;
|
|
||||||
if(firstPart.contains('-')) {
|
|
||||||
// Range is splitted by a dash
|
|
||||||
QList<QByteArray> IPs = firstPart.split('-');
|
|
||||||
if(IPs.size() != 2) {
|
if(IPs.size() != 2) {
|
||||||
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
|
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
|
||||||
|
qDebug("Line was %s", line.constData());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
strStartIP = IPs.at(0).trimmed();
|
|
||||||
strEndIP = IPs.at(1).trimmed();
|
boost::system::error_code ec;
|
||||||
// Check if IPs are correct
|
const QString strStartIP = IPs.at(0).trimmed();
|
||||||
if(strStartIP.contains(is_ipv4) && strEndIP.contains(is_ipv4)) {
|
libtorrent::address startAddr = libtorrent::address::from_string(qPrintable(strStartIP), ec);
|
||||||
IPv4 = true;
|
if(ec) {
|
||||||
} 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);
|
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
|
||||||
|
qDebug("Start IP of the range is malformated: %s", qPrintable(strStartIP));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
const QString strEndIP = IPs.at(1).trimmed();
|
||||||
|
libtorrent::address endAddr = libtorrent::address::from_string(qPrintable(strEndIP), ec);
|
||||||
|
if(ec) {
|
||||||
|
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
|
||||||
|
qDebug("End IP of the range is malformated: %s", qPrintable(strEndIP));
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
if(startAddr.is_v4() != endAddr.is_v4()) {
|
||||||
|
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
|
||||||
|
qDebug("One IP is IPv4 and the other is IPv6!");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if there is an access value (apparently not mandatory)
|
// Check if there is an access value (apparently not mandatory)
|
||||||
|
int nbAccess = 0;
|
||||||
if(nbElem > 1) {
|
if(nbElem > 1) {
|
||||||
// There is possibly one
|
// There is possibly one
|
||||||
bool ok;
|
nbAccess = partsList.at(1).trimmed().toInt();
|
||||||
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) {
|
if(nbAccess > 127) {
|
||||||
// Ignoring this rule because access value is too high
|
// Ignoring this rule because access value is too high
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Now Add to the filter
|
// Now Add to the filter
|
||||||
QStringList IP;
|
|
||||||
try {
|
try {
|
||||||
if(IPv4) {
|
filter.add_rule(startAddr, endAddr, ip_filter::blocked);
|
||||||
//IPv4 addresses
|
|
||||||
IP = strStartIP.split('.');
|
|
||||||
if(IP.size() != 4)
|
|
||||||
throw exception();
|
|
||||||
address_v4 start((IP.at(0).toUInt() << 24) + (IP.at(1).toUInt() << 16) + (IP.at(2).toUInt() << 8) + IP.at(3).toUInt());
|
|
||||||
IP = strEndIP.split('.');
|
|
||||||
if(IP.size() != 4)
|
|
||||||
throw exception();
|
|
||||||
address_v4 last((IP.at(0).toUInt() << 24) + (IP.at(1).toUInt() << 16) + (IP.at(2).toUInt() << 8) + IP.at(3).toUInt());
|
|
||||||
// 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).toLocal8Bit().data());
|
|
||||||
IP = strEndIP.split(':');
|
|
||||||
address_v6 last = address_v6::from_string(strEndIP.remove(':', 0).toLocal8Bit().data());
|
|
||||||
// Apply to bittorrent session
|
|
||||||
filter.add_rule(start, last, ip_filter::blocked);
|
|
||||||
}
|
|
||||||
}catch(exception){
|
}catch(exception){
|
||||||
qDebug("Bad line in filter file, avoided crash...");
|
qDebug("Bad line in filter file, avoided crash...");
|
||||||
}
|
}
|
||||||
|
@ -220,7 +169,6 @@ class FilterParserThread : public QThread {
|
||||||
|
|
||||||
// Parser for PeerGuardian ip filter in p2p format
|
// Parser for PeerGuardian ip filter in p2p format
|
||||||
void parseP2PFilterFile(QString filePath) {
|
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);
|
QFile file(filePath);
|
||||||
QStringList IP;
|
QStringList IP;
|
||||||
if (file.exists()){
|
if (file.exists()){
|
||||||
|
@ -231,13 +179,11 @@ class FilterParserThread : public QThread {
|
||||||
unsigned int nbLine = 0;
|
unsigned int nbLine = 0;
|
||||||
while (!file.atEnd() && !abort) {
|
while (!file.atEnd() && !abort) {
|
||||||
++nbLine;
|
++nbLine;
|
||||||
QByteArray line = file.readLine();
|
QByteArray line = file.readLine().trimmed();
|
||||||
// Ignoring empty lines
|
|
||||||
line = line.trimmed();
|
|
||||||
if(line.isEmpty()) continue;
|
if(line.isEmpty()) continue;
|
||||||
// Ignoring commented lines
|
// Ignoring commented lines
|
||||||
if(line.startsWith('#') || line.startsWith("//")) continue;
|
if(line.startsWith('#') || line.startsWith("//")) continue;
|
||||||
// Line is not commented
|
// Line is splitted by :
|
||||||
QList<QByteArray> partsList = line.split(':');
|
QList<QByteArray> partsList = line.split(':');
|
||||||
if(partsList.size() < 2){
|
if(partsList.size() < 2){
|
||||||
qDebug("p2p file: line %d is malformed.", nbLine);
|
qDebug("p2p file: line %d is malformed.", nbLine);
|
||||||
|
@ -247,21 +193,34 @@ class FilterParserThread : public QThread {
|
||||||
QList<QByteArray> IPs = partsList.last().split('-');
|
QList<QByteArray> IPs = partsList.last().split('-');
|
||||||
if(IPs.size() != 2) {
|
if(IPs.size() != 2) {
|
||||||
qDebug("p2p file: line %d is malformed.", nbLine);
|
qDebug("p2p file: line %d is malformed.", nbLine);
|
||||||
|
qDebug("line was: %s", line.constData());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
boost::system::error_code ec;
|
||||||
QString strStartIP = IPs.at(0).trimmed();
|
QString strStartIP = IPs.at(0).trimmed();
|
||||||
QString strEndIP = IPs.at(1).trimmed();
|
libtorrent::address startAddr = libtorrent::address::from_string(qPrintable(strStartIP), ec);
|
||||||
// Check IPs format (IPv4 only)
|
if(ec) {
|
||||||
if(strStartIP.contains(is_ipv4) && strEndIP.contains(is_ipv4)) {
|
|
||||||
// IPv4
|
|
||||||
IP = strStartIP.split('.');
|
|
||||||
address_v4 start((IP.at(0).toUInt() << 24) + (IP.at(1).toUInt() << 16) + (IP.at(2).toUInt() << 8) + IP.at(3).toUInt());
|
|
||||||
IP = strEndIP.split('.');
|
|
||||||
address_v4 last((IP.at(0).toUInt() << 24) + (IP.at(1).toUInt() << 16) + (IP.at(2).toUInt() << 8) + IP.at(3).toUInt());
|
|
||||||
// Apply to bittorrent session
|
|
||||||
filter.add_rule(start, last, ip_filter::blocked);
|
|
||||||
} else {
|
|
||||||
qDebug("p2p file: line %d is malformed.", nbLine);
|
qDebug("p2p file: line %d is malformed.", nbLine);
|
||||||
|
qDebug("Start IP is invalid: %s", qPrintable(strStartIP));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
QString strEndIP = IPs.at(1).trimmed();
|
||||||
|
libtorrent::address endAddr = libtorrent::address::from_string(qPrintable(strEndIP), ec);
|
||||||
|
if(ec) {
|
||||||
|
qDebug("p2p file: line %d is malformed.", nbLine);
|
||||||
|
qDebug("End IP is invalid: %s", qPrintable(strStartIP));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(startAddr.is_v4() != endAddr.is_v4()) {
|
||||||
|
qDebug("p2p file: line %d is malformed.", nbLine);
|
||||||
|
qDebug("Line was: %s", line.constData());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
filter.add_rule(startAddr, endAddr, ip_filter::blocked);
|
||||||
|
} catch(std::exception&) {
|
||||||
|
qDebug("p2p file: line %d is malformed.", nbLine);
|
||||||
|
qDebug("Line was: %s", line.constData());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
#include <QDialog>
|
#include <QDialog>
|
||||||
#include <QRegExp>
|
#include <QRegExp>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
|
#include <QHostAddress>
|
||||||
#include "ui_peer.h"
|
#include "ui_peer.h"
|
||||||
#include <libtorrent/session.hpp>
|
#include <libtorrent/session.hpp>
|
||||||
|
|
||||||
|
@ -70,16 +71,15 @@ public:
|
||||||
libtorrent::asio::ip::tcp::endpoint ep;
|
libtorrent::asio::ip::tcp::endpoint ep;
|
||||||
PeerAdditionDlg dlg;
|
PeerAdditionDlg dlg;
|
||||||
if(dlg.exec() == QDialog::Accepted) {
|
if(dlg.exec() == QDialog::Accepted) {
|
||||||
const QRegExp is_ipv6(QString::fromUtf8("[0-9a-f]{4}(:[0-9a-f]{4}){7}"), Qt::CaseInsensitive, QRegExp::RegExp);
|
QString ip = dlg.getIP();
|
||||||
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);
|
boost::system::error_code ec;
|
||||||
QString IP = dlg.getIP();
|
libtorrent::address addr = libtorrent::address::from_string(qPrintable(ip), ec);
|
||||||
if(is_ipv4.exactMatch(IP)) {
|
if(ec) {
|
||||||
// IPv4
|
qDebug("Unable to parse the provided IP: %s", qPrintable(ip));
|
||||||
ep = libtorrent::asio::ip::tcp::endpoint(libtorrent::asio::ip::address_v4::from_string(IP.toLocal8Bit().data()), dlg.getPort());
|
return ep;
|
||||||
} else {
|
|
||||||
// IPv6
|
|
||||||
ep = libtorrent::asio::ip::tcp::endpoint(libtorrent::asio::ip::address_v6::from_string(IP.toLocal8Bit().data()), dlg.getPort());
|
|
||||||
}
|
}
|
||||||
|
qDebug("Provided IP is correct");
|
||||||
|
ep = libtorrent::asio::ip::tcp::endpoint(addr, dlg.getPort());
|
||||||
}
|
}
|
||||||
return ep;
|
return ep;
|
||||||
}
|
}
|
||||||
|
@ -87,21 +87,13 @@ public:
|
||||||
|
|
||||||
protected slots:
|
protected slots:
|
||||||
void validateInput() {
|
void validateInput() {
|
||||||
const QRegExp is_ipv6(QString::fromUtf8("[0-9a-f]{4}(:[0-9a-f]{4}){7}"), Qt::CaseInsensitive, QRegExp::RegExp);
|
QHostAddress ip(getIP());
|
||||||
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);
|
if(ip.isNull()) {
|
||||||
QString IP = getIP();
|
|
||||||
if(is_ipv4.exactMatch(IP)) {
|
|
||||||
qDebug("Detected IPv4 address: %s", IP.toLocal8Bit().data());
|
|
||||||
accept();
|
|
||||||
} else {
|
|
||||||
if(is_ipv6.exactMatch(IP)) {
|
|
||||||
qDebug("Detected IPv6 address: %s", IP.toLocal8Bit().data());
|
|
||||||
accept();
|
|
||||||
} else {
|
|
||||||
QMessageBox::warning(this, tr("Invalid IP"),
|
QMessageBox::warning(this, tr("Invalid IP"),
|
||||||
tr("The IP you provided is invalid."),
|
tr("The IP you provided is invalid."),
|
||||||
QMessageBox::Ok);
|
QMessageBox::Ok);
|
||||||
}
|
} else {
|
||||||
|
accept();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue