qBittorrent
Net::GeoIPManager Class Reference

#include <geoipmanager.h>

Inheritance diagram for Net::GeoIPManager:
Collaboration diagram for Net::GeoIPManager:

Public Member Functions

QString lookup (const QHostAddress &hostAddr) const
 

Static Public Member Functions

static void initInstance ()
 
static void freeInstance ()
 
static GeoIPManagerinstance ()
 
static QString CountryName (const QString &countryISOCode)
 

Private Slots

void configure ()
 
void downloadFinished (const DownloadResult &result)
 

Private Member Functions

 GeoIPManager ()
 
 ~GeoIPManager () override
 
void loadDatabase ()
 
void manageDatabaseUpdate ()
 
void downloadDatabaseFile ()
 

Private Attributes

bool m_enabled
 
GeoIPDatabasem_geoIPDatabase
 

Static Private Attributes

static GeoIPManagerm_instance = nullptr
 

Detailed Description

Definition at line 43 of file geoipmanager.h.

Constructor & Destructor Documentation

◆ GeoIPManager()

GeoIPManager::GeoIPManager ( )
private

Definition at line 56 of file geoipmanager.cpp.

57  : m_enabled(false)
58  , m_geoIPDatabase(nullptr)
59 {
60  configure();
62 }
GeoIPDatabase * m_geoIPDatabase
Definition: geoipmanager.h:70
static Preferences * instance()
void changed()

References Preferences::changed(), configure(), and Preferences::instance().

Referenced by initInstance().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~GeoIPManager()

GeoIPManager::~GeoIPManager ( )
overrideprivate

Definition at line 64 of file geoipmanager.cpp.

65 {
66  delete m_geoIPDatabase;
67 }

References m_geoIPDatabase.

Member Function Documentation

◆ configure

void GeoIPManager::configure ( )
privateslot

Definition at line 405 of file geoipmanager.cpp.

406 {
407  const bool enabled = Preferences::instance()->resolvePeerCountries();
408  if (m_enabled != enabled)
409  {
410  m_enabled = enabled;
411  if (m_enabled && !m_geoIPDatabase)
412  {
413  loadDatabase();
414  }
415  else if (!m_enabled)
416  {
417  delete m_geoIPDatabase;
418  m_geoIPDatabase = nullptr;
419  }
420  }
421 }
bool resolvePeerCountries() const

References Preferences::instance(), loadDatabase(), m_enabled, m_geoIPDatabase, and Preferences::resolvePeerCountries().

Referenced by GeoIPManager().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CountryName()

QString GeoIPManager::CountryName ( const QString &  countryISOCode)
static

Definition at line 141 of file geoipmanager.cpp.

