Verified Commit 6c1ae6b2 authored by Alexander Lohnau's avatar Alexander Lohnau 💬
Browse files

Fix broken GeoIp plugin

Instead of the broken custom logic use only the system provided lib.

BUG: 403054
FIXED-IN: 21.08
parent ce382c22
# Add an option to compile & link against system GeoIP (off by default)
option(WITH_SYSTEM_GEOIP
# Add an option to compile & link against system GeoIP
option(BUILD_WITH_GEOIP
"Link InfoWidget plugin against system GeoIP library and use system-wide default GeoIP Country database"
OFF)
ON)
if (WITH_SYSTEM_GEOIP)
if (BUILD_WITH_GEOIP)
find_path(GEOIP_INCLUDE_DIR NAMES GeoIP.h PATHS / ${INCLUDE_INSTALL_DIR}/)
find_library(GEOIP_LIBRARY NAMES GeoIP PATHS ${KDE4_LIB_DIR} ${LIB_INSTALL_DIR})
find_library(GEOIP_LIBRARY NAMES GeoIP PATHS ${LIB_INSTALL_DIR})
if (NOT GEOIP_INCLUDE_DIR OR NOT GEOIP_LIBRARY)
set(WITH_SYSTEM_GEOIP OFF CACHE BOOL
set(BUILD_WITH_GEOIP OFF CACHE BOOL
"GeoIP development files could not be found on this system. Forcing this option to OFF"
FORCE)
message(SEND_ERROR
"GeoIP library development files could not be found on your system. Embedding a local copy of GeoIP instead")
endif(NOT GEOIP_INCLUDE_DIR OR NOT GEOIP_LIBRARY)
endif(WITH_SYSTEM_GEOIP)
if (WITH_SYSTEM_GEOIP)
message(STATUS " Linking InfoWidget against system GeoIP library")
add_definitions( -DUSE_SYSTEM_GEOIP )
include_directories(GEOIP_INCLUDE_DIR)
set(geoip_link ${GEOIP_LIBRARY})
else(WITH_SYSTEM_GEOIP)
message(STATUS " Compiling GeoIP support directly into InfoWidget plugin")
set(geoip_src GeoIP.c)
endif(WITH_SYSTEM_GEOIP)
message(WARNING "GeoIP library development files could not be found on your system.")
else()
message(STATUS " Linking InfoWidget against system GeoIP library")
include_directories(GEOIP_INCLUDE_DIR)
set(geoip_link ${GEOIP_LIBRARY})
endif()
endif(BUILD_WITH_GEOIP)
set(ktinfowidgetplugin_SRC
infowidgetplugin.cpp
......@@ -41,14 +34,19 @@ set(ktinfowidgetplugin_SRC
trackerview.cpp
trackermodel.cpp
addtrackersdialog.cpp
${geoip_src}
geoipmanager.cpp
flagdb.cpp
iwfiletreemodel.cpp
iwfilelistmodel.cpp
webseedstab.cpp
webseedsmodel.cpp)
if (BUILD_WITH_GEOIP)
set(ktinfowidgetplugin_SRC ${ktinfowidgetplugin_SRC} geoipmanager.cpp)
add_definitions(-DBUILD_WITH_GEOIP=1)
else()
add_definitions(-DBUILD_WITH_GEOIP=0)
endif()
ki18n_wrap_ui(ktinfowidgetplugin_SRC iwprefpage.ui statustab.ui chunkdownloadview.ui trackerview.ui webseedstab.ui)
kconfig_add_kcfg_files(ktinfowidgetplugin_SRC infowidgetpluginsettings.kcfgc)
add_library(ktorrent_infowidget MODULE ${ktinfowidgetplugin_SRC})
......
This diff is collapsed.
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
/* GeoIP.h
SPDX-FileCopyrightText: 2003 MaxMind LLC
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef GEOIP_H
#define GEOIP_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h> /* for fstat */
#include <sys/types.h> /* for fstat */
#define SEGMENT_RECORD_LENGTH 3
#define STANDARD_RECORD_LENGTH 3
#define ORG_RECORD_LENGTH 4
#define MAX_RECORD_LENGTH 4
#define NUM_DB_TYPES 16
typedef struct GeoIPTag {
FILE *GeoIPDatabase;
char *file_path;
unsigned char *cache;
unsigned char *index_cache;
unsigned int *databaseSegments;
char databaseType;
time_t mtime;
int flags;
char record_length;
int record_iter; /* used in GeoIP_next_record */
} GeoIP;
typedef struct GeoIPRegionTag {
char country_code[3];
char region[3];
} GeoIPRegion;
typedef enum {
GEOIP_STANDARD = 0,
GEOIP_MEMORY_CACHE = 1,
GEOIP_CHECK_CACHE = 2,
GEOIP_INDEX_CACHE = 4,
} GeoIPOptions;
typedef enum {
GEOIP_COUNTRY_EDITION = 1,
GEOIP_REGION_EDITION_REV0 = 7,
GEOIP_CITY_EDITION_REV0 = 6,
GEOIP_ORG_EDITION = 5,
GEOIP_ISP_EDITION = 4,
GEOIP_CITY_EDITION_REV1 = 2,
GEOIP_REGION_EDITION_REV1 = 3,
GEOIP_PROXY_EDITION = 8,
GEOIP_ASNUM_EDITION = 9,
GEOIP_NETSPEED_EDITION = 10
} GeoIPDBTypes;
typedef enum {
GEOIP_ANON_PROXY = 1,
GEOIP_HTTP_X_FORWARDED_FOR_PROXY = 2,
GEOIP_HTTP_CLIENT_IP_PROXY = 3,
} GeoIPProxyTypes;
typedef enum {
GEOIP_UNKNOWN_SPEED = 0,
GEOIP_DIALUP_SPEED = 1,
GEOIP_CABLEDSL_SPEED = 2,
GEOIP_CORPORATE_SPEED = 3,
} GeoIPNetspeedValues;
extern char **GeoIPDBFileName;
extern const char *GeoIPDBDescription[NUM_DB_TYPES];
extern const char *GeoIPCountryDBFileName;
extern const char *GeoIPRegionDBFileName;
extern const char *GeoIPCityDBFileName;
extern const char *GeoIPOrgDBFileName;
extern const char *GeoIPISPDBFileName;
extern const char GeoIP_country_code[247][3];
extern const char GeoIP_country_code3[247][4];
extern const char *GeoIP_country_name[247];
extern const char GeoIP_country_continent[247][3];
#ifdef DLL
#define GEOIP_API __declspec(dllexport)
#else
#define GEOIP_API
#endif /* DLL */
GEOIP_API GeoIP *GeoIP_open_type(int type, int flags);
GEOIP_API GeoIP *GeoIP_new(int flags);
GEOIP_API GeoIP *GeoIP_open(const char *filename, int flags);
GEOIP_API int GeoIP_db_avail(int type);
GEOIP_API void GeoIP_delete(GeoIP *gi);
GEOIP_API const char *GeoIP_country_code_by_addr(GeoIP *gi, const char *addr);
GEOIP_API const char *GeoIP_country_code_by_name(GeoIP *gi, const char *host);
GEOIP_API const char *GeoIP_country_code3_by_addr(GeoIP *gi, const char *addr);
GEOIP_API const char *GeoIP_country_code3_by_name(GeoIP *gi, const char *host);
GEOIP_API const char *GeoIP_country_name_by_addr(GeoIP *gi, const char *addr);
GEOIP_API const char *GeoIP_country_name_by_name(GeoIP *gi, const char *host);
GEOIP_API const char *GeoIP_country_name_by_ipnum(GeoIP *gi, unsigned long ipnum);
GEOIP_API const char *GeoIP_country_code_by_ipnum(GeoIP *gi, unsigned long ipnum);
GEOIP_API const char *GeoIP_country_code3_by_ipnum(GeoIP *gi, unsigned long ipnum);
/* Deprecated - for backwards compatibility only */
GEOIP_API int GeoIP_country_id_by_addr(GeoIP *gi, const char *addr);
GEOIP_API int GeoIP_country_id_by_name(GeoIP *gi, const char *host);
GEOIP_API char *GeoIP_org_by_addr(GeoIP *gi, const char *addr);
GEOIP_API char *GeoIP_org_by_name(GeoIP *gi, const char *host);
/* End deprecated */
GEOIP_API int GeoIP_id_by_addr(GeoIP *gi, const char *addr);
GEOIP_API int GeoIP_id_by_name(GeoIP *gi, const char *host);
GEOIP_API int GeoIP_id_by_ipnum(GeoIP *gi, unsigned long ipnum);
GEOIP_API GeoIPRegion *GeoIP_region_by_addr(GeoIP *gi, const char *addr);
GEOIP_API GeoIPRegion *GeoIP_region_by_name(GeoIP *gi, const char *host);
GEOIP_API GeoIPRegion *GeoIP_region_by_ipnum(GeoIP *gi, unsigned long ipnum);
/* Warning - don't call this after GeoIP_assign_region_by_inetaddr calls */
GEOIP_API void GeoIPRegion_delete(GeoIPRegion *gir);
GEOIP_API void GeoIP_assign_region_by_inetaddr(GeoIP *gi, unsigned long inetaddr, GeoIPRegion *gir);
/* Used to query GeoIP Organization, ISP and AS Number databases */
GEOIP_API char *GeoIP_name_by_ipnum(GeoIP *gi, unsigned long ipnum);
GEOIP_API char *GeoIP_name_by_addr(GeoIP *gi, const char *addr);
GEOIP_API char *GeoIP_name_by_name(GeoIP *gi, const char *host);
GEOIP_API char *GeoIP_database_info(GeoIP *gi);
GEOIP_API unsigned char GeoIP_database_edition(GeoIP *gi);
#ifdef BSD
#define memcpy(dest, src, n) bcopy(src, dest, n)
#endif
#ifdef __cplusplus
}
#endif
#endif /* GEOIP_H */
......@@ -20,47 +20,16 @@ using namespace bt;
namespace kt
{
QUrl GeoIPManager::geoip_url = QUrl(QStringLiteral("http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz"));
GeoIPManager::GeoIPManager(QObject *parent)
: QObject(parent)
, geo_ip(nullptr)
, decompress_thread(nullptr)
{
#ifdef USE_SYSTEM_GEOIP
geo_ip = GeoIP_open_type(GEOIP_COUNTRY_EDITION, GEOIP_STANDARD);
#else
geoip_data_file = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("geoip.dat"));
if (geoip_data_file.isEmpty())
geoip_data_file = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("GeoIP.dat"));
if (geoip_data_file.isEmpty()) {
downloadDataBase();
} else {
geo_ip = GeoIP_open(QFile::encodeName(geoip_data_file).data(), 0);
if (geo_ip) {
QFileInfo fi(geoip_data_file);
QDateTime now = QDateTime::currentDateTime();
if (fi.lastModified().daysTo(now) >= 30) {
// if the last time the geoip file was modified, was more then
// 30 days ago, redownload it
downloadDataBase();
}
}
}
#endif
}
GeoIPManager::~GeoIPManager()
{
if (geo_ip)
GeoIP_delete(geo_ip);
if (decompress_thread) {
decompress_thread->cancel();
decompress_thread->wait();
delete decompress_thread;
}
}
int GeoIPManager::findCountry(const QString &addr)
......@@ -87,66 +56,4 @@ QString GeoIPManager::countryName(int country_id)
return QString();
}
void GeoIPManager::downloadDataBase()
{
#ifndef USE_SYSTEM_GEOIP
Out(SYS_INW | LOG_NOTICE) << "Downloading GeoIP database: " << geoip_url << endl;
download_destination = kt::DataDir(CreateIfNotExists) + geoip_url.fileName();
KIO::CopyJob *job = KIO::copy(geoip_url, QUrl::fromLocalFile(download_destination), KIO::Overwrite | KIO::HideProgressInfo);
connect(job, &KIO::CopyJob::result, this, &GeoIPManager::databaseDownloadFinished);
#endif
}
void GeoIPManager::databaseDownloadFinished(KJob *job)
{
if (job->error()) {
Out(SYS_INW | LOG_IMPORTANT) << "Failed to download GeoIP database: " << job->errorString() << endl;
return;
}
if (download_destination.endsWith(QLatin1String(".dat")) || download_destination.endsWith(QLatin1String(".DAT"))) {
Out(SYS_INW | LOG_NOTICE) << "GeoIP database downloaded, opening ... " << endl;
geoip_data_file = download_destination;
if (geo_ip) {
GeoIP_delete(geo_ip);
geo_ip = nullptr;
}
geo_ip = GeoIP_open(QFile::encodeName(geoip_data_file).data(), 0);
if (!geo_ip)
Out(SYS_INW | LOG_NOTICE) << "Failed to open GeoIP database " << endl;
} else {
Out(SYS_INW | LOG_NOTICE) << "GeoIP database downloaded, decompressing ... " << endl;
// decompress the file
decompress_thread = new bt::DecompressThread(download_destination, kt::DataDir() + QLatin1String("geoip.dat"));
connect(decompress_thread, &bt::DecompressThread::finished, this, &GeoIPManager::decompressFinished, Qt::QueuedConnection);
decompress_thread->start(QThread::IdlePriority);
}
}
void GeoIPManager::decompressFinished()
{
Out(SYS_INW | LOG_NOTICE) << "GeoIP database decompressed, opening ... " << endl;
if (!decompress_thread->error()) {
geoip_data_file = kt::DataDir() + QLatin1String("geoip.dat");
if (geo_ip) {
GeoIP_delete(geo_ip);
geo_ip = nullptr;
}
geo_ip = GeoIP_open(QFile::encodeName(geoip_data_file).data(), 0);
if (!geo_ip)
Out(SYS_INW | LOG_NOTICE) << "Failed to open GeoIP database " << endl;
}
decompress_thread->wait();
delete decompress_thread;
decompress_thread = nullptr;
}
void GeoIPManager::setGeoIPUrl(const QUrl &url)
{
geoip_url = url;
}
///////////////////////////////////
}
......@@ -7,21 +7,7 @@
#define KT_GEOIPMANAGER_H
#include <QObject>
#include <QThread>
#include <QUrl>
#ifdef USE_SYSTEM_GEOIP
#include <GeoIP.h>
#else
#include "GeoIP.h"
#endif
class KJob;
namespace bt
{
class DecompressThread;
}
namespace kt
{
......@@ -56,28 +42,8 @@ public:
*/
QString countryCode(int country_id);
/// Get the database URL
static QUrl geoIPUrl()
{
return geoip_url;
}
/// Set the database URL
static void setGeoIPUrl(const QUrl &url);
/// Download the database
void downloadDataBase();
private Q_SLOTS:
void databaseDownloadFinished(KJob *job);
void decompressFinished();
private:
GeoIP *geo_ip;
QString geoip_data_file;
QString download_destination;
bt::DecompressThread *decompress_thread;
static QUrl geoip_url;
};
}
......
......@@ -18,7 +18,6 @@
#include "chunkdownloadview.h"
#include "fileview.h"
#include "geoipmanager.h"
#include "infowidgetpluginsettings.h"
#include "iwprefpage.h"
#include "monitor.h"
......
......@@ -13,10 +13,13 @@
#include <KLocalizedString>
#include "flagdb.h"
#include "geoipmanager.h"
#include <interfaces/torrentinterface.h>
#include <util/functions.h>
#if BUILD_WITH_GEOIP
#include "geoipmanager.h"
#endif
using namespace bt;
namespace kt
......@@ -25,7 +28,12 @@ static QIcon yes, no;
static bool icons_loaded = false;
static FlagDB flagDB(22, 18);
PeerViewModel::Item::Item(bt::PeerInterface *peer, GeoIPManager *geo_ip)
PeerViewModel::Item::Item(bt::PeerInterface *peer
#if BUILD_WITH_GEOIP
,
GeoIPManager *geo_ip
#endif
)
: peer(peer)
{
stats = peer->getStats();
......@@ -39,13 +47,13 @@ PeerViewModel::Item::Item(bt::PeerInterface *peer, GeoIPManager *geo_ip)
flagDB.addFlagSource(path + QStringLiteral("/%1/flag.png"));
}
if (geo_ip) {
int country_id = geo_ip->findCountry(stats.ip_address);
if (country_id > 0) {
country = geo_ip->countryName(country_id);
flag = flagDB.getFlag(geo_ip->countryCode(country_id));
}
#if BUILD_WITH_GEOIP
int country_id = geo_ip->findCountry(stats.ip_address);
if (country_id > 0) {
country = geo_ip->countryName(country_id);
flag = flagDB.getFlag(geo_ip->countryCode(country_id));
}
#endif
}
bool PeerViewModel::Item::changed() const
......@@ -183,9 +191,10 @@ QVariant PeerViewModel::Item::decoration(int col) const
PeerViewModel::PeerViewModel(QObject *parent)
: QAbstractTableModel(parent)
, geo_ip(nullptr)
{
#if BUILD_WITH_GEOIP
geo_ip = new GeoIPManager(this);
#endif
}
PeerViewModel::~PeerViewModel()
......@@ -195,7 +204,12 @@ PeerViewModel::~PeerViewModel()
void PeerViewModel::peerAdded(bt::PeerInterface *peer)
{
items.append(new Item(peer, geo_ip));
items.append(new Item(peer
#if BUILD_WITH_GEOIP
,
geo_ip
#endif
));
insertRow(items.count() - 1);
}
......
......@@ -61,7 +61,12 @@ public:
QString country;
QIcon flag;
Item(bt::PeerInterface *peer, GeoIPManager *geo_ip);
Item(bt::PeerInterface *peer
#if BUILD_WITH_GEOIP
,
GeoIPManager *geo_ip
#endif
);
bool changed() const;
QVariant data(int col) const;
......@@ -71,7 +76,9 @@ public:
private:
QVector<Item *> items;
GeoIPManager *geo_ip;
#if BUILD_WITH_GEOIP
GeoIPManager *geo_ip = nullptr;
#endif
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment