TrinityCore
IPLocation.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "IPLocation.h"
19#include "Config.h"
20#include "Errors.h"
21#include "IpAddress.h"
22#include "Log.h"
23#include "StringConvert.h"
24#include "Util.h"
25#include <algorithm>
26#include <fstream>
27
29{
30}
31
33{
34}
35
37{
38 _ipLocationStore.clear();
39 TC_LOG_INFO("server.loading", "Loading IP Location Database...");
40
41 std::string databaseFilePath = sConfigMgr->GetStringDefault("IPLocationFile", "");
42 if (databaseFilePath.empty())
43 return;
44
45 // Check if file exists
46 std::ifstream databaseFile(databaseFilePath);
47 if (!databaseFile)
48 {
49 TC_LOG_ERROR("server.loading", "IPLocation: No ip database file exists ({}).", databaseFilePath);
50 return;
51 }
52
53 if (!databaseFile.is_open())
54 {
55 TC_LOG_ERROR("server.loading", "IPLocation: Ip database file ({}) can not be opened.", databaseFilePath);
56 return;
57 }
58
59 std::string ipFrom;
60 std::string ipTo;
61 std::string countryCode;
62 std::string countryName;
63
64 while (databaseFile.good())
65 {
66 // Read lines
67 if (!std::getline(databaseFile, ipFrom, ','))
68 break;
69 if (!std::getline(databaseFile, ipTo, ','))
70 break;
71 if (!std::getline(databaseFile, countryCode, ','))
72 break;
73 if (!std::getline(databaseFile, countryName, '\n'))
74 break;
75
76 // Remove new lines and return
77 countryName.erase(std::remove(countryName.begin(), countryName.end(), '\r'), countryName.end());
78 countryName.erase(std::remove(countryName.begin(), countryName.end(), '\n'), countryName.end());
79
80 // Remove quotation marks
81 ipFrom.erase(std::remove(ipFrom.begin(), ipFrom.end(), '"'), ipFrom.end());
82 ipTo.erase(std::remove(ipTo.begin(), ipTo.end(), '"'), ipTo.end());
83 countryCode.erase(std::remove(countryCode.begin(), countryCode.end(), '"'), countryCode.end());
84 countryName.erase(std::remove(countryName.begin(), countryName.end(), '"'), countryName.end());
85
86 // Convert country code to lowercase
87 strToLower(countryCode);
88
89 Optional<uint32> from = Trinity::StringTo<uint32>(ipFrom);
90 if (!from)
91 continue;
92
93 Optional<uint32> to = Trinity::StringTo<uint32>(ipTo);
94 if (!to)
95 continue;
96
97 _ipLocationStore.emplace_back(*from, *to, std::move(countryCode), std::move(countryName));
98 }
99
100 std::sort(_ipLocationStore.begin(), _ipLocationStore.end(), [](IpLocationRecord const& a, IpLocationRecord const& b) { return a.IpFrom < b.IpFrom; });
101 ASSERT(std::is_sorted(_ipLocationStore.begin(), _ipLocationStore.end(), [](IpLocationRecord const& a, IpLocationRecord const& b) { return a.IpFrom < b.IpTo; }),
102 "Overlapping IP ranges detected in database file");
103
104 databaseFile.close();
105
106 TC_LOG_INFO("server.loading", ">> Loaded {} ip location entries.", _ipLocationStore.size());
107}
108
109IpLocationRecord const* IpLocationStore::GetLocationRecord(std::string const& ipAddress) const
110{
111 boost::system::error_code error;
112 boost::asio::ip::address_v4 address = Trinity::Net::make_address_v4(ipAddress, error);
113 if (error)
114 return nullptr;
115
117 auto itr = std::upper_bound(_ipLocationStore.begin(), _ipLocationStore.end(), ip, [](uint32 ip, IpLocationRecord const& loc) { return ip < loc.IpTo; });
118 if (itr == _ipLocationStore.end())
119 return nullptr;
120
121 if (ip < itr->IpFrom)
122 return nullptr;
123
124 return &(*itr);
125}
126
128{
129 static IpLocationStore instance;
130 return &instance;
131}
#define sConfigMgr
Definition: Config.h:61
uint32_t uint32
Definition: Define.h:142
#define ASSERT
Definition: Errors.h:68
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:165
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:159
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:25
void strToLower(std::string &str)
Definition: Util.cpp:482
IpLocationRecord const * GetLocationRecord(std::string const &ipAddress) const
Definition: IPLocation.cpp:109
static IpLocationStore * Instance()
Definition: IPLocation.cpp:127
std::vector< IpLocationRecord > _ipLocationStore
Definition: IPLocation.h:48
uint32 address_to_uint(boost::asio::ip::address_v4 const &address)
Definition: IpAddress.h:31