142 {
143  static const QHash<QString, QString> countries =
144  {
145  // ISO 3166-1 alpha-2 codes
146  // http://www.iso.org/iso/home/standards/country_codes/country_names_and_code_elements_txt-temp.htm
147 
148  // Officially assigned
149  {"AD", tr("Andorra")},
150  {"AE", tr("United Arab Emirates")},
151  {"AF", tr("Afghanistan")},
152  {"AG", tr("Antigua and Barbuda")},
153  {"AI", tr("Anguilla")},
154  {"AL", tr("Albania")},
155  {"AM", tr("Armenia")},
156  {"AO", tr("Angola")},
157  {"AQ", tr("Antarctica")},
158  {"AR", tr("Argentina")},
159  {"AS", tr("American Samoa")},
160  {"AT", tr("Austria")},
161  {"AU", tr("Australia")},
162  {"AW", tr("Aruba")},
163  {"AX", tr("Aland Islands")},
164  {"AZ", tr("Azerbaijan")},
165  {"BA", tr("Bosnia and Herzegovina")},
166  {"BB", tr("Barbados")},
167  {"BD", tr("Bangladesh")},
168  {"BE", tr("Belgium")},
169  {"BF", tr("Burkina Faso")},
170  {"BG", tr("Bulgaria")},
171  {"BH", tr("Bahrain")},
172  {"BI", tr("Burundi")},
173  {"BJ", tr("Benin")},
174  {"BL", tr("Saint Barthelemy")},
175  {"BM", tr("Bermuda")},
176  {"BN", tr("Brunei Darussalam")},
177  {"BO", tr("Bolivia, Plurinational State of")},
178  {"BQ", tr("Bonaire, Sint Eustatius and Saba")},
179  {"BR", tr("Brazil")},
180  {"BS", tr("Bahamas")},
181  {"BT", tr("Bhutan")},
182  {"BV", tr("Bouvet Island")},
183  {"BW", tr("Botswana")},
184  {"BY", tr("Belarus")},
185  {"BZ", tr("Belize")},
186  {"CA", tr("Canada")},
187  {"CC", tr("Cocos (Keeling) Islands")},
188  {"CD", tr("Congo, The Democratic Republic of the")},
189  {"CF", tr("Central African Republic")},
190  {"CG", tr("Congo")},
191  {"CH", tr("Switzerland")},
192  {"CI", tr("Cote d'Ivoire")},
193  {"CK", tr("Cook Islands")},
194  {"CL", tr("Chile")},
195  {"CM", tr("Cameroon")},
196  {"CN", tr("China")},
197  {"CO", tr("Colombia")},
198  {"CR", tr("Costa Rica")},
199  {"CU", tr("Cuba")},
200  {"CV", tr("Cape Verde")},
201  {"CW", tr("Curacao")},
202  {"CX", tr("Christmas Island")},
203  {"CY", tr("Cyprus")},
204  {"CZ", tr("Czech Republic")},
205  {"DE", tr("Germany")},
206  {"DJ", tr("Djibouti")},
207  {"DK", tr("Denmark")},
208  {"DM", tr("Dominica")},
209  {"DO", tr("Dominican Republic")},
210  {"DZ", tr("Algeria")},
211  {"EC", tr("Ecuador")},
212  {"EE", tr("Estonia")},
213  {"EG", tr("Egypt")},
214  {"EH", tr("Western Sahara")},
215  {"ER", tr("Eritrea")},
216  {"ES", tr("Spain")},
217  {"ET", tr("Ethiopia")},
218  {"FI", tr("Finland")},
219  {"FJ", tr("Fiji")},
220  {"FK", tr("Falkland Islands (Malvinas)")},
221  {"FM", tr("Micronesia, Federated States of")},
222  {"FO", tr("Faroe Islands")},
223  {"FR", tr("France")},
224  {"GA", tr("Gabon")},
225  {"GB", tr("United Kingdom")},
226  {"GD", tr("Grenada")},
227  {"GE", tr("Georgia")},
228  {"GF", tr("French Guiana")},
229  {"GG", tr("Guernsey")},
230  {"GH", tr("Ghana")},
231  {"GI", tr("Gibraltar")},
232  {"GL", tr("Greenland")},
233  {"GM", tr("Gambia")},
234  {"GN", tr("Guinea")},
235  {"GP", tr("Guadeloupe")},
236  {"GQ", tr("Equatorial Guinea")},
237  {"GR", tr("Greece")},
238  {"GS", tr("South Georgia and the South Sandwich Islands")},
239  {"GT", tr("Guatemala")},
240  {"GU", tr("Guam")},
241  {"GW", tr("Guinea-Bissau")},
242  {"GY", tr("Guyana")},
243  {"HK", tr("Hong Kong")},
244  {"HM", tr("Heard Island and McDonald Islands")},
245  {"HN", tr("Honduras")},
246  {"HR", tr("Croatia")},
247  {"HT", tr("Haiti")},
248  {"HU", tr("Hungary")},
249  {"ID", tr("Indonesia")},
250  {"IE", tr("Ireland")},
251  {"IL", tr("Israel")},
252  {"IM", tr("Isle of Man")},
253  {"IN", tr("India")},
254  {"IO", tr("British Indian Ocean Territory")},
255  {"IQ", tr("Iraq")},
256  {"IR", tr("Iran, Islamic Republic of")},
257  {"IS", tr("Iceland")},
258  {"IT", tr("Italy")},
259  {"JE", tr("Jersey")},
260  {"JM", tr("Jamaica")},
261  {"JO", tr("Jordan")},
262  {"JP", tr("Japan")},
263  {"KE", tr("Kenya")},
264  {"KG", tr("Kyrgyzstan")},
265  {"KH", tr("Cambodia")},
266  {"KI", tr("Kiribati")},
267  {"KM", tr("Comoros")},
268  {"KN", tr("Saint Kitts and Nevis")},
269  {"KP", tr("Korea, Democratic People's Republic of")},
270  {"KR", tr("Korea, Republic of")},
271  {"KW", tr("Kuwait")},
272  {"KY", tr("Cayman Islands")},
273  {"KZ", tr("Kazakhstan")},
274  {"LA", tr("Lao People's Democratic Republic")},
275  {"LB", tr("Lebanon")},
276  {"LC", tr("Saint Lucia")},
277  {"LI", tr("Liechtenstein")},
278  {"LK", tr("Sri Lanka")},
279  {"LR", tr("Liberia")},
280  {"LS", tr("Lesotho")},
281  {"LT", tr("Lithuania")},
282  {"LU", tr("Luxembourg")},
283  {"LV", tr("Latvia")},
284  {"LY", tr("Libya")},
285  {"MA", tr("Morocco")},
286  {"MC", tr("Monaco")},
287  {"MD", tr("Moldova, Republic of")},
288  {"ME", tr("Montenegro")},
289  {"MF", tr("Saint Martin (French part)")},
290  {"MG", tr("Madagascar")},
291  {"MH", tr("Marshall Islands")},
292  {"MK", tr("Macedonia, The Former Yugoslav Republic of")},
293  {"ML", tr("Mali")},
294  {"MM", tr("Myanmar")},
295  {"MN", tr("Mongolia")},
296  {"MO", tr("Macao")},
297  {"MP", tr("Northern Mariana Islands")},
298  {"MQ", tr("Martinique")},
299  {"MR", tr("Mauritania")},
300  {"MS", tr("Montserrat")},
301  {"MT", tr("Malta")},
302  {"MU", tr("Mauritius")},
303  {"MV", tr("Maldives")},
304  {"MW", tr("Malawi")},
305  {"MX", tr("Mexico")},
306  {"MY", tr("Malaysia")},
307  {"MZ", tr("Mozambique")},
308  {"NA", tr("Namibia")},
309  {"NC", tr("New Caledonia")},
310  {"NE", tr("Niger")},
311  {"NF", tr("Norfolk Island")},
312  {"NG", tr("Nigeria")},
313  {"NI", tr("Nicaragua")},
314  {"NL", tr("Netherlands")},
315  {"NO", tr("Norway")},
316  {"NP", tr("Nepal")},
317  {"NR", tr("Nauru")},
318  {"NU", tr("Niue")},
319  {"NZ", tr("New Zealand")},
320  {"OM", tr("Oman")},
321  {"PA", tr("Panama")},
322  {"PE", tr("Peru")},
323  {"PF", tr("French Polynesia")},
324  {"PG", tr("Papua New Guinea")},
325  {"PH", tr("Philippines")},
326  {"PK", tr("Pakistan")},
327  {"PL", tr("Poland")},
328  {"PM", tr("Saint Pierre and Miquelon")},
329  {"PN", tr("Pitcairn")},
330  {"PR", tr("Puerto Rico")},
331  {"PS", tr("Palestine, State of")},
332  {"PT", tr("Portugal")},
333  {"PW", tr("Palau")},
334  {"PY", tr("Paraguay")},
335  {"QA", tr("Qatar")},
336  {"RE", tr("Reunion")},
337  {"RO", tr("Romania")},
338  {"RS", tr("Serbia")},
339  {"RU", tr("Russian Federation")},
340  {"RW", tr("Rwanda")},
341  {"SA", tr("Saudi Arabia")},
342  {"SB", tr("Solomon Islands")},
343  {"SC", tr("Seychelles")},
344  {"SD", tr("Sudan")},
345  {"SE", tr("Sweden")},
346  {"SG", tr("Singapore")},
347  {"SH", tr("Saint Helena, Ascension and Tristan da Cunha")},
348  {"SI", tr("Slovenia")},
349  {"SJ", tr("Svalbard and Jan Mayen")},
350  {"SK", tr("Slovakia")},
351  {"SL", tr("Sierra Leone")},
352  {"SM", tr("San Marino")},
353  {"SN", tr("Senegal")},
354  {"SO", tr("Somalia")},
355  {"SR", tr("Suriname")},
356  {"SS", tr("South Sudan")},
357  {"ST", tr("Sao Tome and Principe")},
358  {"SV", tr("El Salvador")},
359  {"SX", tr("Sint Maarten (Dutch part)")},
360  {"SY", tr("Syrian Arab Republic")},
361  {"SZ", tr("Swaziland")},
362  {"TC", tr("Turks and Caicos Islands")},
363  {"TD", tr("Chad")},
364  {"TF", tr("French Southern Territories")},
365  {"TG", tr("Togo")},
366  {"TH", tr("Thailand")},
367  {"TJ", tr("Tajikistan")},
368  {"TK", tr("Tokelau")},
369  {"TL", tr("Timor-Leste")},
370  {"TM", tr("Turkmenistan")},
371  {"TN", tr("Tunisia")},
372  {"TO", tr("Tonga")},
373  {"TR", tr("Turkey")},
374  {"TT", tr("Trinidad and Tobago")},
375  {"TV", tr("Tuvalu")},
376  {"TW", tr("Taiwan")},
377  {"TZ", tr("Tanzania, United Republic of")},
378  {"UA", tr("Ukraine")},
379  {"UG", tr("Uganda")},
380  {"UM", tr("United States Minor Outlying Islands")},
381  {"US", tr("United States")},
382  {"UY", tr("Uruguay")},
383  {"UZ", tr("Uzbekistan")},
384  {"VA", tr("Holy See (Vatican City State)")},
385  {"VC", tr("Saint Vincent and the Grenadines")},
386  {"VE", tr("Venezuela, Bolivarian Republic of")},
387  {"VG", tr("Virgin Islands, British")},
388  {"VI", tr("Virgin Islands, U.S.")},
389  {"VN", tr("Vietnam")},
390  {"VU", tr("Vanuatu")},
391  {"WF", tr("Wallis and Futuna")},
392  {"WS", tr("Samoa")},
393  {"YE", tr("Yemen")},
394  {"YT", tr("Mayotte")},
395  {"ZA", tr("South Africa")},
396  {"ZM", tr("Zambia")},
397  {"ZW", tr("Zimbabwe")},
398 
399  {{}, tr("N/A")}
400  };
401 
402  return countries.value(countryISOCode, tr("N/A"));
403 }

