From 9e9e6f028c03d142cdae91ef0ede81cfce035808 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sun, 15 Nov 2009 20:03:23 +0000 Subject: [PATCH] - Integrate Geoip database to speed up country resolution --- src/TransferListFiltersWidget.h | 30 +++++++++ src/bittorrent.cpp | 10 ++- src/bittorrent.h | 1 + src/geoip.h | 111 ++++++++++++++++++++++++++++++++ src/src.pro | 3 +- 5 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 src/geoip.h diff --git a/src/TransferListFiltersWidget.h b/src/TransferListFiltersWidget.h index 1337e4c0d..3e297bbf7 100644 --- a/src/TransferListFiltersWidget.h +++ b/src/TransferListFiltersWidget.h @@ -1,3 +1,33 @@ +/* + * 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. + * + * In addition, as a special exception, the copyright holders give permission to + * link this program with the OpenSSL project's "OpenSSL" library (or with + * modified versions of it that use the same license as the "OpenSSL" library), + * and distribute the linked executables. You must obey the GNU General Public + * License in all respects for all of the code used other than "OpenSSL". If you + * modify file(s), you may extend this exception to your version of the file(s), + * but you are not obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + * + * Contact : chris@qbittorrent.org + */ + #ifndef TRANSFERLISTFILTERSWIDGET_H #define TRANSFERLISTFILTERSWIDGET_H diff --git a/src/bittorrent.cpp b/src/bittorrent.cpp index 6b432336a..0cc770e15 100644 --- a/src/bittorrent.cpp +++ b/src/bittorrent.cpp @@ -40,6 +40,7 @@ #include "downloadThread.h" #include "filterParserThread.h" #include "preferences.h" +#include "geoip.h" #include "torrentPersistentData.h" #include #include @@ -58,7 +59,7 @@ enum ProxyType {HTTP=1, SOCKS5=2, HTTP_PW=3, SOCKS5_PW=4}; // Main constructor -bittorrent::bittorrent() : DHTEnabled(false), preAllocateAll(false), addInPause(false), maxConnecsPerTorrent(500), maxUploadsPerTorrent(4), ratio_limit(-1), UPnPEnabled(false), NATPMPEnabled(false), LSDEnabled(false), queueingEnabled(false) { +bittorrent::bittorrent() : DHTEnabled(false), preAllocateAll(false), addInPause(false), maxConnecsPerTorrent(500), maxUploadsPerTorrent(4), ratio_limit(-1), UPnPEnabled(false), NATPMPEnabled(false), LSDEnabled(false), queueingEnabled(false), geoipDBLoaded(false) { resolve_countries = false; // To avoid some exceptions fs::path::default_name_check(fs::no_check); @@ -264,9 +265,16 @@ void bittorrent::configureSession() { setUploadRateLimit(up_limit*1024); } // Resolve countries + qDebug("Loading country resolution settings"); bool new_resolv_countries = Preferences::resolvePeerCountries(); if(resolve_countries != new_resolv_countries) { + qDebug("in country reoslution settings"); resolve_countries = new_resolv_countries; + if(resolve_countries && !geoipDBLoaded) { + qDebug("Loading geoip database"); + GeoIP::loadDatabase(s); + geoipDBLoaded = true; + } // Update torrent handles std::vector torrents = getTorrents(); std::vector::iterator torrentIT; diff --git a/src/bittorrent.h b/src/bittorrent.h index 9c869fbfe..f6fdbc29d 100644 --- a/src/bittorrent.h +++ b/src/bittorrent.h @@ -76,6 +76,7 @@ class bittorrent : public QObject { QStringList url_skippingDlg; QHash savepath_fromurl; bool resolve_countries; + bool geoipDBLoaded; protected: QString getSavePath(QString hash); diff --git a/src/geoip.h b/src/geoip.h new file mode 100644 index 000000000..6da142615 --- /dev/null +++ b/src/geoip.h @@ -0,0 +1,111 @@ +/* + * 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. + * + * In addition, as a special exception, the copyright holders give permission to + * link this program with the OpenSSL project's "OpenSSL" library (or with + * modified versions of it that use the same license as the "OpenSSL" library), + * and distribute the linked executables. You must obey the GNU General Public + * License in all respects for all of the code used other than "OpenSSL". If you + * modify file(s), you may extend this exception to your version of the file(s), + * but you are not obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + * + * Contact : chris@qbittorrent.org + */ + +#ifndef GEOIP_H +#define GEOIP_H + +#include +#include +#include +#include +#include "misc.h" + +using namespace libtorrent; + +// TODO: Update from Internet +class GeoIP { +protected: + static QString geoipFolder(bool embedded=false) { + if(embedded) + return ":/geoip/"; + return misc::qBittorrentPath()+"geoip"+QDir::separator(); + } + + static QString geoipDBpath(bool embedded=false) { + return geoipFolder(embedded)+"GeoIP.dat"; + } + + static QString geoipVersionPath(bool embedded=false) { + return geoipFolder(embedded)+"VERSION"; + } + + static int getDBVersion(bool embedded = false) { + QFile vFile(geoipVersionPath(embedded)); + qDebug("Reading file at %s", geoipVersionPath(embedded).toLocal8Bit().data()); + if(vFile.exists() && vFile.open(QIODevice::ReadOnly | QIODevice::Text)) { + qDebug("File exists and was opened"); + QByteArray v = vFile.readAll().trimmed(); + /*while(!v.isEmpty() && v[0] == '0') { + v = v.mid(1); + }*/ + qDebug("Read version: %s", v.data()); + bool ok = false; + int version = v.toInt(&ok); + qDebug("Read version %d (Error? %d)", version, (int) !ok); + if(ok) return version; + } + return 0; + } + + static void updateLocalDb() { + if(getDBVersion(true) > getDBVersion(false)) { // Update required + qDebug("A local Geoip database update is required, proceeding..."); + // Create geoip folder is necessary + QDir gfolder(geoipFolder(false)); + if(!gfolder.exists()) { + if(!gfolder.mkpath(geoipFolder(false))) return; + } + // Remove destination files + if(QFile::exists(geoipDBpath(false))) + QFile::remove(geoipDBpath(false)); + if(QFile::exists(geoipVersionPath(false))) + QFile::remove(geoipVersionPath(false)); + // Copy from executable to hard disk + QFile::copy(geoipDBpath(true), geoipDBpath(false)); + QFile::copy(geoipVersionPath(true), geoipVersionPath(false)); + qDebug("Local Geoip database was updated"); + } + } + +public: + static void loadDatabase(session *s) { + updateLocalDb(); + if(QFile::exists(geoipDBpath(false))) { + qDebug("Loading GeoIP database from %s...", geoipDBpath(false).toLocal8Bit().data()); + if(!s->load_country_db(geoipDBpath(false).toLocal8Bit().data())) { + std::cerr << "Failed to load Geoip Database at " << geoipDBpath(false).toLocal8Bit().data() << std::endl; + } + } else { + qDebug("ERROR: Impossible to find local Geoip Database"); + } + } +}; + +#endif // GEOIP_H diff --git a/src/src.pro b/src/src.pro index cd4d7f9f2..57e79263a 100644 --- a/src/src.pro +++ b/src/src.pro @@ -190,7 +190,8 @@ HEADERS += GUI.h \ peerlistwidget.h \ peerlistdelegate.h \ reverseresolution.h \ - preferences.h + preferences.h \ + geoip.h FORMS += MainWindow.ui \ options.ui \ about.ui \