2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "geoipmanager.h"
33 #include <QHostAddress>
36 #include "base/global.h"
37 #include "base/logger.h"
38 #include "base/preferences.h"
39 #include "base/profile.h"
40 #include "base/utils/fs.h"
41 #include "base/utils/gzip.h"
42 #include "base/utils/io.h"
43 #include "downloadmanager.h"
44 #include "geoipdatabase.h"
46 const QString DATABASE_URL
= u
"https://download.db-ip.com/free/dbip-country-lite-%1.mmdb.gz"_s
;
47 const QString GEODB_FOLDER
= u
"GeoDB"_s
;
48 const QString GEODB_FILENAME
= u
"dbip-country-lite.mmdb"_s
;
54 GeoIPManager
*GeoIPManager::m_instance
= nullptr;
56 GeoIPManager::GeoIPManager()
59 connect(Preferences::instance(), &Preferences::changed
, this, &GeoIPManager::configure
);
62 GeoIPManager::~GeoIPManager()
64 delete m_geoIPDatabase
;
67 void GeoIPManager::initInstance()
70 m_instance
= new GeoIPManager
;
73 void GeoIPManager::freeInstance()
79 GeoIPManager
*GeoIPManager::instance()
84 void GeoIPManager::loadDatabase()
86 delete m_geoIPDatabase
;
87 m_geoIPDatabase
= nullptr;
89 const Path filepath
= specialFolderLocation(SpecialFolder::Data
)
90 / Path(GEODB_FOLDER
) / Path(GEODB_FILENAME
);
93 m_geoIPDatabase
= GeoIPDatabase::load(filepath
, error
);
96 LogMsg(tr("IP geolocation database loaded. Type: %1. Build time: %2.")
97 .arg(m_geoIPDatabase
->type(), m_geoIPDatabase
->buildEpoch().toString()),
102 LogMsg(tr("Couldn't load IP geolocation database. Reason: %1").arg(error
), Log::WARNING
);
105 manageDatabaseUpdate();
108 void GeoIPManager::manageDatabaseUpdate()
110 const auto expired
= [](const QDateTime
&testDateTime
)
112 const QDate testDate
= testDateTime
.date();
113 const QDate curDate
= QDateTime::currentDateTimeUtc().date();
115 if ((testDate
.year() < curDate
.year()) && (curDate
.day() > 1))
118 if ((testDate
.month() < curDate
.month()) && (curDate
.day() > 1))
124 if (!m_geoIPDatabase
|| expired(m_geoIPDatabase
->buildEpoch()))
125 downloadDatabaseFile();
128 void GeoIPManager::downloadDatabaseFile()
130 const QDateTime curDatetime
= QDateTime::currentDateTimeUtc();
131 const QString curUrl
= DATABASE_URL
.arg(QLocale::c().toString(curDatetime
, u
"yyyy-MM"));
132 DownloadManager::instance()->download(
133 {curUrl
}, Preferences::instance()->useProxyForGeneralPurposes()
134 , this, &GeoIPManager::downloadFinished
);
137 QString
GeoIPManager::lookup(const QHostAddress
&hostAddr
) const
139 if (m_enabled
&& m_geoIPDatabase
)
140 return m_geoIPDatabase
->lookup(hostAddr
);
145 QString
GeoIPManager::CountryName(const QString
&countryISOCode
)
147 static const QHash
<QString
, QString
> countries
=
149 // ISO 3166-1 alpha-2 codes
150 // http://www.iso.org/iso/home/standards/country_codes/country_names_and_code_elements_txt-temp.htm
152 // Officially assigned
153 {u
"AD"_s
, tr("Andorra")},
154 {u
"AE"_s
, tr("United Arab Emirates")},
155 {u
"AF"_s
, tr("Afghanistan")},
156 {u
"AG"_s
, tr("Antigua and Barbuda")},
157 {u
"AI"_s
, tr("Anguilla")},
158 {u
"AL"_s
, tr("Albania")},
159 {u
"AM"_s
, tr("Armenia")},
160 {u
"AO"_s
, tr("Angola")},
161 {u
"AQ"_s
, tr("Antarctica")},
162 {u
"AR"_s
, tr("Argentina")},
163 {u
"AS"_s
, tr("American Samoa")},
164 {u
"AT"_s
, tr("Austria")},
165 {u
"AU"_s
, tr("Australia")},
166 {u
"AW"_s
, tr("Aruba")},
167 {u
"AX"_s
, tr("Aland Islands")},
168 {u
"AZ"_s
, tr("Azerbaijan")},
169 {u
"BA"_s
, tr("Bosnia and Herzegovina")},
170 {u
"BB"_s
, tr("Barbados")},
171 {u
"BD"_s
, tr("Bangladesh")},
172 {u
"BE"_s
, tr("Belgium")},
173 {u
"BF"_s
, tr("Burkina Faso")},
174 {u
"BG"_s
, tr("Bulgaria")},
175 {u
"BH"_s
, tr("Bahrain")},
176 {u
"BI"_s
, tr("Burundi")},
177 {u
"BJ"_s
, tr("Benin")},
178 {u
"BL"_s
, tr("Saint Barthelemy")},
179 {u
"BM"_s
, tr("Bermuda")},
180 {u
"BN"_s
, tr("Brunei Darussalam")},
181 {u
"BO"_s
, tr("Bolivia, Plurinational State of")},
182 {u
"BQ"_s
, tr("Bonaire, Sint Eustatius and Saba")},
183 {u
"BR"_s
, tr("Brazil")},
184 {u
"BS"_s
, tr("Bahamas")},
185 {u
"BT"_s
, tr("Bhutan")},
186 {u
"BV"_s
, tr("Bouvet Island")},
187 {u
"BW"_s
, tr("Botswana")},
188 {u
"BY"_s
, tr("Belarus")},
189 {u
"BZ"_s
, tr("Belize")},
190 {u
"CA"_s
, tr("Canada")},
191 {u
"CC"_s
, tr("Cocos (Keeling) Islands")},
192 {u
"CD"_s
, tr("Congo, The Democratic Republic of the")},
193 {u
"CF"_s
, tr("Central African Republic")},
194 {u
"CG"_s
, tr("Congo")},
195 {u
"CH"_s
, tr("Switzerland")},
196 {u
"CI"_s
, tr("Cote d'Ivoire")},
197 {u
"CK"_s
, tr("Cook Islands")},
198 {u
"CL"_s
, tr("Chile")},
199 {u
"CM"_s
, tr("Cameroon")},
200 {u
"CN"_s
, tr("China")},
201 {u
"CO"_s
, tr("Colombia")},
202 {u
"CR"_s
, tr("Costa Rica")},
203 {u
"CU"_s
, tr("Cuba")},
204 {u
"CV"_s
, tr("Cape Verde")},
205 {u
"CW"_s
, tr("Curacao")},
206 {u
"CX"_s
, tr("Christmas Island")},
207 {u
"CY"_s
, tr("Cyprus")},
208 {u
"CZ"_s
, tr("Czech Republic")},
209 {u
"DE"_s
, tr("Germany")},
210 {u
"DJ"_s
, tr("Djibouti")},
211 {u
"DK"_s
, tr("Denmark")},
212 {u
"DM"_s
, tr("Dominica")},
213 {u
"DO"_s
, tr("Dominican Republic")},
214 {u
"DZ"_s
, tr("Algeria")},
215 {u
"EC"_s
, tr("Ecuador")},
216 {u
"EE"_s
, tr("Estonia")},
217 {u
"EG"_s
, tr("Egypt")},
218 {u
"EH"_s
, tr("Western Sahara")},
219 {u
"ER"_s
, tr("Eritrea")},
220 {u
"ES"_s
, tr("Spain")},
221 {u
"ET"_s
, tr("Ethiopia")},
222 {u
"FI"_s
, tr("Finland")},
223 {u
"FJ"_s
, tr("Fiji")},
224 {u
"FK"_s
, tr("Falkland Islands (Malvinas)")},
225 {u
"FM"_s
, tr("Micronesia, Federated States of")},
226 {u
"FO"_s
, tr("Faroe Islands")},
227 {u
"FR"_s
, tr("France")},
228 {u
"GA"_s
, tr("Gabon")},
229 {u
"GB"_s
, tr("United Kingdom")},
230 {u
"GD"_s
, tr("Grenada")},
231 {u
"GE"_s
, tr("Georgia")},
232 {u
"GF"_s
, tr("French Guiana")},
233 {u
"GG"_s
, tr("Guernsey")},
234 {u
"GH"_s
, tr("Ghana")},
235 {u
"GI"_s
, tr("Gibraltar")},
236 {u
"GL"_s
, tr("Greenland")},
237 {u
"GM"_s
, tr("Gambia")},
238 {u
"GN"_s
, tr("Guinea")},
239 {u
"GP"_s
, tr("Guadeloupe")},
240 {u
"GQ"_s
, tr("Equatorial Guinea")},
241 {u
"GR"_s
, tr("Greece")},
242 {u
"GS"_s
, tr("South Georgia and the South Sandwich Islands")},
243 {u
"GT"_s
, tr("Guatemala")},
244 {u
"GU"_s
, tr("Guam")},
245 {u
"GW"_s
, tr("Guinea-Bissau")},
246 {u
"GY"_s
, tr("Guyana")},
247 {u
"HK"_s
, tr("Hong Kong")},
248 {u
"HM"_s
, tr("Heard Island and McDonald Islands")},
249 {u
"HN"_s
, tr("Honduras")},
250 {u
"HR"_s
, tr("Croatia")},
251 {u
"HT"_s
, tr("Haiti")},
252 {u
"HU"_s
, tr("Hungary")},
253 {u
"ID"_s
, tr("Indonesia")},
254 {u
"IE"_s
, tr("Ireland")},
255 {u
"IL"_s
, tr("Israel")},
256 {u
"IM"_s
, tr("Isle of Man")},
257 {u
"IN"_s
, tr("India")},
258 {u
"IO"_s
, tr("British Indian Ocean Territory")},
259 {u
"IQ"_s
, tr("Iraq")},
260 {u
"IR"_s
, tr("Iran, Islamic Republic of")},
261 {u
"IS"_s
, tr("Iceland")},
262 {u
"IT"_s
, tr("Italy")},
263 {u
"JE"_s
, tr("Jersey")},
264 {u
"JM"_s
, tr("Jamaica")},
265 {u
"JO"_s
, tr("Jordan")},
266 {u
"JP"_s
, tr("Japan")},
267 {u
"KE"_s
, tr("Kenya")},
268 {u
"KG"_s
, tr("Kyrgyzstan")},
269 {u
"KH"_s
, tr("Cambodia")},
270 {u
"KI"_s
, tr("Kiribati")},
271 {u
"KM"_s
, tr("Comoros")},
272 {u
"KN"_s
, tr("Saint Kitts and Nevis")},
273 {u
"KP"_s
, tr("Korea, Democratic People's Republic of")},
274 {u
"KR"_s
, tr("Korea, Republic of")},
275 {u
"KW"_s
, tr("Kuwait")},
276 {u
"KY"_s
, tr("Cayman Islands")},
277 {u
"KZ"_s
, tr("Kazakhstan")},
278 {u
"LA"_s
, tr("Lao People's Democratic Republic")},
279 {u
"LB"_s
, tr("Lebanon")},
280 {u
"LC"_s
, tr("Saint Lucia")},
281 {u
"LI"_s
, tr("Liechtenstein")},
282 {u
"LK"_s
, tr("Sri Lanka")},
283 {u
"LR"_s
, tr("Liberia")},
284 {u
"LS"_s
, tr("Lesotho")},
285 {u
"LT"_s
, tr("Lithuania")},
286 {u
"LU"_s
, tr("Luxembourg")},
287 {u
"LV"_s
, tr("Latvia")},
288 {u
"LY"_s
, tr("Libya")},
289 {u
"MA"_s
, tr("Morocco")},
290 {u
"MC"_s
, tr("Monaco")},
291 {u
"MD"_s
, tr("Moldova, Republic of")},
292 {u
"ME"_s
, tr("Montenegro")},
293 {u
"MF"_s
, tr("Saint Martin (French part)")},
294 {u
"MG"_s
, tr("Madagascar")},
295 {u
"MH"_s
, tr("Marshall Islands")},
296 {u
"MK"_s
, tr("Macedonia, The Former Yugoslav Republic of")},
297 {u
"ML"_s
, tr("Mali")},
298 {u
"MM"_s
, tr("Myanmar")},
299 {u
"MN"_s
, tr("Mongolia")},
300 {u
"MO"_s
, tr("Macao")},
301 {u
"MP"_s
, tr("Northern Mariana Islands")},
302 {u
"MQ"_s
, tr("Martinique")},
303 {u
"MR"_s
, tr("Mauritania")},
304 {u
"MS"_s
, tr("Montserrat")},
305 {u
"MT"_s
, tr("Malta")},
306 {u
"MU"_s
, tr("Mauritius")},
307 {u
"MV"_s
, tr("Maldives")},
308 {u
"MW"_s
, tr("Malawi")},
309 {u
"MX"_s
, tr("Mexico")},
310 {u
"MY"_s
, tr("Malaysia")},
311 {u
"MZ"_s
, tr("Mozambique")},
312 {u
"NA"_s
, tr("Namibia")},
313 {u
"NC"_s
, tr("New Caledonia")},
314 {u
"NE"_s
, tr("Niger")},
315 {u
"NF"_s
, tr("Norfolk Island")},
316 {u
"NG"_s
, tr("Nigeria")},
317 {u
"NI"_s
, tr("Nicaragua")},
318 {u
"NL"_s
, tr("Netherlands")},
319 {u
"NO"_s
, tr("Norway")},
320 {u
"NP"_s
, tr("Nepal")},
321 {u
"NR"_s
, tr("Nauru")},
322 {u
"NU"_s
, tr("Niue")},
323 {u
"NZ"_s
, tr("New Zealand")},
324 {u
"OM"_s
, tr("Oman")},
325 {u
"PA"_s
, tr("Panama")},
326 {u
"PE"_s
, tr("Peru")},
327 {u
"PF"_s
, tr("French Polynesia")},
328 {u
"PG"_s
, tr("Papua New Guinea")},
329 {u
"PH"_s
, tr("Philippines")},
330 {u
"PK"_s
, tr("Pakistan")},
331 {u
"PL"_s
, tr("Poland")},
332 {u
"PM"_s
, tr("Saint Pierre and Miquelon")},
333 {u
"PN"_s
, tr("Pitcairn")},
334 {u
"PR"_s
, tr("Puerto Rico")},
335 {u
"PS"_s
, tr("Palestine, State of")},
336 {u
"PT"_s
, tr("Portugal")},
337 {u
"PW"_s
, tr("Palau")},
338 {u
"PY"_s
, tr("Paraguay")},
339 {u
"QA"_s
, tr("Qatar")},
340 {u
"RE"_s
, tr("Reunion")},
341 {u
"RO"_s
, tr("Romania")},
342 {u
"RS"_s
, tr("Serbia")},
343 {u
"RU"_s
, tr("Russian Federation")},
344 {u
"RW"_s
, tr("Rwanda")},
345 {u
"SA"_s
, tr("Saudi Arabia")},
346 {u
"SB"_s
, tr("Solomon Islands")},
347 {u
"SC"_s
, tr("Seychelles")},
348 {u
"SD"_s
, tr("Sudan")},
349 {u
"SE"_s
, tr("Sweden")},
350 {u
"SG"_s
, tr("Singapore")},
351 {u
"SH"_s
, tr("Saint Helena, Ascension and Tristan da Cunha")},
352 {u
"SI"_s
, tr("Slovenia")},
353 {u
"SJ"_s
, tr("Svalbard and Jan Mayen")},
354 {u
"SK"_s
, tr("Slovakia")},
355 {u
"SL"_s
, tr("Sierra Leone")},
356 {u
"SM"_s
, tr("San Marino")},
357 {u
"SN"_s
, tr("Senegal")},
358 {u
"SO"_s
, tr("Somalia")},
359 {u
"SR"_s
, tr("Suriname")},
360 {u
"SS"_s
, tr("South Sudan")},
361 {u
"ST"_s
, tr("Sao Tome and Principe")},
362 {u
"SV"_s
, tr("El Salvador")},
363 {u
"SX"_s
, tr("Sint Maarten (Dutch part)")},
364 {u
"SY"_s
, tr("Syrian Arab Republic")},
365 {u
"SZ"_s
, tr("Swaziland")},
366 {u
"TC"_s
, tr("Turks and Caicos Islands")},
367 {u
"TD"_s
, tr("Chad")},
368 {u
"TF"_s
, tr("French Southern Territories")},
369 {u
"TG"_s
, tr("Togo")},
370 {u
"TH"_s
, tr("Thailand")},
371 {u
"TJ"_s
, tr("Tajikistan")},
372 {u
"TK"_s
, tr("Tokelau")},
373 {u
"TL"_s
, tr("Timor-Leste")},
374 {u
"TM"_s
, tr("Turkmenistan")},
375 {u
"TN"_s
, tr("Tunisia")},
376 {u
"TO"_s
, tr("Tonga")},
377 {u
"TR"_s
, tr("Turkey")},
378 {u
"TT"_s
, tr("Trinidad and Tobago")},
379 {u
"TV"_s
, tr("Tuvalu")},
380 {u
"TW"_s
, tr("Taiwan")},
381 {u
"TZ"_s
, tr("Tanzania, United Republic of")},
382 {u
"UA"_s
, tr("Ukraine")},
383 {u
"UG"_s
, tr("Uganda")},
384 {u
"UM"_s
, tr("United States Minor Outlying Islands")},
385 {u
"US"_s
, tr("United States")},
386 {u
"UY"_s
, tr("Uruguay")},
387 {u
"UZ"_s
, tr("Uzbekistan")},
388 {u
"VA"_s
, tr("Holy See (Vatican City State)")},
389 {u
"VC"_s
, tr("Saint Vincent and the Grenadines")},
390 {u
"VE"_s
, tr("Venezuela, Bolivarian Republic of")},
391 {u
"VG"_s
, tr("Virgin Islands, British")},
392 {u
"VI"_s
, tr("Virgin Islands, U.S.")},
393 {u
"VN"_s
, tr("Vietnam")},
394 {u
"VU"_s
, tr("Vanuatu")},
395 {u
"WF"_s
, tr("Wallis and Futuna")},
396 {u
"WS"_s
, tr("Samoa")},
397 {u
"YE"_s
, tr("Yemen")},
398 {u
"YT"_s
, tr("Mayotte")},
399 {u
"ZA"_s
, tr("South Africa")},
400 {u
"ZM"_s
, tr("Zambia")},
401 {u
"ZW"_s
, tr("Zimbabwe")},
406 return countries
.value(countryISOCode
, tr("N/A"));
409 void GeoIPManager::configure()
411 const bool enabled
= Preferences::instance()->resolvePeerCountries();
412 if (m_enabled
!= enabled
)
415 if (m_enabled
&& !m_geoIPDatabase
)
421 delete m_geoIPDatabase
;
422 m_geoIPDatabase
= nullptr;
427 void GeoIPManager::downloadFinished(const DownloadResult
&result
)
429 if (result
.status
!= DownloadStatus::Success
)
431 LogMsg(tr("Couldn't download IP geolocation database file. Reason: %1").arg(result
.errorString
), Log::WARNING
);
436 const QByteArray data
= Utils::Gzip::decompress(result
.data
, &ok
);
439 LogMsg(tr("Could not decompress IP geolocation database file."), Log::WARNING
);
444 GeoIPDatabase
*geoIPDatabase
= GeoIPDatabase::load(data
, error
);
447 if (!m_geoIPDatabase
|| (geoIPDatabase
->buildEpoch() > m_geoIPDatabase
->buildEpoch()))
449 delete m_geoIPDatabase
;
450 m_geoIPDatabase
= geoIPDatabase
;
451 LogMsg(tr("IP geolocation database loaded. Type: %1. Build time: %2.")
452 .arg(m_geoIPDatabase
->type(), m_geoIPDatabase
->buildEpoch().toString())
454 const Path targetPath
= specialFolderLocation(SpecialFolder::Data
) / Path(GEODB_FOLDER
);
455 if (!targetPath
.exists())
456 Utils::Fs::mkpath(targetPath
);
458 const auto path
= targetPath
/ Path(GEODB_FILENAME
);
459 const nonstd::expected
<void, QString
> result
= Utils::IO::saveToFile(path
, data
);
462 LogMsg(tr("Successfully updated IP geolocation database."), Log::INFO
);
466 LogMsg(tr("Couldn't save downloaded IP geolocation database file. Reason: %1")
467 .arg(result
.error()), Log::WARNING
);
472 delete geoIPDatabase
;
477 LogMsg(tr("Couldn't load IP geolocation database. Reason: %1").arg(error
), Log::WARNING
);