Referenced by SyncController::torrentPeersAction(), and PeerListWidget::updatePeer().

Here is the caller graph for this function:

◆ downloadDatabaseFile()

void GeoIPManager::downloadDatabaseFile ( )
private

Definition at line 126 of file geoipmanager.cpp.

127 {
128  const QDateTime curDatetime = QDateTime::currentDateTimeUtc();
129  const QString curUrl = DATABASE_URL.arg(QLocale::c().toString(curDatetime, "yyyy-MM"));
131 }
DownloadHandler * download(const DownloadRequest &downloadRequest)
static DownloadManager * instance()
void downloadFinished(const DownloadResult &result)
static const QString DATABASE_URL
QString toString(const lt::socket_type_t socketType)
Definition: session.cpp:183

References DATABASE_URL, Net::DownloadManager::download(), downloadFinished(), Net::DownloadManager::instance(), and anonymous_namespace{session.cpp}::toString().

Referenced by manageDatabaseUpdate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ downloadFinished

void GeoIPManager::downloadFinished ( const DownloadResult result)
privateslot

Definition at line 423 of file geoipmanager.cpp.

424 {
425  if (result.status != DownloadStatus::Success)
426  {
427  LogMsg(tr("Couldn't download IP geolocation database file. Reason: %1").arg(result.errorString), Log::WARNING);
428  return;
429  }
430 
431  bool ok = false;
432  const QByteArray data = Utils::Gzip::decompress(result.data, &ok);
433  if (!ok)
434  {
435  LogMsg(tr("Could not decompress IP geolocation database file."), Log::WARNING);
436  return;
437  }
438 
439  QString error;
440  GeoIPDatabase *geoIPDatabase = GeoIPDatabase::load(data, error);
441  if (geoIPDatabase)
442  {
443  if (!m_geoIPDatabase || (geoIPDatabase->buildEpoch() > m_geoIPDatabase->buildEpoch()))
444  {
445  delete m_geoIPDatabase;
446  m_geoIPDatabase = geoIPDatabase;
447  LogMsg(tr("IP geolocation database loaded. Type: %1. Build time: %2.")
448  .arg(m_geoIPDatabase->type(), m_geoIPDatabase->buildEpoch().toString()),
449  Log::INFO);
450  const QString targetPath = Utils::Fs::expandPathAbs(
451  QDir(specialFolderLocation(SpecialFolder::Data)).absoluteFilePath(GEODB_FOLDER));
452  if (!QDir(targetPath).exists())
453  QDir().mkpath(targetPath);
454 
455  const auto path = QString::fromLatin1("%1/%2").arg(targetPath, GEODB_FILENAME);
456  const nonstd::expected<void, QString> result = Utils::IO::saveToFile(path, data);
457  if (result)
458  {
459  LogMsg(tr("Successfully updated IP geolocation database."), Log::INFO);
460  }
461  else
462  {
463  LogMsg(tr("Couldn't save downloaded IP geolocation database file. Reason: %1")
464  .arg(result.error()), Log::WARNING);
465  }
466  }
467  else
468  {
469  delete geoIPDatabase;
470  }
471  }
472  else
473  {
474  LogMsg(tr("Couldn't load IP geolocation database. Reason: %1").arg(error), Log::WARNING);
475  }
476 }
QDateTime buildEpoch() const
static GeoIPDatabase * load(const QString &filename, QString &error)
QString type() const
static const char GEODB_FOLDER[]
static const char GEODB_FILENAME[]
void LogMsg(const QString &message, const Log::MsgType &type)
Definition: logger.cpp:125
@ WARNING
Definition: logger.h:47
@ INFO
Definition: logger.h:46
QString expandPathAbs(const QString &path)
Definition: fs.cpp:309
QByteArray decompress(const QByteArray &data, bool *ok=nullptr)
Definition: gzip.cpp:102
nonstd::expected< void, QString > saveToFile(const QString &path, const QByteArray &data)
Definition: io.cpp:69
QString specialFolderLocation(const SpecialFolder folder)
Definition: profile.cpp:131
DownloadStatus status

