AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
IpLocationStore Class Reference

#include "IPLocation.h"

Public Member Functions

 IpLocationStore ()
 
 ~IpLocationStore ()
 
void Load ()
 
IpLocationRecord const * GetLocationRecord (std::string const &ipAddress) const
 

Static Public Member Functions

static IpLocationStoreinstance ()
 

Private Attributes

std::vector< IpLocationRecord_ipLocationStore
 

Detailed Description

Constructor & Destructor Documentation

◆ IpLocationStore()

IpLocationStore::IpLocationStore ( )
26{
27}

◆ ~IpLocationStore()

IpLocationStore::~IpLocationStore ( )
30{
31}

Member Function Documentation

◆ GetLocationRecord()

IpLocationRecord const * IpLocationStore::GetLocationRecord ( std::string const &  ipAddress) const
109{
111 auto itr = std::upper_bound(_ipLocationStore.begin(), _ipLocationStore.end(), ip, [](uint32 ip, IpLocationRecord const& loc) { return ip < loc.IpTo; });
112 if (itr == _ipLocationStore.end())
113 {
114 return nullptr;
115 }
116
117 if (ip < itr->IpFrom)
118 {
119 return nullptr;
120 }
121
122 return &(*itr);
123}
std::uint32_t uint32
Definition: Define.h:108
boost::asio::ip::address_v4 make_address_v4(char const *str)
Definition: IpAddress.h:35
uint32 address_to_uint(boost::asio::ip::address_v4 const &address)
Definition: IpAddress.h:39
Definition: IPLocation.h:23
std::vector< IpLocationRecord > _ipLocationStore
Definition: IPLocation.h:46

References _ipLocationStore, Acore::Net::address_to_uint(), and Acore::Net::make_address_v4().

◆ instance()

IpLocationStore * IpLocationStore::instance ( )
static
126{
128 return &instance;
129}
Definition: IPLocation.h:36
static IpLocationStore * instance()
Definition: IPLocation.cpp:125

References instance().

Referenced by instance().

◆ Load()

void IpLocationStore::Load ( )
34{
35 _ipLocationStore.clear();
36 LOG_INFO("server.loading", "Loading IP Location Database...");
37
38 std::string databaseFilePath = sConfigMgr->GetOption<std::string>("IPLocationFile", "");
39 if (databaseFilePath.empty())
40 {
41 LOG_INFO("server.loading", " ");
42 return;
43 }
44
45 // Check if file exists
46 std::ifstream databaseFile(databaseFilePath);
47 if (!databaseFile)
48 {
49 LOG_ERROR("server.loading", "IPLocation: No ip database file exists ({}).", databaseFilePath);
50 return;
51 }
52
53 if (!databaseFile.is_open())
54 {
55 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 std::transform(countryCode.begin(), countryCode.end(), countryCode.begin(), ::tolower);
88
89 auto IpFrom = Acore::StringTo<uint32>(ipFrom);
90 auto IpTo = Acore::StringTo<uint32>(ipTo);
91
92 if (!IpFrom || !IpTo)
93 continue;
94
95 _ipLocationStore.emplace_back(*IpFrom, *IpTo, std::move(countryCode), std::move(countryName));
96 }
97
98 std::sort(_ipLocationStore.begin(), _ipLocationStore.end(), [](IpLocationRecord const& a, IpLocationRecord const& b) { return a.IpFrom < b.IpFrom; });
99 ASSERT(std::is_sorted(_ipLocationStore.begin(), _ipLocationStore.end(), [](IpLocationRecord const& a, IpLocationRecord const& b) { return a.IpFrom < b.IpTo; }),
100 "Overlapping IP ranges detected in database file");
101
102 databaseFile.close();
103
104 LOG_INFO("server.loading", ">> Loaded {} ip location entries.", static_cast<uint32>(_ipLocationStore.size()));
105 LOG_INFO("server.loading", " ");
106}
#define sConfigMgr
Definition: Config.h:95
#define ASSERT
Definition: Errors.h:68
#define LOG_INFO(filterType__,...)
Definition: Log.h:165
#define LOG_ERROR(filterType__,...)
Definition: Log.h:157

References _ipLocationStore, ASSERT, LOG_ERROR, LOG_INFO, and sConfigMgr.

Member Data Documentation

◆ _ipLocationStore

std::vector<IpLocationRecord> IpLocationStore::_ipLocationStore
private

Referenced by GetLocationRecord(), and Load().