- Removed IP filter list and buttons to add/remove ranges, I don't think

people handle this manually anyway.
- Prepare code for P2P/P2B filter files (soon to be coded)
This commit is contained in:
Christophe Dumez 2008-05-17 17:01:03 +00:00
parent 6926b3f7a0
commit f6210d5857
3 changed files with 164 additions and 448 deletions

View file

@ -200,8 +200,6 @@ options_imp::options_imp(QWidget *parent):QDialog(parent){
connect(spinMaxRatio, SIGNAL(valueChanged(QString)), this, SLOT(enableApplyButton()));
// Misc tab
connect(checkIPFilter, SIGNAL(stateChanged(int)), this, SLOT(enableApplyButton()));
connect(addFilterRangeButton, SIGNAL(clicked()), this, SLOT(enableApplyButton()));
connect(delFilterRangeButton, SIGNAL(clicked()), this, SLOT(enableApplyButton()));
connect(textFilterPath, SIGNAL(textChanged(QString)), this, SLOT(enableApplyButton()));
connect(spinRSSRefresh, SIGNAL(valueChanged(QString)), this, SLOT(enableApplyButton()));
connect(spinRSSMaxArticlesPerFeed, SIGNAL(valueChanged(QString)), this, SLOT(enableApplyButton()));
@ -845,17 +843,11 @@ void options_imp::enableMaxUploadsLimitPerTorrent(int checkBoxValue){
void options_imp::enableFilter(int checkBoxValue){
if(checkBoxValue!=2){
//Disable
filtersList->setEnabled(false);
addFilterRangeButton->setEnabled(false);
delFilterRangeButton->setEnabled(false);
lblFilterPath->setEnabled(false);
textFilterPath->setEnabled(false);
browseFilterButton->setEnabled(false);
}else{
//enable
filtersList->setEnabled(true);
addFilterRangeButton->setEnabled(true);
delFilterRangeButton->setEnabled(true);
lblFilterPath->setEnabled(true);
textFilterPath->setEnabled(true);
browseFilterButton->setEnabled(true);
@ -1060,114 +1052,74 @@ void options_imp::on_browseSaveDirButton_clicked(){
}
}
// look for ipfilter.dat file
// reads emule ipfilter files.
// with the following format:
//
// <first-ip> - <last-ip> , <access> , <comment>
//
// first-ip is an ip address that defines the first
// address of the range
// last-ip is the last ip address in the range
// access is a number specifying the access control
// for this ip-range. Right now values > 127 = allowed
// and numbers <= 127 = blocked
// the rest of the line is ignored
//
// Lines may be commented using '#' or '//'
// 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 (TODO)
// * PeerGuardian Binary (P2B): http://wiki.phoenixlabs.org/wiki/P2B_Format (TODO)
void options_imp::processFilterFile(QString filePath){
qDebug("Processing filter files");
filtersList->clear();
QString manualFilters= misc::qBittorrentPath() + QString::fromUtf8("ipfilter.dat");
QStringList filterFiles(manualFilters);
filterFiles.append(filePath);
for(int i=0; i<2; ++i){
QFile file(filterFiles.at(i));
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));
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();
if(line.startsWith('#') || line.startsWith("//")) continue;
// Line is not commented
QList<QByteArray> partsList = line.split(',');
unsigned int nbElem = partsList.size();
if(nbElem < 2){
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
continue;
}
unsigned int nbLine = 0;
while (!file.atEnd()) {
++nbLine;
QByteArray line = file.readLine();
if(line.startsWith('#') || line.startsWith("//")) continue;
// Line is not commented
QList<QByteArray> partsList = line.split(',');
unsigned int nbElem = partsList.size();
if(nbElem < 2){
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
continue;
bool ok;
int nbAccess = partsList.at(1).trimmed().toInt(&ok);
if(!ok){
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
continue;
}
if(nbAccess <= 127){
QString strComment;
QString strStartIP = partsList.at(0).split('-').at(0).trimmed();
QString strEndIP = partsList.at(0).split('-').at(1).trimmed();
if(nbElem > 2){
strComment = partsList.at(2).trimmed();
}else{
strComment = QString();
}
bool ok;
int nbAccess = partsList.at(1).trimmed().toInt(&ok);
if(!ok){
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
continue;
}
if(nbAccess <= 127){
QString strComment;
QString strStartIP = partsList.at(0).split('-').at(0).trimmed();
QString strEndIP = partsList.at(0).split('-').at(1).trimmed();
if(nbElem > 2){
strComment = partsList.at(2).trimmed();
}else{
strComment = QString();
}
// Split IP
QRegExp is_ipv6(QString::fromUtf8("^[0-9a-f]{4}(:[0-9a-f]{4}){7}$"), Qt::CaseInsensitive, QRegExp::RegExp);
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(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());
// add it to list
QStringList item(QString::fromUtf8(start.to_string().c_str()));
item.append(QString::fromUtf8(last.to_string().c_str()));
if(!i){
item.append(QString::fromUtf8("Manual"));
}else{
item.append(QString::fromUtf8("ipfilter.dat"));
}
item.append(strComment);
new QTreeWidgetItem(filtersList, item);
// Apply to bittorrent session
filter.add_rule(start, last, ip_filter::blocked);
} else if(strStartIP.contains(is_ipv6) && strEndIP.contains(is_ipv6)) {
// 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());
// add it to list
QStringList item(QString::fromUtf8(start.to_string().c_str()));
item.append(QString::fromUtf8(last.to_string().c_str()));
if(!i){
item.append(QString::fromUtf8("Manual"));
}else{
item.append(QString::fromUtf8("ipfilter.dat"));
}
item.append(strComment);
new QTreeWidgetItem(filtersList, item);
// Apply to bittorrent session
filter.add_rule(start, last, ip_filter::blocked);
} else {
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
continue;
}
// Split IP
QRegExp is_ipv6(QString::fromUtf8("^[0-9a-f]{4}(:[0-9a-f]{4}){7}$"), Qt::CaseInsensitive, QRegExp::RegExp);
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(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 if(strStartIP.contains(is_ipv6) && strEndIP.contains(is_ipv6)) {
// 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);
} else {
qDebug("Ipfilter.dat: line %d is malformed.", nbLine);
continue;
}
}
file.close();
}
file.close();
}
}
@ -1176,100 +1128,6 @@ ip_filter options_imp::getFilter() const{
return filter;
}
// Add an IP Range to ipFilter
void options_imp::on_addFilterRangeButton_clicked(){
bool ok;
// Ask user for start ip
QString startIP = QInputDialog::getText(this, tr("Range Start IP"),
tr("Start IP:"), QLineEdit::Normal,
QString::fromUtf8("0.0.0.0"), &ok);
QStringList IP1 = startIP.split('.');
// Check IP
bool ipv4 = true;
QRegExp is_ipv6(QString::fromUtf8("^[0-9a-f]{4}(:[0-9a-f]{4}){7}$"), Qt::CaseInsensitive, QRegExp::RegExp);
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(!ok) {
return;
} else if(startIP.isEmpty()
|| (!startIP.contains(is_ipv4) && !startIP.contains(is_ipv6))){
QMessageBox::critical(0, tr("Invalid IP"), tr("This IP is invalid."));
return;
} else if(startIP.contains(is_ipv4)) {
ipv4 = true;
} else if(startIP.contains(is_ipv6)) {
ipv4 = false;
}
// Ask user for last ip
QString lastIP = QInputDialog::getText(this, tr("Range End IP"),
tr("End IP:"), QLineEdit::Normal,
startIP, &ok);
// check IP
if (!ok) {
return;
} else if(lastIP.isEmpty()
|| (!lastIP.contains(is_ipv4) && !lastIP.contains(is_ipv6))
|| (ipv4 == true && !lastIP.contains(is_ipv4))
|| (ipv4 == false && !lastIP.contains(is_ipv6))){
QMessageBox::critical(0, tr("Invalid IP"), tr("This IP is invalid."));
return;
}
// Ask user for Comment
QString comment = QInputDialog::getText(this, tr("IP Range Comment"),
tr("Comment:"), QLineEdit::Normal,
QString::fromUtf8(""), &ok);
if (!ok){
comment = QString::fromUtf8("");
return;
}
QFile ipfilter(misc::qBittorrentPath() + QString::fromUtf8("ipfilter.dat"));
if (!ipfilter.open(QIODevice::Append | QIODevice::WriteOnly | QIODevice::Text)){
std::cerr << "Error: Couldn't write in ipfilter.dat";
return;
}
QTextStream out(&ipfilter);
out << startIP << " - " << lastIP << ", 0, " << comment << "\n";
ipfilter.close();
processFilterFile(textFilterPath->text());
enableApplyButton();
}
// Delete selected IP range in list and ipfilter.dat file
// User can only delete IP added manually
void options_imp::on_delFilterRangeButton_clicked(){
bool changed = false;
QList<QTreeWidgetItem *> selectedItems = filtersList->selectedItems();
// Delete from list
for(int i=0; i<selectedItems.size(); ++i) {
QTreeWidgetItem *item = selectedItems.at(i);
if(item->text(2) == QString::fromUtf8("Manual")){
delete item;
changed = true;
}
if(changed){
enableApplyButton();
}
}
// Update ipfilter.dat
QFile ipfilter(misc::qBittorrentPath() + QString::fromUtf8("ipfilter.dat"));
if (!ipfilter.open(QIODevice::WriteOnly | QIODevice::Text)){
std::cerr << "Error: Couldn't write in ipfilter.dat";
return;
}
QTextStream out(&ipfilter);
for(int i=0; i<filtersList->topLevelItemCount();++i){
QTreeWidgetItem *item = filtersList->topLevelItem(i);
if(item->text(2) == QString::fromUtf8("Manual")){
out << item->text(0) << " - " << item->text(1) << ", 0, " << item->text(3) << "\n";
}
}
ipfilter.close();
}
// Web UI
void options_imp::enableWebUi(bool checkBoxValue){