References GeoIPDatabase::buildEpoch(), Net::DownloadResult::data, Data, Utils::Gzip::decompress(), Net::DownloadResult::errorString, Utils::Fs::expandPathAbs(), GEODB_FILENAME, GEODB_FOLDER, Log::INFO, GeoIPDatabase::load(), LogMsg(), m_geoIPDatabase, Utils::IO::saveToFile(), specialFolderLocation(), Net::DownloadResult::status, Net::Success, GeoIPDatabase::type(), and Log::WARNING.

Referenced by downloadDatabaseFile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ freeInstance()

void GeoIPManager::freeInstance ( )
static

Definition at line 75 of file geoipmanager.cpp.

76 {
77  delete m_instance;
78  m_instance = nullptr;
79 }
static GeoIPManager * m_instance
Definition: geoipmanager.h:72

References m_instance.

Referenced by Application::cleanup().

Here is the caller graph for this function:

◆ initInstance()

void GeoIPManager::initInstance ( )
static

Definition at line 69 of file geoipmanager.cpp.

70 {
71  if (!m_instance)
73 }

References GeoIPManager(), and m_instance.

Referenced by Application::exec().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ instance()

GeoIPManager * GeoIPManager::instance ( )
static

Definition at line 81 of file geoipmanager.cpp.

82 {
83  return m_instance;
84 }

References m_instance.

Referenced by BitTorrent::PeerInfo::country().

Here is the caller graph for this function:

◆ loadDatabase()

void GeoIPManager::loadDatabase ( )
private

Definition at line 86 of file geoipmanager.cpp.

87 {
88  delete m_geoIPDatabase;
89  m_geoIPDatabase = nullptr;
90 
91  const QString filepath = Utils::Fs::expandPathAbs(
92  QString::fromLatin1("%1/%2/%3").arg(specialFolderLocation(SpecialFolder::Data), GEODB_FOLDER, GEODB_FILENAME));
93 
94  QString error;
95  m_geoIPDatabase = GeoIPDatabase::load(filepath, error);
96  if (m_geoIPDatabase)
97  Logger::instance()->addMessage(tr("IP geolocation database loaded. Type: %1. Build time: %2.")
98  .arg(m_geoIPDatabase->type(), m_geoIPDatabase->buildEpoch().toString()),
99  Log::INFO);
100  else
101  Logger::instance()->addMessage(tr("Couldn't load IP geolocation database. Reason: %1").arg(error), Log::WARNING);
102 
104 }
void addMessage(const QString &message, const Log::MsgType &type=Log::NORMAL)
Definition: logger.cpp:73
static Logger * instance()
Definition: logger.cpp:56

References Logger::addMessage(), GeoIPDatabase::buildEpoch(), Data, Utils::Fs::expandPathAbs(), GEODB_FILENAME, GEODB_FOLDER, Log::INFO, Logger::instance(), GeoIPDatabase::load(), m_geoIPDatabase, manageDatabaseUpdate(), specialFolderLocation(), GeoIPDatabase::type(), and Log::WARNING.

Referenced by configure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lookup()

QString GeoIPManager::lookup ( const QHostAddress &  hostAddr) const

Definition at line 133 of file geoipmanager.cpp.

134 {
135  if (m_enabled && m_geoIPDatabase)
136  return m_geoIPDatabase->lookup(hostAddr);
137 
138  return {};
139 }
QString lookup(const QHostAddress &hostAddr) const

References GeoIPDatabase::lookup(), m_enabled, and m_geoIPDatabase.

Referenced by BitTorrent::PeerInfo::country().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ manageDatabaseUpdate()

void GeoIPManager::manageDatabaseUpdate ( )
private

Definition at line 106 of file geoipmanager.cpp.

107 {
108  const auto expired = [](const QDateTime &testDateTime)
109  {
110  const QDate testDate = testDateTime.date();
111  const QDate curDate = QDateTime::currentDateTimeUtc().date();
112 
113  if ((testDate.year() < curDate.year()) && (curDate.day() > 1))
114  return true;
115 
116  if ((testDate.month() < curDate.month()) && (curDate.day() > 1))
117  return true;
118 
119  return false;
120  };
121 
122  if (!m_geoIPDatabase || expired(m_geoIPDatabase->buildEpoch()))
124 }

References GeoIPDatabase::buildEpoch(), downloadDatabaseFile(), and m_geoIPDatabase.

Referenced by loadDatabase().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_enabled

bool Net::GeoIPManager::m_enabled
private

Definition at line 69 of file geoipmanager.h.

Referenced by configure(), and lookup().

◆ m_geoIPDatabase

GeoIPDatabase* Net::GeoIPManager::m_geoIPDatabase
private

◆ m_instance

GeoIPManager * GeoIPManager::m_instance = nullptr
staticprivate

Definition at line 72 of file geoipmanager.h.

Referenced by freeInstance(), initInstance(), and instance().


The documentation for this class was generated from the following files: